From 8d9a4b6d4f76a567848495831ae8dfe2f548d9bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Wed, 10 Jan 2024 11:46:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DHTTP/3=E4=B8=8B=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E4=BC=A0=E9=80=92ServerAddr=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/http_request.go | 22 ++++++++++++++++++---- internal/nodes/listener_http.go | 9 +++++++-- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/internal/nodes/http_request.go b/internal/nodes/http_request.go index e00a204..f86a884 100644 --- a/internal/nodes/http_request.go +++ b/internal/nodes/http_request.go @@ -1430,11 +1430,25 @@ func (this *HTTPRequest) requestScheme() string { // 请求的服务器地址中的端口 func (this *HTTPRequest) requestServerPort() int { - _, port, err := net.SplitHostPort(this.ServerAddr) - if err == nil { - return types.Int(port) + if len(this.ServerAddr) > 0 { + _, port, err := net.SplitHostPort(this.ServerAddr) + if err == nil && len(port) > 0 { + return types.Int(port) + } } - return 0 + + var host = this.RawReq.Host + if len(host) > 0 { + _, port, err := net.SplitHostPort(host) + if err == nil && len(port) > 0 { + return types.Int(port) + } + } + + if this.IsHTTP { + return 80 + } + return 443 } func (this *HTTPRequest) Id() string { diff --git a/internal/nodes/listener_http.go b/internal/nodes/listener_http.go index a77d54a..1db1936 100644 --- a/internal/nodes/listener_http.go +++ b/internal/nodes/listener_http.go @@ -105,8 +105,13 @@ func (this *HTTPListener) Reload(group *serverconfigs.ServerAddressGroup) { this.Reset() } -// ServerHTTP 处理HTTP请求 +// ServeHTTPWithAddr 处理HTTP请求 func (this *HTTPListener) ServeHTTP(rawWriter http.ResponseWriter, rawReq *http.Request) { + this.ServeHTTPWithAddr(rawWriter, rawReq, this.addr) +} + +// ServeHTTPWithAddr 处理HTTP请求并指定服务地址 +func (this *HTTPListener) ServeHTTPWithAddr(rawWriter http.ResponseWriter, rawReq *http.Request, serverAddr string) { if len(rawReq.Host) > 253 { http.Error(rawWriter, "Host too long.", http.StatusBadRequest) time.Sleep(1 * time.Second) // make connection slow down @@ -211,7 +216,7 @@ func (this *HTTPListener) ServeHTTP(rawWriter http.ResponseWriter, rawReq *http. ReqServer: server, ReqHost: reqHost, ServerName: serverName, - ServerAddr: this.addr, + ServerAddr: serverAddr, IsHTTP: this.isHTTP, IsHTTPS: this.isHTTPS, IsHTTP3: this.isHTTP3,