-
Notifications
You must be signed in to change notification settings - Fork 1
/
httptools.go
54 lines (45 loc) · 941 Bytes
/
httptools.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
package main
import (
"crypto/tls"
"net/http"
"net/http/cookiejar"
"net/url"
)
type MyTransport struct {
*http.Transport
Agent string
}
func (mt *MyTransport) RoundTrip(r *http.Request) (*http.Response, error) {
r.Header.Set("User-Agent", mt.Agent)
return mt.Transport.RoundTrip(r)
}
func NewHTTPClient() *http.Client {
transport := &MyTransport{
Transport: http.DefaultTransport.(*http.Transport),
Agent: userAgent,
}
if debug {
proxyURL, err := url.Parse("http://10.10.10.10:8080")
if err != nil {
panic(err)
}
transport.Proxy = http.ProxyURL(proxyURL)
transport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
cookies, err := cookiejar.New(nil)
if err != nil {
panic(err)
}
res := &http.Client{
Transport: transport,
Jar: cookies,
}
return res
}
func URLMustParse(rawurl string) *url.URL {
url, err := url.Parse(rawurl)
if err != nil {
panic(err)
}
return url
}