-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
74 lines (66 loc) · 1.71 KB
/
client.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
package main
import (
"crypto/tls"
"golang.org/x/net/http2"
"net/http"
"net/url"
"strings"
"time"
)
var (
DefaultClient *http.Client
LoginClient *http.Client
tlsConfig = &tls.Config{InsecureSkipVerify: true}
)
func InitClient() {
DefaultClient = &http.Client{
Transport: Transport(),
Timeout: time.Second * 60,
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
},
}
LoginClient = &http.Client{
Transport: Transport(),
Timeout: time.Second * 60,
}
}
func Transport() http.RoundTripper {
var proxyUrl *url.URL
var err error
if proxy.Proxy != "" {
proxyUrl, err = url.Parse(DefaultProxy.Proxy)
if err != nil {
panic(err)
}
}
if proxy.Http2 {
return &http2.Transport{
TLSClientConfig: tlsConfig,
DialTLS: tlsDialOptWithCfg,
}
} else {
return &http.Transport{
TLSClientConfig: tlsConfig,
DialTLS: tlsDialOptWithoutCfg,
Proxy: http.ProxyURL(proxyUrl),
}
}
}
func (p *Proxy) SimpleFetchClient(method, path string, header url.Values, client *http.Client) (*http.Response, error) {
req, err := http.NewRequest(method, "https://vpns.jlu.edu.cn"+path, strings.NewReader(header.Encode()))
if err != nil {
return nil, err
}
if header != nil {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
req.Header.Set("Cookie", p.Cookies)
return client.Do(req)
}
func (p *Proxy) SimpleFetch(method, path string, header url.Values) (*http.Response, error) {
return p.SimpleFetchClient(method, path, header, DefaultClient)
}
func (p *Proxy) SimpleFetchLogin(method, path string, header url.Values) (*http.Response, error) {
return p.SimpleFetchClient(method, path, header, LoginClient)
}