forked from j3ssie/metabigor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
153 lines (132 loc) · 3.97 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
package core
import (
"crypto/tls"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
"github.com/go-resty/resty"
"github.com/sirupsen/logrus"
)
var headers map[string]string
// SendGET just send GET request
func SendGET(url string, options Options) string {
headers = map[string]string{
"UserAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
"Accept": "*/*",
"AcceptLang": "en-US,en;q=0.8",
}
resp, _ := JustSend(options, "GET", url, headers, "")
return resp.Body
}
// SendPOST just send POST request
func SendPOST(url string, options Options) string {
resp, _ := JustSend(options, "POST", url, headers, "")
return resp.Body
}
// JustSend just sending request
func JustSend(options Options, method string, url string, headers map[string]string, body string) (res Response, err error) {
timeout := options.Timeout
// disable log when retry
logger := logrus.New()
if !options.Debug {
logger.Out = ioutil.Discard
}
client := resty.New()
client.SetLogger(logger)
client.SetTransport(&http.Transport{
MaxIdleConns: 100,
MaxConnsPerHost: 1000,
IdleConnTimeout: time.Duration(timeout) * time.Second,
ExpectContinueTimeout: time.Duration(timeout) * time.Second,
ResponseHeaderTimeout: time.Duration(timeout) * time.Second,
TLSHandshakeTimeout: time.Duration(timeout) * time.Second,
DisableCompression: true,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
})
client.SetHeaders(headers)
client.SetCloseConnection(true)
client.SetTimeout(time.Duration(timeout) * time.Second)
client.SetRetryWaitTime(time.Duration(timeout/2) * time.Second)
client.SetRetryMaxWaitTime(time.Duration(timeout) * time.Second)
if options.Proxy != "" {
client.SetProxy(options.Proxy)
}
var resp *resty.Response
// really sending things here
method = strings.ToLower(strings.TrimSpace(method))
switch method {
case "get":
resp, err = client.R().
Get(url)
break
case "post":
resp, err = client.R().
SetBody([]byte(body)).
Post(url)
break
}
// in case we want to get redirect stuff
if res.StatusCode != 0 {
return res, nil
}
if err != nil || resp == nil {
ErrorF("%v %v", url, err)
return Response{}, err
}
return ParseResponse(*resp), nil
}
// ParseResponse field to Response
func ParseResponse(resp resty.Response) (res Response) {
// var res libs.Response
resLength := len(string(resp.Body()))
// format the headers
var resHeaders []map[string]string
for k, v := range resp.RawResponse.Header {
element := make(map[string]string)
element[k] = strings.Join(v[:], "")
resLength += len(fmt.Sprintf("%s: %s\n", k, strings.Join(v[:], "")))
resHeaders = append(resHeaders, element)
}
// response time in second
resTime := float64(resp.Time()) / float64(time.Second)
resHeaders = append(resHeaders,
map[string]string{"Total Length": strconv.Itoa(resLength)},
map[string]string{"Response Time": fmt.Sprintf("%f", resTime)},
)
// set some variable
res.Headers = resHeaders
res.StatusCode = resp.StatusCode()
res.Status = fmt.Sprintf("%v %v", resp.Status(), resp.RawResponse.Proto)
res.Body = string(resp.Body())
res.ResponseTime = resTime
res.Length = resLength
// beautify
res.Beautify = BeautifyResponse(res)
res.BeautifyHeader = BeautifyHeaders(res)
return res
}
// BeautifyHeaders beautify response headers
func BeautifyHeaders(res Response) string {
beautifyHeader := fmt.Sprintf("%v \n", res.Status)
for _, header := range res.Headers {
for key, value := range header {
beautifyHeader += fmt.Sprintf("%v: %v\n", key, value)
}
}
return beautifyHeader
}
// BeautifyResponse beautify response
func BeautifyResponse(res Response) string {
var beautifyRes string
beautifyRes += fmt.Sprintf("%v \n", res.Status)
for _, header := range res.Headers {
for key, value := range header {
beautifyRes += fmt.Sprintf("%v: %v\n", key, value)
}
}
beautifyRes += fmt.Sprintf("\n%v\n", res.Body)
return beautifyRes
}