sing-box/inbound/direct.go

104 lines
3.6 KiB
Go
Raw Permalink Normal View History

2022-07-01 19:34:02 +08:00
package inbound
2022-06-30 21:27:56 +08:00
import (
"context"
"net"
"net/netip"
2022-07-06 15:01:09 +08:00
"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
2022-07-08 23:03:57 +08:00
"github.com/sagernet/sing/common/buf"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/udpnat"
2022-06-30 21:27:56 +08:00
)
2022-07-01 19:34:02 +08:00
var _ adapter.Inbound = (*Direct)(nil)
2022-06-30 21:27:56 +08:00
2022-07-01 19:34:02 +08:00
type Direct struct {
myInboundAdapter
2022-06-30 21:27:56 +08:00
udpNat *udpnat.Service[netip.AddrPort]
overrideOption int
overrideDestination M.Socksaddr
}
2022-07-12 15:17:29 +08:00
func NewDirect(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.DirectInboundOptions) *Direct {
2023-01-19 10:35:26 +08:00
options.UDPFragmentDefault = true
2022-07-01 19:34:02 +08:00
inbound := &Direct{
myInboundAdapter: myInboundAdapter{
protocol: C.TypeDirect,
network: options.Network.Build(),
ctx: ctx,
router: router,
logger: logger,
tag: tag,
listenOptions: options.ListenOptions,
},
2022-06-30 21:27:56 +08:00
}
if options.OverrideAddress != "" && options.OverridePort != 0 {
inbound.overrideOption = 1
inbound.overrideDestination = M.ParseSocksaddrHostPort(options.OverrideAddress, options.OverridePort)
} else if options.OverrideAddress != "" {
inbound.overrideOption = 2
inbound.overrideDestination = M.ParseSocksaddrHostPort(options.OverrideAddress, options.OverridePort)
} else if options.OverridePort != 0 {
inbound.overrideOption = 3
inbound.overrideDestination = M.Socksaddr{Port: options.OverridePort}
}
2022-07-15 08:42:02 +08:00
var udpTimeout int64
if options.UDPTimeout != 0 {
udpTimeout = options.UDPTimeout
} else {
udpTimeout = int64(C.UDPTimeout.Seconds())
2022-07-15 08:42:02 +08:00
}
2022-07-24 13:42:58 +08:00
inbound.udpNat = udpnat.New[netip.AddrPort](udpTimeout, adapter.NewUpstreamContextHandler(inbound.newConnection, inbound.newPacketConnection, inbound))
2022-07-01 19:34:02 +08:00
inbound.connHandler = inbound
inbound.packetHandler = inbound
2022-07-09 10:06:01 +08:00
inbound.packetUpstream = inbound.udpNat
2022-07-01 19:34:02 +08:00
return inbound
2022-06-30 21:27:56 +08:00
}
2022-07-01 19:34:02 +08:00
func (d *Direct) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
2022-06-30 21:27:56 +08:00
switch d.overrideOption {
case 1:
2022-07-02 14:07:50 +08:00
metadata.Destination = d.overrideDestination
case 2:
2022-06-30 21:27:56 +08:00
destination := d.overrideDestination
destination.Port = metadata.Destination.Port
metadata.Destination = destination
2022-07-02 14:07:50 +08:00
case 3:
2022-06-30 21:27:56 +08:00
metadata.Destination.Port = d.overrideDestination.Port
}
2022-07-12 15:17:29 +08:00
d.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
2022-06-30 21:27:56 +08:00
return d.router.RouteConnection(ctx, conn, metadata)
}
2022-07-01 19:34:02 +08:00
func (d *Direct) NewPacket(ctx context.Context, conn N.PacketConn, buffer *buf.Buffer, metadata adapter.InboundContext) error {
2022-06-30 21:27:56 +08:00
switch d.overrideOption {
case 1:
2022-07-02 14:07:50 +08:00
metadata.Destination = d.overrideDestination
case 2:
2022-06-30 21:27:56 +08:00
destination := d.overrideDestination
destination.Port = metadata.Destination.Port
metadata.Destination = destination
2022-07-02 14:07:50 +08:00
case 3:
2022-06-30 21:27:56 +08:00
metadata.Destination.Port = d.overrideDestination.Port
}
2022-07-15 08:42:02 +08:00
d.udpNat.NewContextPacket(ctx, metadata.Source.AddrPort(), buffer, adapter.UpstreamMetadata(metadata), func(natConn N.PacketConn) (context.Context, N.PacketWriter) {
2022-07-24 13:42:58 +08:00
return adapter.WithContext(log.ContextWithNewID(ctx), &metadata), &udpnat.DirectBackWriter{Source: conn, Nat: natConn}
2022-07-15 08:42:02 +08:00
})
2022-06-30 21:27:56 +08:00
return nil
}
2022-07-24 13:42:58 +08:00
func (d *Direct) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
return d.router.RouteConnection(ctx, conn, metadata)
}
func (d *Direct) newPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
ctx = log.ContextWithNewID(ctx)
d.logger.InfoContext(ctx, "inbound packet connection from ", metadata.Source)
return d.router.RoutePacketConnection(ctx, conn, metadata)
}