测试:尝试增加hysteria2协议,尝试增加设备数限制功能

This commit is contained in:
wyx2685
2023-11-18 07:05:28 +09:00
parent f928b4f8f3
commit cf8016f405
18 changed files with 380 additions and 323 deletions

View File

@@ -33,6 +33,7 @@ type NodeInfo struct {
Shadowsocks *ShadowsocksNode
Trojan *TrojanNode
Hysteria *HysteriaNode
Hysteria2 *Hysteria2Node
Common *CommonNode
}
@@ -100,6 +101,14 @@ type HysteriaNode struct {
Obfs string `json:"obfs"`
}
type Hysteria2Node struct {
CommonNode
UpMbps int `json:"up_mbps"`
DownMbps int `json:"down_mbps"`
ObfsType string `json:"obfs"`
ObfsPassword string `json:"obfs-password"`
}
type RawDNS struct {
DNSMap map[string]map[string]interface{}
DNSJson []byte
@@ -166,7 +175,7 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) {
rsp := &ShadowsocksNode{}
err = json.Unmarshal(r.Body(), rsp)
if err != nil {
return nil, fmt.Errorf("decode v2ray params error: %s", err)
return nil, fmt.Errorf("decode shadowsocks params error: %s", err)
}
cm = &rsp.CommonNode
node.Shadowsocks = rsp
@@ -175,7 +184,7 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) {
rsp := &TrojanNode{}
err = json.Unmarshal(r.Body(), rsp)
if err != nil {
return nil, fmt.Errorf("decode v2ray params error: %s", err)
return nil, fmt.Errorf("decode trojan params error: %s", err)
}
cm = (*CommonNode)(rsp)
node.Trojan = rsp
@@ -184,11 +193,20 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) {
rsp := &HysteriaNode{}
err = json.Unmarshal(r.Body(), rsp)
if err != nil {
return nil, fmt.Errorf("decode v2ray params error: %s", err)
return nil, fmt.Errorf("decode hysteria params error: %s", err)
}
cm = &rsp.CommonNode
node.Hysteria = rsp
node.Security = Tls
case "hysteria2":
rsp := &Hysteria2Node{}
err = json.Unmarshal(r.Body(), rsp)
if err != nil {
return nil, fmt.Errorf("decode hysteria2 params error: %s", err)
}
cm = &rsp.CommonNode
node.Hysteria2 = rsp
node.Security = Tls
}
// parse rules and dns

View File

@@ -16,13 +16,14 @@ import (
// Panel is the interface for different panel's api.
type Client struct {
client *resty.Client
APIHost string
Token string
NodeType string
NodeId int
nodeEtag string
userEtag string
client *resty.Client
APIHost string
Token string
NodeType string
NodeId int
nodeEtag string
userEtag string
LastReportOnline map[int]int
}
func New(c *conf.ApiConfig) (*Client, error) {
@@ -52,6 +53,7 @@ func New(c *conf.ApiConfig) (*Client, error) {
"trojan",
"shadowsocks",
"hysteria",
"hysteria2",
"vless":
default:
return nil, fmt.Errorf("unsupported Node type: %s", c.NodeType)

View File

@@ -2,6 +2,7 @@ package panel
import (
"fmt"
"github.com/goccy/go-json"
)
@@ -11,9 +12,11 @@ type OnlineUser struct {
}
type UserInfo struct {
Id int `json:"id"`
Uuid string `json:"uuid"`
SpeedLimit int `json:"speed_limit"`
Id int `json:"id"`
Uuid string `json:"uuid"`
SpeedLimit int `json:"speed_limit"`
DeviceLimit int `json:"device_limit"`
AliveIp int `json:"alive_ip"`
}
type UserListBody struct {
@@ -31,7 +34,7 @@ func (c *Client) GetUserList() (UserList []UserInfo, err error) {
if err != nil {
return nil, err
}
err = c.checkResponse(r, path, err)
if r.StatusCode() == 304 {
return nil, nil
}
@@ -41,7 +44,30 @@ func (c *Client) GetUserList() (UserList []UserInfo, err error) {
return nil, fmt.Errorf("unmarshal userlist error: %s", err)
}
c.userEtag = r.Header().Get("ETag")
return userList.Users, nil
var userinfos []UserInfo
var localDeviceLimit int = 0
for _, user := range userList.Users {
// If there is still device available, add the user
if user.DeviceLimit > 0 && user.AliveIp > 0 {
lastOnline := 0
if v, ok := c.LastReportOnline[user.Id]; ok {
lastOnline = v
}
// If there are any available device.
localDeviceLimit = user.DeviceLimit - user.AliveIp + lastOnline
if localDeviceLimit > 0 {
} else if lastOnline > 0 {
} else {
continue
}
}
userinfos = append(userinfos, user)
}
return userinfos, nil
}
type UserTraffic struct {
@@ -67,3 +93,19 @@ func (c *Client) ReportUserTraffic(userTraffic []UserTraffic) error {
}
return nil
}
func (c *Client) ReportNodeOnlineUsers(data *map[int][]string, reportOnline *map[int]int) error {
c.LastReportOnline = *reportOnline
const path = "/api/v1/server/UniProxy/alive"
r, err := c.client.R().
SetBody(data).
ForceContentType("application/json").
Post(path)
err = c.checkResponse(r, path, err)
if err != nil {
return nil
}
return nil
}