-
Notifications
You must be signed in to change notification settings - Fork 0
/
Requests.go
160 lines (139 loc) · 4.84 KB
/
Requests.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
package FlowX
import (
"context"
"encoding/base64"
"golang.org/x/net/proxy"
"io"
"math/rand"
"net"
"net/http"
"net/url"
"time"
)
type ProxyType int
const (
SOCKS5 ProxyType = iota
SOCKS4
HTTP
)
type ProxyConfig struct {
Type ProxyType
Address string
Username string
Password string
}
var userAgents = []string{
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/B08C390",
"Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; AS; rv:11.0) like Gecko",
"Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64; Trident/7.0; AS; rv:11.0) like Gecko",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0",
"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:54.0) Gecko/20100101 Firefox/54.0",
"Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko",
"Mozilla/5.0 (Windows NT 6.1; Win64; x64; Trident/7.0; AS; rv:11.0) like Gecko",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
"Mozilla/5.0 (Windows NT 6.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134",
"Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; AS; rv:11.0) like Gecko",
"Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko",
}
func AddRequestHeader(request *http.Request, key, value string) {
request.Header.Add(key, value)
}
func AddRequestHeaders(request *http.Request, headers map[string]string) {
for key, value := range headers {
request.Header.Add(key, value)
}
}
func SetUserAgent(request *http.Request, agent string) {
request.Header.Set("User-Agent", agent)
}
func RandomUserAgent(request *http.Request) {
rand.Seed(time.Now().UnixNano())
agent := userAgents[rand.Intn(len(userAgents))]
request.Header.Set("User-Agent", agent)
}
func GetResponseBody(response *http.Response) (string, error) {
body, err := io.ReadAll(response.Body)
if err != nil {
return "", err
}
defer response.Body.Close()
return string(body), nil
}
func CreateGetRequest(url string) (*http.Request, error) {
request, err := http.NewRequest("GET", url, nil)
return request, err
}
func CreatePostRequest(url string, contentType string, body io.Reader) (*http.Request, error) {
request, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err
}
request.Header.Set("Content-Type", contentType)
return request, nil
}
func CreateHTTPClient(autoRedirect bool, proxyConfig *ProxyConfig) (*http.Client, error) {
var transport http.Transport
if proxyConfig != nil {
switch proxyConfig.Type {
case SOCKS5:
dialer, err := proxy.SOCKS5("tcp", proxyConfig.Address, nil, proxy.Direct)
if err != nil {
return nil, err
}
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.Dial(network, addr)
}
case SOCKS4:
dialer, err := proxy.SOCKS5("tcp", proxyConfig.Address, nil, &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
})
if err != nil {
return nil, err
}
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return dialer.Dial(network, addr)
}
case HTTP:
proxyURL, err := url.Parse(proxyConfig.Address)
if err != nil {
return nil, err
}
transport.Proxy = http.ProxyURL(proxyURL)
if proxyConfig.Username != "" && proxyConfig.Password != "" {
auth := proxyConfig.Username + ":" + proxyConfig.Password
basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
transport.ProxyConnectHeader = http.Header{}
transport.ProxyConnectHeader.Set("Proxy-Authorization", basicAuth)
}
}
}
client := &http.Client{Transport: &transport}
if autoRedirect {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
return client, nil
}
func UseProxy(proxyConfig *ProxyConfig, request *http.Request) error {
client, err := CreateHTTPClient(false, proxyConfig)
if err != nil {
return err
}
_, err = ExecuteRequest(client, request)
return err
}
func ExecuteRequest(client *http.Client, request *http.Request) (*http.Response, error) {
response, err := client.Do(request)
if err != nil {
return nil, err
}
return response, nil
}