ClashMeta.php 7.2 KB

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