Clash.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. $user = $this->user;
  17. if (empty($_REQUEST['getsubscribe'])) {
  18. $app_name = 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: 1");
  21. header("content-disposition: filename={$app_name}");
  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. $args = array(
  30. 'token' => $user['token'],
  31. 'flag' => 'clash',
  32. 'getsubscribe' => 'true'
  33. );
  34. $proxy = array(
  35. $app_name => array(
  36. 'type' => 'http',
  37. 'url' => config('v2board.subscribe_url') . '/api/v1/client/subscribe?' . http_build_query($args),
  38. 'interval' => 3600,
  39. 'path' => './Proxy/' . $app_name . '.yaml',
  40. 'health-check' => array(
  41. 'enable' => true,
  42. 'interval' => 900,
  43. 'url' => 'http://www.gstatic.com/generate_204'
  44. )
  45. )
  46. );
  47. $config['proxy-providers'] = array_merge($config['proxy-providers'] ? $config['proxy-providers'] : [], $proxy);
  48. foreach ($config['proxy-groups'] as $k => $v) {
  49. if ( isset($config['proxy-groups'][$k]['use']) ) {
  50. if ( !is_array($config['proxy-groups'][$k]['use']) ) continue;
  51. $config['proxy-groups'][$k]['use'] = [$app_name];
  52. }
  53. }
  54. $yaml = Yaml::dump($config);
  55. $yaml = str_replace('$app_name', $app_name, $yaml);
  56. } else {
  57. $servers = $this->servers;
  58. $proxy = [];
  59. $proxies = [];
  60. foreach ($servers as $item) {
  61. if ($item['type'] === 'shadowsocks') {
  62. array_push($proxy, self::buildShadowsocks($user['uuid'], $item));
  63. array_push($proxies, $item['name']);
  64. }
  65. if ($item['type'] === 'v2ray') {
  66. array_push($proxy, self::buildVmess($user['uuid'], $item));
  67. array_push($proxies, $item['name']);
  68. }
  69. if ($item['type'] === 'trojan') {
  70. array_push($proxy, self::buildTrojan($user['uuid'], $item));
  71. array_push($proxies, $item['name']);
  72. }
  73. }
  74. $config['proxies'] = array_merge($proxy);
  75. $yaml = Yaml::dump($config);
  76. }
  77. return $yaml;
  78. }
  79. public static function buildShadowsocks($uuid, $server)
  80. {
  81. $array = [];
  82. $array['name'] = $server['name'];
  83. $array['type'] = 'ss';
  84. $array['server'] = $server['host'];
  85. $array['port'] = $server['port'];
  86. $array['cipher'] = $server['cipher'];
  87. $array['password'] = $uuid;
  88. $array['udp'] = true;
  89. return $array;
  90. }
  91. public static function buildVmess($uuid, $server)
  92. {
  93. $array = [];
  94. $array['name'] = $server['name'];
  95. $array['type'] = 'vmess';
  96. $array['server'] = $server['host'];
  97. $array['port'] = $server['port'];
  98. $array['uuid'] = $uuid;
  99. $array['alterId'] = $server['alter_id'];
  100. $array['cipher'] = 'auto';
  101. $array['udp'] = true;
  102. if ($server['tls']) {
  103. $array['tls'] = true;
  104. if ($server['tlsSettings']) {
  105. $tlsSettings = json_decode($server['tlsSettings'], true);
  106. if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
  107. $array['skip-cert-verify'] = ($tlsSettings['allowInsecure'] ? true : false);
  108. if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
  109. $array['servername'] = $tlsSettings['serverName'];
  110. }
  111. }
  112. if ($server['network'] === 'ws') {
  113. $array['network'] = 'ws';
  114. if ($server['networkSettings']) {
  115. $wsSettings = json_decode($server['networkSettings'], true);
  116. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  117. $array['ws-path'] = $wsSettings['path'];
  118. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  119. $array['ws-headers'] = ['Host' => $wsSettings['headers']['Host']];
  120. }
  121. }
  122. if ($server['network'] === 'grpc') {
  123. $array['network'] = 'grpc';
  124. if ($server['networkSettings']) {
  125. $grpcObject = json_decode($server['networkSettings'], true);
  126. $array['grpc-opts'] = [];
  127. $array['grpc-opts']['grpc-service-name'] = $grpcObject['serviceName'];
  128. }
  129. }
  130. return $array;
  131. }
  132. public static function buildTrojan($password, $server)
  133. {
  134. $array = [];
  135. $array['name'] = $server['name'];
  136. $array['type'] = 'trojan';
  137. $array['server'] = $server['host'];
  138. $array['port'] = $server['port'];
  139. $array['password'] = $password;
  140. $array['udp'] = true;
  141. if (!empty($server['server_name'])) $array['sni'] = $server['server_name'];
  142. if (!empty($server['allow_insecure'])) $array['skip-cert-verify'] = ($server['allow_insecure'] ? true : false);
  143. return $array;
  144. }
  145. }