-
Notifications
You must be signed in to change notification settings - Fork 25
/
utils.go
236 lines (195 loc) · 4.83 KB
/
utils.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
236
package azuretls
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"golang.org/x/net/idna"
"io"
"math/rand"
"net/url"
"reflect"
"regexp"
"strings"
"time"
"unicode/utf8"
)
const (
SchemeHttp = "http"
SchemeHttps = "https"
SchemeWs = "ws"
SchemeWss = "wss"
Socks5 = "socks5"
Socks5H = "socks5h"
defaultUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36"
)
var (
rdn = rand.New(rand.NewSource(time.Now().UnixNano()))
numberReg = regexp.MustCompile(`\d+`)
)
func formatProxy(proxy string) string {
var split = strings.Split(strings.Trim(proxy, "\n\r"), ":")
if len(split) == 4 {
if numberReg.MatchString(split[1]) {
// proxy = ip:port:username:password
return "http://" + split[2] + ":" + split[3] + "@" + split[0] + ":" + split[1]
}
// proxy = username:password:ip:port
return "http://" + split[0] + ":" + split[1] + "@" + split[2] + ":" + split[3]
} else if len(split) == 2 {
// proxy = ip:port
return "http://" + split[0] + ":" + split[1]
} else if len(split) == 3 {
// proxy = username:password@ip:port
return "http://" + proxy
}
return ""
}
// ToBytes converts any type to []byte, it supports string, []byte, io.Reader,
// strings.Builder and any other type that can be marshaled to json
func ToBytes(b any) []byte {
switch b.(type) {
case string:
return []byte(b.(string))
case []byte:
return b.([]byte)
case io.Reader:
var buf = new(bytes.Buffer)
_, _ = io.Copy(buf, b.(io.Reader))
return buf.Bytes()
case bytes.Buffer:
buf := b.(bytes.Buffer)
return buf.Bytes()
case strings.Builder:
buf := b.(strings.Builder)
return []byte(buf.String())
case *strings.Builder:
buf := b.(*strings.Builder)
return []byte(buf.String())
default:
var dumped []byte
dumped, _ = json.Marshal(b)
return dumped
}
}
func toReader(b any) (io.Reader, error) {
switch b.(type) {
case string:
return strings.NewReader(b.(string)), nil
case []byte:
return bytes.NewReader(b.([]byte)), nil
case io.Reader:
return b.(io.Reader), nil
case bytes.Buffer:
buf := b.(bytes.Buffer)
return bytes.NewReader(buf.Bytes()), nil
case strings.Builder:
buf := b.(strings.Builder)
return strings.NewReader(buf.String()), nil
case *strings.Builder:
buf := b.(*strings.Builder)
return strings.NewReader(buf.String()), nil
default:
value := reflect.ValueOf(b)
if value.Kind() == reflect.Ptr {
value = value.Elem()
}
switch value.Kind() {
case reflect.Struct, reflect.Map, reflect.Slice, reflect.Array:
var (
dumped []byte
err error
)
if dumped, err = json.Marshal(b); err != nil {
return nil, err
}
return bytes.NewReader(dumped), nil
default:
return nil, errors.New("unsupported body type : " + value.Kind().String())
}
}
}
var portMap = map[string]string{
SchemeHttp: "80",
SchemeHttps: "443",
SchemeWs: "80",
SchemeWss: "443",
Socks5: "1080",
Socks5H: "1080",
}
func idnaASCII(v string) (string, error) {
if isASCII(v) {
return v, nil
}
return idna.Lookup.ToASCII(v)
}
func isASCII(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] >= utf8.RuneSelf {
return false
}
}
return true
}
func isDomainOrSubdomain(sub, parent string) bool {
if sub == parent {
return true
}
// If sub is "foo.example.com" and parent is "example.com",
// that means sub must end in "."+parent.
// Do it without allocating.
if !strings.HasSuffix(sub, parent) {
return false
}
return sub[len(sub)-len(parent)-1] == '.'
}
// UrlEncode encodes a map[string]string to url encoded string
// If you want to encode a struct, you will use the `url` tag
func UrlEncode(obj any) string {
r := reflect.ValueOf(obj)
if r.Kind() == reflect.Ptr {
r = r.Elem()
}
switch r.Kind() {
case reflect.Map:
keys := r.MapKeys()
var result = make([]string, 0, len(keys))
for _, key := range keys {
result = append(result, fmt.Sprintf("%s=%v", key, url.QueryEscape(fmt.Sprintf("%v", r.MapIndex(key)))))
}
return strings.Join(result, "&")
case reflect.Struct:
var result = make([]string, 0, r.NumField())
for i := 0; i < r.NumField(); i++ {
if name, ok := r.Type().Field(i).Tag.Lookup("url"); ok {
if name == "-" {
continue
}
//detect if omitempty is set
split := strings.Split(name, ",")
if len(split) > 1 && split[1] == "omitempty" {
if r.Field(i).IsZero() {
continue
}
}
result = append(result, fmt.Sprintf("%s=%s", split[0], url.QueryEscape(fmt.Sprintf("%v", r.Field(i)))))
}
}
return strings.Join(result, "&")
case reflect.String:
return url.QueryEscape(r.String())
default:
return ""
}
}
func (s *Session) urlMatch(host *url.URL, urls []*regexp.Regexp) bool {
if urls == nil {
return false
}
for _, h := range urls {
if h.MatchString(host.String()) {
return true
}
}
return false
}