forked from wyx2685/V2bX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
123 lines (109 loc) · 2.79 KB
/
user.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
package panel
import (
"fmt"
"github.com/goccy/go-json"
)
type OnlineUser struct {
UID int
IP string
}
type UserInfo struct {
Id int `json:"id"`
Uuid string `json:"uuid"`
SpeedLimit int `json:"speed_limit"`
DeviceLimit int `json:"device_limit"`
AliveIp int `json:"alive_ip"`
}
type UserListBody struct {
//Msg string `json:"msg"`
Users []UserInfo `json:"users"`
}
// GetUserList will pull user form sspanel
func (c *Client) GetUserList() (UserList []UserInfo, err error) {
const path = "/api/v1/server/UniProxy/user"
r, err := c.client.R().
SetHeader("If-None-Match", c.userEtag).
ForceContentType("application/json").
Get(path)
if err = c.checkResponse(r, path, err); err != nil {
return nil, err
}
if r != nil {
defer func() {
if r.RawBody() != nil {
r.RawBody().Close()
}
}()
if r.StatusCode() == 304 {
return nil, nil
}
} else {
return nil, fmt.Errorf("received nil response")
}
var userList *UserListBody
if err != nil {
return nil, fmt.Errorf("read body error: %s", err)
}
if err := json.Unmarshal(r.Body(), &userList); err != nil {
return nil, fmt.Errorf("unmarshal userlist error: %s", err)
}
c.userEtag = r.Header().Get("ETag")
var userinfos []UserInfo
var deviceLimit, localDeviceLimit int = 0, 0
for _, user := range userList.Users {
// If there is still device available, add the user
if user.DeviceLimit > 0 && user.AliveIp > 0 {
lastOnline := 0
if v, ok := c.LastReportOnline[user.Id]; ok {
lastOnline = v
}
// If there are any available device.
localDeviceLimit = user.DeviceLimit - user.AliveIp + lastOnline
if localDeviceLimit > 0 {
deviceLimit = localDeviceLimit
} else if lastOnline > 0 {
deviceLimit = lastOnline
} else {
continue
}
}
user.DeviceLimit = deviceLimit
userinfos = append(userinfos, user)
}
return userinfos, nil
}
type UserTraffic struct {
UID int
Upload int64
Download int64
}
// ReportUserTraffic reports the user traffic
func (c *Client) ReportUserTraffic(userTraffic []UserTraffic) error {
data := make(map[int][]int64, len(userTraffic))
for i := range userTraffic {
data[userTraffic[i].UID] = []int64{userTraffic[i].Upload, userTraffic[i].Download}
}
const path = "/api/v1/server/UniProxy/push"
r, err := c.client.R().
SetBody(data).
ForceContentType("application/json").
Post(path)
err = c.checkResponse(r, path, err)
if err != nil {
return err
}
return nil
}
func (c *Client) ReportNodeOnlineUsers(data *map[int][]string, reportOnline *map[int]int) error {
c.LastReportOnline = *reportOnline
const path = "/api/v1/server/UniProxy/alive"
r, err := c.client.R().
SetBody(data).
ForceContentType("application/json").
Post(path)
err = c.checkResponse(r, path, err)
if err != nil {
return nil
}
return nil
}