Stash.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // 暂时使用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. array_push($proxy, self::buildShadowsocks($user['uuid'], $item));
  35. array_push($proxies, $item['name']);
  36. }
  37. if ($item['type'] === 'v2ray') {
  38. array_push($proxy, self::buildVmess($user['uuid'], $item));
  39. array_push($proxies, $item['name']);
  40. }
  41. if ($item['type'] === 'trojan') {
  42. array_push($proxy, self::buildTrojan($user['uuid'], $item));
  43. array_push($proxies, $item['name']);
  44. }
  45. }
  46. $config['proxies'] = array_merge($config['proxies'] ? $config['proxies'] : [], $proxy);
  47. foreach ($config['proxy-groups'] as $k => $v) {
  48. if (!is_array($config['proxy-groups'][$k]['proxies'])) continue;
  49. $isFilter = false;
  50. foreach ($config['proxy-groups'][$k]['proxies'] as $src) {
  51. foreach ($proxies as $dst) {
  52. if ($this->isMatch($src, $dst)) {
  53. $isFilter = true;
  54. $config['proxy-groups'][$k]['proxies'] = array_diff($config['proxy-groups'][$k]['proxies'], [$src]);
  55. array_push($config['proxy-groups'][$k]['proxies'], $dst);
  56. }
  57. }
  58. }
  59. if (!$isFilter) {
  60. $config['proxy-groups'][$k]['proxies'] = array_merge($config['proxy-groups'][$k]['proxies'], $proxies);
  61. }
  62. }
  63. // Force the current subscription domain to be a direct rule
  64. $subsDomain = $_SERVER['SERVER_NAME'];
  65. $subsDomainRule = "DOMAIN,{$subsDomain},DIRECT";
  66. array_unshift($config['rules'], $subsDomainRule);
  67. $yaml = Yaml::dump($config);
  68. $yaml = str_replace('$app_name', config('v2board.app_name', 'V2Board'), $yaml);
  69. return $yaml;
  70. }
  71. public static function buildShadowsocks($uuid, $server)
  72. {
  73. $array = [];
  74. $array['name'] = $server['name'];
  75. $array['type'] = 'ss';
  76. $array['server'] = $server['host'];
  77. $array['port'] = $server['port'];
  78. $array['cipher'] = $server['cipher'];
  79. $array['password'] = $uuid;
  80. $array['udp'] = true;
  81. return $array;
  82. }
  83. public static function buildVmess($uuid, $server)
  84. {
  85. $array = [];
  86. $array['name'] = $server['name'];
  87. $array['type'] = 'vmess';
  88. $array['server'] = $server['host'];
  89. $array['port'] = $server['port'];
  90. $array['uuid'] = $uuid;
  91. $array['alterId'] = $server['alter_id'];
  92. $array['cipher'] = 'auto';
  93. $array['udp'] = true;
  94. if ($server['tls']) {
  95. $array['tls'] = true;
  96. if ($server['tlsSettings']) {
  97. $tlsSettings = $server['tlsSettings'];
  98. if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
  99. $array['skip-cert-verify'] = ($tlsSettings['allowInsecure'] ? true : false);
  100. if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
  101. $array['servername'] = $tlsSettings['serverName'];
  102. }
  103. }
  104. if ($server['network'] === 'ws') {
  105. $array['network'] = 'ws';
  106. if ($server['networkSettings']) {
  107. $wsSettings = $server['networkSettings'];
  108. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  109. $array['ws-path'] = $wsSettings['path'];
  110. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  111. $array['ws-headers'] = ['Host' => $wsSettings['headers']['Host']];
  112. }
  113. }
  114. if ($server['network'] === 'grpc') {
  115. $array['network'] = 'grpc';
  116. if ($server['networkSettings']) {
  117. $grpcObject = $server['networkSettings'];
  118. $array['grpc-opts'] = [];
  119. $array['grpc-opts']['grpc-service-name'] = $grpcObject['serviceName'];
  120. }
  121. }
  122. return $array;
  123. }
  124. public static function buildTrojan($password, $server)
  125. {
  126. $array = [];
  127. $array['name'] = $server['name'];
  128. $array['type'] = 'trojan';
  129. $array['server'] = $server['host'];
  130. $array['port'] = $server['port'];
  131. $array['password'] = $password;
  132. $array['udp'] = true;
  133. if (!empty($server['server_name'])) $array['sni'] = $server['server_name'];
  134. if (!empty($server['allow_insecure'])) $array['skip-cert-verify'] = ($server['allow_insecure'] ? true : false);
  135. return $array;
  136. }
  137. private function isMatch($exp, $str)
  138. {
  139. try {
  140. return preg_match($exp, $str);
  141. } catch (\Exception $e) {
  142. return false;
  143. }
  144. }
  145. }