fix(tcp): return a dummy connection instead of nil

This commit is contained in:
yusing 2025-04-26 07:57:20 +08:00
parent cb2990f6e8
commit f1abb745fe

View file

@ -1,7 +1,9 @@
package acl package acl
import ( import (
"io"
"net" "net"
"time"
) )
type TCPListener struct { type TCPListener struct {
@ -9,6 +11,17 @@ type TCPListener struct {
lis net.Listener lis net.Listener
} }
type noConn struct{}
func (noConn) Read(b []byte) (int, error) { return 0, io.EOF }
func (noConn) Write(b []byte) (int, error) { return 0, io.EOF }
func (noConn) Close() error { return nil }
func (noConn) LocalAddr() net.Addr { return nil }
func (noConn) RemoteAddr() net.Addr { return nil }
func (noConn) SetDeadline(t time.Time) error { return nil }
func (noConn) SetReadDeadline(t time.Time) error { return nil }
func (noConn) SetWriteDeadline(t time.Time) error { return nil }
func (cfg *Config) WrapTCP(lis net.Listener) net.Listener { func (cfg *Config) WrapTCP(lis net.Listener) net.Listener {
if cfg == nil { if cfg == nil {
return lis return lis
@ -32,11 +45,11 @@ func (s *TCPListener) Accept() (net.Conn, error) {
if !ok { if !ok {
// Not a TCPAddr, drop // Not a TCPAddr, drop
c.Close() c.Close()
return nil, nil return noConn{}, nil
} }
if !s.acl.IPAllowed(addr.IP) { if !s.acl.IPAllowed(addr.IP) {
c.Close() c.Close()
return nil, nil return noConn{}, nil
} }
return c, nil return c, nil
} }