2022-07-01 19:34:02 +08:00
|
|
|
package inbound
|
2022-06-30 21:27:56 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"net/netip"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
"github.com/sagernet/sing-box/config"
|
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/log"
|
|
|
|
"github.com/sagernet/sing/common/buf"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
2022-07-01 19:34:02 +08:00
|
|
|
"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-01 19:34:02 +08:00
|
|
|
func NewDirect(ctx context.Context, router adapter.Router, logger log.Logger, tag string, options *config.DirectInboundOptions) *Direct {
|
|
|
|
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-01 19:34:02 +08:00
|
|
|
inbound.udpNat = udpnat.New[netip.AddrPort](options.UDPTimeout, inbound.upstreamContextHandler())
|
|
|
|
inbound.connHandler = inbound
|
|
|
|
inbound.packetHandler = inbound
|
|
|
|
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 0:
|
|
|
|
metadata.Destination = d.overrideDestination
|
|
|
|
case 1:
|
|
|
|
destination := d.overrideDestination
|
|
|
|
destination.Port = metadata.Destination.Port
|
|
|
|
metadata.Destination = destination
|
|
|
|
case 2:
|
|
|
|
metadata.Destination.Port = d.overrideDestination.Port
|
|
|
|
}
|
|
|
|
d.logger.WithContext(ctx).Info("inbound connection to ", metadata.Destination)
|
|
|
|
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 0:
|
|
|
|
metadata.Destination = d.overrideDestination
|
|
|
|
case 1:
|
|
|
|
destination := d.overrideDestination
|
|
|
|
destination.Port = metadata.Destination.Port
|
|
|
|
metadata.Destination = destination
|
|
|
|
case 2:
|
|
|
|
metadata.Destination.Port = d.overrideDestination.Port
|
|
|
|
}
|
2022-07-01 19:34:02 +08:00
|
|
|
var upstreamMetadata M.Metadata
|
|
|
|
upstreamMetadata.Source = M.SocksaddrFromNetIP(metadata.Source)
|
|
|
|
upstreamMetadata.Destination = metadata.Destination
|
|
|
|
d.udpNat.NewPacketDirect(&adapter.MetadataContext{Context: ctx, Metadata: metadata}, metadata.Source, conn, buffer, upstreamMetadata)
|
2022-06-30 21:27:56 +08:00
|
|
|
return nil
|
|
|
|
}
|