-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pull request 1927: 6006-use-address-processor
Updates #6006. Squashed commit of the following: commit ac27db9 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Tue Jul 18 15:47:17 2023 +0300 all: imp code commit 3936288 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Mon Jul 17 19:23:46 2023 +0300 all: imp client resolving
- Loading branch information
Showing
16 changed files
with
443 additions
and
397 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package dnsforward | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"time" | ||
|
||
"github.com/AdguardTeam/golibs/errors" | ||
"github.com/AdguardTeam/golibs/log" | ||
) | ||
|
||
// DialContext is a [whois.DialContextFunc] that uses s to resolve hostnames. | ||
func (s *Server) DialContext(ctx context.Context, network, addr string) (conn net.Conn, err error) { | ||
log.Debug("dnsforward: dialing %q for network %q", addr, network) | ||
|
||
host, port, err := net.SplitHostPort(addr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dialer := &net.Dialer{ | ||
// TODO(a.garipov): Consider making configurable. | ||
Timeout: time.Minute * 5, | ||
} | ||
|
||
if net.ParseIP(host) != nil { | ||
return dialer.DialContext(ctx, network, addr) | ||
} | ||
|
||
addrs, err := s.Resolve(host) | ||
if err != nil { | ||
return nil, fmt.Errorf("resolving %q: %w", host, err) | ||
} | ||
|
||
log.Debug("dnsforward: resolving %q: %v", host, addrs) | ||
|
||
if len(addrs) == 0 { | ||
return nil, fmt.Errorf("no addresses for host %q", host) | ||
} | ||
|
||
var dialErrs []error | ||
for _, a := range addrs { | ||
addr = net.JoinHostPort(a.String(), port) | ||
conn, err = dialer.DialContext(ctx, network, addr) | ||
if err != nil { | ||
dialErrs = append(dialErrs, err) | ||
|
||
continue | ||
} | ||
|
||
return conn, err | ||
} | ||
|
||
// TODO(a.garipov): Use errors.Join in Go 1.20. | ||
return nil, errors.List(fmt.Sprintf("dialing %q", addr), dialErrs...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.