Surge.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace App\Http\Controllers\Client\Protocols;
  3. use App\Utils\Helper;
  4. class Surge
  5. {
  6. public $flag = 'surge';
  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("content-disposition:attachment;filename*=UTF-8''".rawurlencode($appName).".conf");
  20. $proxies = '';
  21. $proxyGroup = '';
  22. foreach ($servers as $item) {
  23. if ($item['type'] === 'shadowsocks'
  24. && in_array($item['cipher'], ['aes-128-gcm', 'aes-256-gcm', 'aes-192-gcm'])
  25. ) {
  26. // [Proxy]
  27. $proxies .= self::buildShadowsocks($user['uuid'], $item);
  28. // [Proxy Group]
  29. $proxyGroup .= $item['name'] . ', ';
  30. }
  31. if ($item['type'] === 'v2ray') {
  32. // [Proxy]
  33. $proxies .= self::buildVmess($user['uuid'], $item);
  34. // [Proxy Group]
  35. $proxyGroup .= $item['name'] . ', ';
  36. }
  37. if ($item['type'] === 'trojan') {
  38. // [Proxy]
  39. $proxies .= self::buildTrojan($user['uuid'], $item);
  40. // [Proxy Group]
  41. $proxyGroup .= $item['name'] . ', ';
  42. }
  43. }
  44. $defaultConfig = base_path() . '/resources/rules/default.surge.conf';
  45. $customConfig = base_path() . '/resources/rules/custom.surge.conf';
  46. if (\File::exists($customConfig)) {
  47. $config = file_get_contents("$customConfig");
  48. } else {
  49. $config = file_get_contents("$defaultConfig");
  50. }
  51. // Subscription link
  52. $subsURL = Helper::getSubscribeUrl("/api/v1/client/subscribe?token={$user['token']}");
  53. $subsDomain = $_SERVER['SERVER_NAME'];
  54. $config = str_replace('$subs_link', $subsURL, $config);
  55. $config = str_replace('$subs_domain', $subsDomain, $config);
  56. $config = str_replace('$proxies', $proxies, $config);
  57. $config = str_replace('$proxy_group', rtrim($proxyGroup, ', '), $config);
  58. return $config;
  59. }
  60. public static function buildShadowsocks($password, $server)
  61. {
  62. $config = [
  63. "{$server['name']}=ss",
  64. "{$server['host']}",
  65. "{$server['port']}",
  66. "encrypt-method={$server['cipher']}",
  67. "password={$password}",
  68. 'tfo=true',
  69. 'udp-relay=true'
  70. ];
  71. $config = array_filter($config);
  72. $uri = implode(',', $config);
  73. $uri .= "\r\n";
  74. return $uri;
  75. }
  76. public static function buildVmess($uuid, $server)
  77. {
  78. $config = [
  79. "{$server['name']}=vmess",
  80. "{$server['host']}",
  81. "{$server['port']}",
  82. "username={$uuid}",
  83. "vmess-aead=true",
  84. 'tfo=true',
  85. 'udp-relay=true'
  86. ];
  87. if ($server['tls']) {
  88. array_push($config, 'tls=true');
  89. if ($server['tlsSettings']) {
  90. $tlsSettings = $server['tlsSettings'];
  91. if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
  92. array_push($config, 'skip-cert-verify=' . ($tlsSettings['allowInsecure'] ? 'true' : 'false'));
  93. if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
  94. array_push($config, "sni={$tlsSettings['serverName']}");
  95. }
  96. }
  97. if ($server['network'] === 'ws') {
  98. array_push($config, 'ws=true');
  99. if ($server['networkSettings']) {
  100. $wsSettings = $server['networkSettings'];
  101. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  102. array_push($config, "ws-path={$wsSettings['path']}");
  103. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  104. array_push($config, "ws-headers=Host:{$wsSettings['headers']['Host']}");
  105. }
  106. }
  107. $uri = implode(',', $config);
  108. $uri .= "\r\n";
  109. return $uri;
  110. }
  111. public static function buildTrojan($password, $server)
  112. {
  113. $config = [
  114. "{$server['name']}=trojan",
  115. "{$server['host']}",
  116. "{$server['port']}",
  117. "password={$password}",
  118. $server['server_name'] ? "sni={$server['server_name']}" : "",
  119. 'tfo=true',
  120. 'udp-relay=true'
  121. ];
  122. if (!empty($server['allow_insecure'])) {
  123. array_push($config, $server['allow_insecure'] ? 'skip-cert-verify=true' : 'skip-cert-verify=false');
  124. }
  125. $config = array_filter($config);
  126. $uri = implode(',', $config);
  127. $uri .= "\r\n";
  128. return $uri;
  129. }
  130. }