-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.go
187 lines (148 loc) · 3.68 KB
/
bot.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
package djangobot
import (
"crypto/tls"
"fmt"
"net/http"
"strings"
"github.com/parnurzeal/gorequest"
"golang.org/x/crypto/acme/autocert"
)
type Bot struct {
RequestUrl string
QueryParams map[string]string
FormData map[string]string
Username string
Password string
Client *gorequest.SuperAgent
Cookies map[string]*http.Cookie
hosts []string
Error error
}
func With(requestUrl string) *Bot {
return &Bot{
QueryParams: make(map[string]string),
FormData: make(map[string]string),
RequestUrl: requestUrl,
}
}
func (c *Bot) Set(key string, val string) *Bot {
c.QueryParams[key] = val
return c
}
func (c *Bot) X(key string, val string) *Bot {
c.FormData[key] = val
return c
}
func (c *Bot) ForHost(host string) *Bot {
c.hosts = append(c.hosts, host)
return c
}
func (c *Bot) SetUsername(username string) *Bot {
c.Username = username
return c
}
func (c *Bot) SetPassword(password string) *Bot {
c.Password = password
return c
}
func (c *Bot) Referrer() (string, string) {
return "Referer", c.RequestUrl
}
func (c *Bot) UserAgent() (string, string) {
return "User-Agent", `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36`
}
func (c *Bot) cookieKey(cookie *http.Cookie) string {
if cookie == nil {
return "nookie"
}
return strings.Split(cookie.Raw, "=")[0]
}
func (c *Bot) Cookie(key string) *http.Cookie {
cookie, ok := c.Cookies[key]
if !ok {
return &http.Cookie{}
}
return cookie
}
func (c *Bot) appendCookies(resp *http.Response) {
if resp != nil {
if c.Cookies == nil {
c.Cookies = make(map[string]*http.Cookie)
}
for _, cookie := range resp.Cookies() {
// Get key
c.Cookies[c.cookieKey(cookie)] = cookie
// Append cookies to the client ( Should've been appended after the requests automatically, but does not )
c.Client = c.Client.AddCookie(cookie)
}
}
}
func (c *Bot) LoadCookies() *Bot {
var body []byte
var errs []error
var resp *http.Response
var tlsConfig *tls.Config
c.Client = gorequest.New().SetCurlCommand(false).SetDebug(false)
// Get certs for SSL connection
m := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(c.hosts...),
}
tlsConfig = &tls.Config{GetCertificate: m.GetCertificate}
c.Client = c.Client.TLSClientConfig(tlsConfig).Get(c.RequestUrl).Set(c.UserAgent())
resp, body, errs = c.Client.EndBytes()
if errs != nil {
c.Error = errs[0]
}
// Add cookies from the response to the client
c.appendCookies(resp)
_ = resp
_ = body
_ = errs
return c
}
func (c *Bot) Login() (*gorequest.SuperAgent, error) {
var body []byte
var errs []error
var resp *http.Response
var rPolicy = func(req gorequest.Request, via []gorequest.Request) error {
return http.ErrUseLastResponse
}
// Set query parameters
url := c.RequestUrl
if len(c.QueryParams) > 0 {
url += "?" + formEncode(c.QueryParams)
}
if len(c.FormData) > 0 {
c.Client = c.Client.
Post(url).
Set(c.UserAgent()).
Set(c.Referrer()).
Send(formEncode(c.FormData)).
RedirectPolicy(rPolicy)
}
resp, body, errs = c.Client.EndBytes()
if errs != nil {
return nil, errs[0]
}
c.appendCookies(resp)
_ = resp
_ = body
_ = errs
return c.Client, nil
}
func formEncode(data map[string]string) string {
values := make([]string, 0)
for key, value := range data {
values = append(values, fmt.Sprintf(`%s=%s`, key, value))
}
return strings.Join(values, "&")
}
func (c *Bot) Requester(method string, endpoint string) *Bot {
c.Client = c.Client.CustomMethod(method, endpoint).
Set(c.UserAgent()).
Set(c.Referrer()).
Set("X-CSRFToken", c.Cookie("csrftoken").Value).
Set("X-Requested-With", "XMLHttpRequest")
return c
}