update: support v2board v1.7.2 routes structure

After update this version, the v2board version must >= 1.7.2 release
This commit is contained in:
Senis John 2022-12-26 12:41:18 +08:00
parent 0c2d03f3e0
commit d44fa11eee
No known key found for this signature in database
GPG Key ID: 845E9E4727C3E1A4
2 changed files with 25 additions and 27 deletions

View File

@ -1 +1,8 @@
package newV2board
type route struct {
Id int `json:"id"`
Match []string `json:"match"`
Action string `json:"action"`
ActionValue *string `json:"action_value"`
}

View File

@ -257,30 +257,18 @@ func (c *APIClient) ReportUserTraffic(userTraffic *[]api.UserTraffic) error {
// GetNodeRule implements the API interface
func (c *APIClient) GetNodeRule() (*[]api.DetectRule, error) {
var routes []route
b, _ := c.resp.Load().(*simplejson.Json).Get("routes").MarshalJSON()
json.Unmarshal(b, &routes)
ruleList := c.LocalRuleList
nodeInfoResponse := c.resp.Load().(*simplejson.Json)
for i, rule := range nodeInfoResponse.Get("routes").MustArray() {
r := rule.(map[string]any)
if r["action"] == "block" {
// todo remove at 2023.6.1, compatible with 1.7.1
switch r["match"].(type) {
case []any:
var s []string
for i := range r["match"].([]any) {
s = append(s, r["match"].([]any)[i].(string))
}
ruleList = append(ruleList, api.DetectRule{
ID: i,
Pattern: regexp.MustCompile(strings.Join(s, "|")),
})
case string:
s := r["match"].(string)
ruleList = append(ruleList, api.DetectRule{
ID: i,
Pattern: regexp.MustCompile(strings.ReplaceAll(s, ",", "|")),
})
}
for i := range routes {
if routes[i].Action == "block" {
ruleList = append(ruleList, api.DetectRule{
ID: i,
Pattern: regexp.MustCompile(strings.Join(routes[i].Match, "|")),
})
}
}
@ -409,12 +397,15 @@ func (c *APIClient) parseV2rayNodeResponse(nodeInfoResponse *simplejson.Json) (*
}
func parseDNSConfig(nodeInfoResponse *simplejson.Json) (nameServerList []*conf.NameServerConfig) {
for _, rule := range nodeInfoResponse.Get("routes").MustArray() {
r := rule.(map[string]any)
if r["action"] == "dns" {
var routes []route
b, _ := nodeInfoResponse.Get("routes").MarshalJSON()
json.Unmarshal(b, &routes)
for i := range routes {
if routes[i].Action == "dns" {
nameServerList = append(nameServerList, &conf.NameServerConfig{
Address: &conf.Address{Address: net.ParseAddress(r["action_value"].(string))},
Domains: strings.Split(r["match"].(string), ","),
Address: &conf.Address{Address: net.ParseAddress(*routes[i].ActionValue)},
Domains: routes[i].Match,
})
}
}