Clash.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. $config['proxy-groups'] = array_filter($config['proxy-groups'], function($group) {
  75. return $group['proxies'];
  76. });
  77. $config['proxy-groups'] = array_values($config['proxy-groups']);
  78. // Force the current subscription domain to be a direct rule
  79. $subsDomain = $_SERVER['HTTP_HOST'];
  80. if ($subsDomain) {
  81. array_unshift($config['rules'], "DOMAIN,{$subsDomain},DIRECT");
  82. }
  83. $yaml = Yaml::dump($config, 2, 4, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
  84. $yaml = str_replace('$app_name', config('v2board.app_name', 'V2Board'), $yaml);
  85. return $yaml;
  86. }
  87. public static function buildShadowsocks($uuid, $server)
  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'] = $uuid;
  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. private function patch(&$config)
  167. {
  168. // fix clash x dns mode
  169. preg_match('#(ClashX)[/ ]([0-9.]*)#', $_SERVER['HTTP_USER_AGENT'], $matches);
  170. if (isset($matches[2]) && $matches[2] < '1.96.2') $config['dns']['enhanced-mode'] = 'redir-host';
  171. }
  172. }