Clash.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. header("subscription-userinfo: upload={$user['u']}; download={$user['d']}; total={$user['transfer_enable']}; expire={$user['expired_at']}");
  19. header('profile-update-interval: 24');
  20. header("content-disposition: filename={$app_name}");
  21. $defaultConfig = base_path() . '/resources/rules/default.clash.yaml';
  22. $customConfig = base_path() . '/resources/rules/custom.clash.yaml';
  23. if (\File::exists($customConfig)) {
  24. $config = Yaml::parseFile($customConfig);
  25. } else {
  26. $config = Yaml::parseFile($defaultConfig);
  27. }
  28. $proxy = [];
  29. $proxies = [];
  30. foreach ($servers as $item) {
  31. if ($item['type'] === 'shadowsocks') {
  32. array_push($proxy, self::buildShadowsocks($user['uuid'], $item));
  33. array_push($proxies, $item['name']);
  34. }
  35. if ($item['type'] === 'v2ray') {
  36. array_push($proxy, self::buildVmess($user['uuid'], $item));
  37. array_push($proxies, $item['name']);
  38. }
  39. if ($item['type'] === 'trojan') {
  40. array_push($proxy, self::buildTrojan($user['uuid'], $item));
  41. array_push($proxies, $item['name']);
  42. }
  43. }
  44. $config['proxies'] = array_merge($config['proxies'] ? $config['proxies'] : [], $proxy);
  45. foreach ($config['proxy-groups'] as $k => $v) {
  46. if (!is_array($config['proxy-groups'][$k]['proxies'])) continue;
  47. $config['proxy-groups'][$k]['proxies'] = array_merge($config['proxy-groups'][$k]['proxies'], $proxies);
  48. }
  49. $yaml = Yaml::dump($config);
  50. $yaml = str_replace('$app_name', config('v2board.app_name', 'V2Board'), $yaml);
  51. return $yaml;
  52. }
  53. public static function buildShadowsocks($uuid, $server)
  54. {
  55. $array = [];
  56. $array['name'] = $server['name'];
  57. $array['type'] = 'ss';
  58. $array['server'] = $server['host'];
  59. $array['port'] = $server['port'];
  60. $array['cipher'] = $server['cipher'];
  61. $array['password'] = $uuid;
  62. $array['udp'] = true;
  63. return $array;
  64. }
  65. public static function buildVmess($uuid, $server)
  66. {
  67. $array = [];
  68. $array['name'] = $server['name'];
  69. $array['type'] = 'vmess';
  70. $array['server'] = $server['host'];
  71. $array['port'] = $server['port'];
  72. $array['uuid'] = $uuid;
  73. $array['alterId'] = $server['alter_id'];
  74. $array['cipher'] = 'auto';
  75. $array['udp'] = true;
  76. if ($server['tls']) {
  77. $array['tls'] = true;
  78. if ($server['tlsSettings']) {
  79. $tlsSettings = $server['tlsSettings'];
  80. if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
  81. $array['skip-cert-verify'] = ($tlsSettings['allowInsecure'] ? true : false);
  82. if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
  83. $array['servername'] = $tlsSettings['serverName'];
  84. }
  85. }
  86. if ($server['network'] === 'ws') {
  87. $array['network'] = 'ws';
  88. if ($server['networkSettings']) {
  89. $wsSettings = $server['networkSettings'];
  90. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  91. $array['ws-path'] = $wsSettings['path'];
  92. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  93. $array['ws-headers'] = ['Host' => $wsSettings['headers']['Host']];
  94. }
  95. }
  96. if ($server['network'] === 'grpc') {
  97. $array['network'] = 'grpc';
  98. if ($server['networkSettings']) {
  99. $grpcObject = $server['networkSettings'];
  100. $array['grpc-opts'] = [];
  101. $array['grpc-opts']['grpc-service-name'] = $grpcObject['serviceName'];
  102. }
  103. }
  104. return $array;
  105. }
  106. public static function buildTrojan($password, $server)
  107. {
  108. $array = [];
  109. $array['name'] = $server['name'];
  110. $array['type'] = 'trojan';
  111. $array['server'] = $server['host'];
  112. $array['port'] = $server['port'];
  113. $array['password'] = $password;
  114. $array['udp'] = true;
  115. if (!empty($server['server_name'])) $array['sni'] = $server['server_name'];
  116. if (!empty($server['allow_insecure'])) $array['skip-cert-verify'] = ($server['allow_insecure'] ? true : false);
  117. return $array;
  118. }
  119. }