Stash.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace App\Http\Controllers\Client\Protocols;
  3. use Symfony\Component\Yaml\Yaml;
  4. class Stash
  5. {
  6. public $flag = 'stash';
  7. private $servers;
  8. private $user;
  9. public function __construct($user, $servers)
  10. {
  11. $this->user = $user;
  12. $this->servers = $servers;
  13. }
  14. public function handle()
  15. {
  16. $servers = $this->servers;
  17. $user = $this->user;
  18. $appName = config('v2board.app_name', 'V2Board');
  19. header("subscription-userinfo: upload={$user['u']}; download={$user['d']}; total={$user['transfer_enable']}; expire={$user['expired_at']}");
  20. header('profile-update-interval: 24');
  21. header("content-disposition: filename*=UTF-8''".rawurlencode($appName));
  22. // 暂时使用clash配置文件,后续根据Stash更新情况更新
  23. $defaultConfig = base_path() . '/resources/rules/default.clash.yaml';
  24. $customConfig = base_path() . '/resources/rules/custom.clash.yaml';
  25. if (\File::exists($customConfig)) {
  26. $config = Yaml::parseFile($customConfig);
  27. } else {
  28. $config = Yaml::parseFile($defaultConfig);
  29. }
  30. $proxy = [];
  31. $proxies = [];
  32. foreach ($servers as $item) {
  33. if ($item['type'] === 'shadowsocks'
  34. && in_array($item['cipher'], [
  35. 'aes-128-gcm',
  36. 'aes-192-gcm',
  37. 'aes-256-gcm',
  38. 'chacha20-ietf-poly1305'
  39. ])
  40. ) {
  41. array_push($proxy, self::buildShadowsocks($user['uuid'], $item));
  42. array_push($proxies, $item['name']);
  43. }
  44. if ($item['type'] === 'vmess') {
  45. array_push($proxy, self::buildVmess($user['uuid'], $item));
  46. array_push($proxies, $item['name']);
  47. }
  48. if ($item['type'] === 'trojan') {
  49. array_push($proxy, self::buildTrojan($user['uuid'], $item));
  50. array_push($proxies, $item['name']);
  51. }
  52. }
  53. $config['proxies'] = array_merge($config['proxies'] ? $config['proxies'] : [], $proxy);
  54. foreach ($config['proxy-groups'] as $k => $v) {
  55. if (!is_array($config['proxy-groups'][$k]['proxies'])) continue;
  56. $isFilter = false;
  57. foreach ($config['proxy-groups'][$k]['proxies'] as $src) {
  58. foreach ($proxies as $dst) {
  59. if (!$this->isRegex($src)) continue;
  60. $isFilter = true;
  61. $config['proxy-groups'][$k]['proxies'] = array_values(array_diff($config['proxy-groups'][$k]['proxies'], [$src]));
  62. if ($this->isMatch($src, $dst)) {
  63. array_push($config['proxy-groups'][$k]['proxies'], $dst);
  64. }
  65. }
  66. if ($isFilter) continue;
  67. }
  68. if ($isFilter) continue;
  69. $config['proxy-groups'][$k]['proxies'] = array_merge($config['proxy-groups'][$k]['proxies'], $proxies);
  70. }
  71. $config['proxy-groups'] = array_filter($config['proxy-groups'], function($group) {
  72. return $group['proxies'];
  73. });
  74. $config['proxy-groups'] = array_values($config['proxy-groups']);
  75. // Force the current subscription domain to be a direct rule
  76. $subsDomain = $_SERVER['HTTP_HOST'];
  77. if ($subsDomain) {
  78. array_unshift($config['rules'], "DOMAIN,{$subsDomain},DIRECT");
  79. }
  80. $yaml = Yaml::dump($config, 2, 4, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
  81. $yaml = str_replace('$app_name', config('v2board.app_name', 'V2Board'), $yaml);
  82. return $yaml;
  83. }
  84. public static function buildShadowsocks($uuid, $server)
  85. {
  86. $array = [];
  87. $array['name'] = $server['name'];
  88. $array['type'] = 'ss';
  89. $array['server'] = $server['host'];
  90. $array['port'] = $server['port'];
  91. $array['cipher'] = $server['cipher'];
  92. $array['password'] = $uuid;
  93. $array['udp'] = true;
  94. return $array;
  95. }
  96. public static function buildVmess($uuid, $server)
  97. {
  98. $array = [];
  99. $array['name'] = $server['name'];
  100. $array['type'] = 'vmess';
  101. $array['server'] = $server['host'];
  102. $array['port'] = $server['port'];
  103. $array['uuid'] = $uuid;
  104. $array['alterId'] = 0;
  105. $array['cipher'] = 'auto';
  106. $array['udp'] = true;
  107. if ($server['tls']) {
  108. $array['tls'] = true;
  109. if ($server['tlsSettings']) {
  110. $tlsSettings = $server['tlsSettings'];
  111. if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
  112. $array['skip-cert-verify'] = ($tlsSettings['allowInsecure'] ? true : false);
  113. if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
  114. $array['servername'] = $tlsSettings['serverName'];
  115. }
  116. }
  117. if ($server['network'] === 'ws') {
  118. $array['network'] = 'ws';
  119. if ($server['networkSettings']) {
  120. $wsSettings = $server['networkSettings'];
  121. $array['ws-opts'] = [];
  122. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  123. $array['ws-opts']['path'] = $wsSettings['path'];
  124. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  125. $array['ws-opts']['headers'] = ['Host' => $wsSettings['headers']['Host']];
  126. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  127. $array['ws-path'] = $wsSettings['path'];
  128. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  129. $array['ws-headers'] = ['Host' => $wsSettings['headers']['Host']];
  130. }
  131. }
  132. if ($server['network'] === 'grpc') {
  133. $array['network'] = 'grpc';
  134. if ($server['networkSettings']) {
  135. $grpcSettings = $server['networkSettings'];
  136. $array['grpc-opts'] = [];
  137. if (isset($grpcSettings['serviceName'])) $array['grpc-opts']['grpc-service-name'] = $grpcSettings['serviceName'];
  138. }
  139. }
  140. return $array;
  141. }
  142. public static function buildTrojan($password, $server)
  143. {
  144. $array = [];
  145. $array['name'] = $server['name'];
  146. $array['type'] = 'trojan';
  147. $array['server'] = $server['host'];
  148. $array['port'] = $server['port'];
  149. $array['password'] = $password;
  150. $array['udp'] = true;
  151. if (!empty($server['server_name'])) $array['sni'] = $server['server_name'];
  152. if (!empty($server['allow_insecure'])) $array['skip-cert-verify'] = ($server['allow_insecure'] ? true : false);
  153. return $array;
  154. }
  155. private function isRegex($exp)
  156. {
  157. return @preg_match($exp, null) !== false;
  158. }
  159. private function isMatch($exp, $str)
  160. {
  161. try {
  162. return preg_match($exp, $str);
  163. } catch (\Exception $e) {
  164. return false;
  165. }
  166. }
  167. }