Skip to content

Commit

Permalink
feat: add global IP limitation for SSpanel
Browse files Browse the repository at this point in the history
  • Loading branch information
crackair committed Sep 15, 2021
1 parent 8f28f06 commit 5faf474
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
31 changes: 16 additions & 15 deletions api/sspanel/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "encoding/json"
type NodeInfoResponse struct {
Group int `json:"node_group"`
Class int `json:"node_class"`
SpeedLimit float64 `json:"node_speedlimit"`
SpeedLimit float64 `json:"node_speedlimit"`
TrafficRate float64 `json:"traffic_rate"`
MuOnly int `json:"mu_only"`
Sort int `json:"sort"`
Expand All @@ -16,21 +16,22 @@ type NodeInfoResponse struct {

// UserResponse is the response of user
type UserResponse struct {
ID int `json:"id"`
Email string `json:"email"`
Passwd string `json:"passwd"`
Port int `json:"port"`
Method string `json:"method"`
ID int `json:"id"`
Email string `json:"email"`
Passwd string `json:"passwd"`
Port int `json:"port"`
Method string `json:"method"`
SpeedLimit float64 `json:"node_speedlimit"`
DeviceLimit int `json:"node_connector"`
Protocol string `json:"protocol"`
ProtocolParam string `json:"protocol_param"`
Obfs string `json:"obfs"`
ObfsParam string `json:"obfs_param"`
ForbiddenIP string `json:"forbidden_ip"`
ForbiddenPort string `json:"forbidden_port"`
UUID string `json:"uuid"`
MultiUser int `json:"is_multi_user"`
DeviceLimit int `json:"node_connector"`
Protocol string `json:"protocol"`
ProtocolParam string `json:"protocol_param"`
Obfs string `json:"obfs"`
ObfsParam string `json:"obfs_param"`
ForbiddenIP string `json:"forbidden_ip"`
ForbiddenPort string `json:"forbidden_port"`
UUID string `json:"uuid"`
MultiUser int `json:"is_multi_user"`
AliveIP int `json:"alive_ip"`
}

// Response is the common response
Expand Down
19 changes: 14 additions & 5 deletions api/sspanel/sspanel.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,21 +628,30 @@ func (c *APIClient) ParseTrojanNodeResponse(nodeInfoResponse *NodeInfoResponse)

// ParseUserListResponse parse the response for the given nodeinfo format
func (c *APIClient) ParseUserListResponse(userInfoResponse *[]UserResponse) (*[]api.UserInfo, error) {
var deviceLimit int = 0
var deviceLimit, localDeviceLimit int = 0, 0
var speedlimit uint64 = 0
userList := make([]api.UserInfo, len(*userInfoResponse))
for i, user := range *userInfoResponse {
userList := []api.UserInfo{}
for _, user := range *userInfoResponse {
if c.DeviceLimit > 0 {
deviceLimit = c.DeviceLimit
} else {
deviceLimit = user.DeviceLimit
}
// If there is still device available, add the user
if deviceLimit > 0 {
if localDeviceLimit = deviceLimit - user.AliveIP; localDeviceLimit <= 0 {
continue
} else {
deviceLimit = localDeviceLimit
}
}

if c.SpeedLimit > 0 {
speedlimit = uint64((c.SpeedLimit * 1000000) / 8)
} else {
speedlimit = uint64((user.SpeedLimit * 1000000) / 8)
}
userList[i] = api.UserInfo{
userList = append(userList, api.UserInfo{
UID: user.ID,
Email: user.Email,
UUID: user.UUID,
Expand All @@ -655,7 +664,7 @@ func (c *APIClient) ParseUserListResponse(userInfoResponse *[]UserResponse) (*[]
ProtocolParam: user.ProtocolParam,
Obfs: user.Obfs,
ObfsParam: user.ObfsParam,
}
})
}

return &userList, nil
Expand Down

0 comments on commit 5faf474

Please sign in to comment.