sing-box/common/dialer/dialer.go

104 lines
3.8 KiB
Go
Raw Normal View History

package dialer
import (
"context"
2024-11-12 11:37:10 +00:00
"net"
"net/netip"
2022-07-08 04:58:43 +00:00
"time"
2022-07-06 07:01:09 +00:00
"github.com/sagernet/sing-box/adapter"
2024-11-12 11:37:10 +00:00
C "github.com/sagernet/sing-box/constant"
2025-01-12 04:45:27 +00:00
"github.com/sagernet/sing-box/experimental/deprecated"
2022-07-06 07:01:09 +00:00
"github.com/sagernet/sing-box/option"
E "github.com/sagernet/sing/common/exceptions"
2024-11-12 11:37:10 +00:00
M "github.com/sagernet/sing/common/metadata"
2022-07-08 15:03:57 +00:00
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/service"
)
2025-01-12 04:45:27 +00:00
func New(ctx context.Context, options option.DialerOptions, remoteIsDomain bool) (N.Dialer, error) {
if options.IsWireGuardListener {
2024-12-14 16:45:41 +00:00
return NewDefault(ctx, options)
}
2023-08-08 08:14:03 +00:00
var (
dialer N.Dialer
err error
)
if options.Detour == "" {
2024-12-14 16:45:41 +00:00
dialer, err = NewDefault(ctx, options)
2023-08-08 08:14:03 +00:00
if err != nil {
return nil, err
}
} else {
outboundManager := service.FromContext[adapter.OutboundManager](ctx)
if outboundManager == nil {
return nil, E.New("missing outbound manager")
}
dialer = NewDetour(outboundManager, options.Detour)
}
2025-01-12 04:45:27 +00:00
if remoteIsDomain && options.Detour == "" && options.DomainResolver == "" {
deprecated.Report(ctx, deprecated.OptionMissingDomainResolverInDialOptions)
}
if (options.Detour == "" && remoteIsDomain) || options.DomainResolver != "" {
2024-12-02 15:17:01 +00:00
router := service.FromContext[adapter.DNSRouter](ctx)
2024-11-10 04:11:21 +00:00
if router != nil {
2025-01-12 04:45:27 +00:00
var resolveTransport adapter.DNSTransport
if options.DomainResolver != "" {
transport, loaded := service.FromContext[adapter.DNSTransportManager](ctx).Transport(options.DomainResolver)
if !loaded {
return nil, E.New("DNS server not found: " + options.DomainResolver)
}
resolveTransport = transport
}
2024-11-10 04:11:21 +00:00
dialer = NewResolveDialer(
router,
dialer,
options.Detour == "" && !options.TCPFastOpen,
2025-01-12 04:45:27 +00:00
resolveTransport,
2024-12-02 15:17:01 +00:00
C.DomainStrategy(options.DomainStrategy),
2024-11-10 04:11:21 +00:00
time.Duration(options.FallbackDelay))
}
}
2023-08-08 08:14:03 +00:00
return dialer, nil
}
2024-11-12 11:37:10 +00:00
func NewDirect(ctx context.Context, options option.DialerOptions) (ParallelInterfaceDialer, error) {
if options.Detour != "" {
return nil, E.New("`detour` is not supported in direct context")
}
if options.IsWireGuardListener {
2024-12-14 16:45:41 +00:00
return NewDefault(ctx, options)
2024-11-12 11:37:10 +00:00
}
2024-12-14 16:45:41 +00:00
dialer, err := NewDefault(ctx, options)
2024-11-12 11:37:10 +00:00
if err != nil {
return nil, err
}
2025-01-12 04:45:27 +00:00
var resolveTransport adapter.DNSTransport
if options.DomainResolver != "" {
transport, loaded := service.FromContext[adapter.DNSTransportManager](ctx).Transport(options.DomainResolver)
if !loaded {
return nil, E.New("DNS server not found: " + options.DomainResolver)
}
resolveTransport = transport
}
2024-11-12 11:37:10 +00:00
return NewResolveParallelInterfaceDialer(
2024-12-02 15:17:01 +00:00
service.FromContext[adapter.DNSRouter](ctx),
2024-11-12 11:37:10 +00:00
dialer,
true,
2025-01-12 04:45:27 +00:00
resolveTransport,
2024-12-02 15:17:01 +00:00
C.DomainStrategy(options.DomainStrategy),
2024-11-12 11:37:10 +00:00
time.Duration(options.FallbackDelay),
), nil
}
type ParallelInterfaceDialer interface {
N.Dialer
2024-12-14 16:45:41 +00:00
DialParallelInterface(ctx context.Context, network string, destination M.Socksaddr, strategy *C.NetworkStrategy, interfaceType []C.InterfaceType, fallbackInterfaceType []C.InterfaceType, fallbackDelay time.Duration) (net.Conn, error)
ListenSerialInterfacePacket(ctx context.Context, destination M.Socksaddr, strategy *C.NetworkStrategy, interfaceType []C.InterfaceType, fallbackInterfaceType []C.InterfaceType, fallbackDelay time.Duration) (net.PacketConn, error)
2024-11-12 11:37:10 +00:00
}
type ParallelNetworkDialer interface {
2024-12-14 16:45:41 +00:00
DialParallelNetwork(ctx context.Context, network string, destination M.Socksaddr, destinationAddresses []netip.Addr, strategy *C.NetworkStrategy, interfaceType []C.InterfaceType, fallbackInterfaceType []C.InterfaceType, fallbackDelay time.Duration) (net.Conn, error)
ListenSerialNetworkPacket(ctx context.Context, destination M.Socksaddr, destinationAddresses []netip.Addr, strategy *C.NetworkStrategy, interfaceType []C.InterfaceType, fallbackInterfaceType []C.InterfaceType, fallbackDelay time.Duration) (net.PacketConn, netip.Addr, error)
2024-11-12 11:37:10 +00:00
}