mirror of
https://github.com/wyx2685/V2bX.git
synced 2026-02-04 04:30:08 +00:00
update
initial support for hysteria update config update api change user email format ...
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package panel
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/goccy/go-json"
|
||||
"reflect"
|
||||
"regexp"
|
||||
@@ -9,21 +10,12 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type NodeInfo struct {
|
||||
NodeId int `json:"-"`
|
||||
NodeType string `json:"-"`
|
||||
Rules []*regexp.Regexp `json:"-"`
|
||||
Host string `json:"host"`
|
||||
ServerPort int `json:"server_port"`
|
||||
ServerName string `json:"server_name"`
|
||||
Network string `json:"network"`
|
||||
NetworkSettings json.RawMessage `json:"networkSettings"`
|
||||
Cipher string `json:"cipher"`
|
||||
ServerKey string `json:"server_key"`
|
||||
Tls int `json:"tls"`
|
||||
Routes []Route `json:"routes"`
|
||||
BaseConfig *BaseConfig `json:"base_config"`
|
||||
type CommonNodeRsp struct {
|
||||
ServerPort int `json:"server_port"`
|
||||
Routes []Route `json:"routes"`
|
||||
BaseConfig BaseConfig `json:"base_config"`
|
||||
}
|
||||
|
||||
type Route struct {
|
||||
Id int `json:"id"`
|
||||
Match interface{} `json:"match"`
|
||||
@@ -35,37 +27,122 @@ type BaseConfig struct {
|
||||
PullInterval any `json:"pull_interval"`
|
||||
}
|
||||
|
||||
func (c *Client) GetNodeInfo() (nodeInfo *NodeInfo, err error) {
|
||||
type V2rayNodeRsp struct {
|
||||
Tls int `json:"tls"`
|
||||
Network string `json:"network"`
|
||||
NetworkSettings json.RawMessage `json:"networkSettings"`
|
||||
ServerName string `json:"server_name"`
|
||||
}
|
||||
|
||||
type ShadowsocksNodeRsp struct {
|
||||
Cipher string `json:"cipher"`
|
||||
ServerKey string `json:"server_key"`
|
||||
}
|
||||
|
||||
type TrojanNodeRsp struct {
|
||||
Host string `json:"host"`
|
||||
ServerName string `json:"server_name"`
|
||||
}
|
||||
|
||||
type HysteriaNodeRsp struct {
|
||||
UpMbps int `json:"up_mbps"`
|
||||
DownMbps int `json:"down_mbps"`
|
||||
Obfs string `json:"obfs"`
|
||||
}
|
||||
|
||||
type NodeInfo struct {
|
||||
Id int
|
||||
Type string
|
||||
Rules []*regexp.Regexp
|
||||
Port int
|
||||
Network string
|
||||
NetworkSettings json.RawMessage
|
||||
Tls bool
|
||||
Host string
|
||||
ServerName string
|
||||
UpMbps int
|
||||
DownMbps int
|
||||
ServerKey string
|
||||
Cipher string
|
||||
HyObfs string
|
||||
PushInterval time.Duration
|
||||
PullInterval time.Duration
|
||||
}
|
||||
|
||||
func (c *Client) GetNodeInfo() (node *NodeInfo, err error) {
|
||||
const path = "/api/v1/server/UniProxy/config"
|
||||
r, err := c.client.R().Get(path)
|
||||
if err = c.checkResponse(r, path, err); err != nil {
|
||||
return
|
||||
}
|
||||
err = json.Unmarshal(r.Body(), &nodeInfo)
|
||||
err = json.Unmarshal(r.Body(), &node)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if c.etag == r.Header().Get("ETag") { // node info not changed
|
||||
return nil, nil
|
||||
}
|
||||
nodeInfo.NodeId = c.NodeId
|
||||
nodeInfo.NodeType = c.NodeType
|
||||
for i := range nodeInfo.Routes { // parse rules from routes
|
||||
if nodeInfo.Routes[i].Action == "block" {
|
||||
// parse common params
|
||||
node.Id = c.NodeId
|
||||
node.Type = c.NodeType
|
||||
common := CommonNodeRsp{}
|
||||
err = json.Unmarshal(r.Body(), &common)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode common params error: %s", err)
|
||||
}
|
||||
for i := range common.Routes { // parse rules from routes
|
||||
if common.Routes[i].Action == "block" {
|
||||
var matchs []string
|
||||
if _, ok := nodeInfo.Routes[i].Match.(string); ok {
|
||||
matchs = strings.Split(nodeInfo.Routes[i].Match.(string), ",")
|
||||
if _, ok := common.Routes[i].Match.(string); ok {
|
||||
matchs = strings.Split(common.Routes[i].Match.(string), ",")
|
||||
} else {
|
||||
matchs = nodeInfo.Routes[i].Match.([]string)
|
||||
matchs = common.Routes[i].Match.([]string)
|
||||
}
|
||||
for _, v := range matchs {
|
||||
nodeInfo.Rules = append(nodeInfo.Rules, regexp.MustCompile(v))
|
||||
node.Rules = append(node.Rules, regexp.MustCompile(v))
|
||||
}
|
||||
}
|
||||
}
|
||||
nodeInfo.Routes = nil
|
||||
nodeInfo.BaseConfig.PullInterval = intervalToTime(nodeInfo.BaseConfig.PullInterval)
|
||||
nodeInfo.BaseConfig.PushInterval = intervalToTime(nodeInfo.BaseConfig.PushInterval)
|
||||
node.PullInterval = intervalToTime(common.BaseConfig.PullInterval)
|
||||
node.PushInterval = intervalToTime(common.BaseConfig.PushInterval)
|
||||
// parse protocol params
|
||||
switch c.NodeType {
|
||||
case "v2ray":
|
||||
rsp := V2rayNodeRsp{}
|
||||
err = json.Unmarshal(r.Body(), &rsp)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode v2ray params error: %s", err)
|
||||
}
|
||||
node.Network = rsp.Network
|
||||
node.NetworkSettings = rsp.NetworkSettings
|
||||
node.ServerName = rsp.ServerName
|
||||
if rsp.Tls == 1 {
|
||||
node.Tls = true
|
||||
}
|
||||
case "shadowsocks":
|
||||
rsp := ShadowsocksNodeRsp{}
|
||||
err = json.Unmarshal(r.Body(), &rsp)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode v2ray params error: %s", err)
|
||||
}
|
||||
node.ServerKey = rsp.ServerKey
|
||||
node.Cipher = rsp.Cipher
|
||||
case "trojan":
|
||||
rsp := TrojanNodeRsp{}
|
||||
err = json.Unmarshal(r.Body(), &rsp)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode v2ray params error: %s", err)
|
||||
}
|
||||
case "hysteria":
|
||||
rsp := HysteriaNodeRsp{}
|
||||
err = json.Unmarshal(r.Body(), &rsp)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("decode v2ray params error: %s", err)
|
||||
}
|
||||
node.DownMbps = rsp.DownMbps
|
||||
node.UpMbps = rsp.UpMbps
|
||||
node.HyObfs = rsp.Obfs
|
||||
}
|
||||
c.etag = r.Header().Get("Etag")
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user