Clash.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace App\Http\Controllers\Client\Protocols;
  3. use App\Utils\Dict;
  4. use phpDocumentor\Reflection\Types\Self_;
  5. use Symfony\Component\Yaml\Yaml;
  6. class Clash
  7. {
  8. public $flag = 'clash';
  9. private $servers;
  10. private $user;
  11. public function __construct($user, $servers)
  12. {
  13. $this->user = $user;
  14. $this->servers = $servers;
  15. }
  16. public function handle()
  17. {
  18. $servers = $this->servers;
  19. $user = $this->user;
  20. $appName = config('v2board.app_name', 'V2Board');
  21. header("subscription-userinfo: upload={$user['u']}; download={$user['d']}; total={$user['transfer_enable']}; expire={$user['expired_at']}");
  22. header('profile-update-interval: 24');
  23. header("content-disposition:attachment;filename*=UTF-8''".rawurlencode($appName));
  24. header("profile-web-page-url:" . config('v2board.app_url'));
  25. $defaultConfig = base_path() . '/resources/rules/default.clash.yaml';
  26. $customConfig = base_path() . '/resources/rules/custom.clash.yaml';
  27. if (\File::exists($customConfig)) {
  28. $config = Yaml::parseFile($customConfig);
  29. } else {
  30. $config = Yaml::parseFile($defaultConfig);
  31. }
  32. $this->patch($config);
  33. $proxy = [];
  34. $proxies = [];
  35. foreach ($servers as $item) {
  36. if ($item['type'] === 'shadowsocks'
  37. && in_array($item['cipher'], [
  38. 'aes-128-gcm',
  39. 'aes-192-gcm',
  40. 'aes-256-gcm',
  41. 'chacha20-ietf-poly1305'
  42. ])
  43. ) {
  44. array_push($proxy, self::buildShadowsocks($user['uuid'], $item));
  45. array_push($proxies, $item['name']);
  46. }
  47. if ($item['type'] === 'vmess') {
  48. array_push($proxy, self::buildVmess($user['uuid'], $item));
  49. array_push($proxies, $item['name']);
  50. }
  51. if ($item['type'] === 'trojan') {
  52. array_push($proxy, self::buildTrojan($user['uuid'], $item));
  53. array_push($proxies, $item['name']);
  54. }
  55. }
  56. $config['proxies'] = array_merge($config['proxies'] ? $config['proxies'] : [], $proxy);
  57. foreach ($config['proxy-groups'] as $k => $v) {
  58. if (!is_array($config['proxy-groups'][$k]['proxies'])) $config['proxy-groups'][$k]['proxies'] = [];
  59. $isFilter = false;
  60. foreach ($config['proxy-groups'][$k]['proxies'] as $src) {
  61. foreach ($proxies as $dst) {
  62. if (!$this->isRegex($src)) continue;
  63. $isFilter = true;
  64. $config['proxy-groups'][$k]['proxies'] = array_values(array_diff($config['proxy-groups'][$k]['proxies'], [$src]));
  65. if ($this->isMatch($src, $dst)) {
  66. array_push($config['proxy-groups'][$k]['proxies'], $dst);
  67. }
  68. }
  69. if ($isFilter) continue;
  70. }
  71. if ($isFilter) continue;
  72. $config['proxy-groups'][$k]['proxies'] = array_merge($config['proxy-groups'][$k]['proxies'], $proxies);
  73. }
  74. // Force the current subscription domain to be a direct rule
  75. $subsDomain = $_SERVER['HTTP_HOST'];
  76. if ($subsDomain) {
  77. array_unshift($config['rules'], "DOMAIN,{$subsDomain},DIRECT");
  78. }
  79. $yaml = Yaml::dump($config, 2, 4, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
  80. $yaml = str_replace('$app_name', config('v2board.app_name', 'V2Board'), $yaml);
  81. return $yaml;
  82. }
  83. public static function buildShadowsocks($uuid, $server)
  84. {
  85. $array = [];
  86. $array['name'] = $server['name'];
  87. $array['type'] = 'ss';
  88. $array['server'] = $server['host'];
  89. $array['port'] = $server['port'];
  90. $array['cipher'] = $server['cipher'];
  91. $array['password'] = $uuid;
  92. $array['udp'] = true;
  93. return $array;
  94. }
  95. public static function buildVmess($uuid, $server)
  96. {
  97. $array = [];
  98. $array['name'] = $server['name'];
  99. $array['type'] = 'vmess';
  100. $array['server'] = $server['host'];
  101. $array['port'] = $server['port'];
  102. $array['uuid'] = $uuid;
  103. $array['alterId'] = 0;
  104. $array['cipher'] = 'auto';
  105. $array['udp'] = true;
  106. if ($server['tls']) {
  107. $array['tls'] = true;
  108. if ($server['tlsSettings']) {
  109. $tlsSettings = $server['tlsSettings'];
  110. if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
  111. $array['skip-cert-verify'] = ($tlsSettings['allowInsecure'] ? true : false);
  112. if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
  113. $array['servername'] = $tlsSettings['serverName'];
  114. }
  115. }
  116. if ($server['network'] === 'ws') {
  117. $array['network'] = 'ws';
  118. if ($server['networkSettings']) {
  119. $wsSettings = $server['networkSettings'];
  120. $array['ws-opts'] = [];
  121. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  122. $array['ws-opts']['path'] = $wsSettings['path'];
  123. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  124. $array['ws-opts']['headers'] = ['Host' => $wsSettings['headers']['Host']];
  125. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  126. $array['ws-path'] = $wsSettings['path'];
  127. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  128. $array['ws-headers'] = ['Host' => $wsSettings['headers']['Host']];
  129. }
  130. }
  131. if ($server['network'] === 'grpc') {
  132. $array['network'] = 'grpc';
  133. if ($server['networkSettings']) {
  134. $grpcSettings = $server['networkSettings'];
  135. $array['grpc-opts'] = [];
  136. if (isset($grpcSettings['serviceName'])) $array['grpc-opts']['grpc-service-name'] = $grpcSettings['serviceName'];
  137. }
  138. }
  139. return $array;
  140. }
  141. public static function buildTrojan($password, $server)
  142. {
  143. $array = [];
  144. $array['name'] = $server['name'];
  145. $array['type'] = 'trojan';
  146. $array['server'] = $server['host'];
  147. $array['port'] = $server['port'];
  148. $array['password'] = $password;
  149. $array['udp'] = true;
  150. if (!empty($server['server_name'])) $array['sni'] = $server['server_name'];
  151. if (!empty($server['allow_insecure'])) $array['skip-cert-verify'] = ($server['allow_insecure'] ? true : false);
  152. return $array;
  153. }
  154. private function isMatch($exp, $str)
  155. {
  156. return @preg_match($exp, $str);
  157. }
  158. private function isRegex($exp)
  159. {
  160. return @preg_match($exp, null) !== false;
  161. }
  162. private function patch(&$config)
  163. {
  164. // fix clash x dns mode
  165. preg_match('#(ClashX)[/ ]([0-9.]*)#', $_SERVER['HTTP_USER_AGENT'], $matches);
  166. if ($matches[2] < '1.96.2') $config['dns']['enhanced-mode'] = 'redir-host';
  167. }
  168. }