Surge.php 4.7 KB

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