Surfboard.php 4.8 KB

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