From 921b3fce4eb9875cc28f8f44b281f29f43e2907f Mon Sep 17 00:00:00 2001 From: v2board Date: Thu, 9 Mar 2023 20:36:35 +0800 Subject: [PATCH] update: add sub unmatch rule --- .../Controllers/Client/ClientController.php | 5 +- .../Controllers/Client/Protocols/General.php | 108 ++++++++++++++++++ 2 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 app/Http/Controllers/Client/Protocols/General.php diff --git a/app/Http/Controllers/Client/ClientController.php b/app/Http/Controllers/Client/ClientController.php index 3ec843f7..1b5c7302 100644 --- a/app/Http/Controllers/Client/ClientController.php +++ b/app/Http/Controllers/Client/ClientController.php @@ -2,7 +2,7 @@ namespace App\Http\Controllers\Client; -use App\Http\Controllers\Client\Protocols\V2rayN; +use App\Http\Controllers\Client\Protocols\General; use App\Http\Controllers\Controller; use App\Services\ServerService; use Illuminate\Http\Request; @@ -31,7 +31,8 @@ class ClientController extends Controller } } } - die('该客户端暂不支持进行订阅'); + $class = new General($user, $servers); + die($class->handle()); } } diff --git a/app/Http/Controllers/Client/Protocols/General.php b/app/Http/Controllers/Client/Protocols/General.php new file mode 100644 index 00000000..d7c641e2 --- /dev/null +++ b/app/Http/Controllers/Client/Protocols/General.php @@ -0,0 +1,108 @@ +user = $user; + $this->servers = $servers; + } + + public function handle() + { + $servers = $this->servers; + $user = $this->user; + $uri = ''; + + foreach ($servers as $item) { + if ($item['type'] === 'vmess') { + $uri .= self::buildVmess($user['uuid'], $item); + } + if ($item['type'] === 'shadowsocks') { + $uri .= self::buildShadowsocks($user['uuid'], $item); + } + if ($item['type'] === 'trojan') { + $uri .= self::buildTrojan($user['uuid'], $item); + } + } + return base64_encode($uri); + } + + public static function buildShadowsocks($password, $server) + { + if ($server['cipher'] === '2022-blake3-aes-128-gcm') { + $serverKey = Helper::getServerKey($server['created_at'], 16); + $userKey = Helper::uuidToBase64($password, 16); + $password = "{$serverKey}:{$userKey}"; + } + if ($server['cipher'] === '2022-blake3-aes-256-gcm') { + $serverKey = Helper::getServerKey($server['created_at'], 32); + $userKey = Helper::uuidToBase64($password, 32); + $password = "{$serverKey}:{$userKey}"; + } + $name = rawurlencode($server['name']); + $str = str_replace( + ['+', '/', '='], + ['-', '_', ''], + base64_encode("{$server['cipher']}:{$password}") + ); + return "ss://{$str}@{$server['host']}:{$server['port']}#{$name}\r\n"; + } + + public static function buildVmess($uuid, $server) + { + $config = [ + "v" => "2", + "ps" => $server['name'], + "add" => $server['host'], + "port" => (string)$server['port'], + "id" => $uuid, + "aid" => '0', + "net" => $server['network'], + "type" => "none", + "host" => "", + "path" => "", + "tls" => $server['tls'] ? "tls" : "", + ]; + if ($server['tls']) { + if ($server['tlsSettings']) { + $tlsSettings = $server['tlsSettings']; + if (isset($tlsSettings['serverName']) && !empty($tlsSettings['serverName'])) + $config['sni'] = $tlsSettings['serverName']; + } + } + if ((string)$server['network'] === 'ws') { + $wsSettings = $server['networkSettings']; + if (isset($wsSettings['path'])) $config['path'] = $wsSettings['path']; + if (isset($wsSettings['headers']['Host'])) $config['host'] = $wsSettings['headers']['Host']; + } + if ((string)$server['network'] === 'grpc') { + $grpcSettings = $server['networkSettings']; + if (isset($grpcSettings['serviceName'])) $config['path'] = $grpcSettings['serviceName']; + } + return "vmess://" . base64_encode(json_encode($config)) . "\r\n"; + } + + public static function buildTrojan($password, $server) + { + $name = rawurlencode($server['name']); + $query = http_build_query([ + 'allowInsecure' => $server['allow_insecure'], + 'peer' => $server['server_name'], + 'sni' => $server['server_name'] + ]); + $uri = "trojan://{$password}@{$server['host']}:{$server['port']}?{$query}#{$name}"; + $uri .= "\r\n"; + return $uri; + } + +}