re-support dynamic speed limit

This commit is contained in:
Yuzuki616
2023-07-22 02:38:07 +08:00
parent adf98fbc81
commit aeb8554b13
9 changed files with 124 additions and 95 deletions

View File

@@ -100,7 +100,7 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) {
const path = "/api/v1/server/UniProxy/config"
r, err := c.client.
R().
SetHeader("If-None-Match", c.etag).
SetHeader("If-None-Match", c.nodeEtag).
Get(path)
if err = c.checkResponse(r, path, err); err != nil {
return
@@ -227,7 +227,7 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) {
node.UpMbps = rsp.UpMbps
node.HyObfs = rsp.Obfs
}
c.etag = r.Header().Get("ETag")
c.nodeEtag = r.Header().Get("ETag")
return
}

View File

@@ -23,7 +23,8 @@ type Client struct {
NodeType string
NodeId int
LocalRuleList []*regexp.Regexp
etag string
nodeEtag string
userEtag string
}
func New(c *conf.ApiConfig) (*Client, error) {

View File

@@ -2,6 +2,7 @@ package panel
import (
"fmt"
"github.com/goccy/go-json"
)
@@ -14,7 +15,6 @@ type UserInfo struct {
Id int `json:"id"`
Uuid string `json:"uuid"`
SpeedLimit int `json:"speed_limit"`
Traffic int64 `json:"-"`
}
type UserListBody struct {
@@ -25,17 +25,23 @@ type UserListBody struct {
// GetUserList will pull user form sspanel
func (c *Client) GetUserList() (UserList []UserInfo, err error) {
const path = "/api/v1/server/UniProxy/user"
res, err := c.client.R().
r, err := c.client.R().
SetHeader("If-None-Match", c.userEtag).
Get(path)
err = c.checkResponse(res, path, err)
err = c.checkResponse(r, path, err)
if err != nil {
return nil, err
}
err = c.checkResponse(r, path, err)
if r.StatusCode() == 304 {
return nil, nil
}
var userList *UserListBody
err = json.Unmarshal(res.Body(), &userList)
err = json.Unmarshal(r.Body(), &userList)
if err != nil {
return nil, fmt.Errorf("unmarshal userlist error: %s", err)
}
c.userEtag = r.Header().Get("ETag")
return userList.Users, nil
}
@@ -52,11 +58,11 @@ func (c *Client) ReportUserTraffic(userTraffic []UserTraffic) error {
data[userTraffic[i].UID] = []int64{userTraffic[i].Upload, userTraffic[i].Download}
}
const path = "/api/v1/server/UniProxy/push"
res, err := c.client.R().
r, err := c.client.R().
SetBody(data).
ForceContentType("application/json").
Post(path)
err = c.checkResponse(res, path, err)
err = c.checkResponse(r, path, err)
if err != nil {
return err
}