sing-box/common/dialer/dialer.go

88 lines
3.1 KiB
Go
Raw Normal View History

package dialer
import (
"context"
2024-11-12 19:37:10 +08:00
"net"
"net/netip"
2022-07-08 12:58:43 +08:00
"time"
2022-07-06 15:01:09 +08:00
"github.com/sagernet/sing-box/adapter"
2024-11-12 19:37:10 +08:00
C "github.com/sagernet/sing-box/constant"
2022-07-06 15:01:09 +08:00
"github.com/sagernet/sing-box/option"
2022-07-11 18:44:59 +08:00
"github.com/sagernet/sing-dns"
E "github.com/sagernet/sing/common/exceptions"
2024-11-12 19:37:10 +08:00
M "github.com/sagernet/sing/common/metadata"
2022-07-08 23:03:57 +08:00
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/service"
)
func New(ctx context.Context, options option.DialerOptions) (N.Dialer, error) {
2024-11-10 12:11:21 +08:00
networkManager := service.FromContext[adapter.NetworkManager](ctx)
if options.IsWireGuardListener {
2024-11-10 12:11:21 +08:00
return NewDefault(networkManager, options)
}
2023-08-08 16:14:03 +08:00
var (
dialer N.Dialer
err error
)
if options.Detour == "" {
2024-11-10 12:11:21 +08:00
dialer, err = NewDefault(networkManager, options)
2023-08-08 16:14:03 +08: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)
}
2024-11-10 12:11:21 +08:00
if networkManager == nil {
return NewDefault(networkManager, options)
2022-07-07 23:36:32 +08:00
}
2024-08-20 18:56:50 +08:00
if options.Detour == "" {
2024-11-10 12:11:21 +08:00
router := service.FromContext[adapter.Router](ctx)
if router != nil {
dialer = NewResolveDialer(
router,
dialer,
options.Detour == "" && !options.TCPFastOpen,
dns.DomainStrategy(options.DomainStrategy),
time.Duration(options.FallbackDelay))
}
}
2023-08-08 16:14:03 +08:00
return dialer, nil
}
2024-11-12 19:37:10 +08: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")
}
networkManager := service.FromContext[adapter.NetworkManager](ctx)
if options.IsWireGuardListener {
return NewDefault(networkManager, options)
}
dialer, err := NewDefault(networkManager, options)
if err != nil {
return nil, err
}
return NewResolveParallelInterfaceDialer(
service.FromContext[adapter.Router](ctx),
dialer,
true,
dns.DomainStrategy(options.DomainStrategy),
time.Duration(options.FallbackDelay),
), nil
}
type ParallelInterfaceDialer interface {
N.Dialer
2024-11-13 19:05:28 +08: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 19:37:10 +08:00
}
type ParallelNetworkDialer interface {
2024-11-13 19:05:28 +08: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 19:37:10 +08:00
}