-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathrequest.go
78 lines (68 loc) · 1.6 KB
/
request.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
package main
import (
tls_client "github.com/bogdanfinn/tls-client"
"github.com/bogdanfinn/tls-client/profiles"
"io"
"luma-api/common"
"net/http"
"net/url"
)
var TlsHTTPClient tls_client.HttpClient
var HTTPClient *http.Client
func init() {
InitTlsHTTPClient()
}
func InitTlsHTTPClient() {
jar := tls_client.NewCookieJar()
options := []tls_client.HttpClientOption{
tls_client.WithTimeoutSeconds(360),
tls_client.WithClientProfile(profiles.Chrome_120),
tls_client.WithNotFollowRedirects(),
tls_client.WithCookieJar(jar), // create cookieJar instance and pass it as argument
}
client, _ := tls_client.NewHttpClient(tls_client.NewNoopLogger(), options...)
TlsHTTPClient = client
HTTPClient = &http.Client{}
if common.Proxy != "" {
TlsHTTPClient.SetProxy(common.Proxy)
u, err := url.Parse(common.Proxy)
if err != nil {
common.Logger.Error("Invalid proxy URL: " + common.Proxy)
panic(err)
}
HTTPClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(u),
},
}
}
}
func DoRequest(method, url string, body io.Reader, otherHeaders map[string]string) (*http.Response, error) {
headers := map[string]string{
"Cookie": GetLumaAuth(),
}
for k, v := range CommonHeaders {
headers[k] = v
}
for k, v := range otherHeaders {
headers[k] = v
}
req, err := http.NewRequest(method, url, body)
if err != nil {
return nil, err
}
defer func() {
if body != nil {
req.Body.Close()
}
}()
for k, v := range headers {
req.Header.Set(k, v)
}
resp, err := HTTPClient.Do(req)
if err != nil {
return nil, err
}
HandleRespCookies(resp)
return resp, nil
}