Clash.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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: filename={$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'])) continue;
  48. $isFilter = false;
  49. foreach ($config['proxy-groups'][$k]['proxies'] as $src) {
  50. foreach ($proxies as $dst) {
  51. if ($this->isMatch($src, $dst)) {
  52. $isFilter = true;
  53. $config['proxy-groups'][$k]['proxies'] = array_diff($config['proxy-groups'][$k]['proxies'], [$src]);
  54. array_push($config['proxy-groups'][$k]['proxies'], $dst);
  55. }
  56. }
  57. }
  58. if (!$isFilter) {
  59. $config['proxy-groups'][$k]['proxies'] = array_merge($config['proxy-groups'][$k]['proxies'], $proxies);
  60. }
  61. }
  62. // Force the current subscription domain to be a direct rule
  63. $subsDomain = $_SERVER['SERVER_NAME'];
  64. $subsDomainRule = "DOMAIN,{$subsDomain},DIRECT";
  65. array_unshift($config['rules'], $subsDomainRule);
  66. $yaml = Yaml::dump($config);
  67. $yaml = str_replace('$app_name', config('v2board.app_name', 'V2Board'), $yaml);
  68. return $yaml;
  69. }
  70. public static function buildShadowsocks($uuid, $server)
  71. {
  72. $array = [];
  73. $array['name'] = $server['name'];
  74. $array['type'] = 'ss';
  75. $array['server'] = $server['host'];
  76. $array['port'] = $server['port'];
  77. $array['cipher'] = $server['cipher'];
  78. $array['password'] = $uuid;
  79. $array['udp'] = true;
  80. return $array;
  81. }
  82. public static function buildVmess($uuid, $server)
  83. {
  84. $array = [];
  85. $array['name'] = $server['name'];
  86. $array['type'] = 'vmess';
  87. $array['server'] = $server['host'];
  88. $array['port'] = $server['port'];
  89. $array['uuid'] = $uuid;
  90. $array['alterId'] = $server['alter_id'];
  91. $array['cipher'] = 'auto';
  92. $array['udp'] = true;
  93. if ($server['tls']) {
  94. $array['tls'] = true;
  95. if ($server['tlsSettings']) {
  96. $tlsSettings = $server['tlsSettings'];
  97. if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
  98. $array['skip-cert-verify'] = ($tlsSettings['allowInsecure'] ? true : false);
  99. if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
  100. $array['servername'] = $tlsSettings['serverName'];
  101. }
  102. }
  103. if ($server['network'] === 'ws') {
  104. $array['network'] = 'ws';
  105. if ($server['networkSettings']) {
  106. $wsSettings = $server['networkSettings'];
  107. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  108. $array['ws-path'] = $wsSettings['path'];
  109. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  110. $array['ws-headers'] = ['Host' => $wsSettings['headers']['Host']];
  111. }
  112. }
  113. if ($server['network'] === 'grpc') {
  114. $array['network'] = 'grpc';
  115. if ($server['networkSettings']) {
  116. $grpcObject = $server['networkSettings'];
  117. $array['grpc-opts'] = [];
  118. $array['grpc-opts']['grpc-service-name'] = $grpcObject['serviceName'];
  119. }
  120. }
  121. return $array;
  122. }
  123. public static function buildTrojan($password, $server)
  124. {
  125. $array = [];
  126. $array['name'] = $server['name'];
  127. $array['type'] = 'trojan';
  128. $array['server'] = $server['host'];
  129. $array['port'] = $server['port'];
  130. $array['password'] = $password;
  131. $array['udp'] = true;
  132. if (!empty($server['server_name'])) $array['sni'] = $server['server_name'];
  133. if (!empty($server['allow_insecure'])) $array['skip-cert-verify'] = ($server['allow_insecure'] ? true : false);
  134. return $array;
  135. }
  136. private function isMatch($exp, $str)
  137. {
  138. try {
  139. return preg_match($exp, $str);
  140. } catch (\Exception $e) {
  141. return false;
  142. }
  143. }
  144. }