Surfboard.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 .= Surfboard::buildShadowsocks($user['uuid'], $item);
  23. // [Proxy Group]
  24. $proxyGroup .= $item['name'] . ', ';
  25. }
  26. if ($item['type'] === 'v2ray') {
  27. // [Proxy]
  28. $proxies .= Surfboard::buildVmess($user['uuid'], $item);
  29. // [Proxy Group]
  30. $proxyGroup .= $item['name'] . ', ';
  31. }
  32. }
  33. $defaultConfig = base_path() . '/resources/rules/default.surfboard.conf';
  34. $customConfig = base_path() . '/resources/rules/custom.surfboard.conf';
  35. if (\File::exists($customConfig)) {
  36. $config = file_get_contents("$customConfig");
  37. } else {
  38. $config = file_get_contents("$defaultConfig");
  39. }
  40. // Subscription link
  41. $subsURL = config('v2board.subscribe_url', config('v2board.app_url', env('APP_URL'))) . '/api/v1/client/subscribe?token=' . $user['token'];
  42. $config = str_replace('$subs_link', $subsURL, $config);
  43. $config = str_replace('$proxies', $proxies, $config);
  44. $config = str_replace('$proxy_group', rtrim($proxyGroup, ', '), $config);
  45. return $config;
  46. }
  47. public static function buildShadowsocks($password, $server)
  48. {
  49. $config = [
  50. "{$server['name']}=custom",
  51. "{$server['host']}",
  52. "{$server['port']}",
  53. "{$server['cipher']}",
  54. "{$password}",
  55. 'https://raw.githubusercontent.com/Hackl0us/proxy-tool-backup/master/SSEncrypt.module',
  56. 'tfo=true',
  57. 'udp-relay=true'
  58. ];
  59. $config = array_filter($config);
  60. $uri = implode(',', $config);
  61. $uri .= "\r\n";
  62. return $uri;
  63. }
  64. public static function buildVmess($uuid, $server)
  65. {
  66. $config = [
  67. "{$server['name']}=vmess",
  68. "{$server['host']}",
  69. "{$server['port']}",
  70. "username={$uuid}",
  71. 'tfo=true',
  72. 'udp-relay=true'
  73. ];
  74. if ($server['tls']) {
  75. array_push($config, 'tls=true');
  76. if ($server['tlsSettings']) {
  77. $tlsSettings = json_decode($server['tlsSettings'], true);
  78. if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
  79. array_push($config, 'skip-cert-verify=' . ($tlsSettings['allowInsecure'] ? 'true' : 'false'));
  80. if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
  81. array_push($config, "sni={$tlsSettings['serverName']}");
  82. }
  83. }
  84. if ($server['network'] === 'ws') {
  85. array_push($config, 'ws=true');
  86. if ($server['networkSettings']) {
  87. $wsSettings = json_decode($server['networkSettings'], true);
  88. if (isset($wsSettings['path']) && !empty($wsSettings['path']))
  89. array_push($config, "ws-path={$wsSettings['path']}");
  90. if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
  91. array_push($config, "ws-headers=Host:{$wsSettings['headers']['Host']}");
  92. }
  93. }
  94. $uri = implode(',', $config);
  95. $uri .= "\r\n";
  96. return $uri;
  97. }
  98. }