github.com/igoogolx/clash@v1.19.8/rule/parser.go (about)

     1  package rules
     2  
     3  import (
     4  	"fmt"
     5  
     6  	C "github.com/igoogolx/clash/constant"
     7  )
     8  
     9  func ParseRule(tp, payload, target string, params []string) (C.Rule, error) {
    10  	var (
    11  		parseErr error
    12  		parsed   C.Rule
    13  	)
    14  
    15  	ruleConfigType := C.RuleConfig(tp)
    16  
    17  	switch ruleConfigType {
    18  	case C.RuleConfigDomain:
    19  		parsed = NewDomain(payload, target)
    20  	case C.RuleConfigDomainSuffix:
    21  		parsed = NewDomainSuffix(payload, target)
    22  	case C.RuleConfigDomainKeyword:
    23  		parsed = NewDomainKeyword(payload, target)
    24  	case C.RuleConfigGeoIP:
    25  		noResolve := HasNoResolve(params)
    26  		parsed = NewGEOIP(payload, target, noResolve)
    27  	case C.RuleConfigIPCIDR, C.RuleConfigIPCIDR6:
    28  		noResolve := HasNoResolve(params)
    29  		parsed, parseErr = NewIPCIDR(payload, target, WithIPCIDRNoResolve(noResolve))
    30  	case C.RuleConfigSrcIPCIDR:
    31  		parsed, parseErr = NewIPCIDR(payload, target, WithIPCIDRSourceIP(true), WithIPCIDRNoResolve(true))
    32  	case C.RuleConfigSrcPort:
    33  		parsed, parseErr = NewPort(payload, target, PortTypeSrc)
    34  	case C.RuleConfigDstPort:
    35  		parsed, parseErr = NewPort(payload, target, PortTypeDest)
    36  	case C.RuleConfigInboundPort:
    37  		parsed, parseErr = NewPort(payload, target, PortTypeInbound)
    38  	case C.RuleConfigProcessName:
    39  		parsed, parseErr = NewProcess(payload, target, true)
    40  	case C.RuleConfigProcessPath:
    41  		parsed, parseErr = NewProcess(payload, target, false)
    42  	case C.RuleConfigIPSet:
    43  		noResolve := HasNoResolve(params)
    44  		parsed, parseErr = NewIPSet(payload, target, noResolve)
    45  	case C.RuleConfigMatch:
    46  		parsed = NewMatch(target)
    47  	case C.RuleConfigRuleSet, C.RuleConfigScript:
    48  		parseErr = fmt.Errorf("unsupported rule type %s", tp)
    49  	default:
    50  		parseErr = fmt.Errorf("unsupported rule type %s", tp)
    51  	}
    52  
    53  	return parsed, parseErr
    54  }