-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclient.go
executable file
·142 lines (120 loc) · 2.96 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
package musdk
import (
"encoding/json"
"errors"
"fmt"
"log/slog"
"net/http"
"sync"
)
var (
UpdateTrafficFail = errors.New("Update Traffic Failed ")
UpdateOnlineCountFail = errors.New("Update Online Count Failed ")
StatusCodeError = errors.New("Status code is not OK ")
)
type Client struct {
baseUrl string
nodeID string
token string
sType int // service Type
userTraffic map[int64]UserTrafficLog
userTFmu *sync.Mutex
logger *slog.Logger
}
func NewClient(baseUrl, token, nodeID string, sType int, logger *slog.Logger) *Client {
return &Client{
baseUrl: baseUrl,
token: token,
nodeID: nodeID,
sType: sType,
userTraffic: make(map[int64]UserTrafficLog),
userTFmu: new(sync.Mutex),
logger: logger,
}
}
func ClientFromEnv(logger *slog.Logger) *Client {
return NewClient(env("MU_URI"), env("MU_TOKEN"), env("MU_NODE_ID"), envInt("MU_SERVICE_TYPE"), logger)
}
func (c *Client) SetLogger(l *slog.Logger) {
c.logger = l
}
func (c *Client) getUsersUri() string {
return fmt.Sprintf("%s/nodes/%s/users", c.baseUrl, c.nodeID)
}
func (c *Client) getV2rayUsersUri() string {
return fmt.Sprintf("%s/nodes/%s/v2rayUsers", c.baseUrl, c.nodeID)
}
func (c *Client) postTrafficUri() string {
return fmt.Sprintf("%s/nodes/%s/traffic", c.baseUrl, c.nodeID)
}
func (c *Client) postIpUri() string {
return fmt.Sprintf("%s/nodes/%s/ip", c.baseUrl, c.nodeID)
}
func (c *Client) getNodesUri() string {
return fmt.Sprintf("%s/nodes", c.baseUrl)
}
func (c *Client) GetUsers() ([]User, error) {
var users []User
resp, statusCode, err := c.httpGet(c.getUsersUri())
if err != nil {
return users, err
}
if statusCode != http.StatusOK {
return users, errors.New(fmt.Sprintf("status code: %d", statusCode))
}
var ret UserDataRet
err = json.Unmarshal([]byte(resp), &ret)
if err != nil {
return users, err
}
return ret.Data, nil
}
func (c *Client) UpdateTraffic(logs []UserTrafficLog) error {
data, err := json.Marshal(logs)
if err != nil {
return err
}
_, statusCode, err := c.httpPost(c.postTrafficUri(), string(data))
if err != nil {
return err
}
if statusCode != http.StatusOK {
return errors.New(fmt.Sprintf("status code: %d", statusCode))
}
return nil
}
func (c *Client) GetNodes() ([]Node, error) {
resp, statusCode, err := c.httpGet(c.getNodesUri())
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
return nil, errors.New(fmt.Sprintf("status code: %d", statusCode))
}
var ret NodeDataRet
err = json.Unmarshal([]byte(resp), &ret)
if err != nil {
return nil, err
}
return ret.Data, nil
}
type ipReq struct {
IP string `json:"ip"`
}
func (c *Client) PostIP(ip string) error {
var req = ipReq{
IP: ip,
}
data, err := json.Marshal(req)
if err != nil {
return err
}
_, statusCode, err := c.httpPost(c.postIpUri(), string(data))
if err != nil {
return err
}
if statusCode != http.StatusOK {
return errors.New(fmt.Sprintf("status code: %d", statusCode))
}
return nil
}