support v2board 1.7.2 rules

This commit is contained in:
yuzuki999 2023-01-07 01:59:24 +08:00
parent 97779326d6
commit 0beba17baa

View File

@ -4,6 +4,7 @@ import (
"github.com/goccy/go-json" "github.com/goccy/go-json"
"regexp" "regexp"
"strconv" "strconv"
"strings"
) )
type NodeInfo struct { type NodeInfo struct {
@ -21,9 +22,9 @@ type NodeInfo struct {
localNodeConfig `json:"-"` localNodeConfig `json:"-"`
} }
type Route struct { type Route struct {
Id int `json:"id"` Id int `json:"id"`
Match string `json:"match"` Match interface{} `json:"match"`
Action string `json:"action"` Action string `json:"action"`
//ActionValue interface{} `json:"action_value"` //ActionValue interface{} `json:"action_value"`
} }
type BaseConfig struct { type BaseConfig struct {
@ -61,10 +62,18 @@ func (c *Client) GetNodeInfo() (nodeInfo *NodeInfo, err error) {
nodeInfo.NodeType = c.NodeType nodeInfo.NodeType = c.NodeType
for i := range nodeInfo.Routes { // parse rules from routes for i := range nodeInfo.Routes { // parse rules from routes
if nodeInfo.Routes[i].Action == "block" { if nodeInfo.Routes[i].Action == "block" {
nodeInfo.Rules = append(nodeInfo.Rules, DestinationRule{ var matchs []string
ID: nodeInfo.Routes[i].Id, if _, ok := nodeInfo.Routes[i].Match.(string); ok {
Pattern: regexp.MustCompile(nodeInfo.Routes[i].Match), 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),
})
}
} }
} }
nodeInfo.Routes = nil nodeInfo.Routes = nil