Clash.php 6.4 KB

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