diff --git a/internal/nodes/http_request_reverse_proxy.go b/internal/nodes/http_request_reverse_proxy.go index 5ab2304..94d9421 100644 --- a/internal/nodes/http_request_reverse_proxy.go +++ b/internal/nodes/http_request_reverse_proxy.go @@ -146,6 +146,12 @@ func (this *HTTPRequest) doReverseProxy() { } else { this.RawReq.Host = requestHost } + + // 是否移除端口 + if this.reverseProxy.RequestHostExcludingPort { + this.RawReq.Host = utils.ParseAddrHost(this.RawReq.Host) + } + this.RawReq.URL.Host = this.RawReq.Host } else if this.reverseProxy.RequestHostType == serverconfigs.RequestHostTypeOrigin { // 源站主机名 @@ -157,9 +163,21 @@ func (this *HTTPRequest) doReverseProxy() { } this.RawReq.Host = hostname + + // 是否移除端口 + if this.reverseProxy.RequestHostExcludingPort { + this.RawReq.Host = utils.ParseAddrHost(this.RawReq.Host) + } + this.RawReq.URL.Host = this.RawReq.Host } else { this.RawReq.URL.Host = this.ReqHost + + // 是否移除端口 + if this.reverseProxy.RequestHostExcludingPort { + this.RawReq.Host = utils.ParseAddrHost(this.RawReq.Host) + this.RawReq.URL.Host = utils.ParseAddrHost(this.RawReq.URL.Host) + } } // 重组请求URL diff --git a/internal/nodes/http_request_utils.go b/internal/nodes/http_request_utils.go index 79789e4..320ede1 100644 --- a/internal/nodes/http_request_utils.go +++ b/internal/nodes/http_request_utils.go @@ -208,20 +208,3 @@ func httpAcceptEncoding(acceptEncodings string, encoding string) bool { } return false } - -// 分隔编码 -func httpAcceptEncodings(acceptEncodings string) (encodings []string) { - if len(acceptEncodings) == 0 { - return - } - var pieces = strings.Split(acceptEncodings, ",") - for _, piece := range pieces { - var qualityIndex = strings.Index(piece, ";") - if qualityIndex >= 0 { - piece = piece[:qualityIndex] - } - - encodings = append(encodings, strings.TrimSpace(piece)) - } - return -} diff --git a/internal/utils/net.go b/internal/utils/net.go index 0d1fd0e..d702bc4 100644 --- a/internal/utils/net.go +++ b/internal/utils/net.go @@ -25,3 +25,16 @@ func ListenReuseAddr(network string, addr string) (net.Listener, error) { } return config.Listen(context.Background(), network, addr) } + +// ParseAddrHost 分析地址中的主机名部分 +func ParseAddrHost(addr string) string { + if len(addr) == 0 { + return addr + } + + host, _, err := net.SplitHostPort(addr) + if err != nil { + return addr + } + return host +} diff --git a/internal/utils/net_test.go b/internal/utils/net_test.go new file mode 100644 index 0000000..513c668 --- /dev/null +++ b/internal/utils/net_test.go @@ -0,0 +1,14 @@ +// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn . + +package utils_test + +import ( + "github.com/TeaOSLab/EdgeNode/internal/utils" + "testing" +) + +func TestParseAddrHost(t *testing.T) { + for _, addr := range []string{"a", "example.com", "example.com:1234", "::1", "[::1]", "[::1]:8080"} { + t.Log(addr + " => " + utils.ParseAddrHost(addr)) + } +}