mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-12-27 21:15:38 +08:00
134 lines
4.1 KiB
Go
134 lines
4.1 KiB
Go
package inbound
|
|
|
|
import (
|
|
"context"
|
|
"net"
|
|
"net/netip"
|
|
"syscall"
|
|
"time"
|
|
|
|
"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"
|
|
"github.com/sagernet/sing/common/control"
|
|
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/udpnat2"
|
|
)
|
|
|
|
type TProxy struct {
|
|
myInboundAdapter
|
|
udpNat *udpnat.Service
|
|
}
|
|
|
|
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
|
|
if options.UDPTimeout != 0 {
|
|
udpTimeout = time.Duration(options.UDPTimeout)
|
|
} else {
|
|
udpTimeout = C.UDPTimeout
|
|
}
|
|
tproxy.connHandler = tproxy
|
|
tproxy.oobPacketHandler = tproxy
|
|
tproxy.udpNat = udpnat.New(tproxy, tproxy.preparePacketConnection, udpTimeout, false)
|
|
return tproxy
|
|
}
|
|
|
|
func (t *TProxy) Start() error {
|
|
err := t.myInboundAdapter.Start()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if t.tcpListener != nil {
|
|
err = control.Conn(common.MustCast[syscall.Conn](t.tcpListener), func(fd uintptr) error {
|
|
return redir.TProxy(fd, M.SocksaddrFromNet(t.tcpListener.Addr()).Addr.Is6())
|
|
})
|
|
if err != nil {
|
|
return E.Cause(err, "configure tproxy TCP listener")
|
|
}
|
|
}
|
|
if t.udpConn != nil {
|
|
err = control.Conn(t.udpConn, func(fd uintptr) error {
|
|
return redir.TProxy(fd, M.SocksaddrFromNet(t.udpConn.LocalAddr()).Addr.Is6())
|
|
})
|
|
if err != nil {
|
|
return E.Cause(err, "configure tproxy UDP listener")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (t *TProxy) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
|
|
metadata.Destination = M.SocksaddrFromNet(conn.LocalAddr()).Unwrap()
|
|
t.newConnectionEx(ctx, conn, metadata, onClose)
|
|
}
|
|
|
|
func (t *TProxy) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
|
|
t.newPacketConnectionEx(ctx, conn, t.createPacketMetadataEx(source, destination), onClose)
|
|
}
|
|
|
|
func (t *TProxy) NewPacketEx(buffer *buf.Buffer, oob []byte, source M.Socksaddr) {
|
|
destination, err := redir.GetOriginalDestinationFromOOB(oob)
|
|
if err != nil {
|
|
t.logger.Warn("process packet from ", source, ": get tproxy destination: ", err)
|
|
return
|
|
}
|
|
t.udpNat.NewPacket([][]byte{buffer.Bytes()}, source, M.SocksaddrFromNetIP(destination), nil)
|
|
}
|
|
|
|
type tproxyPacketWriter struct {
|
|
ctx context.Context
|
|
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) {
|
|
writer := &tproxyPacketWriter{ctx: t.ctx, source: source.AddrPort(), destination: destination}
|
|
return true, t.ctx, writer, func(it error) {
|
|
common.Close(common.PtrOrNil(writer.conn))
|
|
}
|
|
}
|
|
|
|
func (w *tproxyPacketWriter) WritePacket(buffer *buf.Buffer, destination M.Socksaddr) error {
|
|
defer buffer.Release()
|
|
conn := w.conn
|
|
if w.destination == destination && conn != nil {
|
|
_, err := conn.WriteToUDPAddrPort(buffer.Bytes(), w.source)
|
|
if err != nil {
|
|
w.conn = nil
|
|
}
|
|
return err
|
|
}
|
|
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()
|
|
}
|
|
return common.Error(udpConn.WriteToUDPAddrPort(buffer.Bytes(), w.source))
|
|
}
|