Stash.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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'] === 'v2ray') {
  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. // Force the current subscription domain to be a direct rule
  72. $subsDomain = $_SERVER['HTTP_HOST'];
  73. if ($subsDomain) {
  74. array_unshift($config['rules'], "DOMAIN,{$subsDomain},DIRECT");
  75. }
  76. $yaml = Yaml::dump($config);
  77. $yaml = str_replace('$app_name', config('v2board.app_name', 'V2Board'), $yaml);
  78. return $yaml;
  79. }
  80. public static function buildShadowsocks($uuid, $server)
  81. {
  82. $array = [];
  83. $array['name'] = $server['name'];
  84. $array['type'] = 'ss';
  85. $array['server'] = $server['host'];
  86. $array['port'] = $server['port'];
  87. $array['cipher'] = $server['cipher'];
  88. $array['password'] = $uuid;
  89. $array['udp'] = true;
  90. return $array;
  91. }
  92. public static function buildVmess($uuid, $server)
  93. {
  94. $array = [];
  95. $array['name'] = $server['name'];
  96. $array['type'] = 'vmess';
  97. $array['server'] = $server['host'];
  98. $array['port'] = $server['port'];
  99. $array['uuid'] = $uuid;
  100. $array['alterId'] = 0;
  101. $array['cipher'] = 'auto';
  102. $array['udp'] = true;
  103. if ($server['tls']) {
  104. $array['tls'] = true;
  105. if ($server['tlsSettings']) {
  106. $tlsSettings = $server['tlsSettings'];
  107. if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
  108. $array['skip-cert-verify'] = ($tlsSettings['allowInsecure'] ? true : false);
  109. if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
  110. $array['servername'] = $tlsSettings['serverName'];
  111. }
  112. }
  113. if ($server['network'] === 'ws') {
  114. $array['network'] = 'ws';
  115. if ($server['networkSettings']) {
  116. $wsSettings = $server['networkSettings'];
  117. $array['ws-opts'] = [];
  118. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  119. $array['ws-opts']['path'] = $wsSettings['path'];
  120. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  121. $array['ws-opts']['headers'] = ['Host' => $wsSettings['headers']['Host']];
  122. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  123. $array['ws-path'] = $wsSettings['path'];
  124. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  125. $array['ws-headers'] = ['Host' => $wsSettings['headers']['Host']];
  126. }
  127. }
  128. if ($server['network'] === 'grpc') {
  129. $array['network'] = 'grpc';
  130. if ($server['networkSettings']) {
  131. $grpcSettings = $server['networkSettings'];
  132. $array['grpc-opts'] = [];
  133. if (isset($grpcSettings['serviceName'])) $array['grpc-opts']['grpc-service-name'] = $grpcSettings['serviceName'];
  134. }
  135. }
  136. return $array;
  137. }
  138. public static function buildTrojan($password, $server)
  139. {
  140. $array = [];
  141. $array['name'] = $server['name'];
  142. $array['type'] = 'trojan';
  143. $array['server'] = $server['host'];
  144. $array['port'] = $server['port'];
  145. $array['password'] = $password;
  146. $array['udp'] = true;
  147. if (!empty($server['server_name'])) $array['sni'] = $server['server_name'];
  148. if (!empty($server['allow_insecure'])) $array['skip-cert-verify'] = ($server['allow_insecure'] ? true : false);
  149. return $array;
  150. }
  151. private function isRegex($exp)
  152. {
  153. return @preg_match($exp, null) !== false;
  154. }
  155. private function isMatch($exp, $str)
  156. {
  157. try {
  158. return preg_match($exp, $str);
  159. } catch (\Exception $e) {
  160. return false;
  161. }
  162. }
  163. }