Clash.php 5.1 KB

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