From 3d7d8f1e635b3e83dd5897dcf5507bd94c8fa037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Mon, 23 May 2022 11:34:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/listener_http.go | 81 +++++++++++++++++---------------- 1 file changed, 43 insertions(+), 38 deletions(-) diff --git a/internal/nodes/listener_http.go b/internal/nodes/listener_http.go index d97054c..0d2f1aa 100644 --- a/internal/nodes/listener_http.go +++ b/internal/nodes/listener_http.go @@ -172,44 +172,7 @@ func (this *HTTPListener) ServeHTTP(rawWriter http.ResponseWriter, rawReq *http. server, serverName := this.findNamedServer(domain) if server == nil { if server == nil { - // TODO 需要记录访问记录和防止CC - - // 是否为健康检查 - var healthCheckKey = rawReq.Header.Get(serverconfigs.HealthCheckHeaderName) - if len(healthCheckKey) > 0 { - _, err := nodeutils.Base64DecodeMap(healthCheckKey) - if err == nil { - rawWriter.WriteHeader(http.StatusOK) - return - } - } - - // 严格匹配域名模式下,我们拒绝用户访问 - if sharedNodeConfig.GlobalConfig != nil && sharedNodeConfig.GlobalConfig.HTTPAll.MatchDomainStrictly { - httpAllConfig := sharedNodeConfig.GlobalConfig.HTTPAll - mismatchAction := httpAllConfig.DomainMismatchAction - if mismatchAction != nil && mismatchAction.Code == "page" { - if mismatchAction.Options != nil { - rawWriter.Header().Set("Content-Type", "text/html; charset=utf-8") - rawWriter.WriteHeader(mismatchAction.Options.GetInt("statusCode")) - _, _ = rawWriter.Write([]byte(mismatchAction.Options.GetString("contentHTML"))) - } else { - http.Error(rawWriter, "404 page not found: '"+rawReq.URL.String()+"'", http.StatusNotFound) - } - return - } else { - hijacker, ok := rawWriter.(http.Hijacker) - if ok { - conn, _, _ := hijacker.Hijack() - if conn != nil { - _ = conn.Close() - return - } - } - } - } - - http.Error(rawWriter, "404 page not found: '"+rawReq.URL.String()+"'", http.StatusNotFound) + this.handleMismatch(rawReq, rawWriter) return } else { serverName = domain @@ -232,6 +195,48 @@ func (this *HTTPListener) ServeHTTP(rawWriter http.ResponseWriter, rawReq *http. req.Do() } +// 处理域名不匹配的情况 +func (this *HTTPListener) handleMismatch(rawReq *http.Request, rawWriter http.ResponseWriter) { + // TODO 需要记录访问记录和防止CC + + // 是否为健康检查 + var healthCheckKey = rawReq.Header.Get(serverconfigs.HealthCheckHeaderName) + if len(healthCheckKey) > 0 { + _, err := nodeutils.Base64DecodeMap(healthCheckKey) + if err == nil { + rawWriter.WriteHeader(http.StatusOK) + return + } + } + + // 严格匹配域名模式下,我们拒绝用户访问 + if sharedNodeConfig.GlobalConfig != nil && sharedNodeConfig.GlobalConfig.HTTPAll.MatchDomainStrictly { + var httpAllConfig = sharedNodeConfig.GlobalConfig.HTTPAll + var mismatchAction = httpAllConfig.DomainMismatchAction + if mismatchAction != nil && mismatchAction.Code == "page" { + if mismatchAction.Options != nil { + rawWriter.Header().Set("Content-Type", "text/html; charset=utf-8") + rawWriter.WriteHeader(mismatchAction.Options.GetInt("statusCode")) + _, _ = rawWriter.Write([]byte(mismatchAction.Options.GetString("contentHTML"))) + } else { + http.Error(rawWriter, "404 page not found: '"+rawReq.URL.String()+"'", http.StatusNotFound) + } + return + } else { + hijacker, ok := rawWriter.(http.Hijacker) + if ok { + conn, _, _ := hijacker.Hijack() + if conn != nil { + _ = conn.Close() + return + } + } + } + } + + http.Error(rawWriter, "404 page not found: '"+rawReq.URL.String()+"'", http.StatusNotFound) +} + func (this *HTTPListener) isIP(host string) bool { // IPv6 if strings.Index(host, "[") > -1 {