Surge.php 4.7 KB

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