sing-box/common/dialer/resolve.go

165 lines
5.3 KiB
Go
Raw Permalink Normal View History

2022-07-07 13:47:21 +00:00
package dialer
import (
"context"
"net"
"net/netip"
2022-07-08 04:58:43 +00:00
"time"
2022-07-07 13:47:21 +00:00
"github.com/sagernet/sing-box/adapter"
2024-11-12 11:37:10 +00:00
C "github.com/sagernet/sing-box/constant"
2022-08-03 10:55:39 +00:00
"github.com/sagernet/sing-box/log"
2022-07-11 10:44:59 +00:00
"github.com/sagernet/sing-dns"
2023-04-02 04:01:19 +00:00
"github.com/sagernet/sing/common/bufio"
2022-07-08 15:03:57 +00:00
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
2022-07-07 13:47:21 +00:00
)
2024-11-12 11:37:10 +00:00
var (
_ N.Dialer = (*resolveDialer)(nil)
_ ParallelInterfaceDialer = (*resolveParallelNetworkDialer)(nil)
)
type resolveDialer struct {
2022-07-08 04:58:43 +00:00
dialer N.Dialer
2023-09-27 05:17:16 +00:00
parallel bool
2022-07-08 04:58:43 +00:00
router adapter.Router
2022-07-11 10:44:59 +00:00
strategy dns.DomainStrategy
2022-07-08 04:58:43 +00:00
fallbackDelay time.Duration
2022-07-07 13:47:21 +00:00
}
2024-11-12 11:37:10 +00:00
func NewResolveDialer(router adapter.Router, dialer N.Dialer, parallel bool, strategy dns.DomainStrategy, fallbackDelay time.Duration) N.Dialer {
return &resolveDialer{
2022-07-07 13:47:21 +00:00
dialer,
2023-09-27 05:17:16 +00:00
parallel,
2022-07-07 13:47:21 +00:00
router,
strategy,
2022-07-08 04:58:43 +00:00
fallbackDelay,
2022-07-07 13:47:21 +00:00
}
}
2024-11-12 11:37:10 +00:00
type resolveParallelNetworkDialer struct {
resolveDialer
dialer ParallelInterfaceDialer
}
func NewResolveParallelInterfaceDialer(router adapter.Router, dialer ParallelInterfaceDialer, parallel bool, strategy dns.DomainStrategy, fallbackDelay time.Duration) ParallelInterfaceDialer {
return &resolveParallelNetworkDialer{
resolveDialer{
dialer,
parallel,
router,
strategy,
fallbackDelay,
},
dialer,
}
}
func (d *resolveDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
2022-07-07 13:47:21 +00:00
if !destination.IsFqdn() {
return d.dialer.DialContext(ctx, network, destination)
}
2023-10-25 04:00:00 +00:00
ctx, metadata := adapter.ExtendContext(ctx)
2022-08-03 10:55:39 +00:00
ctx = log.ContextWithOverrideLevel(ctx, log.LevelDebug)
2022-07-10 01:15:01 +00:00
metadata.Destination = destination
metadata.Domain = ""
2022-07-07 13:47:21 +00:00
var addresses []netip.Addr
var err error
2022-07-11 10:44:59 +00:00
if d.strategy == dns.DomainStrategyAsIS {
2022-07-07 13:47:21 +00:00
addresses, err = d.router.LookupDefault(ctx, destination.Fqdn)
} else {
addresses, err = d.router.Lookup(ctx, destination.Fqdn, d.strategy)
}
if err != nil {
return nil, err
}
2023-09-27 05:17:16 +00:00
if d.parallel {
return N.DialParallel(ctx, d.dialer, network, destination, addresses, d.strategy == dns.DomainStrategyPreferIPv6, d.fallbackDelay)
} else {
return N.DialSerial(ctx, d.dialer, network, destination, addresses)
}
2022-07-07 13:47:21 +00:00
}
2024-11-12 11:37:10 +00:00
func (d *resolveDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
2022-08-29 11:58:58 +00:00
if !destination.IsFqdn() {
2022-07-07 13:47:21 +00:00
return d.dialer.ListenPacket(ctx, destination)
}
2023-10-25 04:00:00 +00:00
ctx, metadata := adapter.ExtendContext(ctx)
2022-08-03 10:55:39 +00:00
ctx = log.ContextWithOverrideLevel(ctx, log.LevelDebug)
2022-07-10 01:15:01 +00:00
metadata.Destination = destination
metadata.Domain = ""
2022-07-07 13:47:21 +00:00
var addresses []netip.Addr
var err error
2022-07-11 10:44:59 +00:00
if d.strategy == dns.DomainStrategyAsIS {
2022-07-07 13:47:21 +00:00
addresses, err = d.router.LookupDefault(ctx, destination.Fqdn)
} else {
addresses, err = d.router.Lookup(ctx, destination.Fqdn, d.strategy)
}
if err != nil {
return nil, err
}
2023-04-02 04:01:19 +00:00
conn, destinationAddress, err := N.ListenSerial(ctx, d.dialer, destination, addresses)
2022-07-08 04:58:43 +00:00
if err != nil {
return nil, err
}
return bufio.NewNATPacketConn(bufio.NewPacketConn(conn), M.SocksaddrFrom(destinationAddress, destination.Port), destination), nil
2022-07-07 13:47:21 +00:00
}
2024-11-13 11:05:28 +00:00
func (d *resolveParallelNetworkDialer) DialParallelInterface(ctx context.Context, network string, destination M.Socksaddr, strategy C.NetworkStrategy, interfaceType []C.InterfaceType, fallbackInterfaceType []C.InterfaceType, fallbackDelay time.Duration) (net.Conn, error) {
2024-11-12 11:37:10 +00:00
if !destination.IsFqdn() {
return d.dialer.DialContext(ctx, network, destination)
}
ctx, metadata := adapter.ExtendContext(ctx)
ctx = log.ContextWithOverrideLevel(ctx, log.LevelDebug)
metadata.Destination = destination
metadata.Domain = ""
var addresses []netip.Addr
var err error
if d.strategy == dns.DomainStrategyAsIS {
addresses, err = d.router.LookupDefault(ctx, destination.Fqdn)
} else {
addresses, err = d.router.Lookup(ctx, destination.Fqdn, d.strategy)
}
if err != nil {
return nil, err
}
if fallbackDelay == 0 {
fallbackDelay = d.fallbackDelay
}
if d.parallel {
2024-11-13 11:05:28 +00:00
return DialParallelNetwork(ctx, d.dialer, network, destination, addresses, d.strategy == dns.DomainStrategyPreferIPv6, strategy, interfaceType, fallbackInterfaceType, fallbackDelay)
2024-11-12 11:37:10 +00:00
} else {
2024-11-13 11:05:28 +00:00
return DialSerialNetwork(ctx, d.dialer, network, destination, addresses, strategy, interfaceType, fallbackInterfaceType, fallbackDelay)
2024-11-12 11:37:10 +00:00
}
}
2024-11-13 11:05:28 +00:00
func (d *resolveParallelNetworkDialer) 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
if !destination.IsFqdn() {
return d.dialer.ListenPacket(ctx, destination)
}
ctx, metadata := adapter.ExtendContext(ctx)
ctx = log.ContextWithOverrideLevel(ctx, log.LevelDebug)
metadata.Destination = destination
metadata.Domain = ""
var addresses []netip.Addr
var err error
if d.strategy == dns.DomainStrategyAsIS {
addresses, err = d.router.LookupDefault(ctx, destination.Fqdn)
} else {
addresses, err = d.router.Lookup(ctx, destination.Fqdn, d.strategy)
}
if err != nil {
return nil, err
}
2024-11-13 11:05:28 +00:00
conn, destinationAddress, err := ListenSerialNetworkPacket(ctx, d.dialer, destination, addresses, strategy, interfaceType, fallbackInterfaceType, fallbackDelay)
2024-11-12 11:37:10 +00:00
if err != nil {
return nil, err
}
return bufio.NewNATPacketConn(bufio.NewPacketConn(conn), M.SocksaddrFrom(destinationAddress, destination.Port), destination), nil
}
func (d *resolveDialer) Upstream() any {
2022-07-07 13:47:21 +00:00
return d.dialer
}