-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.go
More file actions
246 lines (217 loc) · 5 KB
/
proxy.go
File metadata and controls
246 lines (217 loc) · 5 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
package proxykit
import (
"net"
"net/url"
"regexp"
"strconv"
"sync"
)
var hostnameRegexRFC1123 = sync.OnceValue(func() *regexp.Regexp {
return regexp.MustCompile(`^([a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,62}){1}(\.[a-zA-Z0-9]{1}[a-zA-Z0-9-]{0,62})*?$`)
})
type ProxyScheme string
func (pt ProxyScheme) String() string {
return string(pt)
}
const (
HTTP ProxyScheme = "http"
HTTPS ProxyScheme = "https"
SOCKS5 ProxyScheme = "socks5"
SOCKS5H ProxyScheme = "socks5h"
)
type ProxyProvider interface {
ProxyGetter
ProxySetter
}
type ProxyGetter interface {
ExportURL() *url.URL
GetHost() string
GetPassword() string
GetScheme() ProxyScheme
GetUsername() string
ProxyValidator
}
type ProxySetter interface {
SetHost(s string)
SetPassword(s string)
SetScheme(s string)
SetUsername(s string)
ProxyResetter
}
type ProxyValidator interface {
IsCredentialFilled() bool
IsValid() bool
IsZero() bool
}
type ProxyResetter interface {
Reset()
}
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"`
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"`
}
// Retrieves the scheme from the proxy struct.
func (p *Proxy) GetScheme() ProxyScheme {
if p == nil {
return ""
}
return p.Scheme
}
// Sets the scheme on the proxy struct.
func (p *Proxy) SetScheme(pt ProxyScheme) {
if p == nil {
return
}
p.Scheme = pt
}
// Retrieves the host from the proxy struct.
func (p *Proxy) GetHost() string {
if p == nil {
return ""
}
return p.Host
}
// Sets the host on the proxy struct.
func (p *Proxy) SetHost(s string) {
if p == nil {
return
}
p.Host = s
}
// Retrieves the username from the proxy struct.
func (p *Proxy) GetUsername() string {
if p == nil {
return ""
}
return p.Username
}
// Sets the username on the proxy struct.
func (p *Proxy) SetUsername(s string) {
if p == nil {
return
}
if len(s) > 255 {
return
}
p.Username = s
}
// Retrieves the password from the proxy struct.
func (p *Proxy) GetPassword() string {
if p == nil {
return ""
}
return p.Password
}
// Sets the password on the proxy struct.
func (p *Proxy) SetPassword(s string) {
if p == nil {
return
}
if len(s) > 255 {
return
}
p.Password = s
}
// Exports the proxy as *url.URL format
func (p *Proxy) ExportURL() *url.URL {
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
}
// Checks if the proxy credential has been set.
func (p *Proxy) IsCredentialFilled() bool {
if p == nil {
return false
}
return p.Username != "" || (p.Username != "" && p.Password != "")
}
// Checks whether the Proxy struct is empty or not
func (p Proxy) IsZero() bool {
return Proxy{} == p
}
// Validates the proxy struct.
func (p *Proxy) IsValid() bool {
if p == nil {
return false
}
if !IsValidScheme(p.Scheme) {
return false
}
if !IsValidHostnamePort(p.Host) {
return false
}
if !IsValidCredentials(p.Username, p.Password) {
return false
}
return true
}
// IsValidHostnamePort validates a <dns>:<port> combination for fields typically used for socket address.
func IsValidHostnamePort(hnp string) bool {
host, port, err := net.SplitHostPort(hnp)
if err != nil {
return false
}
if portNum, err := strconv.ParseInt(
port, 10, 32,
); err != nil || portNum < 1 || portNum > 65535 {
return false
}
if host != "" {
return hostnameRegexRFC1123().MatchString(host)
}
return true
}
// IsValidCredentials performs basic validation on username and password:
// a non-empty password requires a username, both must be <=255 bytes, contain no CTL bytes, and be printable ASCII.
// Returns true if all checks pass.
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
}
// IsValidScheme reports whether s is a supported proxy scheme.
// It returns true for the supported schemes: HTTP, HTTPS, SOCKS5 and SOCKS5H.
// For any other ProxyScheme value it returns false.
func IsValidScheme(s ProxyScheme) bool {
switch s {
case HTTP, HTTPS, SOCKS5, SOCKS5H:
return true
default:
return false
}
}
/*
stringContainsCTLByte reports whether s contains any ASCII control character.
Inspired of the "url" std package
*/
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
}
// Resets the proxy struct to it's default values.
func (p *Proxy) Reset() {
if p == nil {
return
}
p.Scheme = ""
p.Host = ""
p.Username = ""
p.Password = ""
}