优化连接代码/细化反向代理相关错误和警告提示

This commit is contained in:
GoEdgeLab
2022-07-15 11:15:55 +08:00
parent 37f68ac640
commit 0e8a42ef75
3 changed files with 25 additions and 18 deletions

View File

@@ -86,11 +86,13 @@ func (this *ClientConn) Read(b []byte) (n int, err error) {
} }
} }
// SYN Flood检测
var isHandshakeError = err != nil && os.IsTimeout(err) && !this.hasRead var isHandshakeError = err != nil && os.IsTimeout(err) && !this.hasRead
if isHandshakeError { if isHandshakeError {
_ = this.SetLinger(0) _ = this.SetLinger(0)
} }
// SYN Flood检测
if this.serverId == 0 || !this.hasResetSYNFlood {
var synFloodConfig = sharedNodeConfig.SYNFloodConfig() var synFloodConfig = sharedNodeConfig.SYNFloodConfig()
if synFloodConfig != nil && synFloodConfig.IsOn { if synFloodConfig != nil && synFloodConfig.IsOn {
if isHandshakeError { if isHandshakeError {
@@ -100,6 +102,7 @@ func (this *ClientConn) Read(b []byte) (n int, err error) {
this.resetSYNFlood() this.resetSYNFlood()
} }
} }
}
return return
} }
@@ -126,7 +129,9 @@ func (this *ClientConn) Close() error {
err := this.rawConn.Close() err := this.rawConn.Close()
// 单个服务并发数限制 // 单个服务并发数限制
if this.hasLimit {
sharedClientConnLimiter.Remove(this.rawConn.RemoteAddr().String()) sharedClientConnLimiter.Remove(this.rawConn.RemoteAddr().String())
}
return err return err
} }

View File

@@ -11,6 +11,7 @@ type BaseClientConn struct {
userId int64 userId int64
serverId int64 serverId int64
remoteAddr string remoteAddr string
hasLimit bool
isClosed bool isClosed bool
} }
@@ -32,6 +33,7 @@ func (this *BaseClientConn) Bind(serverId int64, remoteAddr string, maxConnsPerS
this.isBound = true this.isBound = true
this.serverId = serverId this.serverId = serverId
this.remoteAddr = remoteAddr this.remoteAddr = remoteAddr
this.hasLimit = true
// 检查是否可以连接 // 检查是否可以连接
return sharedClientConnLimiter.Add(this.rawConn.RemoteAddr().String(), serverId, remoteAddr, maxConnsPerServer, maxConnsPerIP) return sharedClientConnLimiter.Add(this.rawConn.RemoteAddr().String(), serverId, remoteAddr, maxConnsPerServer, maxConnsPerIP)

View File

@@ -78,7 +78,7 @@ func (this *HTTPRequest) doReverseProxy() {
// 处理Scheme // 处理Scheme
if origin.Addr == nil { if origin.Addr == nil {
err := errors.New(this.URL() + ": origin '" + strconv.FormatInt(origin.Id, 10) + "' does not has a address") err := errors.New(this.URL() + ": Origin '" + strconv.FormatInt(origin.Id, 10) + "' does not has a address")
remotelogs.Error("HTTP_REQUEST_REVERSE_PROXY", err.Error()) remotelogs.Error("HTTP_REQUEST_REVERSE_PROXY", err.Error())
this.write50x(err, http.StatusBadGateway, true) this.write50x(err, http.StatusBadGateway, true)
return return
@@ -130,7 +130,7 @@ func (this *HTTPRequest) doReverseProxy() {
if origin.FollowPort { if origin.FollowPort {
var originHostIndex = strings.Index(originAddr, ":") var originHostIndex = strings.Index(originAddr, ":")
if originHostIndex < 0 { if originHostIndex < 0 {
var originErr = errors.New("invalid origin address '" + originAddr + "', lacking port") var originErr = errors.New(this.URL() + ": Invalid origin address '" + originAddr + "', lacking port")
remotelogs.Error("HTTP_REQUEST_REVERSE_PROXY", originErr.Error()) remotelogs.Error("HTTP_REQUEST_REVERSE_PROXY", originErr.Error())
this.write50x(originErr, http.StatusBadGateway, true) this.write50x(originErr, http.StatusBadGateway, true)
return return
@@ -210,7 +210,7 @@ func (this *HTTPRequest) doReverseProxy() {
// 获取请求客户端 // 获取请求客户端
client, err := SharedHTTPClientPool.Client(this, origin, originAddr, this.reverseProxy.ProxyProtocol, this.reverseProxy.FollowRedirects) client, err := SharedHTTPClientPool.Client(this, origin, originAddr, this.reverseProxy.ProxyProtocol, this.reverseProxy.FollowRedirects)
if err != nil { if err != nil {
remotelogs.Error("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": get client failed: "+err.Error()) remotelogs.Error("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Create client failed: "+err.Error())
this.write50x(err, http.StatusBadGateway, true) this.write50x(err, http.StatusBadGateway, true)
return return
} }
@@ -231,7 +231,7 @@ func (this *HTTPRequest) doReverseProxy() {
this.reverseProxy.ResetScheduling() this.reverseProxy.ResetScheduling()
}) })
this.write50x(err, http.StatusBadGateway, true) this.write50x(err, http.StatusBadGateway, true)
remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.RawReq.URL.String()+"': request failed: "+err.Error()) remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.RawReq.URL.String()+": Request origin server failed: "+err.Error())
} else if httpErr.Err != context.Canceled { } else if httpErr.Err != context.Canceled {
SharedOriginStateManager.Fail(origin, requestHost, this.reverseProxy, func() { SharedOriginStateManager.Fail(origin, requestHost, this.reverseProxy, func() {
this.reverseProxy.ResetScheduling() this.reverseProxy.ResetScheduling()
@@ -244,7 +244,7 @@ func (this *HTTPRequest) doReverseProxy() {
this.write50x(err, http.StatusBadGateway, true) this.write50x(err, http.StatusBadGateway, true)
} }
if httpErr.Err != io.EOF { if httpErr.Err != io.EOF {
remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": request failed: "+err.Error()) remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Request origin server failed: "+err.Error())
} }
} else { } else {
// 是否为客户端方面的错误 // 是否为客户端方面的错误
@@ -283,7 +283,7 @@ func (this *HTTPRequest) doReverseProxy() {
if this.doWAFResponse(resp) { if this.doWAFResponse(resp) {
err = resp.Body.Close() err = resp.Body.Close()
if err != nil { if err != nil {
remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": "+err.Error()) remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Closing Error (WAF): "+err.Error())
} }
return return
} }
@@ -293,7 +293,7 @@ func (this *HTTPRequest) doReverseProxy() {
if len(this.web.Pages) > 0 && this.doPage(resp.StatusCode) { if len(this.web.Pages) > 0 && this.doPage(resp.StatusCode) {
err = resp.Body.Close() err = resp.Body.Close()
if err != nil { if err != nil {
remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": "+err.Error()) remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Closing error (Page): "+err.Error())
} }
return return
} }
@@ -395,13 +395,13 @@ func (this *HTTPRequest) doReverseProxy() {
var closeErr = resp.Body.Close() var closeErr = resp.Body.Close()
if closeErr != nil { if closeErr != nil {
if !this.canIgnore(closeErr) { if !this.canIgnore(closeErr) {
remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": closing error: "+closeErr.Error()) remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Closing error: "+closeErr.Error())
} }
} }
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
if !this.canIgnore(err) { if !this.canIgnore(err) {
remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": writing error: "+err.Error()) remotelogs.Warn("HTTP_REQUEST_REVERSE_PROXY", this.URL()+": Writing error: "+err.Error())
this.addError(err) this.addError(err)
} }
} }