/
httpx.go
354 lines (305 loc) · 9.66 KB
/
httpx.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package httpx
import (
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"unicode/utf8"
"github.com/corpix/uarand"
"github.com/microcosm-cc/bluemonday"
"github.com/projectdiscovery/cdncheck"
"github.com/projectdiscovery/fastdialer/fastdialer"
pdhttputil "github.com/projectdiscovery/httputil"
"github.com/projectdiscovery/rawhttp"
"github.com/projectdiscovery/retryablehttp-go"
"github.com/projectdiscovery/stringsutil"
"golang.org/x/net/context"
"golang.org/x/net/http2"
)
// HTTPX represent an instance of the library client
type HTTPX struct {
client *retryablehttp.Client
client2 *http.Client
Filters []Filter
Options *Options
htmlPolicy *bluemonday.Policy
CustomHeaders map[string]string
cdn *cdncheck.Client
Dialer *fastdialer.Dialer
}
// New httpx instance
func New(options *Options) (*HTTPX, error) {
httpx := &HTTPX{}
fastdialerOpts := fastdialer.DefaultOptions
fastdialerOpts.EnableFallback = true
fastdialerOpts.Deny = options.Deny
fastdialerOpts.Allow = options.Allow
fastdialerOpts.WithDialerHistory = true
if len(options.Resolvers) > 0 {
fastdialerOpts.BaseResolvers = options.Resolvers
}
dialer, err := fastdialer.NewDialer(fastdialerOpts)
if err != nil {
return nil, fmt.Errorf("could not create resolver cache: %s", err)
}
httpx.Dialer = dialer
httpx.Options = options
httpx.Options.parseCustomCookies()
var retryablehttpOptions = retryablehttp.DefaultOptionsSpraying
retryablehttpOptions.Timeout = httpx.Options.Timeout
retryablehttpOptions.RetryMax = httpx.Options.RetryMax
var redirectFunc = func(_ *http.Request, _ []*http.Request) error {
// Tell the http client to not follow redirect
return http.ErrUseLastResponse
}
if httpx.Options.FollowRedirects {
// Follow redirects up to a maximum number
redirectFunc = func(redirectedRequest *http.Request, previousRequests []*http.Request) error {
// add custom cookies if necessary
httpx.setCustomCookies(redirectedRequest)
if len(previousRequests) >= options.MaxRedirects {
// https://github.com/golang/go/issues/10069
return http.ErrUseLastResponse
}
return nil
}
}
if httpx.Options.FollowHostRedirects {
// Only follow redirects on the same host up to a maximum number
redirectFunc = func(redirectedRequest *http.Request, previousRequests []*http.Request) error {
// add custom cookies if necessary
httpx.setCustomCookies(redirectedRequest)
// Check if we get a redirect to a different host
var newHost = redirectedRequest.URL.Host
var oldHost = previousRequests[0].Host
if oldHost == "" {
oldHost = previousRequests[0].URL.Host
}
if newHost != oldHost {
// Tell the http client to not follow redirect
return http.ErrUseLastResponse
}
if len(previousRequests) >= options.MaxRedirects {
// https://github.com/golang/go/issues/10069
return http.ErrUseLastResponse
}
return nil
}
}
transport := &http.Transport{
DialContext: httpx.Dialer.Dial,
MaxIdleConnsPerHost: -1,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
DisableKeepAlives: true,
}
if httpx.Options.HTTPProxy != "" {
proxyURL, parseErr := url.Parse(httpx.Options.HTTPProxy)
if parseErr != nil {
return nil, parseErr
}
transport.Proxy = http.ProxyURL(proxyURL)
}
httpx.client = retryablehttp.NewWithHTTPClient(&http.Client{
Transport: transport,
Timeout: httpx.Options.Timeout,
CheckRedirect: redirectFunc,
}, retryablehttpOptions)
httpx.client2 = &http.Client{
Transport: &http2.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
AllowHTTP: true,
},
Timeout: httpx.Options.Timeout,
}
httpx.htmlPolicy = bluemonday.NewPolicy()
httpx.CustomHeaders = httpx.Options.CustomHeaders
if options.CdnCheck || options.ExcludeCdn {
httpx.cdn, err = cdncheck.NewWithCache()
if err != nil {
return nil, fmt.Errorf("could not create cdn check: %s", err)
}
}
return httpx, nil
}
// Do http request
func (h *HTTPX) Do(req *retryablehttp.Request, unsafeOptions UnsafeOptions) (*Response, error) {
timeStart := time.Now()
var gzipRetry bool
get_response:
httpresp, err := h.getResponse(req, unsafeOptions)
if err != nil {
return nil, err
}
var shouldIgnoreErrors, shouldIgnoreBodyErrors bool
switch {
case h.Options.Unsafe && req.Method == http.MethodHead && !stringsutil.ContainsAny("i/o timeout"):
shouldIgnoreErrors = true
shouldIgnoreBodyErrors = true
}
var resp Response
resp.Headers = httpresp.Header.Clone()
// httputil.DumpResponse does not handle websockets
headers, rawResp, err := pdhttputil.DumpResponseHeadersAndRaw(httpresp)
if err != nil {
// Edge case - some servers respond with gzip encoding header but uncompressed body, in this case the standard library configures the reader as gzip, triggering an error when read.
// The bytes slice is not accessible because of abstraction, therefore we need to perform the request again tampering the Accept-Encoding header
if !gzipRetry && strings.Contains(err.Error(), "gzip: invalid header") {
gzipRetry = true
req.Header.Set("Accept-Encoding", "identity")
goto get_response
}
if !shouldIgnoreErrors {
return nil, err
}
}
resp.Raw = string(rawResp)
resp.RawHeaders = string(headers)
var respbody []byte
// websockets don't have a readable body
if httpresp.StatusCode != http.StatusSwitchingProtocols {
var err error
respbody, err = ioutil.ReadAll(io.LimitReader(httpresp.Body, h.Options.MaxResponseBodySizeToRead))
if err != nil && !shouldIgnoreBodyErrors {
return nil, err
}
}
closeErr := httpresp.Body.Close()
if closeErr != nil && !shouldIgnoreBodyErrors {
return nil, closeErr
}
respbodystr := string(respbody)
// check if we need to strip html
if h.Options.VHostStripHTML {
respbodystr = h.htmlPolicy.Sanitize(respbodystr)
}
// if content length is not defined
if resp.ContentLength <= 0 {
// check if it's in the header and convert to int
if contentLength, ok := resp.Headers["Content-Length"]; ok {
contentLengthInt, _ := strconv.Atoi(strings.Join(contentLength, ""))
resp.ContentLength = contentLengthInt
}
// if we have a body, then use the number of bytes in the body if the length is still zero
if resp.ContentLength <= 0 && len(respbodystr) > 0 {
resp.ContentLength = utf8.RuneCountInString(respbodystr)
}
}
resp.Data = respbody
// fill metrics
resp.StatusCode = httpresp.StatusCode
// number of words
resp.Words = len(strings.Split(respbodystr, " "))
// number of lines
resp.Lines = len(strings.Split(respbodystr, "\n"))
if !h.Options.Unsafe && h.Options.TLSGrab {
// extracts TLS data if any
resp.TLSData = h.TLSGrab(httpresp)
}
resp.CSPData = h.CSPGrab(&resp)
// build the redirect flow by reverse cycling the response<-request chain
if !h.Options.Unsafe {
chain, err := pdhttputil.GetChain(httpresp)
if err != nil {
return nil, err
}
resp.Chain = chain
}
resp.Duration = time.Since(timeStart)
return &resp, nil
}
// RequestOverride contains the URI path to override the request
type UnsafeOptions struct {
URIPath string
}
// getResponse returns response from safe / unsafe request
func (h *HTTPX) getResponse(req *retryablehttp.Request, unsafeOptions UnsafeOptions) (*http.Response, error) {
if h.Options.Unsafe {
return h.doUnsafeWithOptions(req, unsafeOptions)
}
return h.client.Do(req)
}
// doUnsafe does an unsafe http request
func (h *HTTPX) doUnsafeWithOptions(req *retryablehttp.Request, unsafeOptions UnsafeOptions) (*http.Response, error) {
method := req.Method
headers := req.Header
targetURL := req.URL.String()
body := req.Body
options := rawhttp.DefaultOptions
options.Timeout = h.Options.Timeout
return rawhttp.DoRawWithOptions(method, targetURL, unsafeOptions.URIPath, headers, body, options)
}
// Verify the http calls and apply-cascade all the filters, as soon as one matches it returns true
func (h *HTTPX) Verify(req *retryablehttp.Request, unsafeOptions UnsafeOptions) (bool, error) {
resp, err := h.Do(req, unsafeOptions)
if err != nil {
return false, err
}
// apply all filters
for _, f := range h.Filters {
ok, err := f.Filter(resp)
if err != nil {
return false, err
}
if ok {
return true, nil
}
}
return false, nil
}
// AddFilter cascade
func (h *HTTPX) AddFilter(f Filter) {
h.Filters = append(h.Filters, f)
}
// NewRequest from url
func (h *HTTPX) NewRequest(method, targetURL string) (req *retryablehttp.Request, err error) {
return h.NewRequestWithContext(context.Background(), method, targetURL)
}
// NewRequest from url
func (h *HTTPX) NewRequestWithContext(ctx context.Context, method, targetURL string) (req *retryablehttp.Request, err error) {
req, err = retryablehttp.NewRequestWithContext(ctx, method, targetURL, nil)
if err != nil {
return
}
// Skip if unsafe is used
if !h.Options.Unsafe {
// set default user agent
req.Header.Set("User-Agent", h.Options.DefaultUserAgent)
// set default encoding to accept utf8
req.Header.Add("Accept-Charset", "utf-8")
req.Header.Add("Cookie", "rememberMe=1")
}
return
}
// SetCustomHeaders on the provided request
func (h *HTTPX) SetCustomHeaders(r *retryablehttp.Request, headers map[string]string) {
for name, value := range headers {
switch strings.ToLower(name) {
case "host":
r.Host = value
case "cookie":
// cookies are set in the default branch, and reset during the follow redirect flow
fallthrough
default:
r.Header.Set(name, value)
}
}
if h.Options.RandomAgent {
r.Header.Set("User-Agent", uarand.GetRandom()) //nolint
}
}
func (httpx *HTTPX) setCustomCookies(req *http.Request) {
if httpx.Options.hasCustomCookies() {
for _, cookie := range httpx.Options.customCookies {
req.AddCookie(cookie)
}
}
}