github.com/kelleygo/clashcore@v1.0.2/rules/parser.go (about)

     1  package rules
     2  
     3  import (
     4  	"fmt"
     5  
     6  	C "github.com/kelleygo/clashcore/constant"
     7  	RC "github.com/kelleygo/clashcore/rules/common"
     8  	"github.com/kelleygo/clashcore/rules/logic"
     9  	RP "github.com/kelleygo/clashcore/rules/provider"
    10  )
    11  
    12  func ParseRule(tp, payload, target string, params []string, subRules map[string][]C.Rule) (parsed C.Rule, parseErr error) {
    13  	switch tp {
    14  	case "DOMAIN":
    15  		parsed = RC.NewDomain(payload, target)
    16  	case "DOMAIN-SUFFIX":
    17  		parsed = RC.NewDomainSuffix(payload, target)
    18  	case "DOMAIN-KEYWORD":
    19  		parsed = RC.NewDomainKeyword(payload, target)
    20  	case "DOMAIN-REGEX":
    21  		parsed, parseErr = RC.NewDomainRegex(payload, target)
    22  	case "GEOSITE":
    23  		parsed, parseErr = RC.NewGEOSITE(payload, target)
    24  	case "GEOIP":
    25  		noResolve := RC.HasNoResolve(params)
    26  		parsed, parseErr = RC.NewGEOIP(payload, target, false, noResolve)
    27  	case "SRC-GEOIP":
    28  		parsed, parseErr = RC.NewGEOIP(payload, target, true, true)
    29  	case "IP-ASN":
    30  		noResolve := RC.HasNoResolve(params)
    31  		parsed, parseErr = RC.NewIPASN(payload, target, false, noResolve)
    32  	case "SRC-IP-ASN":
    33  		parsed, parseErr = RC.NewIPASN(payload, target, true, true)
    34  	case "IP-CIDR", "IP-CIDR6":
    35  		noResolve := RC.HasNoResolve(params)
    36  		parsed, parseErr = RC.NewIPCIDR(payload, target, RC.WithIPCIDRNoResolve(noResolve))
    37  	case "SRC-IP-CIDR":
    38  		parsed, parseErr = RC.NewIPCIDR(payload, target, RC.WithIPCIDRSourceIP(true), RC.WithIPCIDRNoResolve(true))
    39  	case "IP-SUFFIX":
    40  		noResolve := RC.HasNoResolve(params)
    41  		parsed, parseErr = RC.NewIPSuffix(payload, target, false, noResolve)
    42  	case "SRC-IP-SUFFIX":
    43  		parsed, parseErr = RC.NewIPSuffix(payload, target, true, true)
    44  	case "SRC-PORT":
    45  		parsed, parseErr = RC.NewPort(payload, target, C.SrcPort)
    46  	case "DST-PORT":
    47  		parsed, parseErr = RC.NewPort(payload, target, C.DstPort)
    48  	case "IN-PORT":
    49  		parsed, parseErr = RC.NewPort(payload, target, C.InPort)
    50  	case "DSCP":
    51  		parsed, parseErr = RC.NewDSCP(payload, target)
    52  	case "PROCESS-NAME":
    53  		parsed, parseErr = RC.NewProcess(payload, target, true)
    54  	case "PROCESS-PATH":
    55  		parsed, parseErr = RC.NewProcess(payload, target, false)
    56  	case "NETWORK":
    57  		parsed, parseErr = RC.NewNetworkType(payload, target)
    58  	case "UID":
    59  		parsed, parseErr = RC.NewUid(payload, target)
    60  	case "IN-TYPE":
    61  		parsed, parseErr = RC.NewInType(payload, target)
    62  	case "IN-USER":
    63  		parsed, parseErr = RC.NewInUser(payload, target)
    64  	case "IN-NAME":
    65  		parsed, parseErr = RC.NewInName(payload, target)
    66  	case "SUB-RULE":
    67  		parsed, parseErr = logic.NewSubRule(payload, target, subRules, ParseRule)
    68  	case "AND":
    69  		parsed, parseErr = logic.NewAND(payload, target, ParseRule)
    70  	case "OR":
    71  		parsed, parseErr = logic.NewOR(payload, target, ParseRule)
    72  	case "NOT":
    73  		parsed, parseErr = logic.NewNOT(payload, target, ParseRule)
    74  	case "RULE-SET":
    75  		noResolve := RC.HasNoResolve(params)
    76  		parsed, parseErr = RP.NewRuleSet(payload, target, noResolve)
    77  	case "MATCH":
    78  		parsed = RC.NewMatch(target)
    79  		parseErr = nil
    80  	default:
    81  		parseErr = fmt.Errorf("unsupported rule type %s", tp)
    82  	}
    83  
    84  	if parseErr != nil {
    85  		return nil, parseErr
    86  	}
    87  
    88  	return
    89  }