2022-08-16 01:04:33 -04:00
|
|
|
package panel
|
2022-06-04 00:05:46 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/goccy/go-json"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OnlineUser struct {
|
|
|
|
UID int
|
|
|
|
IP string
|
|
|
|
}
|
|
|
|
|
|
|
|
type V2RayUserInfo struct {
|
|
|
|
Uuid string `json:"uuid"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
AlterId int `json:"alter_id"`
|
|
|
|
}
|
|
|
|
type TrojanUserInfo struct {
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
type UserInfo struct {
|
2022-12-18 10:31:06 -05:00
|
|
|
Id int `json:"id"`
|
|
|
|
Uuid string `json:"uuid"`
|
|
|
|
Email string `json:"-"`
|
|
|
|
SpeedLimit int `json:"speed_limit"`
|
|
|
|
Traffic int64 `json:"-"`
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// GetUserList will pull user form sspanel
|
2022-08-12 06:02:06 -04:00
|
|
|
func (c *Client) GetUserList() (UserList []UserInfo, err error) {
|
2022-12-18 10:31:06 -05:00
|
|
|
const path = "/api/v1/server/UniProxy/user"
|
2022-06-04 00:05:46 -04:00
|
|
|
res, err := c.client.R().
|
|
|
|
Get(path)
|
|
|
|
err = c.checkResponse(res, path, err)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var userList *UserListBody
|
|
|
|
err = json.Unmarshal(res.Body(), &userList)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unmarshal userlist error: %s", err)
|
|
|
|
}
|
2022-12-18 10:31:06 -05:00
|
|
|
return userList.Users, 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-18 10:31:06 -05:00
|
|
|
const path = "/api/v1/server/UniProxy/user"
|
2022-06-04 00:05:46 -04:00
|
|
|
res, err := c.client.R().
|
2022-12-18 10:31:06 -05:00
|
|
|
SetBody(userTraffic).
|
2022-06-04 00:05:46 -04:00
|
|
|
ForceContentType("application/json").
|
|
|
|
Post(path)
|
|
|
|
err = c.checkResponse(res, path, err)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|