-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTlsChangeHttpClient.go
More file actions
173 lines (156 loc) · 3.9 KB
/
Copy pathTlsChangeHttpClient.go
File metadata and controls
173 lines (156 loc) · 3.9 KB
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
package gohttp
import (
"bytes"
"compress/gzip"
"compress/zlib"
"fmt"
"github.com/Carcraftz/cclient"
"github.com/Carcraftz/fhttp"
"github.com/Carcraftz/fhttp/cookiejar"
tls "github.com/Carcraftz/utls"
"github.com/andybalholm/brotli"
"io/ioutil"
"net/url"
"log"
)
type GohttpClient interface {
DoRequest(b *BasicRequest) (response *Response, err error)
AddInterceptor(f func(r *BasicRequest))
SetCookies(u *url.URL, cookies []*http.Cookie)
GetCookie(url *url.URL, name string) *http.Cookie
GetCookies(url *url.URL, name string) []*http.Cookie
}
type TLsChangeHttpClient struct {
client http.Client
interceptors []interceptor
}
func (this *TLsChangeHttpClient) GetCookies(url *url.URL, name string) []*http.Cookie {
cks := this.client.Jar.Cookies(url)
return cks
}
func (this *TLsChangeHttpClient) GetCookie(url *url.URL, name string) *http.Cookie {
cks := this.client.Jar.Cookies(url)
for _, ck := range cks {
if ck.Name == name {
return ck
}
}
return nil
}
func (this *TLsChangeHttpClient) AddInterceptor(f func(r *BasicRequest)) {
this.interceptors = append(this.interceptors, f)
}
func NewTlsHttpClient(proxyAddr, customId string) *TLsChangeHttpClient {
client, err := cclient.NewClient(tls.HelloIOS_Auto, proxyAddr, true, 1000)
if err != nil {
log.Println(err)
return nil
}
jar, err := cookiejar.New(nil)
if err != nil {
log.Println(err)
return nil
}
client.Jar = jar
tch := &TLsChangeHttpClient{
client: client,
}
return tch
}
func (hc *TLsChangeHttpClient) SetCookies(u *url.URL, cookies []*http.Cookie) {
hc.client.Jar.SetCookies(u, cookies)
}
func (hc *TLsChangeHttpClient) DoRequest(b *BasicRequest) (response *Response, err error) {
if !b.AllowRedirect {
hc.client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
} else {
hc.client.CheckRedirect = nil
}
req, err := http.NewRequest(b.Method, b.UrlString(), bytes.NewReader(b.BodyToBinary()))
if err != nil {
return
}
for _, callback := range hc.interceptors {
callback(b)
}
req.Header = http.Header{
http.PHeaderOrderKey: {":method", ":authority", ":scheme", ":path"},
}
b.ForEachHeaders(func(k, v string) {
req.Header[k] = append(req.Header[k], v)
})
uri, _ := url.Parse(b.UrlString())
for _, cookie := range hc.client.Jar.Cookies(uri) {
b.AddCookie(cookie.Name, cookie.Value)
}
resp, err := hc.client.Do(req)
defer func() {
if resp != nil {
resp.Body.Close()
}
}()
if err != nil {
log.Println(err)
return nil, err
}
encoding := resp.Header["Content-Encoding"]
body, err := ioutil.ReadAll(resp.Body)
finalres := ""
if err != nil {
return
}
if len(encoding) > 0 {
if encoding[0] == "gzip" {
unz, err := gUnzipData(body)
if err != nil {
return nil, err
}
finalres = string(unz)
} else if encoding[0] == "deflate" {
unz, err := enflateData(body)
if err != nil {
return nil, err
}
finalres = string(unz)
} else if encoding[0] == "br" {
unz, err := unBrotliData(body)
if err != nil {
return nil, err
}
finalres = string(unz)
} else {
fmt.Println("UNKNOWN ENCODING: " + encoding[0])
finalres = string(body)
}
} else {
finalres = string(body)
}
response = &Response{
Request: b,
Url: b.UrlString(),
Context: string(finalres),
StatusCode: resp.StatusCode,
Headers: resp.Header,
Proto: resp.Proto,
}
return
}
func gUnzipData(data []byte) (resData []byte, err error) {
gz, _ := gzip.NewReader(bytes.NewReader(data))
defer gz.Close()
respBody, err := ioutil.ReadAll(gz)
return respBody, err
}
func enflateData(data []byte) (resData []byte, err error) {
zr, _ := zlib.NewReader(bytes.NewReader(data))
defer zr.Close()
enflated, err := ioutil.ReadAll(zr)
return enflated, err
}
func unBrotliData(data []byte) (resData []byte, err error) {
br := brotli.NewReader(bytes.NewReader(data))
respBody, err := ioutil.ReadAll(br)
return respBody, err
}