Clash.php 4.8 KB

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