可以修改访问未绑定域名时的状态码

This commit is contained in:
GoEdgeLab
2023-07-27 11:21:35 +08:00
parent cff41b1e30
commit 9361ea0cde

View File

@@ -8,6 +8,7 @@ import (
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
"github.com/TeaOSLab/EdgeNode/internal/ttlcache" "github.com/TeaOSLab/EdgeNode/internal/ttlcache"
"github.com/TeaOSLab/EdgeNode/internal/waf" "github.com/TeaOSLab/EdgeNode/internal/waf"
"github.com/iwind/TeaGo/types"
"net" "net"
"net/http" "net/http"
"time" "time"
@@ -35,9 +36,24 @@ func (this *HTTPRequest) doMismatch() {
// 根据配置进行相应的处理 // 根据配置进行相应的处理
var globalServerConfig = sharedNodeConfig.GlobalServerConfig var globalServerConfig = sharedNodeConfig.GlobalServerConfig
if globalServerConfig != nil && globalServerConfig.HTTPAll.MatchDomainStrictly { if globalServerConfig != nil && globalServerConfig.HTTPAll.MatchDomainStrictly {
var statusCode = 404
var httpAllConfig = globalServerConfig.HTTPAll
var mismatchAction = httpAllConfig.DomainMismatchAction
if mismatchAction != nil && mismatchAction.Options != nil {
var mismatchStatusCode = mismatchAction.Options.GetInt("statusCode")
if mismatchStatusCode > 0 && mismatchStatusCode >= 100 && mismatchStatusCode < 1000 {
statusCode = mismatchStatusCode
}
}
// 是否正在访问IP // 是否正在访问IP
if globalServerConfig.HTTPAll.NodeIPShowPage && net.ParseIP(this.ReqHost) != nil { if globalServerConfig.HTTPAll.NodeIPShowPage && net.ParseIP(this.ReqHost) != nil {
_, _ = this.writer.WriteString(this.Format(globalServerConfig.HTTPAll.NodeIPPageHTML)) var contentHTML = this.Format(globalServerConfig.HTTPAll.NodeIPPageHTML)
this.writer.Header().Set("Content-Type", "text/html; charset=utf-8")
this.writer.Header().Set("Content-Length", types.String(len(contentHTML)))
this.writer.WriteHeader(statusCode)
_, _ = this.writer.WriteString(contentHTML)
return return
} }
@@ -55,13 +71,13 @@ func (this *HTTPRequest) doMismatch() {
} }
// 处理当前连接 // 处理当前连接
var httpAllConfig = globalServerConfig.HTTPAll
var mismatchAction = httpAllConfig.DomainMismatchAction
if mismatchAction != nil && mismatchAction.Code == "page" { if mismatchAction != nil && mismatchAction.Code == "page" {
if mismatchAction.Options != nil { if mismatchAction.Options != nil {
var contentHTML = this.Format(mismatchAction.Options.GetString("contentHTML"))
this.writer.Header().Set("Content-Type", "text/html; charset=utf-8") this.writer.Header().Set("Content-Type", "text/html; charset=utf-8")
this.writer.WriteHeader(mismatchAction.Options.GetInt("statusCode")) this.writer.Header().Set("Content-Length", types.String(len(contentHTML)))
_, _ = this.writer.Write([]byte(this.Format(mismatchAction.Options.GetString("contentHTML")))) this.writer.WriteHeader(statusCode)
_, _ = this.writer.Write([]byte(contentHTML))
} else { } else {
http.Error(this.writer, "404 page not found: '"+this.URL()+"'", http.StatusNotFound) http.Error(this.writer, "404 page not found: '"+this.URL()+"'", http.StatusNotFound)
} }