Skip to content

Commit

Permalink
测试:增加设备数限制功能
Browse files Browse the repository at this point in the history
  • Loading branch information
wyx2685 committed Oct 14, 2023
1 parent ff475f7 commit d60219c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 18 deletions.
2 changes: 2 additions & 0 deletions api/newV2board/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@ type user struct {
Id int `json:"id"`
Uuid string `json:"uuid"`
SpeedLimit int `json:"speed_limit"`
DeviceLimit int `json:"device_limit"`
AliveIp int `json:"alive_ip"`
}
79 changes: 61 additions & 18 deletions api/newV2board/v2board.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type APIClient struct {
SpeedLimit float64
DeviceLimit int
LocalRuleList []api.DetectRule
LastReportOnline map[int]int
resp atomic.Value
eTags map[string]string
}
Expand Down Expand Up @@ -234,27 +235,49 @@ func (c *APIClient) GetUserList() (UserList *[]api.UserInfo, err error) {
return nil, errors.New("users is null")
}

userList := make([]api.UserInfo, len(users))
for i := 0; i < len(users); i++ {
u := api.UserInfo{
UID: users[i].Id,
UUID: users[i].Uuid,
}
var deviceLimit,localDeviceLimit int = 0, 0
var userList []api.UserInfo
for _, user := range users {
u := api.UserInfo{
UID: user.Id,
UUID: user.Uuid,
}
// Support 1.7.1 speed limit
if c.SpeedLimit > 0 {
u.SpeedLimit = uint64(c.SpeedLimit * 1000000 / 8)
} else {
u.SpeedLimit = uint64(user.SpeedLimit * 1000000 / 8)
}
deviceLimit = user.DeviceLimit
if deviceLimit == 0 {
deviceLimit = c.DeviceLimit
}

// Support 1.7.1 speed limit
if c.SpeedLimit > 0 {
u.SpeedLimit = uint64(c.SpeedLimit * 1000000 / 8)
} else {
u.SpeedLimit = uint64(users[i].SpeedLimit * 1000000 / 8)
// If there is still device available, add the user
if deviceLimit > 0 && user.AliveIp > 0 {
lastOnline := 0
if v, ok := c.LastReportOnline[user.Id]; ok {
lastOnline = v
}
// If there are any available device.
if localDeviceLimit = deviceLimit - user.AliveIp + lastOnline; localDeviceLimit > 0 {
deviceLimit = localDeviceLimit
// If this backend server has reported any user in the last reporting period.
} else if lastOnline > 0 {
deviceLimit = lastOnline
// Remove this user.
} else {
continue
}
}
u.DeviceLimit = deviceLimit
u.Email = u.UUID + "@v2board.user"
if c.NodeType == "Shadowsocks" {
u.Passwd = u.UUID
}

u.DeviceLimit = c.DeviceLimit // todo waiting v2board send configuration
u.Email = u.UUID + "@v2board.user"
if c.NodeType == "Shadowsocks" {
u.Passwd = u.UUID
}
userList[i] = u
}
userList = append(userList, u)
}

return &userList, nil
}
Expand Down Expand Up @@ -303,6 +326,26 @@ func (c *APIClient) ReportNodeStatus(nodeStatus *api.NodeStatus) (err error) {

// ReportNodeOnlineUsers implements the API interface
func (c *APIClient) ReportNodeOnlineUsers(onlineUserList *[]api.OnlineUser) error {
reportOnline := make(map[int]int)
data := make(map[int][]string)
for _, onlineuser := range *onlineUserList {
// json structure: { UID1:["ip1","ip2"],UID2:["ip3","ip4"] }
data[onlineuser.UID] = append(data[onlineuser.UID], fmt.Sprintf("%s_%d",onlineuser.IP,c.NodeID))
if _, ok := reportOnline[onlineuser.UID]; ok {
reportOnline[onlineuser.UID]++
} else {
reportOnline[onlineuser.UID] = 1
}
}
c.LastReportOnline = reportOnline // Update LastReportOnline

path := "/api/v1/server/UniProxy/alive"
res, err := c.client.R().SetBody(data).ForceContentType("application/json").Post(path)
_, err = c.parseResponse(res, path, err)
if err != nil {
return err
}

return nil
}

Expand Down

0 comments on commit d60219c

Please sign in to comment.