-
Notifications
You must be signed in to change notification settings - Fork 92
/
client.go
150 lines (141 loc) · 3.89 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
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
package main
import (
"encoding/json"
"fmt"
"github.com/astaxie/beego/httplib"
"github.com/tidwall/gjson"
"net/http"
"net/url"
"os"
"os/user"
"runtime"
"time"
)
type Client struct {
Hosts []string // 服务器地址s
host string // 检查后的服务器地址
}
func (c *Client) SetProxy(lang string) {
defer c.setHost()
proxy := httplib.BeegoHTTPSettings{}.Proxy
proxyText := ""
if os.Getenv("http_proxy") != "" {
proxy = func(request *http.Request) (*url.URL, error) {
return url.Parse(os.Getenv("http_proxy"))
}
proxyText = os.Getenv("http_proxy") + " " + tr.Tr("经由") + " http_proxy " + tr.Tr("代理访问")
}
if os.Getenv("https_proxy") != "" {
proxy = func(request *http.Request) (*url.URL, error) {
return url.Parse(os.Getenv("https_proxy"))
}
proxyText = os.Getenv("https_proxy") + " " + tr.Tr("经由") + " https_proxy " + tr.Tr("代理访问")
}
if os.Getenv("all_proxy") != "" {
proxy = func(request *http.Request) (*url.URL, error) {
return url.Parse(os.Getenv("all_proxy"))
}
proxyText = os.Getenv("all_proxy") + " " + tr.Tr("经由") + " all_proxy " + tr.Tr("代理访问")
}
httplib.SetDefaultSetting(httplib.BeegoHTTPSettings{
Proxy: proxy,
ReadWriteTimeout: 30 * time.Second,
ConnectTimeout: 30 * time.Second,
Gzip: true,
DumpBody: true,
UserAgent: fmt.Sprintf(`{"lang":"%s","GOOS":"%s","ARCH":"%s","version":%d,"deviceID":"%s","machineID":"%s"}`,
lang, runtime.GOOS, runtime.GOARCH, version, deviceID, machineID),
})
if len(proxyText) > 0 {
fmt.Printf(yellow, proxyText)
}
}
func (c *Client) setHost() {
c.host = c.Hosts[0]
for _, v := range c.Hosts {
_, err := httplib.Get(v).SetTimeout(4*time.Second, 4*time.Second).String()
if err == nil {
c.host = v
return
}
}
return
}
func (c *Client) GetAD() (ad string) {
res, err := httplib.Get(c.host + "/ad").String()
if err != nil {
return
}
return res
}
func (c *Client) GetPayUrl() (payUrl, orderID string) {
res, err := httplib.Get(c.host + "/payUrl").String()
if err != nil {
fmt.Println(err)
return
}
payUrl = gjson.Get(res, "payUrl").String()
orderID = gjson.Get(res, "orderID").String()
return
}
func (c *Client) PayCheck(orderID, deviceID string) (isPay bool) {
res, err := httplib.Get(c.host + "/payCheck?orderID=" + orderID + "&deviceID=" + deviceID).String()
if err != nil {
fmt.Println(err)
return
}
isPay = gjson.Get(res, "isPay").Bool()
return
}
func (c *Client) GetMyInfo(deviceID string) (sCount, sPayCount, isPay, ticket, exp string) {
body, _ := json.Marshal(map[string]string{
"device": deviceID,
"deviceMac": getMac_241018(),
"sDevice": getPromotion(),
})
dUser, _ := user.Current()
deviceName := ""
if dUser != nil {
deviceName = dUser.Name
if deviceName == "" {
deviceName = dUser.Username
}
}
res, err := httplib.Post(c.host+"/my").Body(body).Header("deviceName", deviceName).String()
if err != nil {
panic(fmt.Sprintf("\u001B[31m%s\u001B[0m", err))
return
}
sCount = gjson.Get(res, "sCount").String()
sPayCount = gjson.Get(res, "sPayCount").String()
isPay = gjson.Get(res, "isPay").String()
ticket = gjson.Get(res, "ticket").String()
exp = gjson.Get(res, "exp").String()
return
}
func (c *Client) CheckVersion(version string) (upUrl string) {
res, err := httplib.Get(c.host + "/version?version=" + version + "&plat=" + runtime.GOOS + "_" + runtime.GOARCH).String()
if err != nil {
return ""
}
upUrl = gjson.Get(res, "url").String()
return
}
func (c *Client) GetLic(product string, dur int) (isOk bool, result string) {
req := httplib.Get(c.host + "/getLic?device=" + getMacMD5_241018() + "&dur=" + fmt.Sprint(dur) + "&product=" + product)
res, err := req.String()
if err != nil {
isOk = false
result = err.Error()
return
}
code := gjson.Get(res, "code").Int()
msg := gjson.Get(res, "msg").String()
result = msg
if code != 0 {
isOk = false
return
}
isOk = true
return
}