ClashMeta.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App\Http\Controllers\Client\Protocols;
  3. use App\Utils\Helper;
  4. use Symfony\Component\Yaml\Yaml;
  5. class ClashMeta
  6. {
  7. public $flag = 'clashmeta';
  8. private $servers;
  9. private $user;
  10. public function __construct($user, $servers)
  11. {
  12. $this->user = $user;
  13. $this->servers = $servers;
  14. }
  15. public function handle()
  16. {
  17. $servers = $this->servers;
  18. $user = $this->user;
  19. $appName = config('v2board.app_name', 'V2Board');
  20. header("subscription-userinfo: upload={$user['u']}; download={$user['d']}; total={$user['transfer_enable']}; expire={$user['expired_at']}");
  21. header('profile-update-interval: 24');
  22. header("content-disposition:attachment;filename*=UTF-8''".rawurlencode($appName));
  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'] === 'vmess') {
  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'])) $config['proxy-groups'][$k]['proxies'] = [];
  49. $isFilter = false;
  50. foreach ($config['proxy-groups'][$k]['proxies'] as $src) {
  51. foreach ($proxies as $dst) {
  52. if (!$this->isRegex($src)) continue;
  53. $isFilter = true;
  54. $config['proxy-groups'][$k]['proxies'] = array_values(array_diff($config['proxy-groups'][$k]['proxies'], [$src]));
  55. if ($this->isMatch($src, $dst)) {
  56. array_push($config['proxy-groups'][$k]['proxies'], $dst);
  57. }
  58. }
  59. if ($isFilter) continue;
  60. }
  61. if ($isFilter) continue;
  62. $config['proxy-groups'][$k]['proxies'] = array_merge($config['proxy-groups'][$k]['proxies'], $proxies);
  63. }
  64. $config['proxy-groups'] = array_filter($config['proxy-groups'], function($group) {
  65. return $group['proxies'];
  66. });
  67. // Force the current subscription domain to be a direct rule
  68. $subsDomain = $_SERVER['HTTP_HOST'];
  69. if ($subsDomain) {
  70. array_unshift($config['rules'], "DOMAIN,{$subsDomain},DIRECT");
  71. }
  72. $yaml = Yaml::dump($config, 2, 4, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
  73. $yaml = str_replace('$app_name', config('v2board.app_name', 'V2Board'), $yaml);
  74. return $yaml;
  75. }
  76. public static function buildShadowsocks($password, $server)
  77. {
  78. if ($server['cipher'] === '2022-blake3-aes-128-gcm') {
  79. $serverKey = Helper::getServerKey($server['created_at'], 16);
  80. $userKey = Helper::uuidToBase64($password, 16);
  81. $password = "{$serverKey}:{$userKey}";
  82. }
  83. if ($server['cipher'] === '2022-blake3-aes-256-gcm') {
  84. $serverKey = Helper::getServerKey($server['created_at'], 32);
  85. $userKey = Helper::uuidToBase64($password, 32);
  86. $password = "{$serverKey}:{$userKey}";
  87. }
  88. $array = [];
  89. $array['name'] = $server['name'];
  90. $array['type'] = 'ss';
  91. $array['server'] = $server['host'];
  92. $array['port'] = $server['port'];
  93. $array['cipher'] = $server['cipher'];
  94. $array['password'] = $password;
  95. $array['udp'] = true;
  96. return $array;
  97. }
  98. public static function buildVmess($uuid, $server)
  99. {
  100. $array = [];
  101. $array['name'] = $server['name'];
  102. $array['type'] = 'vmess';
  103. $array['server'] = $server['host'];
  104. $array['port'] = $server['port'];
  105. $array['uuid'] = $uuid;
  106. $array['alterId'] = 0;
  107. $array['cipher'] = 'auto';
  108. $array['udp'] = true;
  109. if ($server['tls']) {
  110. $array['tls'] = true;
  111. if ($server['tlsSettings']) {
  112. $tlsSettings = $server['tlsSettings'];
  113. if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
  114. $array['skip-cert-verify'] = ($tlsSettings['allowInsecure'] ? true : false);
  115. if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
  116. $array['servername'] = $tlsSettings['serverName'];
  117. }
  118. }
  119. if ($server['network'] === 'ws') {
  120. $array['network'] = 'ws';
  121. if ($server['networkSettings']) {
  122. $wsSettings = $server['networkSettings'];
  123. $array['ws-opts'] = [];
  124. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  125. $array['ws-opts']['path'] = $wsSettings['path'];
  126. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  127. $array['ws-opts']['headers'] = ['Host' => $wsSettings['headers']['Host']];
  128. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  129. $array['ws-path'] = $wsSettings['path'];
  130. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  131. $array['ws-headers'] = ['Host' => $wsSettings['headers']['Host']];
  132. }
  133. }
  134. if ($server['network'] === 'grpc') {
  135. $array['network'] = 'grpc';
  136. if ($server['networkSettings']) {
  137. $grpcSettings = $server['networkSettings'];
  138. $array['grpc-opts'] = [];
  139. if (isset($grpcSettings['serviceName'])) $array['grpc-opts']['grpc-service-name'] = $grpcSettings['serviceName'];
  140. }
  141. }
  142. return $array;
  143. }
  144. public static function buildTrojan($password, $server)
  145. {
  146. $array = [];
  147. $array['name'] = $server['name'];
  148. $array['type'] = 'trojan';
  149. $array['server'] = $server['host'];
  150. $array['port'] = $server['port'];
  151. $array['password'] = $password;
  152. $array['udp'] = true;
  153. if (!empty($server['server_name'])) $array['sni'] = $server['server_name'];
  154. if (!empty($server['allow_insecure'])) $array['skip-cert-verify'] = ($server['allow_insecure'] ? true : false);
  155. return $array;
  156. }
  157. private function isMatch($exp, $str)
  158. {
  159. return @preg_match($exp, $str);
  160. }
  161. private function isRegex($exp)
  162. {
  163. return @preg_match($exp, null) !== false;
  164. }
  165. }