Clash.php 7.0 KB

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