github.com/metacubex/mihomo@v1.18.5/rules/parser.go (about)

     1  package rules
     2  
     3  import (
     4  	"fmt"
     5  
     6  	C "github.com/metacubex/mihomo/constant"
     7  	RC "github.com/metacubex/mihomo/rules/common"
     8  	"github.com/metacubex/mihomo/rules/logic"
     9  	RP "github.com/metacubex/mihomo/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, false)
    54  	case "PROCESS-PATH":
    55  		parsed, parseErr = RC.NewProcess(payload, target, false, false)
    56  	case "PROCESS-NAME-REGEX":
    57  		parsed, parseErr = RC.NewProcess(payload, target, true, true)
    58  	case "PROCESS-PATH-REGEX":
    59  		parsed, parseErr = RC.NewProcess(payload, target, false, true)
    60  	case "NETWORK":
    61  		parsed, parseErr = RC.NewNetworkType(payload, target)
    62  	case "UID":
    63  		parsed, parseErr = RC.NewUid(payload, target)
    64  	case "IN-TYPE":
    65  		parsed, parseErr = RC.NewInType(payload, target)
    66  	case "IN-USER":
    67  		parsed, parseErr = RC.NewInUser(payload, target)
    68  	case "IN-NAME":
    69  		parsed, parseErr = RC.NewInName(payload, target)
    70  	case "SUB-RULE":
    71  		parsed, parseErr = logic.NewSubRule(payload, target, subRules, ParseRule)
    72  	case "AND":
    73  		parsed, parseErr = logic.NewAND(payload, target, ParseRule)
    74  	case "OR":
    75  		parsed, parseErr = logic.NewOR(payload, target, ParseRule)
    76  	case "NOT":
    77  		parsed, parseErr = logic.NewNOT(payload, target, ParseRule)
    78  	case "RULE-SET":
    79  		noResolve := RC.HasNoResolve(params)
    80  		parsed, parseErr = RP.NewRuleSet(payload, target, noResolve)
    81  	case "MATCH":
    82  		parsed = RC.NewMatch(target)
    83  		parseErr = nil
    84  	default:
    85  		parseErr = fmt.Errorf("unsupported rule type %s", tp)
    86  	}
    87  
    88  	if parseErr != nil {
    89  		return nil, parseErr
    90  	}
    91  
    92  	return
    93  }