Surfboard.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Http\Controllers\Client\Protocols;
  3. class Surfboard
  4. {
  5. public $flag = 'surfboard';
  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.surfboard.conf';
  40. $customConfig = base_path() . '/resources/rules/custom.surfboard.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. $config = str_replace('$subs_link', $subsURL, $config);
  49. $config = str_replace('$proxies', $proxies, $config);
  50. $config = str_replace('$proxy_group', rtrim($proxyGroup, ', '), $config);
  51. return $config;
  52. }
  53. public static function buildShadowsocks($password, $server)
  54. {
  55. $config = [
  56. "{$server['name']}=custom",
  57. "{$server['host']}",
  58. "{$server['port']}",
  59. "{$server['cipher']}",
  60. "{$password}",
  61. 'https://raw.githubusercontent.com/Hackl0us/proxy-tool-backup/master/SSEncrypt.module',
  62. 'tfo=true',
  63. 'udp-relay=true'
  64. ];
  65. $config = array_filter($config);
  66. $uri = implode(',', $config);
  67. $uri .= "\r\n";
  68. return $uri;
  69. }
  70. public static function buildVmess($uuid, $server)
  71. {
  72. $config = [
  73. "{$server['name']}=vmess",
  74. "{$server['host']}",
  75. "{$server['port']}",
  76. "username={$uuid}",
  77. 'tfo=true',
  78. 'udp-relay=true'
  79. ];
  80. if ($server['tls']) {
  81. array_push($config, 'tls=true');
  82. if ($server['tlsSettings']) {
  83. $tlsSettings = $server['tlsSettings'];
  84. if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
  85. array_push($config, 'skip-cert-verify=' . ($tlsSettings['allowInsecure'] ? 'true' : 'false'));
  86. if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
  87. array_push($config, "sni={$tlsSettings['serverName']}");
  88. }
  89. }
  90. if ($server['network'] === 'ws') {
  91. array_push($config, 'ws=true');
  92. if ($server['networkSettings']) {
  93. $wsSettings = $server['networkSettings'];
  94. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  95. array_push($config, "ws-path={$wsSettings['path']}");
  96. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  97. array_push($config, "ws-headers=Host:{$wsSettings['headers']['Host']}");
  98. }
  99. }
  100. $uri = implode(',', $config);
  101. $uri .= "\r\n";
  102. return $uri;
  103. }
  104. public static function buildTrojan($password, $server)
  105. {
  106. $config = [
  107. "{$server['name']}=trojan",
  108. "{$server['host']}",
  109. "{$server['port']}",
  110. "password={$password}",
  111. $server['server_name'] ? "sni={$server['server_name']}" : "",
  112. 'tfo=true',
  113. 'udp-relay=true'
  114. ];
  115. if (!empty($server['allow_insecure'])) {
  116. array_push($config, $server['allow_insecure'] ? 'skip-cert-verify=true' : 'skip-cert-verify=false');
  117. }
  118. $config = array_filter($config);
  119. $uri = implode(',', $config);
  120. $uri .= "\r\n";
  121. return $uri;
  122. }
  123. }