123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- <?php
- namespace App\Http\Controllers\Client\Protocols;
- class Surfboard
- {
- public $flag = 'surfboard';
- private $servers;
- private $user;
- public function __construct($user, $servers)
- {
- $this->user = $user;
- $this->servers = $servers;
- }
- public function handle()
- {
- $servers = $this->servers;
- $user = $this->user;
- $proxies = '';
- $proxyGroup = '';
- foreach ($servers as $item) {
- if ($item['type'] === 'shadowsocks') {
- // [Proxy]
- $proxies .= Surfboard::buildShadowsocks($user['uuid'], $item);
- // [Proxy Group]
- $proxyGroup .= $item['name'] . ', ';
- }
- if ($item['type'] === 'v2ray') {
- // [Proxy]
- $proxies .= Surfboard::buildVmess($user['uuid'], $item);
- // [Proxy Group]
- $proxyGroup .= $item['name'] . ', ';
- }
- }
- $defaultConfig = base_path() . '/resources/rules/default.surfboard.conf';
- $customConfig = base_path() . '/resources/rules/custom.surfboard.conf';
- if (\File::exists($customConfig)) {
- $config = file_get_contents("$customConfig");
- } else {
- $config = file_get_contents("$defaultConfig");
- }
- // Subscription link
- $subsURL = config('v2board.subscribe_url', config('v2board.app_url', env('APP_URL'))) . '/api/v1/client/subscribe?token=' . $user['token'];
- $config = str_replace('$subs_link', $subsURL, $config);
- $config = str_replace('$proxies', $proxies, $config);
- $config = str_replace('$proxy_group', rtrim($proxyGroup, ', '), $config);
- return $config;
- }
- public static function buildShadowsocks($password, $server)
- {
- $config = [
- "{$server['name']}=custom",
- "{$server['host']}",
- "{$server['port']}",
- "{$server['cipher']}",
- "{$password}",
- 'https://raw.githubusercontent.com/Hackl0us/proxy-tool-backup/master/SSEncrypt.module',
- 'tfo=true',
- 'udp-relay=true'
- ];
- $config = array_filter($config);
- $uri = implode(',', $config);
- $uri .= "\r\n";
- return $uri;
- }
- public static function buildVmess($uuid, $server)
- {
- $config = [
- "{$server['name']}=vmess",
- "{$server['host']}",
- "{$server['port']}",
- "username={$uuid}",
- 'tfo=true',
- 'udp-relay=true'
- ];
- if ($server['tls']) {
- array_push($config, 'tls=true');
- if ($server['tlsSettings']) {
- $tlsSettings = json_decode($server['tlsSettings'], true);
- if (isset($tlsSettings['allowInsecure']) && !empty($tlsSettings['allowInsecure']))
- array_push($config, 'skip-cert-verify=' . ($tlsSettings['allowInsecure'] ? 'true' : 'false'));
- if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName']))
- array_push($config, "sni={$tlsSettings['serverName']}");
- }
- }
- if ($server['network'] === 'ws') {
- array_push($config, 'ws=true');
- if ($server['networkSettings']) {
- $wsSettings = json_decode($server['networkSettings'], true);
- if (isset($wsSettings['path']) && !empty($wsSettings['path']))
- array_push($config, "ws-path={$wsSettings['path']}");
- if (isset($wsSettings['headers']['Host']) && !empty($wsSettings['headers']['Host']))
- array_push($config, "ws-headers=Host:{$wsSettings['headers']['Host']}");
- }
- }
- $uri = implode(',', $config);
- $uri .= "\r\n";
- return $uri;
- }
- }
|