mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-27 01:50:30 +08:00
优化WAF关闭连接操作
This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
|||||||
"github.com/iwind/TeaGo/types"
|
"github.com/iwind/TeaGo/types"
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -305,3 +306,17 @@ func (this *HTTPRequest) WAFRestoreBody(data []byte) {
|
|||||||
func (this *HTTPRequest) WAFServerId() int64 {
|
func (this *HTTPRequest) WAFServerId() int64 {
|
||||||
return this.Server.Id
|
return this.Server.Id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WAFClose 关闭连接
|
||||||
|
func (this *HTTPRequest) WAFClose() {
|
||||||
|
requestConn := this.RawReq.Context().Value(HTTPConnContextKey)
|
||||||
|
if requestConn == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
conn, ok := requestConn.(net.Conn)
|
||||||
|
if ok {
|
||||||
|
_ = conn.Close()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|||||||
@@ -66,16 +66,7 @@ func (this *BlockAction) Perform(waf *WAF, group *RuleGroup, set *RuleSet, reque
|
|||||||
|
|
||||||
if writer != nil {
|
if writer != nil {
|
||||||
// close the connection
|
// close the connection
|
||||||
defer func() {
|
defer request.WAFClose()
|
||||||
hijack, ok := writer.(http.Hijacker)
|
|
||||||
if ok {
|
|
||||||
conn, _, _ := hijack.Hijack()
|
|
||||||
if conn != nil {
|
|
||||||
_ = conn.Close()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
// output response
|
// output response
|
||||||
if this.StatusCode > 0 {
|
if this.StatusCode > 0 {
|
||||||
@@ -128,5 +119,6 @@ func (this *BlockAction) Perform(waf *WAF, group *RuleGroup, set *RuleSet, reque
|
|||||||
_, _ = writer.Write([]byte("The request is blocked by " + teaconst.ProductName))
|
_, _ = writer.Write([]byte("The request is blocked by " + teaconst.ProductName))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,9 @@ type Request interface {
|
|||||||
// WAFServerId 服务ID
|
// WAFServerId 服务ID
|
||||||
WAFServerId() int64
|
WAFServerId() int64
|
||||||
|
|
||||||
|
// WAFClose 关闭当前请求所在的连接
|
||||||
|
WAFClose()
|
||||||
|
|
||||||
// Format 格式化变量
|
// Format 格式化变量
|
||||||
Format(string) string
|
Format(string) string
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,6 +66,10 @@ func (this *TestRequest) WAFServerId() int64 {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WAFClose 关闭当前请求所在的连接
|
||||||
|
func (this *TestRequest) WAFClose() {
|
||||||
|
}
|
||||||
|
|
||||||
func (this *TestRequest) Format(s string) string {
|
func (this *TestRequest) Format(s string) string {
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user