package loopback import ( "errors" "fmt" "net/netip" "github.com/metacubex/mihomo/common/callback" C "github.com/metacubex/mihomo/constant" "github.com/puzpuzpuz/xsync/v3" ) var ErrReject = errors.New("reject loopback connection") type Detector struct { connMap *xsync.MapOf[netip.AddrPort, struct{}] packetConnMap *xsync.MapOf[netip.AddrPort, struct{}] } func NewDetector() *Detector { return &Detector{ connMap: xsync.NewMapOf[netip.AddrPort, struct{}](), packetConnMap: xsync.NewMapOf[netip.AddrPort, struct{}](), } } func (l *Detector) NewConn(conn C.Conn) C.Conn { metadata := C.Metadata{} if metadata.SetRemoteAddr(conn.LocalAddr()) != nil { return conn } connAddr := metadata.AddrPort() if !connAddr.IsValid() { return conn } l.connMap.Store(connAddr, struct{}{}) return callback.NewCloseCallbackConn(conn, func() { l.connMap.Delete(connAddr) }) } func (l *Detector) NewPacketConn(conn C.PacketConn) C.PacketConn { metadata := C.Metadata{} if metadata.SetRemoteAddr(conn.LocalAddr()) != nil { return conn } connAddr := metadata.AddrPort() if !connAddr.IsValid() { return conn } l.packetConnMap.Store(connAddr, struct{}{}) return callback.NewCloseCallbackPacketConn(conn, func() { l.packetConnMap.Delete(connAddr) }) } func (l *Detector) CheckConn(metadata *C.Metadata) error { connAddr := metadata.SourceAddrPort() if !connAddr.IsValid() { return nil } if _, ok := l.connMap.Load(connAddr); ok { return fmt.Errorf("%w to: %s", ErrReject, metadata.RemoteAddress()) } return nil } func (l *Detector) CheckPacketConn(metadata *C.Metadata) error { connAddr := metadata.SourceAddrPort() if !connAddr.IsValid() { return nil } if _, ok := l.packetConnMap.Load(connAddr); ok { return fmt.Errorf("%w to: %s", ErrReject, metadata.RemoteAddress()) } return nil }