Clash.php 4.9 KB

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