2022-07-07 13:47:21 +00:00
|
|
|
package dialer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
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"
|
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"
|
2025-01-12 04:45:27 +00:00
|
|
|
"github.com/sagernet/sing/service"
|
2022-07-07 13:47:21 +00:00
|
|
|
)
|
|
|
|
|
2024-11-12 11:37:10 +00:00
|
|
|
var (
|
|
|
|
_ N.Dialer = (*resolveDialer)(nil)
|
|
|
|
_ ParallelInterfaceDialer = (*resolveParallelNetworkDialer)(nil)
|
|
|
|
)
|
|
|
|
|
2025-01-12 04:45:27 +00:00
|
|
|
type ResolveDialer interface {
|
|
|
|
N.Dialer
|
|
|
|
QueryOptions() adapter.DNSQueryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
type ParallelInterfaceResolveDialer interface {
|
|
|
|
ParallelInterfaceDialer
|
|
|
|
QueryOptions() adapter.DNSQueryOptions
|
|
|
|
}
|
|
|
|
|
2024-11-12 11:37:10 +00:00
|
|
|
type resolveDialer struct {
|
2025-01-12 04:45:27 +00:00
|
|
|
router adapter.DNSRouter
|
2022-07-08 04:58:43 +00:00
|
|
|
dialer N.Dialer
|
2023-09-27 05:17:16 +00:00
|
|
|
parallel bool
|
2025-01-12 04:45:27 +00:00
|
|
|
queryOptions adapter.DNSQueryOptions
|
2022-07-08 04:58:43 +00:00
|
|
|
fallbackDelay time.Duration
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|
|
|
|
|
2025-01-12 04:45:27 +00:00
|
|
|
func NewResolveDialer(ctx context.Context, dialer N.Dialer, parallel bool, queryOptions adapter.DNSQueryOptions, fallbackDelay time.Duration) ResolveDialer {
|
2024-11-12 11:37:10 +00:00
|
|
|
return &resolveDialer{
|
2025-01-12 04:45:27 +00:00
|
|
|
service.FromContext[adapter.DNSRouter](ctx),
|
2022-07-07 13:47:21 +00:00
|
|
|
dialer,
|
2023-09-27 05:17:16 +00:00
|
|
|
parallel,
|
2025-01-12 04:45:27 +00:00
|
|
|
queryOptions,
|
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
|
|
|
|
}
|
|
|
|
|
2025-01-12 04:45:27 +00:00
|
|
|
func NewResolveParallelInterfaceDialer(ctx context.Context, dialer ParallelInterfaceDialer, parallel bool, queryOptions adapter.DNSQueryOptions, fallbackDelay time.Duration) ParallelInterfaceResolveDialer {
|
2024-11-12 11:37:10 +00:00
|
|
|
return &resolveParallelNetworkDialer{
|
|
|
|
resolveDialer{
|
2025-01-12 04:45:27 +00:00
|
|
|
service.FromContext[adapter.DNSRouter](ctx),
|
2024-11-12 11:37:10 +00:00
|
|
|
dialer,
|
|
|
|
parallel,
|
2025-01-12 04:45:27 +00:00
|
|
|
queryOptions,
|
2024-11-12 11:37:10 +00:00
|
|
|
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)
|
|
|
|
}
|
2022-08-03 10:55:39 +00:00
|
|
|
ctx = log.ContextWithOverrideLevel(ctx, log.LevelDebug)
|
2025-01-12 04:45:27 +00:00
|
|
|
addresses, err := d.router.Lookup(ctx, destination.Fqdn, d.queryOptions)
|
2022-07-07 13:47:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-09-27 05:17:16 +00:00
|
|
|
if d.parallel {
|
2025-01-12 04:45:27 +00:00
|
|
|
return N.DialParallel(ctx, d.dialer, network, destination, addresses, d.queryOptions.Strategy == C.DomainStrategyPreferIPv6, d.fallbackDelay)
|
2023-09-27 05:17:16 +00:00
|
|
|
} 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)
|
|
|
|
}
|
2022-08-03 10:55:39 +00:00
|
|
|
ctx = log.ContextWithOverrideLevel(ctx, log.LevelDebug)
|
2025-01-12 04:45:27 +00:00
|
|
|
addresses, err := d.router.Lookup(ctx, destination.Fqdn, d.queryOptions)
|
2022-07-07 13:47:21 +00:00
|
|
|
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
|
|
|
|
}
|
2023-04-12 12:44:17 +00:00
|
|
|
return bufio.NewNATPacketConn(bufio.NewPacketConn(conn), M.SocksaddrFrom(destinationAddress, destination.Port), destination), nil
|
2022-07-07 13:47:21 +00:00
|
|
|
}
|
|
|
|
|
2025-01-12 04:45:27 +00:00
|
|
|
func (d *resolveDialer) QueryOptions() adapter.DNSQueryOptions {
|
|
|
|
return d.queryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *resolveDialer) Upstream() any {
|
|
|
|
return d.dialer
|
|
|
|
}
|
|
|
|
|
2024-12-14 16:45:41 +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 = log.ContextWithOverrideLevel(ctx, log.LevelDebug)
|
2025-01-12 04:45:27 +00:00
|
|
|
addresses, err := d.router.Lookup(ctx, destination.Fqdn, d.queryOptions)
|
2024-11-12 11:37:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if fallbackDelay == 0 {
|
|
|
|
fallbackDelay = d.fallbackDelay
|
|
|
|
}
|
|
|
|
if d.parallel {
|
2025-01-12 04:45:27 +00:00
|
|
|
return DialParallelNetwork(ctx, d.dialer, network, destination, addresses, d.queryOptions.Strategy == C.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-12-14 16:45:41 +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 = log.ContextWithOverrideLevel(ctx, log.LevelDebug)
|
2025-01-12 04:45:27 +00:00
|
|
|
addresses, err := d.router.Lookup(ctx, destination.Fqdn, d.queryOptions)
|
2024-11-12 11:37:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2025-01-12 04:45:27 +00:00
|
|
|
if fallbackDelay == 0 {
|
|
|
|
fallbackDelay = d.fallbackDelay
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2025-01-12 04:45:27 +00:00
|
|
|
func (d *resolveParallelNetworkDialer) QueryOptions() adapter.DNSQueryOptions {
|
|
|
|
return d.queryOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *resolveParallelNetworkDialer) Upstream() any {
|
2022-07-07 13:47:21 +00:00
|
|
|
return d.dialer
|
|
|
|
}
|