V2bX/api/panel/node.go

156 lines
3.9 KiB
Go
Raw Normal View History

package panel
2022-06-04 00:05:46 -04:00
import (
"fmt"
2022-06-04 00:05:46 -04:00
"github.com/goccy/go-json"
"reflect"
2022-06-04 00:05:46 -04:00
"regexp"
"strconv"
2023-01-06 12:59:24 -05:00
"strings"
"time"
2022-06-04 00:05:46 -04:00
)
type CommonNodeRsp struct {
2023-06-09 00:36:49 -04:00
Host string `json:"host"`
ServerPort int `json:"server_port"`
2023-06-09 00:36:49 -04:00
ServerName string `json:"server_name"`
Routes []Route `json:"routes"`
BaseConfig BaseConfig `json:"base_config"`
}
type Route struct {
2023-01-06 12:59:24 -05:00
Id int `json:"id"`
Match interface{} `json:"match"`
Action string `json:"action"`
//ActionValue interface{} `json:"action_value"`
}
type BaseConfig struct {
PushInterval any `json:"push_interval"`
PullInterval any `json:"pull_interval"`
2022-09-14 21:24:14 -04:00
}
2022-06-04 00:05:46 -04:00
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 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
2023-06-09 00:36:49 -04:00
Host string
Port int
Network string
NetworkSettings json.RawMessage
Tls bool
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
2022-06-04 00:05:46 -04:00
}
if c.etag == r.Header().Get("ETag") { // node info not changed
2022-09-14 21:24:14 -04:00
return nil, nil
2022-06-04 00:05:46 -04:00
}
// parse common params
2023-06-09 00:36:49 -04:00
node = &NodeInfo{
Id: c.NodeId,
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" {
2023-01-06 12:59:24 -05:00
var matchs []string
if _, ok := common.Routes[i].Match.(string); ok {
matchs = strings.Split(common.Routes[i].Match.(string), ",")
2023-01-06 12:59:24 -05:00
} else {
matchs = common.Routes[i].Match.([]string)
2023-01-06 12:59:24 -05:00
}
for _, v := range matchs {
node.Rules = append(node.Rules, regexp.MustCompile(v))
2023-01-06 12:59:24 -05:00
}
2022-06-04 00:05:46 -04:00
}
}
2023-06-09 00:36:49 -04:00
node.ServerName = common.ServerName
node.Host = common.Host
node.Port = common.ServerPort
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":
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
}
2023-06-20 05:08:36 -04:00
c.etag = r.Header().Get("ETag")
return
2022-06-04 00:05:46 -04:00
}
func intervalToTime(i interface{}) time.Duration {
switch reflect.TypeOf(i).Kind() {
case reflect.Int:
return time.Duration(i.(int)) * time.Second
case reflect.String:
i, _ := strconv.Atoi(i.(string))
return time.Duration(i) * time.Second
case reflect.Float64:
return time.Duration(i.(float64)) * time.Second
2023-05-19 00:38:16 -04:00
default:
return time.Duration(reflect.ValueOf(i).Int()) * time.Second
}
}