Stash.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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={$appName}");
  22. header("content-disposition: filename*=UTF-8''".rawurlencode($appName));
  23. // 暂时使用clash配置文件,后续根据Stash更新情况更新
  24. $defaultConfig = base_path() . '/resources/rules/default.clash.yaml';
  25. $customConfig = base_path() . '/resources/rules/custom.clash.yaml';
  26. if (\File::exists($customConfig)) {
  27. $config = Yaml::parseFile($customConfig);
  28. } else {
  29. $config = Yaml::parseFile($defaultConfig);
  30. }
  31. $proxy = [];
  32. $proxies = [];
  33. foreach ($servers as $item) {
  34. if ($item['type'] === 'shadowsocks') {
  35. array_push($proxy, self::buildShadowsocks($user['uuid'], $item));
  36. array_push($proxies, $item['name']);
  37. }
  38. if ($item['type'] === 'v2ray') {
  39. array_push($proxy, self::buildVmess($user['uuid'], $item));
  40. array_push($proxies, $item['name']);
  41. }
  42. if ($item['type'] === 'trojan') {
  43. array_push($proxy, self::buildTrojan($user['uuid'], $item));
  44. array_push($proxies, $item['name']);
  45. }
  46. }
  47. $config['proxies'] = array_merge($config['proxies'] ? $config['proxies'] : [], $proxy);
  48. foreach ($config['proxy-groups'] as $k => $v) {
  49. if (!is_array($config['proxy-groups'][$k]['proxies'])) continue;
  50. $isFilter = false;
  51. foreach ($config['proxy-groups'][$k]['proxies'] as $src) {
  52. foreach ($proxies as $dst) {
  53. if (!$this->isRegex($src)) continue;
  54. $isFilter = true;
  55. $config['proxy-groups'][$k]['proxies'] = array_values(array_diff($config['proxy-groups'][$k]['proxies'], [$src]));
  56. if ($this->isMatch($src, $dst)) {
  57. array_push($config['proxy-groups'][$k]['proxies'], $dst);
  58. }
  59. }
  60. if ($isFilter) continue;
  61. }
  62. if ($isFilter) continue;
  63. $config['proxy-groups'][$k]['proxies'] = array_merge($config['proxy-groups'][$k]['proxies'], $proxies);
  64. }
  65. // Force the current subscription domain to be a direct rule
  66. $subsDomain = $_SERVER['SERVER_NAME'];
  67. $subsDomainRule = "DOMAIN,{$subsDomain},DIRECT";
  68. array_unshift($config['rules'], $subsDomainRule);
  69. $yaml = Yaml::dump($config);
  70. $yaml = str_replace('$app_name', config('v2board.app_name', 'V2Board'), $yaml);
  71. return $yaml;
  72. }
  73. public static function buildShadowsocks($uuid, $server)
  74. {
  75. $array = [];
  76. $array['name'] = $server['name'];
  77. $array['type'] = 'ss';
  78. $array['server'] = $server['host'];
  79. $array['port'] = $server['port'];
  80. $array['cipher'] = $server['cipher'];
  81. $array['password'] = $uuid;
  82. $array['udp'] = true;
  83. return $array;
  84. }
  85. public static function buildVmess($uuid, $server)
  86. {
  87. $array = [];
  88. $array['name'] = $server['name'];
  89. $array['type'] = 'vmess';
  90. $array['server'] = $server['host'];
  91. $array['port'] = $server['port'];
  92. $array['uuid'] = $uuid;
  93. $array['alterId'] = 0;
  94. $array['cipher'] = 'auto';
  95. $array['udp'] = true;
  96. if ($server['tls']) {
  97. $array['tls'] = true;
  98. if ($server['tlsSettings']) {
  99. $tlsSettings = $server['tlsSettings'];
  100. if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
  101. $array['skip-cert-verify'] = ($tlsSettings['allowInsecure'] ? true : false);
  102. if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
  103. $array['servername'] = $tlsSettings['serverName'];
  104. }
  105. }
  106. if ($server['network'] === 'ws') {
  107. $array['network'] = 'ws';
  108. if ($server['networkSettings']) {
  109. $wsSettings = $server['networkSettings'];
  110. $array['ws-opts'] = [];
  111. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  112. $array['ws-opts']['path'] = $wsSettings['path'];
  113. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  114. $array['ws-opts']['headers'] = ['Host' => $wsSettings['headers']['Host']];
  115. // TODO: 2022.06.01 remove it
  116. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  117. $array['ws-path'] = $wsSettings['path'];
  118. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  119. $array['ws-headers'] = ['Host' => $wsSettings['headers']['Host']];
  120. }
  121. }
  122. if ($server['network'] === 'grpc') {
  123. $array['network'] = 'grpc';
  124. if ($server['networkSettings']) {
  125. $grpcSettings = $server['networkSettings'];
  126. $array['grpc-opts'] = [];
  127. if (isset($grpcSettings['serviceName'])) $array['grpc-opts']['grpc-service-name'] = $grpcSettings['serviceName'];
  128. }
  129. }
  130. return $array;
  131. }
  132. public static function buildTrojan($password, $server)
  133. {
  134. $array = [];
  135. $array['name'] = $server['name'];
  136. $array['type'] = 'trojan';
  137. $array['server'] = $server['host'];
  138. $array['port'] = $server['port'];
  139. $array['password'] = $password;
  140. $array['udp'] = true;
  141. if (!empty($server['server_name'])) $array['sni'] = $server['server_name'];
  142. if (!empty($server['allow_insecure'])) $array['skip-cert-verify'] = ($server['allow_insecure'] ? true : false);
  143. return $array;
  144. }
  145. private function isMatch($exp, $str)
  146. {
  147. try {
  148. return preg_match($exp, $str);
  149. } catch (\Exception $e) {
  150. return false;
  151. }
  152. }
  153. }