From a0d18d93d39a0dfde2c1a53222b56807162092f6 Mon Sep 17 00:00:00 2001 From: Beta Soft Date: Sat, 3 Jul 2021 22:44:53 +0800 Subject: [PATCH] Protocols: add V2ray Client Protocol Usage: add &flag=v2rayng at the end of subscription link for V2rayNG Client add &flag=v2rayn at the end of subscription link for V2rayN Client --- .../Controllers/Client/Protocols/V2rayN.php | 60 +++++++++++++++++++ .../Controllers/Client/Protocols/V2rayNG.php | 60 +++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 app/Http/Controllers/Client/Protocols/V2rayN.php create mode 100644 app/Http/Controllers/Client/Protocols/V2rayNG.php diff --git a/app/Http/Controllers/Client/Protocols/V2rayN.php b/app/Http/Controllers/Client/Protocols/V2rayN.php new file mode 100644 index 00000000..ae6153fb --- /dev/null +++ b/app/Http/Controllers/Client/Protocols/V2rayN.php @@ -0,0 +1,60 @@ +user = $user; + $this->servers = $servers; + } + + public function handle() + { + $servers = $this->servers; + $user = $this->user; + $uri = ''; + + foreach ($servers as $item) { + if ($item['type'] === 'v2ray') { + $uri .= self::buildVmess($user['uuid'], $item); + } + } + return base64_encode($uri); + } + + public static function buildVmess($uuid, $server) + { + $config = [ + "v" => "2", + "ps" => $server['name'], + "add" => $server['host'], + "port" => (string)$server['port'], + "id" => $uuid, + "aid" => (string)$server['alter_id'], + "net" => $server['network'], + "type" => "none", + "host" => "", + "path" => "", + "tls" => $server['tls'] ? "tls" : "", + "sni" => $server['tls'] ? isset(json_decode($server['tlsSettings'], true)['serverName']) : "" + ]; + if ((string)$server['network'] === 'ws') { + $wsSettings = json_decode($server['networkSettings'], true); + if (isset($wsSettings['path'])) $config['path'] = $wsSettings['path']; + if (isset($wsSettings['headers']['Host'])) $config['host'] = $wsSettings['headers']['Host']; + } + if ((string)$server['network'] === 'grpc') { + $grpcSettings = json_decode($server['networkSettings'], true); + if (isset($grpcSettings['path'])) $config['path'] = $grpcSettings['serviceName']; + } + return "vmess://" . base64_encode(json_encode($config)) . "\r\n"; + } + +} diff --git a/app/Http/Controllers/Client/Protocols/V2rayNG.php b/app/Http/Controllers/Client/Protocols/V2rayNG.php new file mode 100644 index 00000000..b7544d91 --- /dev/null +++ b/app/Http/Controllers/Client/Protocols/V2rayNG.php @@ -0,0 +1,60 @@ +user = $user; + $this->servers = $servers; + } + + public function handle() + { + $servers = $this->servers; + $user = $this->user; + $uri = ''; + + foreach ($servers as $item) { + if ($item['type'] === 'v2ray') { + $uri .= self::buildVmess($user['uuid'], $item); + } + } + return base64_encode($uri); + } + + public static function buildVmess($uuid, $server) + { + $config = [ + "v" => "2", + "ps" => $server['name'], + "add" => $server['host'], + "port" => (string)$server['port'], + "id" => $uuid, + "aid" => (string)$server['alter_id'], + "net" => $server['network'], + "type" => "none", + "host" => "", + "path" => "", + "tls" => $server['tls'] ? "tls" : "", + "sni" => $server['tls'] ? isset(json_decode($server['tlsSettings'], true)['serverName']) : "" + ]; + if ((string)$server['network'] === 'ws') { + $wsSettings = json_decode($server['networkSettings'], true); + if (isset($wsSettings['path'])) $config['path'] = $wsSettings['path']; + if (isset($wsSettings['headers']['Host'])) $config['host'] = $wsSettings['headers']['Host']; + } + if ((string)$server['network'] === 'grpc') { + $grpcSettings = json_decode($server['networkSettings'], true); + if (isset($grpcSettings['path'])) $config['path'] = $grpcSettings['serviceName']; + } + return "vmess://" . base64_encode(json_encode($config)) . "\r\n"; + } + +}