-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.go
More file actions
334 lines (292 loc) · 7.95 KB
/
Copy pathproxy.go
File metadata and controls
334 lines (292 loc) · 7.95 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// Package proxykit defines proxy addresses, schemes, credentials, and
// validation helpers.
package proxykit
import (
"net"
"net/url"
)
// ProxyScheme identifies a proxy protocol accepted by [IsValidScheme].
type ProxyScheme string
// String returns the string representation of s.
func (s ProxyScheme) String() string {
return string(s)
}
const (
// HTTP is the [ProxyScheme] for an HTTP proxy.
HTTP ProxyScheme = "http"
// HTTPS is the [ProxyScheme] for an HTTPS proxy.
HTTPS ProxyScheme = "https"
// SOCKS5 is the [ProxyScheme] for a SOCKS5 proxy.
SOCKS5 ProxyScheme = "socks5"
// SOCKS5H is the [ProxyScheme] for a SOCKS5 proxy that resolves hostnames remotely.
SOCKS5H ProxyScheme = "socks5h"
)
// ProxyProvider combines [ProxyGetter] and [ProxySetter].
type ProxyProvider interface {
ProxyGetter
ProxySetter
}
// ProxyGetter describes read access to proxy fields and validation.
type ProxyGetter interface {
// ExportURL returns the proxy as a [url.URL].
ExportURL() *url.URL
// GetHost returns the proxy host and port.
GetHost() string
// GetPassword returns the proxy password.
GetPassword() string
// GetScheme returns the proxy scheme.
GetScheme() ProxyScheme
// GetUsername returns the proxy username.
GetUsername() string
ProxyValidator
}
// ProxySetter describes write access to proxy fields.
type ProxySetter interface {
// SetHost sets the proxy host and port.
SetHost(s string)
// SetPassword sets the proxy password.
SetPassword(s string)
// SetScheme sets the proxy scheme.
SetScheme(s string)
// SetUsername sets the proxy username.
SetUsername(s string)
ProxyResetter
}
// ProxyValidator describes proxy state and validation methods.
type ProxyValidator interface {
// IsCredentialFilled reports whether the proxy has a username.
IsCredentialFilled() bool
// IsValidCredentials reports whether the proxy credentials are valid.
IsValidCredentials() bool
// IsValidHostnamePort reports whether the proxy host and port are valid.
IsValidHostnamePort() bool
// IsValidScheme reports whether the proxy scheme is supported.
IsValidScheme() bool
// IsValid reports whether the proxy fields are valid.
IsValid() bool
// IsZero reports whether every proxy field is empty.
IsZero() bool
}
// ProxyResetter describes a proxy reset operation.
type ProxyResetter interface {
// Reset clears every proxy field.
Reset()
}
// Proxy represents a proxy endpoint and its optional credentials.
type Proxy struct {
Scheme ProxyScheme `json:"scheme,omitempty" validate:"required_with=Host,omitempty,lowercase,oneof=http https socks5 socks5h"`
Host string `json:"host,omitempty" validate:"required_with=Scheme,omitempty,hostname_port|tcp_addr"`
Username string `json:"username,omitempty" validate:"required_with_all=Scheme Host Password,omitempty,printascii,max=255"`
Password string `json:"password,omitempty" validate:"omitempty,printascii,max=255"`
}
// GetScheme returns p's [ProxyScheme].
// It returns the zero scheme when p is nil.
func (p *Proxy) GetScheme() ProxyScheme {
if p == nil {
return ""
}
return p.Scheme
}
// SetScheme sets p's [ProxyScheme].
// It has no effect when p is nil.
func (p *Proxy) SetScheme(s ProxyScheme) {
if p == nil {
return
}
p.Scheme = s
}
// GetHost returns p's host and port.
// It returns an empty string when p is nil.
func (p *Proxy) GetHost() string {
if p == nil {
return ""
}
return p.Host
}
// SetHost sets p's host and port.
// It has no effect when p is nil.
func (p *Proxy) SetHost(s string) {
if p == nil {
return
}
p.Host = s
}
// GetUsername returns p's username.
// It returns an empty string when p is nil.
func (p *Proxy) GetUsername() string {
if p == nil {
return ""
}
return p.Username
}
// SetUsername sets p's username when s is at most 255 bytes.
// It has no effect when p is nil or s is longer than 255 bytes.
func (p *Proxy) SetUsername(s string) {
if p == nil {
return
}
if len(s) > 255 {
return
}
p.Username = s
}
// GetPassword returns p's password.
// It returns an empty string when p is nil.
func (p *Proxy) GetPassword() string {
if p == nil {
return ""
}
return p.Password
}
// SetPassword sets p's password when s is at most 255 bytes.
// It has no effect when p is nil or s is longer than 255 bytes.
func (p *Proxy) SetPassword(s string) {
if p == nil {
return
}
if len(s) > 255 {
return
}
p.Password = s
}
// ExportURL returns a [url.URL] containing p's scheme, host, and credentials.
// It returns nil when p is nil.
func (p *Proxy) ExportURL() *url.URL {
if p == nil {
return nil
}
u := new(url.URL)
u.Scheme = p.Scheme.String()
u.Host = p.Host
if p.Username != "" {
u.User = url.UserPassword(p.Username, p.Password)
}
return u
}
// IsValidScheme reports whether p's scheme passes [IsValidScheme].
func (p *Proxy) IsValidScheme() bool {
return p != nil && IsValidScheme(p.Scheme)
}
// IsValidHostnamePort reports whether p's host passes [IsValidHostnamePort].
func (p *Proxy) IsValidHostnamePort() bool {
return p != nil && IsValidHostnamePort(p.Host)
}
// IsCredentialFilled reports whether p has a username.
// It reports false when p is nil.
func (p *Proxy) IsCredentialFilled() bool {
return p != nil && p.Username != ""
}
// IsValidCredentials reports whether p's credentials pass
// [IsValidCredentials].
func (p *Proxy) IsValidCredentials() bool {
return p != nil && IsValidCredentials(p.Username, p.Password)
}
// IsZero reports whether every field in p is empty.
func (p Proxy) IsZero() bool {
return Proxy{} == p
}
// IsValid reports whether p passes [Proxy.IsValidScheme],
// [Proxy.IsValidHostnamePort], and [Proxy.IsValidCredentials].
func (p *Proxy) IsValid() bool {
return p != nil &&
p.IsValidScheme() &&
p.IsValidHostnamePort() &&
p.IsValidCredentials()
}
// IsValidScheme reports whether s is a supported proxy scheme.
// It returns true for [HTTP], [HTTPS], [SOCKS5], and [SOCKS5H].
func IsValidScheme(s ProxyScheme) bool {
switch s {
case HTTP, HTTPS, SOCKS5, SOCKS5H:
return true
default:
return false
}
}
// IsValidHostnamePort reports whether hnp is a valid host-port pair.
// The port must be between 1 and 65535. A non-empty host must be an IP address
// or contain only letters, decimal digits, hyphens, and dot-separated labels of
// at most 63 bytes.
func IsValidHostnamePort(hnp string) bool {
host, port, err := net.SplitHostPort(hnp)
if err != nil {
return false
}
if len(port) == 0 || len(port) > 5 {
return false
}
var portNumber int
for i := range len(port) {
c := port[i]
if c < '0' || c > '9' {
return false
}
portNumber = portNumber*10 + int(c-'0')
}
if portNumber == 0 || portNumber > 65535 {
return false
}
if host == "" {
return true
}
var lblLength int
for i := range len(host) {
c := host[i]
if c == ':' {
return net.ParseIP(host) != nil
}
if c == '.' {
if lblLength == 0 {
return false
}
lblLength = 0
continue
}
if !((c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == '-') {
return false
}
lblLength++
if lblLength > 63 {
return false
}
}
return lblLength != 0
}
// IsValidCredentials reports whether username and password form valid proxy
// credentials. Each value may contain at most 255 bytes and no ASCII control
// bytes. A non-empty password requires a non-empty username.
func IsValidCredentials(username, password string) bool {
if username == "" && password != "" {
return false
}
if len(username) > 255 || len(password) > 255 {
return false
}
if stringContainsCTLByte(username) || stringContainsCTLByte(password) {
return false
}
return true
}
// stringContainsCTLByte reports whether s contains an ASCII control byte.
func stringContainsCTLByte(s string) bool {
for i, b, length := 0, byte(0), len(s); i < length; i++ {
if b = s[i]; b < 0x20 || b == 0x7f {
return true
}
}
return false
}
// Reset clears every field in p.
// It has no effect when p is nil.
func (p *Proxy) Reset() {
if p == nil {
return
}
p.Scheme = ""
p.Host = ""
p.Username = ""
p.Password = ""
}