mirror of
https://github.com/wyx2685/V2bX.git
synced 2026-02-04 12:40:11 +00:00
更新内核,尝试优化代码
This commit is contained in:
@@ -3,7 +3,10 @@ package panel
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/goccy/go-json"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/bitly/go-simplejson"
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
type OnlineUser struct {
|
||||
@@ -29,6 +32,7 @@ func (c *Client) GetUserList() (UserList []UserInfo, err error) {
|
||||
const path = "/api/v1/server/UniProxy/user"
|
||||
r, err := c.client.R().
|
||||
SetHeader("If-None-Match", c.userEtag).
|
||||
ForceContentType("application/json").
|
||||
Get(path)
|
||||
if err = c.checkResponse(r, path, err); err != nil {
|
||||
return nil, err
|
||||
@@ -47,10 +51,12 @@ func (c *Client) GetUserList() (UserList []UserInfo, err error) {
|
||||
return nil, fmt.Errorf("received nil response")
|
||||
}
|
||||
var userList *UserListBody
|
||||
err = json.Unmarshal(r.Body(), &userList)
|
||||
usersResp, err := c.parseResponse(r, path, err)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unmarshal userlist error: %s", err)
|
||||
}
|
||||
b, _ := usersResp.Encode()
|
||||
json.Unmarshal(b, &userList)
|
||||
c.userEtag = r.Header().Get("ETag")
|
||||
|
||||
var userinfos []UserInfo
|
||||
@@ -117,3 +123,20 @@ func (c *Client) ReportNodeOnlineUsers(data *map[int][]string, reportOnline *map
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) parseResponse(res *resty.Response, path string, err error) (*simplejson.Json, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("request %s failed: %v", c.assembleURL(path), err)
|
||||
}
|
||||
|
||||
if res.StatusCode() > 399 {
|
||||
return nil, fmt.Errorf("request %s failed: %s, %v", c.assembleURL(path), res.String(), err)
|
||||
}
|
||||
|
||||
rtn, err := simplejson.NewJson(res.Body())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ret %s invalid", res.String())
|
||||
}
|
||||
|
||||
return rtn, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user