github.com/TeaOSLab/EdgeNode@v1.3.8/internal/nodes/client_listener.go (about) 1 // Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved. 2 3 package nodes 4 5 import ( 6 "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/firewallconfigs" 7 "github.com/TeaOSLab/EdgeNode/internal/firewalls" 8 "github.com/TeaOSLab/EdgeNode/internal/iplibrary" 9 "github.com/TeaOSLab/EdgeNode/internal/waf" 10 "net" 11 ) 12 13 // ClientListener 客户端网络监听 14 type ClientListener struct { 15 rawListener net.Listener 16 isHTTP bool 17 isTLS bool 18 } 19 20 func NewClientListener(listener net.Listener, isHTTP bool) *ClientListener { 21 return &ClientListener{ 22 rawListener: listener, 23 isHTTP: isHTTP, 24 } 25 } 26 27 func (this *ClientListener) SetIsTLS(isTLS bool) { 28 this.isTLS = isTLS 29 } 30 31 func (this *ClientListener) IsTLS() bool { 32 return this.isTLS 33 } 34 35 func (this *ClientListener) Accept() (net.Conn, error) { 36 conn, err := this.rawListener.Accept() 37 if err != nil { 38 return nil, err 39 } 40 41 // 是否在WAF名单中 42 ip, _, err := net.SplitHostPort(conn.RemoteAddr().String()) 43 var isInAllowList = false 44 if err == nil { 45 canGoNext, inAllowList, expiresAt := iplibrary.AllowIP(ip, 0) 46 isInAllowList = inAllowList 47 if !canGoNext { 48 firewalls.DropTemporaryTo(ip, expiresAt) 49 } else { 50 if !waf.SharedIPWhiteList.Contains(waf.IPTypeAll, firewallconfigs.FirewallScopeGlobal, 0, ip) { 51 var ok bool 52 expiresAt, ok = waf.SharedIPBlackList.ContainsExpires(waf.IPTypeAll, firewallconfigs.FirewallScopeGlobal, 0, ip) 53 if ok { 54 canGoNext = false 55 firewalls.DropTemporaryTo(ip, expiresAt) 56 } 57 } 58 } 59 60 if !canGoNext { 61 tcpConn, ok := conn.(*net.TCPConn) 62 if ok { 63 _ = tcpConn.SetLinger(0) 64 } 65 66 _ = conn.Close() 67 68 return this.Accept() 69 } 70 } 71 72 return NewClientConn(conn, this.isHTTP, this.isTLS, isInAllowList), nil 73 } 74 75 func (this *ClientListener) Close() error { 76 return this.rawListener.Close() 77 } 78 79 func (this *ClientListener) Addr() net.Addr { 80 return this.rawListener.Addr() 81 }