mirror of
https://github.com/SagerNet/sing-box.git
synced 2025-06-08 09:32:06 +08:00
Fix tproxy inbound with netns
This commit is contained in:
parent
4fe48c1a80
commit
9656bc0938
@ -121,14 +121,6 @@ func (t *TProxy) NewPacketEx(buffer *buf.Buffer, oob []byte, source M.Socksaddr)
|
|||||||
t.udpNat.NewPacket([][]byte{buffer.Bytes()}, source, M.SocksaddrFromNetIP(destination), nil)
|
t.udpNat.NewPacket([][]byte{buffer.Bytes()}, source, M.SocksaddrFromNetIP(destination), nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
type tproxyPacketWriter struct {
|
|
||||||
ctx context.Context
|
|
||||||
listener *listener.Listener
|
|
||||||
source netip.AddrPort
|
|
||||||
destination M.Socksaddr
|
|
||||||
conn *net.UDPConn
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *TProxy) preparePacketConnection(source M.Socksaddr, destination M.Socksaddr, userData any) (bool, context.Context, N.PacketWriter, N.CloseHandlerFunc) {
|
func (t *TProxy) preparePacketConnection(source M.Socksaddr, destination M.Socksaddr, userData any) (bool, context.Context, N.PacketWriter, N.CloseHandlerFunc) {
|
||||||
ctx := log.ContextWithNewID(t.ctx)
|
ctx := log.ContextWithNewID(t.ctx)
|
||||||
writer := &tproxyPacketWriter{
|
writer := &tproxyPacketWriter{
|
||||||
@ -142,15 +134,25 @@ func (t *TProxy) preparePacketConnection(source M.Socksaddr, destination M.Socks
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type tproxyPacketWriter struct {
|
||||||
|
ctx context.Context
|
||||||
|
listener *listener.Listener
|
||||||
|
source netip.AddrPort
|
||||||
|
destination M.Socksaddr
|
||||||
|
conn *net.UDPConn
|
||||||
|
}
|
||||||
|
|
||||||
func (w *tproxyPacketWriter) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
|
func (w *tproxyPacketWriter) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
conn := w.conn
|
if w.listener.ListenOptions().NetNs == "" {
|
||||||
if w.destination == destination && conn != nil {
|
conn := w.conn
|
||||||
_, err := conn.WriteToUDPAddrPort(buffer.Bytes(), w.source)
|
if w.destination == destination && conn != nil {
|
||||||
if err != nil {
|
_, err := conn.WriteToUDPAddrPort(buffer.Bytes(), w.source)
|
||||||
w.conn = nil
|
if err != nil {
|
||||||
|
w.conn = nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
var listenConfig net.ListenConfig
|
var listenConfig net.ListenConfig
|
||||||
listenConfig.Control = control.Append(listenConfig.Control, control.ReuseAddr())
|
listenConfig.Control = control.Append(listenConfig.Control, control.ReuseAddr())
|
||||||
@ -160,7 +162,7 @@ func (w *tproxyPacketWriter) WritePacket(buffer *buf.Buffer, destination M.Socks
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
udpConn := packetConn.(*net.UDPConn)
|
udpConn := packetConn.(*net.UDPConn)
|
||||||
if w.destination == destination {
|
if w.listener.ListenOptions().NetNs == "" && w.destination == destination {
|
||||||
w.conn = udpConn
|
w.conn = udpConn
|
||||||
} else {
|
} else {
|
||||||
defer udpConn.Close()
|
defer udpConn.Close()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user