Clash.php 7.3 KB

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