Clash.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. array_push($proxy, self::buildShadowsocks($user['uuid'], $item));
  35. array_push($proxies, $item['name']);
  36. }
  37. if ($item['type'] === 'v2ray') {
  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. // Force the current subscription domain to be a direct rule
  65. $subsDomain = $_SERVER['SERVER_NAME'];
  66. $subsDomainRule = "DOMAIN,{$subsDomain},DIRECT";
  67. array_unshift($config['rules'], $subsDomainRule);
  68. $yaml = Yaml::dump($config);
  69. $yaml = str_replace('$app_name', config('v2board.app_name', 'V2Board'), $yaml);
  70. return $yaml;
  71. }
  72. public static function buildShadowsocks($uuid, $server)
  73. {
  74. $array = [];
  75. $array['name'] = $server['name'];
  76. $array['type'] = 'ss';
  77. $array['server'] = $server['host'];
  78. $array['port'] = $server['port'];
  79. $array['cipher'] = $server['cipher'];
  80. $array['password'] = $uuid;
  81. $array['udp'] = true;
  82. return $array;
  83. }
  84. public static function buildVmess($uuid, $server)
  85. {
  86. $array = [];
  87. $array['name'] = $server['name'];
  88. $array['type'] = 'vmess';
  89. $array['server'] = $server['host'];
  90. $array['port'] = $server['port'];
  91. $array['uuid'] = $uuid;
  92. $array['alterId'] = 0;
  93. $array['cipher'] = 'auto';
  94. $array['udp'] = true;
  95. if ($server['tls']) {
  96. $array['tls'] = true;
  97. if ($server['tlsSettings']) {
  98. $tlsSettings = $server['tlsSettings'];
  99. if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
  100. $array['skip-cert-verify'] = ($tlsSettings['allowInsecure'] ? true : false);
  101. if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
  102. $array['servername'] = $tlsSettings['serverName'];
  103. }
  104. }
  105. if ($server['network'] === 'ws') {
  106. $array['network'] = 'ws';
  107. if ($server['networkSettings']) {
  108. $wsSettings = $server['networkSettings'];
  109. $array['ws-opts'] = [];
  110. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  111. $array['ws-opts']['path'] = $wsSettings['path'];
  112. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  113. $array['ws-opts']['headers'] = ['Host' => $wsSettings['headers']['Host']];
  114. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  115. $array['ws-path'] = $wsSettings['path'];
  116. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  117. $array['ws-headers'] = ['Host' => $wsSettings['headers']['Host']];
  118. }
  119. }
  120. if ($server['network'] === 'grpc') {
  121. $array['network'] = 'grpc';
  122. if ($server['networkSettings']) {
  123. $grpcSettings = $server['networkSettings'];
  124. $array['grpc-opts'] = [];
  125. if (isset($grpcSettings['serviceName'])) $array['grpc-opts']['grpc-service-name'] = $grpcSettings['serviceName'];
  126. }
  127. }
  128. return $array;
  129. }
  130. public static function buildTrojan($password, $server)
  131. {
  132. $array = [];
  133. $array['name'] = $server['name'];
  134. $array['type'] = 'trojan';
  135. $array['server'] = $server['host'];
  136. $array['port'] = $server['port'];
  137. $array['password'] = $password;
  138. $array['udp'] = true;
  139. if (!empty($server['server_name'])) $array['sni'] = $server['server_name'];
  140. if (!empty($server['allow_insecure'])) $array['skip-cert-verify'] = ($server['allow_insecure'] ? true : false);
  141. return $array;
  142. }
  143. private function isMatch($exp, $str)
  144. {
  145. return @preg_match($exp, $str);
  146. }
  147. private function isRegex($exp)
  148. {
  149. return @preg_match($exp, null) !== false;
  150. }
  151. }