2022-07-01 11:34:02 +00:00
|
|
|
package inbound
|
2022-06-30 13:27:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
2023-12-20 11:55:14 +00:00
|
|
|
"time"
|
2022-06-30 13:27:56 +00:00
|
|
|
|
2022-07-06 07:01:09 +00: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 15:03:57 +00:00
|
|
|
"github.com/sagernet/sing/common/buf"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
2024-10-21 15:38:34 +00:00
|
|
|
"github.com/sagernet/sing/common/udpnat2"
|
2022-06-30 13:27:56 +00:00
|
|
|
)
|
|
|
|
|
2022-07-01 11:34:02 +00:00
|
|
|
var _ adapter.Inbound = (*Direct)(nil)
|
2022-06-30 13:27:56 +00:00
|
|
|
|
2022-07-01 11:34:02 +00:00
|
|
|
type Direct struct {
|
|
|
|
myInboundAdapter
|
2024-10-21 15:38:34 +00:00
|
|
|
udpNat *udpnat.Service
|
2022-06-30 13:27:56 +00:00
|
|
|
overrideOption int
|
|
|
|
overrideDestination M.Socksaddr
|
|
|
|
}
|
|
|
|
|
2022-07-12 07:17:29 +00:00
|
|
|
func NewDirect(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.DirectInboundOptions) *Direct {
|
2023-01-19 02:35:26 +00:00
|
|
|
options.UDPFragmentDefault = true
|
2022-07-01 11:34:02 +00: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 13:27:56 +00: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}
|
|
|
|
}
|
2023-12-20 11:55:14 +00:00
|
|
|
var udpTimeout time.Duration
|
2022-07-15 00:42:02 +00:00
|
|
|
if options.UDPTimeout != 0 {
|
2023-12-20 11:55:14 +00:00
|
|
|
udpTimeout = time.Duration(options.UDPTimeout)
|
2022-07-15 00:42:02 +00:00
|
|
|
} else {
|
2023-12-20 11:55:14 +00:00
|
|
|
udpTimeout = C.UDPTimeout
|
2022-07-15 00:42:02 +00:00
|
|
|
}
|
2024-10-21 15:38:34 +00:00
|
|
|
inbound.udpNat = udpnat.New(inbound, inbound.preparePacketConnection, udpTimeout)
|
2022-07-01 11:34:02 +00:00
|
|
|
inbound.connHandler = inbound
|
|
|
|
inbound.packetHandler = inbound
|
|
|
|
return inbound
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 11:34:02 +00:00
|
|
|
func (d *Direct) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
2022-06-30 13:27:56 +00:00
|
|
|
switch d.overrideOption {
|
|
|
|
case 1:
|
2022-07-02 06:07:50 +00:00
|
|
|
metadata.Destination = d.overrideDestination
|
|
|
|
case 2:
|
2022-06-30 13:27:56 +00:00
|
|
|
destination := d.overrideDestination
|
|
|
|
destination.Port = metadata.Destination.Port
|
|
|
|
metadata.Destination = destination
|
2022-07-02 06:07:50 +00:00
|
|
|
case 3:
|
2022-06-30 13:27:56 +00:00
|
|
|
metadata.Destination.Port = d.overrideDestination.Port
|
|
|
|
}
|
2022-07-12 07:17:29 +00:00
|
|
|
d.logger.InfoContext(ctx, "inbound connection to ", metadata.Destination)
|
2022-06-30 13:27:56 +00:00
|
|
|
return d.router.RouteConnection(ctx, conn, metadata)
|
|
|
|
}
|
|
|
|
|
2024-10-21 15:38:34 +00:00
|
|
|
func (d *Direct) NewPacketEx(buffer *buf.Buffer, source M.Socksaddr) {
|
|
|
|
var destination M.Socksaddr
|
2022-06-30 13:27:56 +00:00
|
|
|
switch d.overrideOption {
|
|
|
|
case 1:
|
2024-10-21 15:38:34 +00:00
|
|
|
destination = d.overrideDestination
|
2022-07-02 06:07:50 +00:00
|
|
|
case 2:
|
2024-10-21 15:38:34 +00:00
|
|
|
destination = d.overrideDestination
|
|
|
|
destination.Port = source.Port
|
2022-07-02 06:07:50 +00:00
|
|
|
case 3:
|
2024-10-21 15:38:34 +00:00
|
|
|
destination = source
|
|
|
|
destination.Port = d.overrideDestination.Port
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2024-10-21 15:38:34 +00:00
|
|
|
d.udpNat.NewPacket([][]byte{buffer.Bytes()}, source, destination, nil)
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2022-07-24 05:42:58 +00:00
|
|
|
|
2024-10-21 15:38:34 +00:00
|
|
|
func (d *Direct) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
|
|
|
|
d.newConnectionEx(ctx, conn, metadata, onClose)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Direct) NewPacketConnectionEx(ctx context.Context, conn N.PacketConn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
|
|
|
|
d.newPacketConnectionEx(ctx, conn, d.createPacketMetadataEx(source, destination), onClose)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Direct) preparePacketConnection(source M.Socksaddr, destination M.Socksaddr, userData any) (bool, context.Context, N.PacketWriter, N.CloseHandlerFunc) {
|
|
|
|
return true, d.ctx, &directPacketWriter{d.packetConn(), source}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type directPacketWriter struct {
|
|
|
|
writer N.PacketWriter
|
|
|
|
source M.Socksaddr
|
2022-07-24 05:42:58 +00:00
|
|
|
}
|
|
|
|
|
2024-10-21 15:38:34 +00:00
|
|
|
func (w *directPacketWriter) WritePacket(buffer *buf.Buffer, addr M.Socksaddr) error {
|
|
|
|
return w.writer.WritePacket(buffer, w.source)
|
2022-07-24 05:42:58 +00:00
|
|
|
}
|