From 4722379e793acccf9cf1f9f76730474eb38de5ce Mon Sep 17 00:00:00 2001 From: tokumeikoi Date: Sun, 4 Jul 2021 13:21:10 +0900 Subject: [PATCH] update: add new client protocol --- .../Controllers/Client/Protocols/V2rayN.php | 30 ++++++++++++++++++ .../Controllers/Client/Protocols/V2rayNG.php | 31 +++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/app/Http/Controllers/Client/Protocols/V2rayN.php b/app/Http/Controllers/Client/Protocols/V2rayN.php index ae6153fb..bd9aa005 100644 --- a/app/Http/Controllers/Client/Protocols/V2rayN.php +++ b/app/Http/Controllers/Client/Protocols/V2rayN.php @@ -25,10 +25,27 @@ class V2rayN if ($item['type'] === 'v2ray') { $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) + { + $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 = [ @@ -57,4 +74,17 @@ class V2rayN 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; + } + } diff --git a/app/Http/Controllers/Client/Protocols/V2rayNG.php b/app/Http/Controllers/Client/Protocols/V2rayNG.php index b7544d91..d765c372 100644 --- a/app/Http/Controllers/Client/Protocols/V2rayNG.php +++ b/app/Http/Controllers/Client/Protocols/V2rayNG.php @@ -25,10 +25,27 @@ class V2rayNG if ($item['type'] === 'v2ray') { $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) + { + $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 = [ @@ -57,4 +74,18 @@ class V2rayNG 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; + } + + }