-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
hosts.go
184 lines (174 loc) · 4.48 KB
/
hosts.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package runn
import (
"context"
"fmt"
"net"
"net/url"
"strings"
"time"
"github.com/chromedp/chromedp"
"github.com/minio/pkg/wildcard"
"github.com/xo/dburl"
)
type hostRule struct {
host string
rule string
}
type hostRules []hostRule
func (r hostRules) chromedpOpt() chromedp.ExecAllocatorOption { //nostyle:recvtype
var values []string
for _, rule := range r {
values = append(values, fmt.Sprintf("MAP %s %s", rule.host, rule.rule))
}
return chromedp.Flag("host-resolver-rules", strings.Join(values, ","))
}
// dialContextFunc returns DialContext() for http.Transport.DialContext.
func (r hostRules) dialContextFunc() func(ctx context.Context, network, addr string) (net.Conn, error) { //nostyle:recvtype
dialer := &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}
return func(ctx context.Context, network, addr string) (net.Conn, error) {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
for _, rule := range r {
if wildcard.MatchSimple(rule.host, host) {
var rhost, rport string
if strings.Contains(rule.rule, ":") {
rhost, rport, err = net.SplitHostPort(rule.rule)
if err != nil {
return nil, err
}
} else {
rhost = rule.rule
rport = port
}
return dialer.DialContext(ctx, network, net.JoinHostPort(rhost, rport))
}
}
return dialer.DialContext(ctx, network, addr)
}
}
// contextDialerFunc returns Dialer() for grpc.WithContextDialer.
func (r hostRules) contextDialerFunc() func(ctx context.Context, address string) (net.Conn, error) { //nostyle:recvtype
dialer := &net.Dialer{} // Same as google.golang.org/[email protected]/internal/transport.dial()
return func(ctx context.Context, address string) (net.Conn, error) {
host, port, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
var network string
for _, rule := range r {
if wildcard.MatchSimple(rule.host, host) {
var rhost, rport string
if strings.Contains(rule.rule, ":") {
rhost, rport, err = net.SplitHostPort(rule.rule)
if err != nil {
return nil, err
}
} else {
rhost = rule.rule
rport = port
}
address = net.JoinHostPort(rhost, rport)
network, address = parseDialTarget(address)
return dialer.DialContext(ctx, network, address)
}
}
network, address = parseDialTarget(address)
return dialer.DialContext(ctx, network, address)
}
}
// dialTimeoutFunc returns DialTimeout() for sshc.DialTimeoutFunc.
func (r hostRules) dialTimeoutFunc() func(network, address string, timeout time.Duration) (net.Conn, error) { //nostyle:recvtype
return func(network, address string, timeout time.Duration) (net.Conn, error) {
host, port, err := net.SplitHostPort(address)
if err != nil {
return nil, err
}
for _, rule := range r {
if wildcard.MatchSimple(rule.host, host) {
var rhost, rport string
if strings.Contains(rule.rule, ":") {
rhost, rport, err = net.SplitHostPort(rule.rule)
if err != nil {
return nil, err
}
} else {
rhost = rule.rule
rport = port
}
address = net.JoinHostPort(rhost, rport)
return net.DialTimeout(network, address, timeout)
}
}
return net.DialTimeout(network, address, timeout)
}
}
func (r hostRules) replaceDSN(dsn string) string { //nostyle:recvtype
u, err := dburl.Parse(dsn)
if err != nil {
return dsn
}
if u.Host == "" {
return dsn
}
var host, port string
if strings.Contains(u.Host, ":") {
host, port, err = net.SplitHostPort(u.Host)
if err != nil {
return dsn
}
} else {
host = u.Host
}
for _, rule := range r {
if wildcard.MatchSimple(rule.host, host) {
var rhost, rport string
if strings.Contains(rule.rule, ":") {
rhost, rport, err = net.SplitHostPort(rule.rule)
if err != nil {
return dsn
}
} else {
rhost = rule.rule
rport = port
}
if rport != "" {
u.Host = net.JoinHostPort(rhost, rport)
} else {
u.Host = rhost
}
return u.String()
}
}
return dsn
}
func parseDialTarget(target string) (string, string) {
net := "tcp"
m1 := strings.Index(target, ":")
m2 := strings.Index(target, ":/")
// handle unix:addr which will fail with url.Parse
if m1 >= 0 && m2 < 0 {
if n := target[0:m1]; n == "unix" {
return n, target[m1+1:]
}
}
if m2 >= 0 {
t, err := url.Parse(target)
if err != nil {
return net, target
}
scheme := t.Scheme
addr := t.Path
if scheme == "unix" {
if addr == "" {
addr = t.Host
}
return scheme, addr
}
}
return net, target
}