-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsessions.go
264 lines (230 loc) · 6.2 KB
/
sessions.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
package requests
import (
"context"
"crypto/tls"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"github.com/cvelab/requests/ext"
"github.com/cvelab/requests/internal/processbar"
"github.com/cvelab/requests/types"
"golang.org/x/net/proxy"
"log"
"net/http"
nUrl "net/url"
"time"
)
type Session struct {
types.ExtensionPackage
Client *http.Client
adapter *adapter
cacheRequest *prepareRequest
}
func HTMLSession() *Session {
s := &Session{
adapter: &adapter{context: context.Background()},
Client: &http.Client{
Transport: new(http.Transport),
},
}
s.Timeout = 5
s.Redirect = true
s.Verify = true
return s
}
func (s *Session) init(method string, url string, exts *[]types.Ext) *Session {
s.Method = method
s.Url = url
for _, fn := range *exts {
fn(&s.ExtensionPackage)
}
return s
}
func (s *Session) request() *Response {
var err error
err, s.cacheRequest = s.prepareRequest()
if err != nil {
log.Printf("%+v\n", errors.WithStack(err))
return nil
}
if err = s.prepareClient(); err != nil {
log.Printf("%+v\n", errors.WithStack(err))
return nil
}
s.prepareProcessOptions()
s.prepareContext()
s.adapter.tracerEnable = s.Tracer
return s.Send(s.cacheRequest)
}
func (s *Session) prepareRequest() (error, *prepareRequest) {
err, prep := PrepareRequest(s.Protocol, s.Method, s.Url, s.Params, s.Headers, s.Cookies, s.Form, s.Json, s.Files, s.Stream, s.Auth)
if err != nil {
return err, nil
}
return nil, prep
}
func (s *Session) prepareClient() error {
_ = s.prepareTimeout()
_ = s.prepareCipherSuites()
if err := s.prepareProxy(); err != nil {
return err
}
_ = s.prepareRedirect()
_ = s.prepareVerify()
return nil
}
func (s *Session) Send(prep *prepareRequest) *Response {
// 计时开机
// 后续根据协议,切换 adapter(实际go对应client配置)
resp := s.adapter.send(s.Client, prep, &s.Hooks)
history := make([]*Response, 0)
if resp != nil {
if s.Redirect {
s.cacheRequest = prep
history = s.resolveRedirects(resp)
}
if len(history) > 0 {
history = append([]*Response{resp}, history...)
resp = history[len(history)-1]
resp.History = history[:len(history)-1]
}
}
if resp == nil {
return nil
}
resp.Session = s
return resp
}
func (s *Session) RegisterHook(key string, hook types.Hook) error {
if s.Hooks == nil {
s.Hooks = ext.DefaultHooks()
}
return ext.RegisterHook(&s.Hooks, key, hook)
}
func (s *Session) resolveRedirects(resp *Response) []*Response {
var err error
history := make([]*Response, 0)
url := resp.Header.Get("Location")
for url != "" {
if url[0:2] == "//" {
url = resp.Request.URL.Scheme + ":" + url
} else if u := string(url[0]); u == "/" || u == "." {
uTmp := resp.Request.URL
uTmp.Path = url
url = uTmp.String()
}
redirectPrep := &prepareRequest{}
_ = copier.CopyWithOption(&redirectPrep, &s.cacheRequest, copier.Option{IgnoreEmpty: true, DeepCopy: true})
if err = redirectPrep.prepareUrl(url, nil); err != nil {
log.Printf("%+v\n", errors.WithStack(err))
return history
}
redirectPrep.cookies = resp.Cookies()
s.Redirect = false
resp = s.Send(redirectPrep)
if resp == nil {
break
}
url = resp.Header.Get("Location")
history = append(history, resp)
}
return history
}
func (s *Session) prepareRedirect() error {
s.Client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
return nil
}
// net/http/Transport 支持 http/https/socks5,如果使用其他协议则会转为 http
// proxy支持socks/socks5h,为了简便只使用其处理socks5h
func (s *Session) prepareProxy() error {
if s.Proxy == "" {
return nil
}
_proxy, err := nUrl.Parse(s.Proxy)
if err != nil {
return err
}
switch _proxy.Scheme {
case "http", "https", "socks5":
s.Client.Transport.(*http.Transport).Proxy = http.ProxyURL(_proxy)
case "socks5h":
dialer, err := proxy.FromURL(_proxy, nil)
if err != nil {
return err
}
s.Client.Transport.(*http.Transport).DialContext = dialer.(proxy.ContextDialer).DialContext
}
// 设置 ProxyConnectHeader
if ua := s.cacheRequest.headers.Get("User-Agent"); ua != "" {
header := &http.Header{}
header.Add("User-Agent", ua)
s.Client.Transport.(*http.Transport).ProxyConnectHeader = *header
}
return nil
}
func (s *Session) prepareTimeout() error {
if s.Timeout == 0 {
s.Timeout = DefaultTimeout
}
s.Client.Timeout = time.Duration(s.Timeout) * time.Second
return nil
}
func (s *Session) prepareVerify() error {
if s.Verify == false {
if s.Client.Transport != nil {
s.Client.Transport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
} else {
s.Client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
}
}
return nil
}
func (s *Session) prepareProcessOptions() {
if s.ProcessOptions == nil {
s.ProcessOptions = make([]processbar.Option, 0)
}
s.ProcessOptions = append(s.ProcessOptions, processbar.OptionsUrl(s.Url))
}
func (s *Session) prepareContext() {
s.adapter.context = context.WithValue(s.adapter.context, "processOptions", s.ProcessOptions)
}
func (s *Session) prepareHooks(hooks types.HooksDict) {
if s.Hooks == nil {
s.Hooks = ext.DefaultHooks()
}
for event, _hooks := range hooks {
if s.Hooks[event] != nil {
s.Hooks[event] = append(s.Hooks[event], _hooks...)
} else {
s.Hooks[event] = _hooks
}
}
}
// crypto/tls 已做nil检查,直接传入即可
func (s *Session) prepareCipherSuites() error {
s.Client.Transport.(*http.Transport).TLSClientConfig = &tls.Config{
CipherSuites: s.CipherSuites,
}
return nil
}
func (s *Session) Get(url string, ext ...types.Ext) *Response {
return s.init("Get", url, &ext).request()
}
func (s *Session) Post(url string, ext ...types.Ext) *Response {
return s.init("Post", url, &ext).request()
}
func (s *Session) Put(url string, ext ...types.Ext) *Response {
return s.init("Put", url, &ext).request()
}
func (s *Session) Delete(url string, ext ...types.Ext) *Response {
return s.init("Delete", url, &ext).request()
}
func (s *Session) Head(url string, ext ...types.Ext) *Response {
return s.init("Head", url, &ext).request()
}
func (s *Session) Options(url string, ext ...types.Ext) *Response {
return s.init("Option", url, &ext).request()
}