mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-22 08:31:30 +00:00
Improve tproxy udp write back
This commit is contained in:
parent
835ae1217b
commit
ca94a2ddcb
|
@ -12,6 +12,7 @@ import (
|
||||||
"github.com/sagernet/sing-box/option"
|
"github.com/sagernet/sing-box/option"
|
||||||
"github.com/sagernet/sing/common"
|
"github.com/sagernet/sing/common"
|
||||||
"github.com/sagernet/sing/common/buf"
|
"github.com/sagernet/sing/common/buf"
|
||||||
|
"github.com/sagernet/sing/common/control"
|
||||||
E "github.com/sagernet/sing/common/exceptions"
|
E "github.com/sagernet/sing/common/exceptions"
|
||||||
M "github.com/sagernet/sing/common/metadata"
|
M "github.com/sagernet/sing/common/metadata"
|
||||||
N "github.com/sagernet/sing/common/network"
|
N "github.com/sagernet/sing/common/network"
|
||||||
|
@ -54,21 +55,17 @@ func (t *TProxy) Start() error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if t.tcpListener != nil {
|
if t.tcpListener != nil {
|
||||||
tcpFd, err := common.GetFileDescriptor(t.tcpListener)
|
err = control.Conn(t.tcpListener, func(fd uintptr) error {
|
||||||
if err != nil {
|
return redir.TProxy(fd, M.SocksaddrFromNet(t.tcpListener.Addr()).Addr.Is6())
|
||||||
return err
|
})
|
||||||
}
|
|
||||||
err = redir.TProxy(tcpFd, M.SocksaddrFromNet(t.tcpListener.Addr()).Addr.Is6())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return E.Cause(err, "configure tproxy TCP listener")
|
return E.Cause(err, "configure tproxy TCP listener")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if t.udpConn != nil {
|
if t.udpConn != nil {
|
||||||
udpFd, err := common.GetFileDescriptor(t.udpConn)
|
err = control.Conn(t.udpConn, func(fd uintptr) error {
|
||||||
if err != nil {
|
return redir.TProxy(fd, M.SocksaddrFromNet(t.udpConn.LocalAddr()).Addr.Is6())
|
||||||
return err
|
})
|
||||||
}
|
|
||||||
err = redir.TProxy(udpFd, M.SocksaddrFromNet(t.udpConn.LocalAddr()).Addr.Is6())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return E.Cause(err, "configure tproxy UDP listener")
|
return E.Cause(err, "configure tproxy UDP listener")
|
||||||
}
|
}
|
||||||
|
@ -88,21 +85,40 @@ func (t *TProxy) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.B
|
||||||
}
|
}
|
||||||
metadata.Destination = M.SocksaddrFromNetIP(destination)
|
metadata.Destination = M.SocksaddrFromNetIP(destination)
|
||||||
t.udpNat.NewContextPacket(ctx, metadata.Source.AddrPort(), buffer, adapter.UpstreamMetadata(metadata), func(natConn N.PacketConn) (context.Context, N.PacketWriter) {
|
t.udpNat.NewContextPacket(ctx, metadata.Source.AddrPort(), buffer, adapter.UpstreamMetadata(metadata), func(natConn N.PacketConn) (context.Context, N.PacketWriter) {
|
||||||
return adapter.WithContext(log.ContextWithNewID(ctx), &metadata), &tproxyPacketWriter{natConn}
|
return adapter.WithContext(log.ContextWithNewID(ctx), &metadata), &tproxyPacketWriter{source: natConn}
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type tproxyPacketWriter struct {
|
type tproxyPacketWriter struct {
|
||||||
source N.PacketConn
|
source N.PacketConn
|
||||||
|
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()
|
||||||
udpConn, err := redir.DialUDP(destination.UDPAddr(), M.SocksaddrFromNet(w.source.LocalAddr()).UDPAddr())
|
var udpConn *net.UDPConn
|
||||||
if err != nil {
|
if w.destination == destination {
|
||||||
return E.Cause(err, "tproxy udp write back")
|
if w.conn != nil {
|
||||||
|
udpConn = w.conn
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if udpConn == nil {
|
||||||
|
var err error
|
||||||
|
udpConn, err = redir.DialUDP(destination.UDPAddr(), M.SocksaddrFromNet(w.source.LocalAddr()).UDPAddr())
|
||||||
|
if err != nil {
|
||||||
|
return E.Cause(err, "tproxy udp write back")
|
||||||
|
}
|
||||||
|
if w.destination == destination {
|
||||||
|
w.conn = udpConn
|
||||||
|
} else {
|
||||||
|
defer udpConn.Close()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
defer udpConn.Close()
|
|
||||||
return common.Error(udpConn.Write(buffer.Bytes()))
|
return common.Error(udpConn.Write(buffer.Bytes()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (w *tproxyPacketWriter) Close() error {
|
||||||
|
return common.Close(common.PtrOrNil(w.conn))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue