MCPcopy
hub / github.com/AdguardTeam/dnsproxy / ResolveDialContext

Function ResolveDialContext

internal/bootstrap/bootstrap.go:48–93  ·  view source on GitHub ↗

ResolveDialContext returns a DialHandler that uses addresses resolved from u using resolver. l and u must not be nil.

(
	u *url.URL,
	timeout time.Duration,
	r Resolver,
	preferV6 bool,
	l *slog.Logger,
)

Source from the content-addressed store, hash-verified

46// ResolveDialContext returns a DialHandler that uses addresses resolved from u
47// using resolver. l and u must not be nil.
48func ResolveDialContext(
49 u *url.URL,
50 timeout time.Duration,
51 r Resolver,
52 preferV6 bool,
53 l *slog.Logger,
54) (h DialHandler, err error) {
55 defer func() { err = errors.Annotate(err, "dialing %q: %w", u.Host) }()
56
57 host, port, err := netutil.SplitHostPort(u.Host)
58 if err != nil {
59 // Don't wrap the error since it's informative enough as is and there is
60 // already deferred annotation here.
61 return nil, err
62 }
63
64 if r == nil {
65 return nil, fmt.Errorf("resolver is nil: %w", ErrNoResolvers)
66 }
67
68 ctx := context.Background()
69 if timeout > 0 {
70 var cancel func()
71 ctx, cancel = context.WithTimeout(ctx, timeout)
72 defer cancel()
73 }
74
75 // TODO(e.burkov): Use network properly, perhaps, pass it through options.
76 ips, err := r.LookupNetIP(ctx, NetworkIP, host)
77 if err != nil {
78 return nil, fmt.Errorf("resolving hostname: %w", err)
79 }
80
81 if preferV6 {
82 slices.SortStableFunc(ips, netutil.PreferIPv6)
83 } else {
84 slices.SortStableFunc(ips, netutil.PreferIPv4)
85 }
86
87 addrs := make([]string, 0, len(ips))
88 for _, ip := range ips {
89 addrs = append(addrs, netip.AddrPortFrom(ip, port).String())
90 }
91
92 return NewDialContext(timeout, l, addrs...), nil
93}
94
95// NewDialContext returns a DialHandler that dials addrs and returns the first
96// successful connection. At least a single addr should be specified. l must

Callers 2

newDialerInitializerFunction · 0.92
TestResolveDialContextFunction · 0.92

Calls 3

NewDialContextFunction · 0.85
LookupNetIPMethod · 0.65
StringMethod · 0.45

Tested by 1

TestResolveDialContextFunction · 0.74

Used in the wild real call sites across dependent graphs

searching dependent graphs…