2022-08-16 01:04:33 -04:00
|
|
|
package panel
|
2022-06-04 00:05:46 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/goccy/go-json"
|
2023-05-17 21:11:28 -04:00
|
|
|
"reflect"
|
2022-06-04 00:05:46 -04:00
|
|
|
"regexp"
|
2022-12-18 10:31:06 -05:00
|
|
|
"strconv"
|
2023-01-06 12:59:24 -05:00
|
|
|
"strings"
|
2023-05-17 21:11:28 -04:00
|
|
|
"time"
|
2022-06-04 00:05:46 -04:00
|
|
|
)
|
|
|
|
|
2022-12-18 10:31:06 -05:00
|
|
|
type NodeInfo struct {
|
|
|
|
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"`
|
|
|
|
Rules []DestinationRule `json:"-"`
|
|
|
|
localNodeConfig `json:"-"`
|
|
|
|
}
|
|
|
|
type Route struct {
|
2023-01-06 12:59:24 -05:00
|
|
|
Id int `json:"id"`
|
|
|
|
Match interface{} `json:"match"`
|
|
|
|
Action string `json:"action"`
|
2022-12-18 10:31:06 -05:00
|
|
|
//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
|
|
|
}
|
|
|
|
type DestinationRule struct {
|
2022-06-04 00:05:46 -04:00
|
|
|
ID int
|
|
|
|
Pattern *regexp.Regexp
|
|
|
|
}
|
2022-12-18 10:31:06 -05:00
|
|
|
type localNodeConfig struct {
|
2023-05-15 21:15:29 -04:00
|
|
|
NodeId int
|
|
|
|
NodeType string
|
2022-06-04 00:05:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) GetNodeInfo() (nodeInfo *NodeInfo, err error) {
|
2022-12-18 10:31:06 -05:00
|
|
|
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
|
|
|
}
|
2022-12-18 10:31:06 -05:00
|
|
|
err = json.Unmarshal(r.Body(), &nodeInfo)
|
2022-06-04 00:05:46 -04:00
|
|
|
if err != nil {
|
2022-12-18 10:31:06 -05:00
|
|
|
return
|
2022-06-04 00:05:46 -04:00
|
|
|
}
|
2022-12-18 10:31:06 -05: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
|
|
|
}
|
2022-12-18 10:31:06 -05:00
|
|
|
nodeInfo.NodeId = c.NodeId
|
|
|
|
nodeInfo.NodeType = c.NodeType
|
|
|
|
for i := range nodeInfo.Routes { // parse rules from routes
|
2022-12-20 13:49:23 -05:00
|
|
|
if nodeInfo.Routes[i].Action == "block" {
|
2023-01-06 12:59:24 -05:00
|
|
|
var matchs []string
|
|
|
|
if _, ok := nodeInfo.Routes[i].Match.(string); ok {
|
|
|
|
matchs = strings.Split(nodeInfo.Routes[i].Match.(string), ",")
|
|
|
|
} else {
|
|
|
|
matchs = nodeInfo.Routes[i].Match.([]string)
|
|
|
|
}
|
|
|
|
for _, v := range matchs {
|
|
|
|
nodeInfo.Rules = append(nodeInfo.Rules, DestinationRule{
|
|
|
|
ID: nodeInfo.Routes[i].Id,
|
|
|
|
Pattern: regexp.MustCompile(v),
|
|
|
|
})
|
|
|
|
}
|
2022-06-04 00:05:46 -04:00
|
|
|
}
|
|
|
|
}
|
2022-12-18 10:31:06 -05:00
|
|
|
nodeInfo.Routes = nil
|
|
|
|
if _, ok := nodeInfo.BaseConfig.PullInterval.(int); !ok {
|
2023-05-17 21:11:28 -04:00
|
|
|
nodeInfo.BaseConfig.PullInterval = intervalToTime(nodeInfo.BaseConfig.PullInterval)
|
2022-06-04 00:05:46 -04:00
|
|
|
}
|
2022-12-18 10:31:06 -05:00
|
|
|
if _, ok := nodeInfo.BaseConfig.PushInterval.(int); !ok {
|
2023-05-17 21:11:28 -04:00
|
|
|
nodeInfo.BaseConfig.PushInterval = intervalToTime(nodeInfo.BaseConfig.PullInterval)
|
2022-06-06 03:29:01 -04:00
|
|
|
}
|
2022-12-18 10:31:06 -05:00
|
|
|
c.etag = r.Header().Get("Etag")
|
|
|
|
return
|
2022-06-04 00:05:46 -04:00
|
|
|
}
|
2023-05-17 21:11:28 -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
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|