Clash.php 6.8 KB

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