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