ret阻断
This commit is contained in:
@@ -2,8 +2,11 @@ package socket
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
@@ -37,7 +40,7 @@ func (s *Server) Stop() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) OnClose(c gnet.Conn, _ error) (action gnet.Action) {
|
||||
func (s *Server) OnClose(c gnet.Conn, err error) (action gnet.Action) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil { // 恢复 panic,err 为 panic 错误值
|
||||
// 1. 打印错误信息
|
||||
@@ -46,7 +49,16 @@ func (s *Server) OnClose(c gnet.Conn, _ error) (action gnet.Action) {
|
||||
|
||||
}
|
||||
}()
|
||||
// 识别 RST 导致的连接中断(错误信息含 "connection reset")
|
||||
if err != nil && (strings.Contains(err.Error(), "connection reset") || strings.Contains(err.Error(), "reset by peer")) {
|
||||
remoteIP := c.RemoteAddr().(*net.TCPAddr).IP.String()
|
||||
|
||||
log.Printf("RST 攻击检测: 来源 %s, 累计攻击次数 %d", remoteIP)
|
||||
|
||||
// 防护逻辑:临时封禁异常 IP(可扩展为 IP 黑名单)
|
||||
// go s.tempBlockIP(remoteIP, 5*time.Minute)
|
||||
}
|
||||
fmt.Println(err, c.RemoteAddr().String(), "断开连接")
|
||||
atomic.AddInt64(&s.connected, -1)
|
||||
|
||||
//logging.Infof("conn[%v] disconnected", c.RemoteAddr().String())
|
||||
|
||||
Reference in New Issue
Block a user