2022-08-16 01:04:33 -04:00
|
|
|
package panel
|
2022-06-04 00:05:46 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-11-17 17:05:28 -05:00
|
|
|
|
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 {
|
2023-11-17 17:05:28 -05:00
|
|
|
Id int `json:"id"`
|
|
|
|
Uuid string `json:"uuid"`
|
|
|
|
SpeedLimit int `json:"speed_limit"`
|
|
|
|
DeviceLimit int `json:"device_limit"`
|
2022-06-04 00:05:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type UserListBody struct {
|
|
|
|
//Msg string `json:"msg"`
|
2022-12-18 10:31:06 -05:00
|
|
|
Users []UserInfo `json:"users"`
|
2022-06-04 00:05:46 -04:00
|
|
|
}
|
|
|
|
|
2024-08-29 17:48:41 -04:00
|
|
|
type AliveMap struct {
|
|
|
|
Alive map[int]int `json:"alive"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserList will pull user from v2board
|
|
|
|
func (c *Client) GetUserList() ([]UserInfo, error) {
|
2022-12-18 10:31:06 -05:00
|
|
|
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-09-11 11:10:31 -04:00
|
|
|
if r == nil || r.RawResponse == nil {
|
|
|
|
return nil, fmt.Errorf("received nil response or raw response")
|
2023-07-21 14:38:07 -04:00
|
|
|
}
|
2024-09-11 11:10:31 -04:00
|
|
|
defer r.RawResponse.Body.Close()
|
2024-08-29 17:48:41 -04:00
|
|
|
|
|
|
|
if r.StatusCode() == 304 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
2024-09-11 11:10:31 -04:00
|
|
|
|
|
|
|
if err = c.checkResponse(r, path, err); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
userlist := &UserListBody{}
|
|
|
|
if err := json.Unmarshal(r.Body(), userlist); err != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshal user list error: %w", err)
|
|
|
|
}
|
|
|
|
c.userEtag = r.Header().Get("ETag")
|
|
|
|
return userlist.Users, nil
|
2024-08-29 17:48:41 -04:00
|
|
|
}
|
|
|
|
|
2024-08-31 16:27:11 -04:00
|
|
|
// GetUserAlive will fetch the alive_ip count for users
|
2024-08-29 17:48:41 -04:00
|
|
|
func (c *Client) GetUserAlive() (map[int]int, error) {
|
2024-12-01 18:38:23 -05:00
|
|
|
c.AliveMap = &AliveMap{}
|
2024-08-29 17:48:41 -04:00
|
|
|
const path = "/api/v1/server/UniProxy/alivelist"
|
|
|
|
r, err := c.client.R().
|
|
|
|
ForceContentType("application/json").
|
|
|
|
Get(path)
|
2024-09-11 11:10:31 -04:00
|
|
|
if err != nil || r.StatusCode() >= 399 {
|
|
|
|
c.AliveMap.Alive = make(map[int]int)
|
2025-01-15 00:31:12 -05:00
|
|
|
return c.AliveMap.Alive, nil
|
2022-06-04 00:05:46 -04:00
|
|
|
}
|
2024-12-01 18:38:23 -05:00
|
|
|
if r == nil || r.RawResponse == nil {
|
|
|
|
fmt.Printf("received nil response or raw response")
|
|
|
|
c.AliveMap.Alive = make(map[int]int)
|
2025-01-15 00:31:12 -05:00
|
|
|
return c.AliveMap.Alive, nil
|
2024-12-01 18:38:23 -05:00
|
|
|
}
|
|
|
|
defer r.RawResponse.Body.Close()
|
2024-08-29 17:48:41 -04:00
|
|
|
if err := json.Unmarshal(r.Body(), c.AliveMap); err != nil {
|
2024-12-01 18:38:23 -05:00
|
|
|
fmt.Printf("unmarshal user alive list error: %s", err)
|
|
|
|
c.AliveMap.Alive = make(map[int]int)
|
2023-11-17 17:05:28 -05:00
|
|
|
}
|
|
|
|
|
2024-08-29 17:48:41 -04:00
|
|
|
return c.AliveMap.Alive, nil
|
2022-06-04 00:05:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type UserTraffic struct {
|
2022-12-18 10:31:06 -05:00
|
|
|
UID int
|
|
|
|
Upload int64
|
|
|
|
Download int64
|
2022-06-04 00:05:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReportUserTraffic reports the user traffic
|
2022-08-12 06:02:06 -04:00
|
|
|
func (c *Client) ReportUserTraffic(userTraffic []UserTraffic) error {
|
2022-12-18 10:31:06 -05:00
|
|
|
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
|
|
|
|
}
|
2023-11-17 17:05:28 -05:00
|
|
|
|
2024-08-31 03:02:50 -04:00
|
|
|
func (c *Client) ReportNodeOnlineUsers(data *map[int][]string) error {
|
2023-11-17 17:05:28 -05:00
|
|
|
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
|
|
|
|
}
|