|
|
@@ -12,21 +12,20 @@ import (
|
|
12
|
12
|
doh "github.com/babolivier/go-doh-client"
|
|
13
|
13
|
)
|
|
14
|
14
|
|
|
15
|
|
-type Network struct {
|
|
16
|
|
- HTTP http.Client
|
|
17
|
|
- DNS doh.Resolver
|
|
18
|
|
-
|
|
19
|
|
- dialer Dialer
|
|
|
15
|
+type network struct {
|
|
|
16
|
+ idleTimeout time.Duration
|
|
|
17
|
+ dialer Dialer
|
|
|
18
|
+ dns doh.Resolver
|
|
20
|
19
|
}
|
|
21
|
20
|
|
|
22
|
|
-func (d *Network) Dial(network, address string) (net.Conn, error) {
|
|
23
|
|
- return d.DialContext(context.Background(), network, address)
|
|
|
21
|
+func (n *network) Dial(protocol, address string) (net.Conn, error) {
|
|
|
22
|
+ return n.DialContext(context.Background(), protocol, address)
|
|
24
|
23
|
}
|
|
25
|
24
|
|
|
26
|
|
-func (d *Network) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
|
|
25
|
+func (n *network) DialContext(ctx context.Context, protocol, address string) (net.Conn, error) {
|
|
27
|
26
|
host, port, _ := net.SplitHostPort(address)
|
|
28
|
27
|
|
|
29
|
|
- ips, err := d.resolveIPs(network, host)
|
|
|
28
|
+ ips, err := n.DNSResolve(protocol, host)
|
|
30
|
29
|
if err != nil {
|
|
31
|
30
|
return nil, fmt.Errorf("cannot resolve dns names: %w", err)
|
|
32
|
31
|
}
|
|
|
@@ -37,16 +36,20 @@ func (d *Network) DialContext(ctx context.Context, network, address string) (net
|
|
37
|
36
|
})
|
|
38
|
37
|
}
|
|
39
|
38
|
|
|
|
39
|
+ var conn net.Conn
|
|
|
40
|
+
|
|
40
|
41
|
for _, v := range ips {
|
|
41
|
|
- if conn, err := d.dialer.DialContext(ctx, network, net.JoinHostPort(v, port)); err == nil {
|
|
|
42
|
+ conn, err = n.dialer.DialContext(ctx, protocol, net.JoinHostPort(v, port))
|
|
|
43
|
+
|
|
|
44
|
+ if err == nil {
|
|
42
|
45
|
return conn, nil
|
|
43
|
46
|
}
|
|
44
|
47
|
}
|
|
45
|
48
|
|
|
46
|
|
- return nil, fmt.Errorf("cannot dial to %s:%s", network, address)
|
|
|
49
|
+ return nil, fmt.Errorf("cannot dial to %s:%s: %w", protocol, address, err)
|
|
47
|
50
|
}
|
|
48
|
51
|
|
|
49
|
|
-func (d *Network) resolveIPs(network, address string) ([]string, error) {
|
|
|
52
|
+func (n *network) DNSResolve(protocol, address string) ([]string, error) {
|
|
50
|
53
|
if net.ParseIP(address) != nil {
|
|
51
|
54
|
return []string{address}, nil
|
|
52
|
55
|
}
|
|
|
@@ -55,14 +58,14 @@ func (d *Network) resolveIPs(network, address string) ([]string, error) {
|
|
55
|
58
|
wg := &sync.WaitGroup{}
|
|
56
|
59
|
mutex := &sync.Mutex{}
|
|
57
|
60
|
|
|
58
|
|
- switch network {
|
|
|
61
|
+ switch protocol {
|
|
59
|
62
|
case "tcp", "tcp4":
|
|
60
|
63
|
wg.Add(1)
|
|
61
|
64
|
|
|
62
|
65
|
go func() {
|
|
63
|
66
|
defer wg.Done()
|
|
64
|
67
|
|
|
65
|
|
- if recs, _, err := d.DNS.LookupA(address); err == nil {
|
|
|
68
|
+ if recs, _, err := n.dns.LookupA(address); err == nil {
|
|
66
|
69
|
mutex.Lock()
|
|
67
|
70
|
defer mutex.Unlock()
|
|
68
|
71
|
|
|
|
@@ -73,14 +76,14 @@ func (d *Network) resolveIPs(network, address string) ([]string, error) {
|
|
73
|
76
|
}()
|
|
74
|
77
|
}
|
|
75
|
78
|
|
|
76
|
|
- switch network {
|
|
|
79
|
+ switch protocol {
|
|
77
|
80
|
case "tcp", "tcp6":
|
|
78
|
81
|
wg.Add(1)
|
|
79
|
82
|
|
|
80
|
83
|
go func() {
|
|
81
|
84
|
defer wg.Done()
|
|
82
|
85
|
|
|
83
|
|
- if recs, _, err := d.DNS.LookupAAAA(address); err == nil {
|
|
|
86
|
+ if recs, _, err := n.dns.LookupAAAA(address); err == nil {
|
|
84
|
87
|
mutex.Lock()
|
|
85
|
88
|
defer mutex.Unlock()
|
|
86
|
89
|
|
|
|
@@ -94,44 +97,53 @@ func (d *Network) resolveIPs(network, address string) ([]string, error) {
|
|
94
|
97
|
wg.Wait()
|
|
95
|
98
|
|
|
96
|
99
|
if len(ips) == 0 {
|
|
97
|
|
- return nil, fmt.Errorf("cannot find any ips for %s:%s", network, address)
|
|
|
100
|
+ return nil, fmt.Errorf("cannot find any ips for %s:%s", protocol, address)
|
|
98
|
101
|
}
|
|
99
|
102
|
|
|
100
|
103
|
return ips, nil
|
|
101
|
104
|
}
|
|
102
|
105
|
|
|
103
|
|
-func NewNetwork(dialer Dialer, dohHostname string, httpTimeout time.Duration) (*Network, error) {
|
|
104
|
|
- switch {
|
|
105
|
|
- case httpTimeout < 0:
|
|
106
|
|
- return nil, fmt.Errorf("timeout should be positive number %v", httpTimeout)
|
|
107
|
|
- case httpTimeout == 0:
|
|
108
|
|
- httpTimeout = DefaultHTTPTimeout
|
|
109
|
|
- }
|
|
|
106
|
+func (n *network) IdleTimeout() time.Duration {
|
|
|
107
|
+ return n.idleTimeout
|
|
|
108
|
+}
|
|
110
|
109
|
|
|
111
|
|
- if net.ParseIP(dohHostname) == nil {
|
|
112
|
|
- return nil, fmt.Errorf("hostname %s should be IP address", dohHostname)
|
|
|
110
|
+func (n *network) MakeHTTPClient(timeout time.Duration) *http.Client {
|
|
|
111
|
+ if timeout <= 0 {
|
|
|
112
|
+ timeout = DefaultHTTPTimeout
|
|
113
|
113
|
}
|
|
114
|
114
|
|
|
115
|
|
- dohHTTPClient := &http.Client{
|
|
116
|
|
- Timeout: DefaultDNSTimeout,
|
|
|
115
|
+ return &http.Client{
|
|
|
116
|
+ Timeout: timeout,
|
|
117
|
117
|
Transport: &http.Transport{
|
|
118
|
|
- DialContext: dialer.DialContext,
|
|
|
118
|
+ DialContext: n.DialContext,
|
|
119
|
119
|
},
|
|
120
|
120
|
}
|
|
121
|
|
- network := &Network{
|
|
122
|
|
- dialer: dialer,
|
|
123
|
|
- DNS: doh.Resolver{
|
|
124
|
|
- Host: dohHostname,
|
|
125
|
|
- Class: doh.IN,
|
|
126
|
|
- HTTPClient: dohHTTPClient,
|
|
127
|
|
- },
|
|
|
121
|
+}
|
|
|
122
|
+
|
|
|
123
|
+func NewNetwork(dialer Dialer, dohHostname string, idleTimeout time.Duration) (Network, error) {
|
|
|
124
|
+ switch {
|
|
|
125
|
+ case idleTimeout < 0:
|
|
|
126
|
+ return nil, fmt.Errorf("timeout should be positive number %s", idleTimeout)
|
|
|
127
|
+ case idleTimeout == 0:
|
|
|
128
|
+ idleTimeout = DefaultIdleTimeout
|
|
128
|
129
|
}
|
|
129
|
|
- network.HTTP = http.Client{
|
|
130
|
|
- Timeout: httpTimeout,
|
|
131
|
|
- Transport: &http.Transport{
|
|
132
|
|
- DialContext: network.DialContext,
|
|
133
|
|
- },
|
|
|
130
|
+
|
|
|
131
|
+ if net.ParseIP(dohHostname) == nil {
|
|
|
132
|
+ return nil, fmt.Errorf("hostname %s should be IP address", dohHostname)
|
|
134
|
133
|
}
|
|
135
|
134
|
|
|
136
|
|
- return network, nil
|
|
|
135
|
+ return &network{
|
|
|
136
|
+ dialer: dialer,
|
|
|
137
|
+ idleTimeout: idleTimeout,
|
|
|
138
|
+ dns: doh.Resolver{
|
|
|
139
|
+ Host: dohHostname,
|
|
|
140
|
+ Class: doh.IN,
|
|
|
141
|
+ HTTPClient: &http.Client{
|
|
|
142
|
+ Timeout: DNSTimeout,
|
|
|
143
|
+ Transport: &http.Transport{
|
|
|
144
|
+ DialContext: dialer.DialContext,
|
|
|
145
|
+ },
|
|
|
146
|
+ },
|
|
|
147
|
+ },
|
|
|
148
|
+ }, nil
|
|
137
|
149
|
}
|