-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathmediaunlock.go
239 lines (218 loc) · 7.25 KB
/
mediaunlock.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
package mediaunlocktest
import (
"crypto/tls"
"errors"
"net"
"net/http"
"strings"
"syscall"
"time"
)
var (
Version = "1.0"
StatusOK = 1
StatusNetworkErr = -1
StatusErr = -2
StatusRestricted = 2
StatusNo = 3
StatusBanned = 4
StatusFailed = 5
StatusUnexpected = 6
)
type Result struct {
Status int
Region string
Info string
Err error
}
var (
UA_Browser = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
UA_Dalvik = "Dalvik/2.1.0 (Linux; U; Android 9; ALP-AL00 Build/HUAWEIALP-AL00)"
)
var Dialer = &net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
// Resolver: &net.Resolver{},
}
var ClientProxy = http.ProxyFromEnvironment
var Ipv4Transport = &http.Transport{
Proxy: ClientProxy,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second, KeepAlive: 30 * time.Second,
Control: func(network, address string, c syscall.RawConn) error {
if network == "ipv4" {
return errors.New("force ipv6")
}
return nil
}}).DialContext,
// ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 30 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: tlsConfig,
}
func UseLastResponse(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }
var defaultCipherSuites = []uint16{0xc02f, 0xc030, 0xc02b, 0xc02c, 0xcca8, 0xcca9, 0xc013, 0xc009, 0xc014, 0xc00a, 0x009c, 0x009d, 0x002f, 0x0035, 0xc012, 0x000a}
var Ipv4HttpClient = http.Client{
Timeout: 30 * time.Second,
CheckRedirect: UseLastResponse,
Transport: Ipv4Transport,
}
var Ipv6Transport = &http.Transport{
Proxy: ClientProxy,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second, KeepAlive: 30 * time.Second,
Control: func(network, address string, c syscall.RawConn) error {
if network == "ipv6" {
return errors.New("force ipv4")
}
return nil
}}).DialContext,
// ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 30 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: tlsConfig,
}
var Ipv6HttpClient = http.Client{
Timeout: 30 * time.Second,
CheckRedirect: UseLastResponse,
Transport: Ipv6Transport,
}
var AutoHttpClient = NewAutoHttpClient()
func AutoTransport() *http.Transport {
return &http.Transport{
Proxy: ClientProxy,
DialContext: (&net.Dialer{Timeout: 30 * time.Second, KeepAlive: 30 * time.Second}).DialContext,
// ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 30 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: tlsConfig,
}
}
func NewAutoHttpClient() http.Client {
return http.Client{
Timeout: 30 * time.Second,
CheckRedirect: UseLastResponse,
Transport: AutoTransport(),
}
}
var tlsConfig = &tls.Config{
CipherSuites: append(defaultCipherSuites[8:], defaultCipherSuites[:8]...),
}
type H [2]string
func GET(c http.Client, url string, headers ...H) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("user-agent", UA_Browser)
req.Header.Set("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
// req.Header.Set("accept-encoding", "gzip, deflate, br")
// req.Header.Set("accept-language", "zh-CN,zh;q=0.9")
req.Header.Set("cache-control", "no-cache")
req.Header.Set("dnt", "1")
req.Header.Set("pragma", "no-cache")
req.Header.Set("sec-ch-ua", `"Chromium";v="106", "Google Chrome";v="106", "Not;A=Brand";v="99"`)
req.Header.Set("sec-ch-ua-mobile", "?0")
req.Header.Set("sec-ch-ua-platform", "Windows")
req.Header.Set("sec-fetch-dest", "document")
req.Header.Set("sec-fetch-mode", "navigate")
req.Header.Set("sec-fetch-site", "none")
req.Header.Set("sec-fetch-user", "?1")
req.Header.Set("upgrade-insecure-requests", "1")
for _, h := range headers {
req.Header.Set(h[0], h[1])
}
// return c.Do(req)
return cdo(c, req)
}
func GET_Dalvik(c http.Client, url string) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add("User-Agent", UA_Dalvik)
return cdo(c, req)
}
var ErrNetwork = errors.New("network error")
func cdo(c http.Client, req *http.Request) (resp *http.Response, err error) {
// resp, err = c.Do(req)
// if err != nil {
// err = ErrNetwork
// }
// return
deadline := time.Now().Add(30 * time.Second)
for i := 0; i < 3; i++ {
if time.Now().After(deadline) {
break
}
if resp, err = c.Do(req); err == nil {
return resp, nil
}
if strings.Contains(err.Error(), "no such host") {
break
}
if strings.Contains(err.Error(), "timeout") {
break
}
}
// log.Println(err)
return nil, err
}
func PostJson(c http.Client, url string, data string, headers ...H) (*http.Response, error) {
req, err := http.NewRequest("POST", url, strings.NewReader(data))
if err != nil {
return nil, err
}
req.Header.Set("content-type", "application/json")
req.Header.Set("user-agent", UA_Browser)
req.Header.Set("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
// req.Header.Set("accept-encoding", "gzip, deflate, br")
// req.Header.Set("accept-language", "zh-CN,zh;q=0.9")
req.Header.Set("cache-control", "no-cache")
req.Header.Set("dnt", "1")
req.Header.Set("pragma", "no-cache")
req.Header.Set("sec-ch-ua", `"Chromium";v="106", "Google Chrome";v="106", "Not;A=Brand";v="99"`)
req.Header.Set("sec-ch-ua-mobile", "?0")
req.Header.Set("sec-ch-ua-platform", "Windows")
req.Header.Set("sec-fetch-dest", "document")
req.Header.Set("sec-fetch-mode", "navigate")
req.Header.Set("sec-fetch-site", "none")
req.Header.Set("sec-fetch-user", "?1")
req.Header.Set("upgrade-insecure-requests", "1")
for _, h := range headers {
req.Header.Set(h[0], h[1])
}
return cdo(c, req)
}
func PostForm(c http.Client, url string, data string, headers ...H) (*http.Response, error) {
req, err := http.NewRequest("POST", url, strings.NewReader(data))
if err != nil {
return nil, err
}
req.Header.Set("content-type", "application/x-www-form-urlencoded")
req.Header.Set("user-agent", UA_Browser)
req.Header.Set("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
// req.Header.Set("accept-encoding", "gzip, deflate, br")
// req.Header.Set("accept-language", "zh-CN,zh;q=0.9")
req.Header.Set("cache-control", "no-cache")
req.Header.Set("dnt", "1")
req.Header.Set("pragma", "no-cache")
req.Header.Set("sec-ch-ua", `"Chromium";v="106", "Google Chrome";v="106", "Not;A=Brand";v="99"`)
req.Header.Set("sec-ch-ua-mobile", "?0")
req.Header.Set("sec-ch-ua-platform", "Windows")
req.Header.Set("sec-fetch-dest", "document")
req.Header.Set("sec-fetch-mode", "navigate")
req.Header.Set("sec-fetch-site", "none")
req.Header.Set("sec-fetch-user", "?1")
req.Header.Set("upgrade-insecure-requests", "1")
for _, h := range headers {
req.Header.Set(h[0], h[1])
}
return cdo(c, req)
}