Surge.php 5.0 KB

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