2021-04-29 16:48:47 +08:00
|
|
|
|
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
|
|
|
|
|
|
|
|
|
|
package nodes
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
2022-01-10 19:54:10 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs"
|
2021-11-13 21:30:24 +08:00
|
|
|
|
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
|
2022-01-10 19:54:10 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/iplibrary"
|
2021-12-09 17:34:05 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/ratelimit"
|
2022-01-10 19:54:10 +08:00
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/ttlcache"
|
|
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/utils"
|
|
|
|
|
|
"github.com/TeaOSLab/EdgeNode/internal/waf"
|
|
|
|
|
|
"github.com/iwind/TeaGo/types"
|
2021-04-29 16:48:47 +08:00
|
|
|
|
"net"
|
2022-01-10 19:54:10 +08:00
|
|
|
|
"os"
|
2021-12-09 17:34:05 +08:00
|
|
|
|
"sync"
|
2021-04-29 16:48:47 +08:00
|
|
|
|
"sync/atomic"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2021-10-20 22:32:02 +08:00
|
|
|
|
// ClientConn 客户端连接
|
|
|
|
|
|
type ClientConn struct {
|
2021-12-12 11:48:01 +08:00
|
|
|
|
once sync.Once
|
|
|
|
|
|
globalLimiter *ratelimit.Counter
|
2021-12-09 17:34:05 +08:00
|
|
|
|
|
2022-01-10 19:54:10 +08:00
|
|
|
|
isTLS bool
|
|
|
|
|
|
hasDeadline bool
|
|
|
|
|
|
hasRead bool
|
2021-12-18 19:17:40 +08:00
|
|
|
|
|
2021-12-12 11:48:01 +08:00
|
|
|
|
BaseClientConn
|
2021-04-29 16:48:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-18 19:17:40 +08:00
|
|
|
|
func NewClientConn(conn net.Conn, isTLS bool, quickClose bool, globalLimiter *ratelimit.Counter) net.Conn {
|
2021-10-25 19:00:42 +08:00
|
|
|
|
if quickClose {
|
2021-12-09 17:34:05 +08:00
|
|
|
|
// TCP
|
2021-10-25 19:00:42 +08:00
|
|
|
|
tcpConn, ok := conn.(*net.TCPConn)
|
|
|
|
|
|
if ok {
|
2021-12-12 11:48:01 +08:00
|
|
|
|
// TODO 可以在配置中设置此值
|
2021-12-09 17:34:05 +08:00
|
|
|
|
_ = tcpConn.SetLinger(nodeconfigs.DefaultTCPLinger)
|
2021-10-25 19:00:42 +08:00
|
|
|
|
}
|
2021-10-20 22:32:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-12-18 19:17:40 +08:00
|
|
|
|
return &ClientConn{BaseClientConn: BaseClientConn{rawConn: conn}, isTLS: isTLS, globalLimiter: globalLimiter}
|
2021-04-29 16:48:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-20 22:32:02 +08:00
|
|
|
|
func (this *ClientConn) Read(b []byte) (n int, err error) {
|
2021-12-18 19:17:40 +08:00
|
|
|
|
if this.isTLS {
|
2022-01-10 19:54:10 +08:00
|
|
|
|
if !this.hasDeadline {
|
2021-12-22 16:43:16 +08:00
|
|
|
|
_ = this.rawConn.SetReadDeadline(time.Now().Add(time.Duration(nodeconfigs.DefaultTLSHandshakeTimeout) * time.Second)) // TODO 握手超时时间可以设置
|
2022-01-10 19:54:10 +08:00
|
|
|
|
this.hasDeadline = true
|
2021-12-18 19:17:40 +08:00
|
|
|
|
defer func() {
|
|
|
|
|
|
_ = this.rawConn.SetReadDeadline(time.Time{})
|
|
|
|
|
|
}()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-29 16:48:47 +08:00
|
|
|
|
n, err = this.rawConn.Read(b)
|
|
|
|
|
|
if n > 0 {
|
2021-11-13 21:30:24 +08:00
|
|
|
|
atomic.AddUint64(&teaconst.InTrafficBytes, uint64(n))
|
2022-01-10 19:54:10 +08:00
|
|
|
|
this.hasRead = true
|
2021-04-29 16:48:47 +08:00
|
|
|
|
}
|
2022-01-10 19:54:10 +08:00
|
|
|
|
|
|
|
|
|
|
// SYN Flood检测
|
|
|
|
|
|
var synFloodConfig = sharedNodeConfig.SYNFloodConfig()
|
|
|
|
|
|
if synFloodConfig != nil && synFloodConfig.IsOn {
|
|
|
|
|
|
if err != nil && os.IsTimeout(err) {
|
|
|
|
|
|
if !this.hasRead {
|
|
|
|
|
|
this.checkSYNFlood()
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
this.resetSYNFlood()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-04-29 16:48:47 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-20 22:32:02 +08:00
|
|
|
|
func (this *ClientConn) Write(b []byte) (n int, err error) {
|
2021-04-29 16:48:47 +08:00
|
|
|
|
n, err = this.rawConn.Write(b)
|
|
|
|
|
|
if n > 0 {
|
2021-11-13 21:30:24 +08:00
|
|
|
|
atomic.AddUint64(&teaconst.OutTrafficBytes, uint64(n))
|
2021-04-29 16:48:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-20 22:32:02 +08:00
|
|
|
|
func (this *ClientConn) Close() error {
|
2021-09-29 09:19:45 +08:00
|
|
|
|
this.isClosed = true
|
2021-12-12 11:48:01 +08:00
|
|
|
|
|
2021-12-22 16:43:16 +08:00
|
|
|
|
err := this.rawConn.Close()
|
|
|
|
|
|
|
2021-12-12 11:48:01 +08:00
|
|
|
|
// 全局并发数限制
|
2021-12-09 17:34:05 +08:00
|
|
|
|
this.once.Do(func() {
|
2021-12-12 11:48:01 +08:00
|
|
|
|
if this.globalLimiter != nil {
|
|
|
|
|
|
this.globalLimiter.Release()
|
2021-12-09 17:34:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
})
|
2021-12-12 11:48:01 +08:00
|
|
|
|
|
|
|
|
|
|
// 单个服务并发数限制
|
|
|
|
|
|
sharedClientConnLimiter.Remove(this.rawConn.RemoteAddr().String())
|
|
|
|
|
|
|
2021-12-22 16:43:16 +08:00
|
|
|
|
return err
|
2021-04-29 16:48:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-20 22:32:02 +08:00
|
|
|
|
func (this *ClientConn) LocalAddr() net.Addr {
|
2021-04-29 16:48:47 +08:00
|
|
|
|
return this.rawConn.LocalAddr()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-20 22:32:02 +08:00
|
|
|
|
func (this *ClientConn) RemoteAddr() net.Addr {
|
2021-04-29 16:48:47 +08:00
|
|
|
|
return this.rawConn.RemoteAddr()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-20 22:32:02 +08:00
|
|
|
|
func (this *ClientConn) SetDeadline(t time.Time) error {
|
2021-04-29 16:48:47 +08:00
|
|
|
|
return this.rawConn.SetDeadline(t)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-20 22:32:02 +08:00
|
|
|
|
func (this *ClientConn) SetReadDeadline(t time.Time) error {
|
2021-04-29 16:48:47 +08:00
|
|
|
|
return this.rawConn.SetReadDeadline(t)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-20 22:32:02 +08:00
|
|
|
|
func (this *ClientConn) SetWriteDeadline(t time.Time) error {
|
2021-04-29 16:48:47 +08:00
|
|
|
|
return this.rawConn.SetWriteDeadline(t)
|
|
|
|
|
|
}
|
2022-01-10 19:54:10 +08:00
|
|
|
|
|
|
|
|
|
|
func (this *ClientConn) resetSYNFlood() {
|
|
|
|
|
|
// 为了不影响性能,暂时不清除状态
|
|
|
|
|
|
//ttlcache.SharedCache.Delete("SYN_FLOOD:" + this.RawIP())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (this *ClientConn) checkSYNFlood() {
|
|
|
|
|
|
var synFloodConfig = sharedNodeConfig.SYNFloodConfig()
|
|
|
|
|
|
if synFloodConfig == nil || !synFloodConfig.IsOn {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var ip = this.RawIP()
|
|
|
|
|
|
if len(ip) > 0 && !iplibrary.IsInWhiteList(ip) && (!synFloodConfig.IgnoreLocal || !utils.IsLocalIP(ip)) {
|
2022-01-11 09:25:34 +08:00
|
|
|
|
var timestamp = utils.NextMinuteUnixTime()
|
2022-01-10 19:54:10 +08:00
|
|
|
|
var result = ttlcache.SharedCache.IncreaseInt64("SYN_FLOOD:"+ip, 1, timestamp)
|
|
|
|
|
|
var minAttempts = synFloodConfig.MinAttempts
|
|
|
|
|
|
if minAttempts < 3 {
|
|
|
|
|
|
minAttempts = 3
|
|
|
|
|
|
}
|
|
|
|
|
|
if result >= int64(minAttempts) {
|
|
|
|
|
|
var timeout = synFloodConfig.TimeoutSeconds
|
|
|
|
|
|
if timeout <= 0 {
|
|
|
|
|
|
timeout = 600
|
|
|
|
|
|
}
|
|
|
|
|
|
waf.SharedIPBlackList.RecordIP(waf.IPTypeAll, firewallconfigs.FirewallScopeGlobal, 0, ip, time.Now().Unix()+int64(timeout), 0, true, 0, 0, "疑似SYN Flood攻击,当前1分钟"+types.String(result)+"次空连接")
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|