/
request.go
205 lines (194 loc) · 4.29 KB
/
request.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
package request
import (
"compress/gzip"
"crypto/tls"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"time"
"github.com/shadowsocks/shadowsocks-go/shadowsocks"
"github.com/tongsq/go-lib/ecode"
"github.com/tongsq/go-lib/util"
"h12.io/socks"
)
var DefaultTimeout = time.Second * 5
func SetTimeout(t time.Duration) {
DefaultTimeout = t
}
/**
format query params
*/
func GetReqData(d map[string]string) string {
s := ""
for k, v := range d {
s = s + fmt.Sprintf("%s=%s&", k, url.QueryEscape(v))
}
return s
}
/**
http request
*/
func request(req *http.Request, proxy *ProxyDto, timeout time.Duration) (*HttpResultDto, error) {
var client *http.Client
var tran *http.Transport
if proxy != nil {
proxyServer := getProxyUrl(proxy)
switch proxy.Proto {
case PROTO_SS:
cipher, err := shadowsocks.NewCipher(proxy.User, proxy.Password)
if err != nil {
return nil, err
}
tran = &http.Transport{
Dial: func(_, addr string) (net.Conn, error) {
return shadowsocks.Dial(addr, fmt.Sprintf("%s:%s", proxy.Host, proxy.Port), cipher.Copy())
},
}
case PROTO_SOCKS4, PROTO_SOCKS4A:
dial := socks.Dial(proxyServer)
tran = &http.Transport{
Dial: dial,
}
default:
proxyUrl, err := url.Parse(proxyServer)
if err != nil {
return nil, err
}
tran = &http.Transport{
Proxy: http.ProxyURL(proxyUrl),
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
}
if tran != nil {
client = &http.Client{
Transport: tran,
Timeout: timeout,
}
} else {
client = &http.Client{
Timeout: timeout,
}
}
result := NewHttpResultDto()
resp, err := client.Do(req)
if err != nil {
return result, err
}
saveResponse(result, resp)
defer resp.Body.Close()
data := resp.Body
if resp.Header.Get("Content-Encoding") == "gzip" {
data, err = gzip.NewReader(resp.Body)
if err != nil {
return result, ecode.HTTP_GZIP_DECODE_ERROR
}
defer data.Close()
}
body, err := ioutil.ReadAll(data)
if err != nil {
return result, ecode.HTTP_READ_ERROR
}
types, ok := result.Header["Content-Type"]
bodyStr := string(body)
if ok {
for _, t := range types {
if util.Contains(t, []string{"GBK", "gbk", "gb2312", "GB2312"}) {
b, err := util.GbkToUtf8(bodyStr)
if err == nil {
bodyStr = b
}
break
}
}
}
result.Body = bodyStr
if resp.StatusCode != HTTP_CODE_OK {
return result, ecode.HTTP_CODE_ERROR
}
return result, nil
}
/**
add request header
*/
func addHeader(req *http.Request, h *HeaderDto) *http.Request {
if h == nil {
return req
}
if h.Host != "" {
req.Header.Set("Host", h.Host)
req.Host = h.Host
}
if h.Accept != "" {
req.Header.Set("Accept", h.Accept)
}
if h.AcceptEncoding != "" {
req.Header.Set("Accept-Encoding", h.AcceptEncoding)
}
if h.Referer != "" {
req.Header.Set("Referer", h.Referer)
}
if h.UpgradeInsecureRequests != "" {
req.Header.Set("Upgrade-Insecure-Requests", h.UpgradeInsecureRequests)
}
if h.UserAgent != "" {
req.Header.Set("User-Agent", h.UserAgent)
}
if h.AcceptLanguage != "" {
req.Header.Set("Accept-Language", h.AcceptLanguage)
}
if h.SecFetchDest != "" {
req.Header.Set("Sec-Fetch-Dest", h.SecFetchDest)
}
if h.SecFetchMode != "" {
req.Header.Set("Sec-Fetch-Mode", h.SecFetchMode)
}
if h.XRequestedWith != "" {
req.Header.Set("X-Requested-With", h.XRequestedWith)
}
if h.ContentType != "" {
req.Header.Set("Content-Type", h.ContentType)
} else {
req.Header.Set("Content-Type", CONTENT_TYPE_FORM)
}
if h.Other != nil {
for k, v := range h.Other {
req.Header.Set(k, v)
}
}
return req
}
/**
add request cookie
*/
func addCookie(req *http.Request, c map[string]string) *http.Request {
for k, v := range c {
cookie := &http.Cookie{Name: k, Value: v, HttpOnly: true}
req.AddCookie(cookie)
}
return req
}
/**
add http response info to dto
*/
func saveResponse(data *HttpResultDto, resp *http.Response) {
data.HttpCode = resp.StatusCode
data.ContentLength = resp.ContentLength
data.Header = resp.Header
for _, c := range resp.Cookies() {
data.Cookies[c.Name] = c.Value
}
}
func getProxyUrl(p *ProxyDto) string {
proto := p.Proto
if proto == "" {
proto = PROTO_HTTP
}
if p.User == "" {
return fmt.Sprintf("%s://%s:%s", proto, p.Host, p.Port)
} else {
return fmt.Sprintf("%s://%s:%s@%s:%s", proto, p.User, p.Password, p.Host, p.Port)
}
}