From d57e031424c55e53a327c7faa1655c62ff97ee46 Mon Sep 17 00:00:00 2001 From: Alireza Ahmadi Date: Mon, 15 Apr 2024 20:00:52 +0200 Subject: [PATCH] ws host+backward compatibility #1154 --- sub/subService.go | 32 ++++++++++++++++++------ web/assets/js/model/xray.js | 50 ++++++++++++++++++------------------- 2 files changed, 49 insertions(+), 33 deletions(-) diff --git a/sub/subService.go b/sub/subService.go index 2241e2c5..72728f8d 100644 --- a/sub/subService.go +++ b/sub/subService.go @@ -194,8 +194,12 @@ func (s *SubService) genVmessLink(inbound *model.Inbound, email string) string { case "ws": ws, _ := stream["wsSettings"].(map[string]interface{}) obj["path"] = ws["path"].(string) - headers, _ := ws["headers"].(map[string]interface{}) - obj["host"] = searchHost(headers) + if host, ok := ws["host"].(string); ok && len(host) > 0 { + obj["host"] = host + } else { + headers, _ := ws["headers"].(map[string]interface{}) + obj["host"] = searchHost(headers) + } case "http": obj["net"] = "h2" http, _ := stream["httpSettings"].(map[string]interface{}) @@ -334,8 +338,12 @@ func (s *SubService) genVlessLink(inbound *model.Inbound, email string) string { case "ws": ws, _ := stream["wsSettings"].(map[string]interface{}) params["path"] = ws["path"].(string) - headers, _ := ws["headers"].(map[string]interface{}) - params["host"] = searchHost(headers) + if host, ok := ws["host"].(string); ok && len(host) > 0 { + params["host"] = host + } else { + headers, _ := ws["headers"].(map[string]interface{}) + params["host"] = searchHost(headers) + } case "http": http, _ := stream["httpSettings"].(map[string]interface{}) params["path"] = http["path"].(string) @@ -520,8 +528,12 @@ func (s *SubService) genTrojanLink(inbound *model.Inbound, email string) string case "ws": ws, _ := stream["wsSettings"].(map[string]interface{}) params["path"] = ws["path"].(string) - headers, _ := ws["headers"].(map[string]interface{}) - params["host"] = searchHost(headers) + if host, ok := ws["host"].(string); ok && len(host) > 0 { + params["host"] = host + } else { + headers, _ := ws["headers"].(map[string]interface{}) + params["host"] = searchHost(headers) + } case "http": http, _ := stream["httpSettings"].(map[string]interface{}) params["path"] = http["path"].(string) @@ -702,8 +714,12 @@ func (s *SubService) genShadowsocksLink(inbound *model.Inbound, email string) st case "ws": ws, _ := stream["wsSettings"].(map[string]interface{}) params["path"] = ws["path"].(string) - headers, _ := ws["headers"].(map[string]interface{}) - params["host"] = searchHost(headers) + if host, ok := ws["host"].(string); ok && len(host) > 0 { + params["host"] = host + } else { + headers, _ := ws["headers"].(map[string]interface{}) + params["host"] = searchHost(headers) + } case "http": http, _ := stream["httpSettings"].(map[string]interface{}) params["path"] = http["path"].(string) diff --git a/web/assets/js/model/xray.js b/web/assets/js/model/xray.js index 021423c2..72b46bd6 100644 --- a/web/assets/js/model/xray.js +++ b/web/assets/js/model/xray.js @@ -206,15 +206,6 @@ TcpStreamSettings.TcpRequest = class extends XrayCommonClass { this.headers.push({ name: name, value: value }); } - getHeader(name) { - for (const header of this.headers) { - if (header.name.toLowerCase() === name.toLowerCase()) { - return header.value; - } - } - return null; - } - removeHeader(index) { this.headers.splice(index, 1); } @@ -951,12 +942,23 @@ class Inbound extends XrayCommonClass { return ''; } + getHeader(obj, name) { + for (const header of obj.headers) { + if (header.name.toLowerCase() === name.toLowerCase()) { + return header.value; + } + } + return null; + } + get host() { if (this.isTcp) { - return this.stream.tcp.request.getHeader("Host"); + return this.getHeader(this.stream.tcp.request, 'host'); } else if (this.isH2) { return this.stream.http.host[0]; - } else if (this.isHttpupgrade || this.isWs) { + } else if (this.isWs) { + return this.stream.ws.host?.length>0 ? this.stream.ws.host : this.getHeader(this.stream.ws, 'host'); + } else if (this.isHttpupgrade) { return this.stream.httpupgrade.host; } return null; @@ -1051,26 +1053,24 @@ class Inbound extends XrayCommonClass { type: 'none', tls: security, }; - let network = this.stream.network; + const network = this.stream.network; if (network === 'tcp') { - let tcp = this.stream.tcp; + const tcp = this.stream.tcp; obj.type = tcp.type; if (tcp.type === 'http') { - let request = tcp.request; + const request = tcp.request; obj.path = request.path.join(','); - let index = request.headers.findIndex(header => header.name.toLowerCase() === 'host'); - if (index >= 0) { - obj.host = request.headers[index].value; - } + const host = this.getHeader(request,'host'); + if (host) obj.host = host; } } else if (network === 'kcp') { - let kcp = this.stream.kcp; + const kcp = this.stream.kcp; obj.type = kcp.type; obj.path = kcp.seed; } else if (network === 'ws') { - let ws = this.stream.ws; + const ws = this.stream.ws; obj.path = ws.path; - obj.host = ws.host; + obj.host = ws.host?.length>0 ? ws.host : this.getHeader(ws, 'host'); } else if (network === 'http') { obj.net = 'h2'; obj.path = this.stream.http.path; @@ -1086,7 +1086,7 @@ class Inbound extends XrayCommonClass { obj.type = 'multi' } } else if (network === 'httpupgrade') { - let httpupgrade = this.stream.httpupgrade; + const httpupgrade = this.stream.httpupgrade; obj.path = httpupgrade.path; obj.host = httpupgrade.host; } @@ -1137,7 +1137,7 @@ class Inbound extends XrayCommonClass { case "ws": const ws = this.stream.ws; params.set("path", ws.path); - params.set("host", httpupgrade.host); + params.set("host", ws.host?.length>0 ? ws.host : this.getHeader(ws, 'host')); break; case "http": const http = this.stream.http; @@ -1241,7 +1241,7 @@ class Inbound extends XrayCommonClass { case "ws": const ws = this.stream.ws; params.set("path", ws.path); - params.set("host", httpupgrade.host); + params.set("host", ws.host?.length>0 ? ws.host : this.getHeader(ws, 'host')); break; case "http": const http = this.stream.http; @@ -1324,7 +1324,7 @@ class Inbound extends XrayCommonClass { case "ws": const ws = this.stream.ws; params.set("path", ws.path); - params.set("host", httpupgrade.host); + params.set("host", ws.host?.length>0 ? ws.host : this.getHeader(ws, 'host')); break; case "http": const http = this.stream.http;