sing-box/common/dialer/resolve.go

88 lines
2.5 KiB
Go
Raw 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"
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
)
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
}
2023-09-27 05:17:16 +00:00
func NewResolveDialer(router adapter.Router, dialer N.Dialer, parallel bool, strategy dns.DomainStrategy, fallbackDelay time.Duration) *ResolveDialer {
2022-07-07 13:47:21 +00:00
return &ResolveDialer{
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
}
}
func (d *ResolveDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
if !destination.IsFqdn() {
return d.dialer.DialContext(ctx, network, destination)
}
2022-07-10 01:15:01 +00:00
ctx, metadata := adapter.AppendContext(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
}
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-07-10 01:15:01 +00:00
ctx, metadata := adapter.AppendContext(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
}
func (d *ResolveDialer) Upstream() any {
return d.dialer
}