-
Notifications
You must be signed in to change notification settings - Fork 8
/
config.go
235 lines (189 loc) · 5.06 KB
/
config.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package safeurl
import (
"crypto/tls"
"fmt"
"net"
"net/http"
"strings"
"time"
)
type configBuilder struct {
timeout time.Duration
checkRedirect func(req *http.Request, via []*http.Request) error
jar http.CookieJar
allowedPorts []int
allowedSchemes []string
allowedHosts []string
blockedIPs []string
allowedIPs []string
blockedIPsCIDR []string
allowedIPsCIDR []string
allowSendingCredentials bool
isIPv6Enabled bool
isDebugLoggingEnabled bool
inTestMode bool
tlsConfig *tls.Config
}
type Config struct {
Timeout time.Duration
CheckRedirect func(req *http.Request, via []*http.Request) error
Jar http.CookieJar
AllowedPorts []int
AllowedSchemes []string
AllowedHosts []string
BlockedIPs []net.IP
AllowedIPs []net.IP
BlockedIPsCIDR []net.IPNet
AllowedIPsCIDR []net.IPNet
AllowSendingCredentials bool
IsIPv6Enabled bool
IsDebugLoggingEnabled bool
InTestMode bool
TlsConfig *tls.Config
}
func GetConfigBuilder() *configBuilder {
return &configBuilder{
allowedSchemes: nil,
allowedHosts: nil,
allowedPorts: nil,
blockedIPs: nil,
allowedIPs: nil,
blockedIPsCIDR: nil,
allowedIPsCIDR: nil,
isIPv6Enabled: false,
isDebugLoggingEnabled: false,
inTestMode: false,
tlsConfig: nil,
}
}
func (cb *configBuilder) SetTimeout(timeout time.Duration) *configBuilder {
cb.timeout = timeout
return cb
}
func (cb *configBuilder) SetCheckRedirect(checkRedirectFunc func(req *http.Request, via []*http.Request) error) *configBuilder {
cb.checkRedirect = checkRedirectFunc
return cb
}
func (cb *configBuilder) SetCookieJar(jar http.CookieJar) *configBuilder {
cb.jar = jar
return cb
}
func (cb *configBuilder) SetAllowedSchemes(schemas ...string) *configBuilder {
cb.allowedSchemes = schemas
return cb
}
func (cb *configBuilder) SetAllowedHosts(hosts ...string) *configBuilder {
cb.allowedHosts = hosts
return cb
}
func (cb *configBuilder) SetAllowedPorts(ports ...int) *configBuilder {
cb.allowedPorts = ports
return cb
}
func (cb *configBuilder) SetBlockedIPs(ips ...string) *configBuilder {
cb.blockedIPs = ips
return cb
}
func (cb *configBuilder) SetAllowedIPs(ips ...string) *configBuilder {
cb.allowedIPs = ips
return cb
}
func (cb *configBuilder) SetBlockedIPsCIDR(ipsCIDR ...string) *configBuilder {
cb.blockedIPsCIDR = ipsCIDR
return cb
}
func (cb *configBuilder) SetAllowedIPsCIDR(ipsCIDR ...string) *configBuilder {
cb.allowedIPsCIDR = ipsCIDR
return cb
}
func (cb *configBuilder) EnableIPv6(enable bool) *configBuilder {
cb.isIPv6Enabled = enable
return cb
}
func (cb *configBuilder) EnableDebugLogging(enable bool) *configBuilder {
cb.isDebugLoggingEnabled = enable
return cb
}
func (cb *configBuilder) AllowSendingCredentials(allow bool) *configBuilder {
cb.allowSendingCredentials = allow
return cb
}
func (cb *configBuilder) EnableTestMode(enable bool) *configBuilder {
cb.inTestMode = enable
return cb
}
func (cb *configBuilder) SetTlsConfig(tlsConfig *tls.Config) *configBuilder {
cb.tlsConfig = tlsConfig
return cb
}
func (cb *configBuilder) Build() *Config {
wc := &Config{
Timeout: cb.timeout,
CheckRedirect: cb.checkRedirect,
Jar: cb.jar,
IsIPv6Enabled: cb.isIPv6Enabled,
AllowSendingCredentials: cb.allowSendingCredentials,
IsDebugLoggingEnabled: cb.isDebugLoggingEnabled,
InTestMode: cb.inTestMode,
TlsConfig: cb.tlsConfig,
}
if cb.allowedSchemes == nil {
// allow only HTTP and HTTPS by default
wc.AllowedSchemes = []string{"http", "https"}
} else {
for _, scheme := range cb.allowedSchemes {
wc.AllowedSchemes = append(wc.AllowedSchemes, strings.ToLower(strings.TrimSpace(scheme)))
}
}
if cb.allowedHosts == nil {
wc.AllowedHosts = nil
} else {
for _, host := range cb.allowedHosts {
wc.AllowedHosts = append(wc.AllowedHosts, strings.ToLower(strings.TrimSpace(host)))
}
}
if cb.allowedPorts == nil {
// allow only HTTP and HTTPS ports by default
wc.AllowedPorts = append(cb.allowedPorts, 80, 443)
} else {
for _, port := range cb.allowedPorts {
if port <= 0 || port > 65535 {
panic(fmt.Sprintf("invalid port: %v", port))
}
wc.AllowedPorts = append(wc.AllowedPorts, port)
}
}
if cb.blockedIPs == nil {
wc.BlockedIPs = nil
} else {
for _, ip := range cb.blockedIPs {
parsed := parseIP(ip)
wc.BlockedIPs = append(wc.BlockedIPs, parsed)
}
}
if cb.allowedIPs == nil {
wc.AllowedIPs = nil
} else {
for _, ip := range cb.allowedIPs {
parsed := parseIP(ip)
wc.AllowedIPs = append(wc.AllowedIPs, parsed)
}
}
if cb.blockedIPsCIDR == nil {
wc.BlockedIPsCIDR = nil
} else {
for _, blockedNet := range cb.blockedIPsCIDR {
parsedBlockedNet := parseCIDR(blockedNet)
wc.BlockedIPsCIDR = append(wc.BlockedIPsCIDR, parsedBlockedNet)
}
}
if cb.allowedIPsCIDR == nil {
wc.AllowedIPsCIDR = nil
} else {
for _, allowedNet := range cb.allowedIPsCIDR {
parsedAllowedNet := parseCIDR(allowedNet)
wc.AllowedIPsCIDR = append(wc.AllowedIPsCIDR, parsedAllowedNet)
}
}
return wc
}