-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
dialer.go
41 lines (35 loc) · 899 Bytes
/
dialer.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
package extconfig
import (
"context"
"fmt"
"net"
"net/http"
)
func Resolver() *net.Resolver {
return &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, network, Get().FallbackDNS)
},
}
}
func Transport() http.RoundTripper {
dialer := &net.Dialer{
Resolver: Resolver(),
}
return NewUserAgentTransport(fmt.Sprintf("gravity/%s", FullVersion()), &http.Transport{
Proxy: http.ProxyFromEnvironment,
Dial: dialer.Dial,
})
}
type userAgentTransport struct {
inner http.RoundTripper
ua string
}
func NewUserAgentTransport(ua string, inner http.RoundTripper) *userAgentTransport {
return &userAgentTransport{inner, ua}
}
func (uat *userAgentTransport) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Set("User-Agent", uat.ua)
return uat.inner.RoundTrip(r)
}