fix: sspanel version >= 2023.2 will discard node status report.

fix: return error message for null user list
This commit is contained in:
Senis John 2023-05-15 19:55:08 +08:00
parent af9224f5bb
commit b49798ab16
No known key found for this signature in database
GPG Key ID: 845E9E4727C3E1A4
2 changed files with 47 additions and 16 deletions

View File

@ -211,6 +211,9 @@ func (c *APIClient) GetUserList() (UserList *[]api.UserInfo, err error) {
} }
b, _ := usersResp.Get("users").Encode() b, _ := usersResp.Get("users").Encode()
json.Unmarshal(b, &users) json.Unmarshal(b, &users)
if len(users) == 0 {
return nil, errors.New("users is null")
}
userList := make([]api.UserInfo, len(users)) userList := make([]api.UserInfo, len(users))
for i := 0; i < len(users); i++ { for i := 0; i < len(users); i++ {

View File

@ -38,6 +38,7 @@ type APIClient struct {
LocalRuleList []api.DetectRule LocalRuleList []api.DetectRule
LastReportOnline map[int]int LastReportOnline map[int]int
access sync.Mutex access sync.Mutex
version string
} }
// New creat a api instance // New creat a api instance
@ -167,6 +168,7 @@ func (c *APIClient) GetNodeInfo() (nodeInfo *api.NodeInfo, err error) {
} }
// New sspanel API // New sspanel API
c.version = nodeInfoResponse.Version
disableCustomConfig := c.DisableCustomConfig disableCustomConfig := c.DisableCustomConfig
if nodeInfoResponse.Version != "" && !disableCustomConfig { if nodeInfoResponse.Version != "" && !disableCustomConfig {
// Check if custom_config is empty // Check if custom_config is empty
@ -236,23 +238,25 @@ func (c *APIClient) GetUserList() (UserList *[]api.UserInfo, err error) {
// ReportNodeStatus reports the node status to the sspanel // ReportNodeStatus reports the node status to the sspanel
func (c *APIClient) ReportNodeStatus(nodeStatus *api.NodeStatus) (err error) { func (c *APIClient) ReportNodeStatus(nodeStatus *api.NodeStatus) (err error) {
path := fmt.Sprintf("/mod_mu/nodes/%d/info", c.NodeID) // Determine whether a status report is in need
systemload := SystemLoad{ if compareVersion(c.version, "2023.2") == -1 {
Uptime: strconv.FormatUint(nodeStatus.Uptime, 10), path := fmt.Sprintf("/mod_mu/nodes/%d/info", c.NodeID)
Load: fmt.Sprintf("%.2f %.2f %.2f", nodeStatus.CPU/100, nodeStatus.Mem/100, nodeStatus.Disk/100), systemload := SystemLoad{
Uptime: strconv.FormatUint(nodeStatus.Uptime, 10),
Load: fmt.Sprintf("%.2f %.2f %.2f", nodeStatus.CPU/100, nodeStatus.Mem/100, nodeStatus.Disk/100),
}
res, err := c.client.R().
SetBody(systemload).
SetResult(&Response{}).
ForceContentType("application/json").
Post(path)
_, err = c.parseResponse(res, path, err)
if err != nil {
return err
}
} }
res, err := c.client.R().
SetBody(systemload).
SetResult(&Response{}).
ForceContentType("application/json").
Post(path)
_, err = c.parseResponse(res, path, err)
if err != nil {
return err
}
return nil return nil
} }
@ -793,3 +797,27 @@ func (c *APIClient) ParseSSPanelNodeInfo(nodeInfoResponse *NodeInfoResponse) (*a
return nodeinfo, nil return nodeinfo, nil
} }
func compareVersion(version1, version2 string) int {
n, m := len(version1), len(version2)
i, j := 0, 0
for i < n || j < m {
x := 0
for ; i < n && version1[i] != '.'; i++ {
x = x*10 + int(version1[i]-'0')
}
i++ // jump dot
y := 0
for ; j < m && version2[j] != '.'; j++ {
y = y*10 + int(version2[j]-'0')
}
j++ // jump dot
if x > y {
return 1
}
if x < y {
return -1
}
}
return 0
}