V2bX/api/panel/user.go

124 lines
2.8 KiB
Go
Raw Normal View History

package panel
2022-06-04 00:05:46 -04:00
import (
"fmt"
2024-01-27 11:04:47 -05:00
"github.com/goccy/go-json"
2022-06-04 00:05:46 -04:00
)
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"`
2022-06-04 00:05:46 -04:00
}
type UserListBody struct {
//Msg string `json:"msg"`
Users []UserInfo `json:"users"`
2022-06-04 00:05:46 -04:00
}
// GetUserList will pull user form sspanel
func (c *Client) GetUserList() (UserList []UserInfo, err error) {
const path = "/api/v1/server/UniProxy/user"
2023-07-21 14:38:07 -04:00
r, err := c.client.R().
SetHeader("If-None-Match", c.userEtag).
2024-01-26 23:19:55 -05:00
ForceContentType("application/json").
2022-06-04 00:05:46 -04:00
Get(path)
2024-01-25 09:06:37 -05:00
if err = c.checkResponse(r, path, err); err != nil {
2022-06-04 00:05:46 -04:00
return nil, err
}
2024-01-25 09:06:37 -05:00
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")
2023-07-21 14:38:07 -04:00
}
2022-06-04 00:05:46 -04:00
var userList *UserListBody
if err != nil {
2024-01-27 11:04:47 -05:00
return nil, fmt.Errorf("read body error: %s", err)
}
if err := json.Unmarshal(r.Body(), &userList); err != nil {
2022-06-04 00:05:46 -04:00
return nil, fmt.Errorf("unmarshal userlist error: %s", err)
}
2023-07-21 14:38:07 -04:00
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
2022-06-04 00:05:46 -04:00
}
type UserTraffic struct {
UID int
Upload int64
Download int64
2022-06-04 00:05:46 -04:00
}
// 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}
2022-06-04 00:05:46 -04:00
}
2022-12-20 14:49:34 -05:00
const path = "/api/v1/server/UniProxy/push"
2023-07-21 14:38:07 -04:00
r, err := c.client.R().
2022-12-22 12:52:44 -05:00
SetBody(data).
2022-06-04 00:05:46 -04:00
ForceContentType("application/json").
Post(path)
2023-07-21 14:38:07 -04:00
err = c.checkResponse(r, path, err)
2022-06-04 00:05:46 -04:00
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
}