From 95abb7bfae3042a461959cfa1e39361ce21e4d89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Sat, 1 Apr 2023 09:57:24 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E7=BE=A4=E6=9C=8D=E5=8A=A1=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E5=A2=9E=E5=8A=A0=E2=80=9C=E6=94=AF=E6=8C=81=E4=BD=8E?= =?UTF-8?q?=E7=89=88=E6=9C=ACHTTP=E2=80=9D=E9=80=89=E9=A1=B9/=E5=88=86?= =?UTF-8?q?=E7=89=87=E5=86=85=E5=AE=B9=E6=8F=90=E7=A4=BA=E4=B8=8D=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E4=BD=8E=E7=89=88=E6=9C=ACHTTP=E5=8D=8F=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/http_request_reverse_proxy.go | 16 ++++++++++++++-- internal/nodes/listener_http.go | 6 ++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/internal/nodes/http_request_reverse_proxy.go b/internal/nodes/http_request_reverse_proxy.go index 30f6e3b..2f406c9 100644 --- a/internal/nodes/http_request_reverse_proxy.go +++ b/internal/nodes/http_request_reverse_proxy.go @@ -21,13 +21,15 @@ func (this *HTTPRequest) doReverseProxy() { return } + var isLowVersionHTTP = this.RawReq.ProtoMajor < 1 /** 0.x **/ || (this.RawReq.ProtoMajor == 1 && this.RawReq.ProtoMinor == 0 /** 1.0 **/) + var retries = 3 var failedOriginIds []int64 var failedLnNodeIds []int64 for i := 0; i < retries; i++ { - originId, lnNodeId, shouldRetry := this.doOriginRequest(failedOriginIds, failedLnNodeIds, i == 0, i == retries-1) + originId, lnNodeId, shouldRetry := this.doOriginRequest(failedOriginIds, failedLnNodeIds, i == 0, i == retries-1, isLowVersionHTTP) if !shouldRetry { break } @@ -41,7 +43,7 @@ func (this *HTTPRequest) doReverseProxy() { } // 请求源站 -func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeIds []int64, isFirstTry bool, isLastRetry bool) (originId int64, lnNodeId int64, shouldRetry bool) { +func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeIds []int64, isFirstTry bool, isLastRetry bool, isLowVersionHTTP bool) (originId int64, lnNodeId int64, shouldRetry bool) { // 对URL的处理 var stripPrefix = this.reverseProxy.StripPrefix var requestURI = this.reverseProxy.RequestURI @@ -321,6 +323,16 @@ func (this *HTTPRequest) doOriginRequest(failedOriginIds []int64, failedLnNodeId return } + // 是否为1.1以下 + if isLowVersionHTTP && resp.ContentLength < 0 { + this.writer.WriteHeader(http.StatusBadRequest) + _, _ = this.writer.WriteString("The content does not support " + this.RawReq.Proto + " request.") + if resp.Body != nil { + _ = resp.Body.Close() + } + return + } + // 记录相关数据 this.originStatus = int32(resp.StatusCode) diff --git a/internal/nodes/listener_http.go b/internal/nodes/listener_http.go index f9c660b..071032e 100644 --- a/internal/nodes/listener_http.go +++ b/internal/nodes/listener_http.go @@ -114,6 +114,12 @@ func (this *HTTPListener) Reload(group *serverconfigs.ServerAddressGroup) { // ServerHTTP 处理HTTP请求 func (this *HTTPListener) ServeHTTP(rawWriter http.ResponseWriter, rawReq *http.Request) { + var globalServerConfig = sharedNodeConfig.GlobalServerConfig + if globalServerConfig != nil && !globalServerConfig.HTTPAll.SupportsLowVersionHTTP && (rawReq.ProtoMajor < 1 /** 0.x **/ || (rawReq.ProtoMajor == 1 && rawReq.ProtoMinor == 0 /** 1.0 **/)) { + http.Error(rawWriter, rawReq.Proto+" request is not supported.", http.StatusBadRequest) + return + } + // 不支持Connect if rawReq.Method == http.MethodConnect { http.Error(rawWriter, "Method Not Allowed", http.StatusMethodNotAllowed)