mirror of
https://github.com/wyx2685/V2bX.git
synced 2026-02-04 04:30:08 +00:00
调整设备数限制实现方式
This commit is contained in:
@@ -25,6 +25,8 @@ type Client struct {
|
||||
userEtag string
|
||||
responseBodyHash string
|
||||
LastReportOnline map[int]int
|
||||
UserList *UserListBody
|
||||
AliveMap *AliveMap
|
||||
}
|
||||
|
||||
func New(c *conf.ApiConfig) (*Client, error) {
|
||||
@@ -71,5 +73,7 @@ func New(c *conf.ApiConfig) (*Client, error) {
|
||||
APIHost: c.APIHost,
|
||||
NodeType: c.NodeType,
|
||||
NodeId: c.NodeID,
|
||||
UserList: &UserListBody{},
|
||||
AliveMap: &AliveMap{},
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@ type UserInfo struct {
|
||||
Uuid string `json:"uuid"`
|
||||
SpeedLimit int `json:"speed_limit"`
|
||||
DeviceLimit int `json:"device_limit"`
|
||||
AliveIp int `json:"alive_ip"`
|
||||
}
|
||||
|
||||
type UserListBody struct {
|
||||
@@ -24,8 +23,12 @@ type UserListBody struct {
|
||||
Users []UserInfo `json:"users"`
|
||||
}
|
||||
|
||||
// GetUserList will pull user form sspanel
|
||||
func (c *Client) GetUserList() (UserList []UserInfo, err error) {
|
||||
type AliveMap struct {
|
||||
Alive map[int]int `json:"alive"`
|
||||
}
|
||||
|
||||
// GetUserList will pull user from v2board
|
||||
func (c *Client) GetUserList() ([]UserInfo, error) {
|
||||
const path = "/api/v1/server/UniProxy/user"
|
||||
r, err := c.client.R().
|
||||
SetHeader("If-None-Match", c.userEtag).
|
||||
@@ -34,52 +37,44 @@ func (c *Client) GetUserList() (UserList []UserInfo, err error) {
|
||||
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
|
||||
}
|
||||
defer r.RawResponse.Body.Close()
|
||||
} 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
|
||||
}
|
||||
if r.StatusCode() == 304 {
|
||||
return nil, nil
|
||||
} else {
|
||||
if err := json.Unmarshal(r.Body(), c.UserList); err != nil {
|
||||
return nil, fmt.Errorf("unmarshal user list error: %w", err)
|
||||
}
|
||||
user.DeviceLimit = deviceLimit
|
||||
userinfos = append(userinfos, user)
|
||||
c.userEtag = r.Header().Get("ETag")
|
||||
}
|
||||
return c.UserList.Users, nil
|
||||
}
|
||||
|
||||
// GetUserAlive will fetch the alive IPs for users
|
||||
func (c *Client) GetUserAlive() (map[int]int, error) {
|
||||
const path = "/api/v1/server/UniProxy/alivelist"
|
||||
r, err := c.client.R().
|
||||
ForceContentType("application/json").
|
||||
Get(path)
|
||||
if err = c.checkResponse(r, path, err); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return userinfos, nil
|
||||
if r != nil {
|
||||
defer r.RawResponse.Body.Close()
|
||||
} else {
|
||||
return nil, fmt.Errorf("received nil response")
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(r.Body(), c.AliveMap); err != nil {
|
||||
return nil, fmt.Errorf("unmarshal user alive list error: %s", err)
|
||||
}
|
||||
|
||||
return c.AliveMap.Alive, nil
|
||||
}
|
||||
|
||||
type UserTraffic struct {
|
||||
|
||||
Reference in New Issue
Block a user