-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdns.go
37 lines (34 loc) · 879 Bytes
/
dns.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
package sidecar
import (
"context"
"net"
"net/http"
"time"
)
var (
globalResolver string
defaultTransport http.RoundTripper
)
func ChangeResolver(ipAddr string) {
globalResolver = ""
defaultTransport = http.DefaultTransport.(*http.Transport).Clone()
valid := net.ParseIP(ipAddr)
if valid != nil {
globalResolver = ipAddr
customResolverDialer := &net.Dialer{
Resolver: &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{
Timeout: time.Duration(5000) * time.Millisecond,
}
return d.DialContext(ctx, "udp", ipAddr+":53")
},
},
}
dialContext := func(ctx context.Context, network, addr string) (net.Conn, error) {
return customResolverDialer.DialContext(ctx, network, addr)
}
defaultTransport.(*http.Transport).DialContext = dialContext
}
}