sing-box/inbound/tproxy.go

131 lines
4 KiB
Go
Raw Normal View History

2022-07-15 00:42:02 +00:00
package inbound
import (
"context"
"net"
"net/netip"
2022-08-23 11:44:40 +00:00
"syscall"
"time"
2022-07-15 00:42:02 +00:00
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/redir"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/buf"
2022-08-16 10:37:37 +00:00
"github.com/sagernet/sing/common/control"
2022-07-15 00:42:02 +00:00
E "github.com/sagernet/sing/common/exceptions"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/udpnat"
)
type TProxy struct {
myInboundAdapter
udpNat *udpnat.Service[netip.AddrPort]
}
func NewTProxy(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.TProxyInboundOptions) *TProxy {
tproxy := &TProxy{
myInboundAdapter: myInboundAdapter{
protocol: C.TypeTProxy,
network: options.Network.Build(),
ctx: ctx,
router: router,
logger: logger,
tag: tag,
listenOptions: options.ListenOptions,
},
}
var udpTimeout time.Duration
2022-07-15 00:42:02 +00:00
if options.UDPTimeout != 0 {
udpTimeout = time.Duration(options.UDPTimeout)
2022-07-15 00:42:02 +00:00
} else {
udpTimeout = C.UDPTimeout
2022-07-15 00:42:02 +00:00
}
tproxy.connHandler = tproxy
tproxy.oobPacketHandler = tproxy
tproxy.udpNat = udpnat.New[netip.AddrPort](int64(udpTimeout.Seconds()), tproxy.upstreamContextHandler())
2022-07-15 00:42:02 +00:00
tproxy.packetUpstream = tproxy.udpNat
return tproxy
}
func (t *TProxy) Start() error {
err := t.myInboundAdapter.Start()
if err != nil {
return err
}
if t.tcpListener != nil {
2022-08-23 11:44:40 +00:00
err = control.Conn(common.MustCast[syscall.Conn](t.tcpListener), func(fd uintptr) error {
2022-08-16 10:37:37 +00:00
return redir.TProxy(fd, M.SocksaddrFromNet(t.tcpListener.Addr()).Addr.Is6())
})
2022-07-15 00:42:02 +00:00
if err != nil {
return E.Cause(err, "configure tproxy TCP listener")
}
}
if t.udpConn != nil {
2022-08-16 10:37:37 +00:00
err = control.Conn(t.udpConn, func(fd uintptr) error {
return redir.TProxy(fd, M.SocksaddrFromNet(t.udpConn.LocalAddr()).Addr.Is6())
})
2022-07-15 00:42:02 +00:00
if err != nil {
return E.Cause(err, "configure tproxy UDP listener")
}
}
return nil
}
func (t *TProxy) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
2022-09-25 14:16:24 +00:00
metadata.Destination = M.SocksaddrFromNet(conn.LocalAddr()).Unwrap()
2022-07-15 00:42:02 +00:00
return t.newConnection(ctx, conn, metadata)
}
func (t *TProxy) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, oob []byte, metadata adapter.InboundContext) error {
destination, err := redir.GetOriginalDestinationFromOOB(oob)
if err != nil {
return E.Cause(err, "get tproxy destination")
}
2022-09-25 14:16:24 +00:00
metadata.Destination = M.SocksaddrFromNetIP(destination).Unwrap()
2022-07-15 00:42:02 +00:00
t.udpNat.NewContextPacket(ctx, metadata.Source.AddrPort(), buffer, adapter.UpstreamMetadata(metadata), func(natConn N.PacketConn) (context.Context, N.PacketWriter) {
2022-09-25 14:16:24 +00:00
return adapter.WithContext(log.ContextWithNewID(ctx), &metadata), &tproxyPacketWriter{ctx: ctx, source: natConn, destination: metadata.Destination}
2022-07-15 00:42:02 +00:00
})
return nil
}
type tproxyPacketWriter struct {
2022-09-25 09:13:42 +00:00
ctx context.Context
2022-08-16 10:37:37 +00:00
source N.PacketConn
destination M.Socksaddr
conn *net.UDPConn
2022-07-15 00:42:02 +00:00
}
func (w *tproxyPacketWriter) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
defer buffer.Release()
2023-02-19 10:56:38 +00:00
conn := w.conn
if w.destination == destination && conn != nil {
_, err := conn.WriteToUDPAddrPort(buffer.Bytes(), M.AddrPortFromNet(w.source.LocalAddr()))
if err != nil {
2022-09-25 09:13:42 +00:00
w.conn = nil
2022-08-16 10:37:37 +00:00
}
2022-09-25 09:13:42 +00:00
return err
2022-08-16 10:37:37 +00:00
}
2022-09-25 09:13:42 +00:00
var listener net.ListenConfig
listener.Control = control.Append(listener.Control, control.ReuseAddr())
listener.Control = control.Append(listener.Control, redir.TProxyWriteBack())
packetConn, err := listener.ListenPacket(w.ctx, "udp", destination.String())
if err != nil {
return err
}
udpConn := packetConn.(*net.UDPConn)
if w.destination == destination {
w.conn = udpConn
} else {
defer udpConn.Close()
2022-07-15 00:42:02 +00:00
}
2022-09-25 09:13:42 +00:00
return common.Error(udpConn.WriteToUDPAddrPort(buffer.Bytes(), M.AddrPortFromNet(w.source.LocalAddr())))
2022-07-15 00:42:02 +00:00
}
2022-08-16 10:37:37 +00:00
func (w *tproxyPacketWriter) Close() error {
return common.Close(common.PtrOrNil(w.conn))
}