-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_client.go
68 lines (57 loc) · 1.46 KB
/
http_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package server
import (
"context"
"fmt"
"net"
"net/http"
"time"
)
const (
requestTimeoutSecs = 60
)
var (
dialer = &net.Dialer{}
resolver = &net.Resolver{}
)
func publicUnicastOnlyDialContext(ctx context.Context, network, addr string) (net.Conn, error) {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
ip := net.ParseIP(host)
var ipAddrs []string
if ip != nil {
ipAddrs = append(ipAddrs, ip.String())
} else {
ipAddrs, err = resolver.LookupHost(ctx, host)
if err != nil {
return nil, fmt.Errorf("failed to lookup host: %w", err)
}
}
var dialErrs []error
connectTimeout := requestTimeoutSecs * time.Second / time.Duration(len(ipAddrs)+1)
for _, ipAddr := range ipAddrs {
ip = net.ParseIP(ipAddr)
switch {
case !ip.IsGlobalUnicast() || ip.IsPrivate():
dialErrs = append(dialErrs, fmt.Errorf("forbidden address: %s", ipAddr))
continue
}
connCtx, cancel := context.WithTimeout(ctx, connectTimeout)
conn, err := dialer.DialContext(connCtx, network, net.JoinHostPort(ipAddr, port))
cancel()
if nil == err {
return conn, nil
}
dialErrs = append(dialErrs, fmt.Errorf("failed to dial '%s:%s': %w", ipAddr, port, err))
}
return nil, fmt.Errorf("failed to dial: %q", dialErrs)
}
type WithUserAgent struct {
http.RoundTripper
UserAgent string
}
func (w *WithUserAgent) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Set("User-Agent", w.UserAgent)
return w.RoundTripper.RoundTrip(r)
}