2024-11-02 00:39:02 +08:00
|
|
|
package redirect
|
2022-07-15 08:42:02 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/netip"
|
2022-08-23 19:44:40 +08:00
|
|
|
"syscall"
|
2023-12-20 19:55:14 +08:00
|
|
|
"time"
|
2022-07-15 08:42:02 +08:00
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2024-11-02 00:39:02 +08:00
|
|
|
"github.com/sagernet/sing-box/adapter/inbound"
|
|
|
|
"github.com/sagernet/sing-box/common/listener"
|
2022-07-15 08:42:02 +08:00
|
|
|
"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 18:37:37 +08:00
|
|
|
"github.com/sagernet/sing/common/control"
|
2022-07-15 08:42:02 +08:00
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
2024-10-21 23:38:34 +08:00
|
|
|
"github.com/sagernet/sing/common/udpnat2"
|
2022-07-15 08:42:02 +08:00
|
|
|
)
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
func RegisterTProxy(registry *inbound.Registry) {
|
|
|
|
inbound.Register[option.TProxyInboundOptions](registry, C.TypeTProxy, NewTProxy)
|
|
|
|
}
|
|
|
|
|
2022-07-15 08:42:02 +08:00
|
|
|
type TProxy struct {
|
2024-11-02 00:39:02 +08:00
|
|
|
inbound.Adapter
|
|
|
|
ctx context.Context
|
|
|
|
router adapter.Router
|
|
|
|
logger log.ContextLogger
|
|
|
|
listener *listener.Listener
|
|
|
|
udpNat *udpnat.Service
|
2022-07-15 08:42:02 +08:00
|
|
|
}
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
func NewTProxy(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.TProxyInboundOptions) (adapter.Inbound, error) {
|
2022-07-15 08:42:02 +08:00
|
|
|
tproxy := &TProxy{
|
2024-11-02 00:39:02 +08:00
|
|
|
Adapter: inbound.NewAdapter(C.TypeTProxy, tag),
|
|
|
|
ctx: ctx,
|
|
|
|
router: router,
|
|
|
|
logger: logger,
|
2022-07-15 08:42:02 +08:00
|
|
|
}
|
2023-12-20 19:55:14 +08:00
|
|
|
var udpTimeout time.Duration
|
2022-07-15 08:42:02 +08:00
|
|
|
if options.UDPTimeout != 0 {
|
2023-12-20 19:55:14 +08:00
|
|
|
udpTimeout = time.Duration(options.UDPTimeout)
|
2022-07-15 08:42:02 +08:00
|
|
|
} else {
|
2023-12-20 19:55:14 +08:00
|
|
|
udpTimeout = C.UDPTimeout
|
2022-07-15 08:42:02 +08:00
|
|
|
}
|
2024-10-21 23:38:34 +08:00
|
|
|
tproxy.udpNat = udpnat.New(tproxy, tproxy.preparePacketConnection, udpTimeout, false)
|
2024-11-02 00:39:02 +08:00
|
|
|
tproxy.listener = listener.New(listener.Options{
|
|
|
|
Context: ctx,
|
|
|
|
Logger: logger,
|
|
|
|
Network: options.Network.Build(),
|
|
|
|
Listen: options.ListenOptions,
|
|
|
|
ConnectionHandler: tproxy,
|
|
|
|
OOBPacketHandler: tproxy,
|
|
|
|
})
|
|
|
|
return tproxy, nil
|
2022-07-15 08:42:02 +08:00
|
|
|
}
|
|
|
|
|
2024-11-21 18:10:41 +08:00
|
|
|
func (t *TProxy) Start(stage adapter.StartStage) error {
|
|
|
|
if stage != adapter.StartStateStart {
|
|
|
|
return nil
|
|
|
|
}
|
2024-11-02 00:39:02 +08:00
|
|
|
err := t.listener.Start()
|
2022-07-15 08:42:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-11-02 00:39:02 +08:00
|
|
|
if listener := t.listener.TCPListener(); listener != nil {
|
|
|
|
err = control.Conn(common.MustCast[syscall.Conn](listener), func(fd uintptr) error {
|
|
|
|
return redir.TProxy(fd, M.SocksaddrFromNet(listener.Addr()).Addr.Is6())
|
2022-08-16 18:37:37 +08:00
|
|
|
})
|
2022-07-15 08:42:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return E.Cause(err, "configure tproxy TCP listener")
|
|
|
|
}
|
|
|
|
}
|
2024-11-02 00:39:02 +08:00
|
|
|
if conn := t.listener.UDPConn(); conn != nil {
|
|
|
|
err = control.Conn(conn, func(fd uintptr) error {
|
|
|
|
return redir.TProxy(fd, M.SocksaddrFromNet(conn.LocalAddr()).Addr.Is6())
|
2022-08-16 18:37:37 +08:00
|
|
|
})
|
2022-07-15 08:42:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return E.Cause(err, "configure tproxy UDP listener")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
func (t *TProxy) Close() error {
|
|
|
|
return t.listener.Close()
|
|
|
|
}
|
|
|
|
|
2024-10-21 23:38:34 +08:00
|
|
|
func (t *TProxy) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
|
2024-11-09 21:16:11 +08:00
|
|
|
metadata.Inbound = t.Tag()
|
|
|
|
metadata.InboundType = t.Type()
|
2022-09-25 22:16:24 +08:00
|
|
|
metadata.Destination = M.SocksaddrFromNet(conn.LocalAddr()).Unwrap()
|
2024-11-02 00:39:02 +08:00
|
|
|
t.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
|
|
|
|
t.router.RouteConnectionEx(ctx, conn, metadata, onClose)
|
2022-07-15 08:42:02 +08:00
|
|
|
}
|
|
|
|
|
2024-10-21 23:38:34 +08:00
|
|
|
func (t *TProxy) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
|
2024-11-02 00:39:02 +08:00
|
|
|
t.logger.InfoContext(ctx, "inbound packet connection from ", source)
|
|
|
|
t.logger.InfoContext(ctx, "inbound packet connection to ", destination)
|
|
|
|
var metadata adapter.InboundContext
|
|
|
|
metadata.Inbound = t.Tag()
|
|
|
|
metadata.InboundType = t.Type()
|
|
|
|
metadata.Source = source
|
|
|
|
metadata.Destination = destination
|
|
|
|
metadata.OriginDestination = t.listener.UDPAddr()
|
|
|
|
t.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
|
2024-10-21 23:38:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TProxy) NewPacketEx(buffer *buf.Buffer, oob []byte, source M.Socksaddr) {
|
2022-07-15 08:42:02 +08:00
|
|
|
destination, err := redir.GetOriginalDestinationFromOOB(oob)
|
|
|
|
if err != nil {
|
2024-10-21 23:38:34 +08:00
|
|
|
t.logger.Warn("process packet from ", source, ": get tproxy destination: ", err)
|
|
|
|
return
|
2022-07-15 08:42:02 +08:00
|
|
|
}
|
2024-10-21 23:38:34 +08:00
|
|
|
t.udpNat.NewPacket([][]byte{buffer.Bytes()}, source, M.SocksaddrFromNetIP(destination), nil)
|
2022-07-15 08:42:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type tproxyPacketWriter struct {
|
2022-09-25 17:13:42 +08:00
|
|
|
ctx context.Context
|
2024-10-21 23:38:34 +08:00
|
|
|
source netip.AddrPort
|
2022-08-16 18:37:37 +08:00
|
|
|
destination M.Socksaddr
|
|
|
|
conn *net.UDPConn
|
2022-07-15 08:42:02 +08:00
|
|
|
}
|
|
|
|
|
2024-10-21 23:38:34 +08:00
|
|
|
func (t *TProxy) preparePacketConnection(source M.Socksaddr, destination M.Socksaddr, userData any) (bool, context.Context, N.PacketWriter, N.CloseHandlerFunc) {
|
2024-11-02 00:39:02 +08:00
|
|
|
ctx := log.ContextWithNewID(t.ctx)
|
|
|
|
writer := &tproxyPacketWriter{ctx: ctx, source: source.AddrPort(), destination: destination}
|
|
|
|
return true, ctx, writer, func(it error) {
|
2024-10-21 23:38:34 +08:00
|
|
|
common.Close(common.PtrOrNil(writer.conn))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-15 08:42:02 +08:00
|
|
|
func (w *tproxyPacketWriter) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
|
|
|
|
defer buffer.Release()
|
2023-02-19 18:56:38 +08:00
|
|
|
conn := w.conn
|
|
|
|
if w.destination == destination && conn != nil {
|
2024-10-21 23:38:34 +08:00
|
|
|
_, err := conn.WriteToUDPAddrPort(buffer.Bytes(), w.source)
|
2023-02-19 18:56:38 +08:00
|
|
|
if err != nil {
|
2022-09-25 17:13:42 +08:00
|
|
|
w.conn = nil
|
2022-08-16 18:37:37 +08:00
|
|
|
}
|
2022-09-25 17:13:42 +08:00
|
|
|
return err
|
2022-08-16 18:37:37 +08:00
|
|
|
}
|
2022-09-25 17:13:42 +08: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 08:42:02 +08:00
|
|
|
}
|
2024-10-21 23:38:34 +08:00
|
|
|
return common.Error(udpConn.WriteToUDPAddrPort(buffer.Bytes(), w.source))
|
2022-08-16 18:37:37 +08:00
|
|
|
}
|