Clash.php 6.6 KB

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