diff --git a/internal/nodes/traffic_conn.go b/internal/nodes/client_conn.go similarity index 68% rename from internal/nodes/traffic_conn.go rename to internal/nodes/client_conn.go index 97b151e..81219a4 100644 --- a/internal/nodes/traffic_conn.go +++ b/internal/nodes/client_conn.go @@ -42,17 +42,23 @@ func init() { }) } -// TrafficConn 用于统计流量的连接 -type TrafficConn struct { +// ClientConn 客户端连接 +type ClientConn struct { rawConn net.Conn isClosed bool } -func NewTrafficConn(conn net.Conn) net.Conn { - return &TrafficConn{rawConn: conn} +func NewClientConn(conn net.Conn) net.Conn { + tcpConn, ok := conn.(*net.TCPConn) + if ok { + // TODO 可以设置此值 + _ = tcpConn.SetLinger(0) + } + + return &ClientConn{rawConn: conn} } -func (this *TrafficConn) Read(b []byte) (n int, err error) { +func (this *ClientConn) Read(b []byte) (n int, err error) { n, err = this.rawConn.Read(b) if n > 0 { atomic.AddUint64(&inTrafficBytes, uint64(n)) @@ -60,7 +66,7 @@ func (this *TrafficConn) Read(b []byte) (n int, err error) { return } -func (this *TrafficConn) Write(b []byte) (n int, err error) { +func (this *ClientConn) Write(b []byte) (n int, err error) { n, err = this.rawConn.Write(b) if n > 0 { atomic.AddUint64(&outTrafficBytes, uint64(n)) @@ -68,31 +74,31 @@ func (this *TrafficConn) Write(b []byte) (n int, err error) { return } -func (this *TrafficConn) Close() error { +func (this *ClientConn) Close() error { this.isClosed = true return this.rawConn.Close() } -func (this *TrafficConn) LocalAddr() net.Addr { +func (this *ClientConn) LocalAddr() net.Addr { return this.rawConn.LocalAddr() } -func (this *TrafficConn) RemoteAddr() net.Addr { +func (this *ClientConn) RemoteAddr() net.Addr { return this.rawConn.RemoteAddr() } -func (this *TrafficConn) SetDeadline(t time.Time) error { +func (this *ClientConn) SetDeadline(t time.Time) error { return this.rawConn.SetDeadline(t) } -func (this *TrafficConn) SetReadDeadline(t time.Time) error { +func (this *ClientConn) SetReadDeadline(t time.Time) error { return this.rawConn.SetReadDeadline(t) } -func (this *TrafficConn) SetWriteDeadline(t time.Time) error { +func (this *ClientConn) SetWriteDeadline(t time.Time) error { return this.rawConn.SetWriteDeadline(t) } -func (this *TrafficConn) IsClosed() bool { +func (this *ClientConn) IsClosed() bool { return this.isClosed } diff --git a/internal/nodes/traffic_listener.go b/internal/nodes/client_listener.go similarity index 67% rename from internal/nodes/traffic_listener.go rename to internal/nodes/client_listener.go index d68a285..f2588fd 100644 --- a/internal/nodes/traffic_listener.go +++ b/internal/nodes/client_listener.go @@ -8,16 +8,16 @@ import ( "net" ) -// TrafficListener 用于统计流量的网络监听 -type TrafficListener struct { +// ClientListener 客户端网络监听 +type ClientListener struct { rawListener net.Listener } -func NewTrafficListener(listener net.Listener) net.Listener { - return &TrafficListener{rawListener: listener} +func NewClientListener(listener net.Listener) net.Listener { + return &ClientListener{rawListener: listener} } -func (this *TrafficListener) Accept() (net.Conn, error) { +func (this *ClientListener) Accept() (net.Conn, error) { conn, err := this.rawListener.Accept() if err != nil { return nil, err @@ -33,13 +33,13 @@ func (this *TrafficListener) Accept() (net.Conn, error) { } } - return NewTrafficConn(conn), nil + return NewClientConn(conn), nil } -func (this *TrafficListener) Close() error { +func (this *ClientListener) Close() error { return this.rawListener.Close() } -func (this *TrafficListener) Addr() net.Addr { +func (this *ClientListener) Addr() net.Addr { return this.rawListener.Addr() } diff --git a/internal/nodes/http_request_waf.go b/internal/nodes/http_request_waf.go index 82c2140..7446de3 100644 --- a/internal/nodes/http_request_waf.go +++ b/internal/nodes/http_request_waf.go @@ -20,7 +20,7 @@ func (this *HTTPRequest) doWAFRequest() (blocked bool) { // 当前连接是否已关闭 var conn = this.RawReq.Context().Value(HTTPConnContextKey) if conn != nil { - trafficConn, ok := conn.(*TrafficConn) + trafficConn, ok := conn.(*ClientConn) if ok && trafficConn.IsClosed() { this.disableLog = true return true @@ -32,7 +32,7 @@ func (this *HTTPRequest) doWAFRequest() (blocked bool) { this.disableLog = true if conn != nil { - trafficConn, ok := conn.(*TrafficConn) + trafficConn, ok := conn.(*ClientConn) if ok && !trafficConn.IsClosed() { _ = trafficConn.Close() } diff --git a/internal/nodes/listener.go b/internal/nodes/listener.go index 0055c7b..d02fa6b 100644 --- a/internal/nodes/listener.go +++ b/internal/nodes/listener.go @@ -59,7 +59,7 @@ func (this *Listener) listenTCP() error { if err != nil { return err } - netListener = NewTrafficListener(netListener) + netListener = NewClientListener(netListener) events.On(events.EventQuit, func() { remotelogs.Println("LISTENER", "quit "+this.group.FullAddr()) _ = netListener.Close()