From e35ec76f81aea580ef0f43ccaa6efdb4ad6f1a43 Mon Sep 17 00:00:00 2001 From: tokumeikoi Date: Thu, 29 Jul 2021 20:13:01 +0900 Subject: [PATCH] update: add subscribe anxray --- .../Controllers/Client/Protocols/AnXray.php | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 app/Http/Controllers/Client/Protocols/AnXray.php diff --git a/app/Http/Controllers/Client/Protocols/AnXray.php b/app/Http/Controllers/Client/Protocols/AnXray.php new file mode 100644 index 00000000..9bd5835a --- /dev/null +++ b/app/Http/Controllers/Client/Protocols/AnXray.php @@ -0,0 +1,93 @@ +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); + } + 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($uuid, $server) + { + $name = rawurlencode($server['name']); + $str = str_replace( + ['+', '/', '='], + ['-', '_', ''], + base64_encode("{$server['cipher']}:{$uuid}") + ); + return "ss://{$str}@{$server['host']}:{$server['port']}#{$name}\r\n"; + } + + public static function buildShadowsocksSIP008($uuid, $server) + { + $config = [ + "id" => $server['id'], + "remarks" => $server['name'], + "server" => $server['host'], + "server_port" => $server['port'], + "password" => $uuid, + "method" => $server['cipher'] + ]; + return $config; + } + + public static function buildVmess($uuid, $server) + { + $config = [ + "encryption" => "none", + "type" => urlencode($server['network']), + "security" => $server['tls'] ? "tls" : "", + "sni" => $server['tls'] ? urlencode(json_decode($server['tlsSettings'], true)['serverName']) : "" + ]; + if ((string)$server['network'] === 'ws') { + $wsSettings = json_decode($server['networkSettings'], true); + if (isset($wsSettings['path'])) $config['path'] = urlencode($wsSettings['path']); + if (isset($wsSettings['headers']['Host'])) $config['host'] = urlencode($wsSettings['headers']['Host']); + } + if ((string)$server['network'] === 'grpc') { + $grpcSettings = json_decode($server['networkSettings'], true); + if (isset($grpcSettings['serviceName'])) $config['serviceName'] = urlencode($grpcSettings['serviceName']); + } + return "vmess://" . $uuid . "@" . $server['host'] . ":" . $server['port'] . "?" . http_build_query($config) . "#" . urlencode($server['name']) . "\r\n"; + } + + public static function buildTrojan($uuid, $server) + { + $name = rawurlencode($server['name']); + $query = http_build_query([ + 'allowInsecure' => $server['allow_insecure'], + 'peer' => $server['server_name'], + 'sni' => $server['server_name'] + ]); + $uri = "trojan://{$uuid}@{$server['host']}:{$server['port']}?{$query}#{$name}"; + $uri .= "\r\n"; + return $uri; + } +}