mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-22 08:31:30 +00:00
Fix loop back detector
This commit is contained in:
parent
e478d3c2dc
commit
dc7b7afc06
|
@ -148,7 +148,7 @@ func (h *Direct) ListenPacket(ctx context.Context, destination M.Socksaddr) (net
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
conn = h.loopBack.NewPacketConn(conn)
|
conn = h.loopBack.NewPacketConn(bufio.NewPacketConn(conn))
|
||||||
if originDestination != destination {
|
if originDestination != destination {
|
||||||
conn = bufio.NewNATPacketConn(bufio.NewPacketConn(conn), destination, originDestination)
|
conn = bufio.NewNATPacketConn(bufio.NewPacketConn(conn), destination, originDestination)
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
|
N "github.com/sagernet/sing/common/network"
|
||||||
)
|
)
|
||||||
|
|
||||||
type loopBackDetector struct {
|
type loopBackDetector struct {
|
||||||
|
@ -40,7 +41,7 @@ func (l *loopBackDetector) NewConn(conn net.Conn) net.Conn {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *loopBackDetector) NewPacketConn(conn net.PacketConn) net.PacketConn {
|
func (l *loopBackDetector) NewPacketConn(conn N.NetPacketConn) N.NetPacketConn {
|
||||||
connAddr := M.AddrPortFromNet(conn.LocalAddr())
|
connAddr := M.AddrPortFromNet(conn.LocalAddr())
|
||||||
if !connAddr.IsValid() {
|
if !connAddr.IsValid() {
|
||||||
return conn
|
return conn
|
||||||
|
@ -48,7 +49,7 @@ func (l *loopBackDetector) NewPacketConn(conn net.PacketConn) net.PacketConn {
|
||||||
l.packetConnAccess.Lock()
|
l.packetConnAccess.Lock()
|
||||||
l.packetConnMap[connAddr] = true
|
l.packetConnMap[connAddr] = true
|
||||||
l.packetConnAccess.Unlock()
|
l.packetConnAccess.Unlock()
|
||||||
return &loopBackDetectPacketWrapper{PacketConn: conn, detector: l, connAddr: connAddr}
|
return &loopBackDetectPacketWrapper{NetPacketConn: conn, detector: l, connAddr: connAddr}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *loopBackDetector) CheckConn(connAddr netip.AddrPort) bool {
|
func (l *loopBackDetector) CheckConn(connAddr netip.AddrPort) bool {
|
||||||
|
@ -92,7 +93,7 @@ func (w *loopBackDetectWrapper) Upstream() any {
|
||||||
}
|
}
|
||||||
|
|
||||||
type loopBackDetectPacketWrapper struct {
|
type loopBackDetectPacketWrapper struct {
|
||||||
net.PacketConn
|
N.NetPacketConn
|
||||||
detector *loopBackDetector
|
detector *loopBackDetector
|
||||||
connAddr netip.AddrPort
|
connAddr netip.AddrPort
|
||||||
closeOnce sync.Once
|
closeOnce sync.Once
|
||||||
|
@ -104,7 +105,7 @@ func (w *loopBackDetectPacketWrapper) Close() error {
|
||||||
delete(w.detector.packetConnMap, w.connAddr)
|
delete(w.detector.packetConnMap, w.connAddr)
|
||||||
w.detector.packetConnAccess.Unlock()
|
w.detector.packetConnAccess.Unlock()
|
||||||
})
|
})
|
||||||
return w.PacketConn.Close()
|
return w.NetPacketConn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *loopBackDetectPacketWrapper) ReaderReplaceable() bool {
|
func (w *loopBackDetectPacketWrapper) ReaderReplaceable() bool {
|
||||||
|
@ -116,7 +117,7 @@ func (w *loopBackDetectPacketWrapper) WriterReplaceable() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *loopBackDetectPacketWrapper) Upstream() any {
|
func (w *loopBackDetectPacketWrapper) Upstream() any {
|
||||||
return w.PacketConn
|
return w.NetPacketConn
|
||||||
}
|
}
|
||||||
|
|
||||||
type abstractUDPConn interface {
|
type abstractUDPConn interface {
|
||||||
|
|
Loading…
Reference in a new issue