-
Notifications
You must be signed in to change notification settings - Fork 8
/
request.go
103 lines (94 loc) · 2.65 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
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
package utils
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
U "net/url"
"os"
)
func request(payload []byte, token string, id string, action string) ([]byte, error) {
var url, method string
var teamToken string
if action == "register" {
url = "https://api.cloudflareclient.com/v0a2158/reg"
method = "POST"
} else if action == "license" {
url = "https://api.cloudflareclient.com/v0a2158/reg/" + id + "/account"
method = "PUT"
} else if action == "bind" {
url = "https://api.cloudflareclient.com/v0a2158/reg/" + id + "/account/devices"
method = "GET"
} else if action == "name" || action == "unbind" {
url = "https://api.cloudflareclient.com/v0a2158/reg/" + id + "/account/reg/" + id
method = "PATCH"
} else if action == "cancle" {
url = "https://api.cloudflareclient.com/v0a2158/reg/" + id
method = "DELETE"
} else if action == "registerTeam" {
url = "https://api.cloudflareclient.com/v0a2158/reg"
method = "POST"
teamToken = token
token = ""
} else if action == "update" {
url = "https://api.cloudflareclient.com/v0a2158/reg/" + id
method = "GET"
}
var body []byte
var request *http.Request
var response *http.Response
var err error
var proxy string
var proxyURL *U.URL
httpProxy := os.Getenv("http_proxy")
httpsProxy := os.Getenv("https_proxy")
if httpProxy != "" {
proxy = httpProxy
} else if httpsProxy != "" {
proxy = httpsProxy
}
if proxyURL, err = U.Parse(proxy); err != nil {
panic("Error parsing proxy URL: " + err.Error())
}
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
MinVersion: tls.VersionTLS12,
MaxVersion: tls.VersionTLS13,
},
},
}
if proxy != "" {
client.Transport.(*http.Transport).Proxy = http.ProxyURL(proxyURL)
}
if request, err = http.NewRequest(method, url, bytes.NewBuffer(payload)); err != nil {
panic(err)
}
request.Header.Add("CF-Client-Version", "a-7.21-0721")
request.Header.Add("User-Agent", "okhttp/0.7.21")
if token != "" {
request.Header.Add("Authorization", "Bearer "+token)
}
if teamToken != "" {
request.Header.Add("Cf-Access-Jwt-Assertion", teamToken)
}
if response, err = client.Do(request); err != nil {
panic(err)
}
defer response.Body.Close()
if body, err = io.ReadAll(response.Body); err != nil {
panic(err)
}
if response.StatusCode != 204 && response.StatusCode != 200 {
var prettyJSON bytes.Buffer
if err = json.Indent(&prettyJSON, body, "", " "); err != nil {
fmt.Println(string(body))
} else {
fmt.Println(prettyJSON.String())
}
err = fmt.Errorf("REST API returned " + fmt.Sprint(response.StatusCode) + " " + http.StatusText(response.StatusCode))
}
return body, err
}