github.com/yaling888/clash@v1.53.0/rule/parser.go (about)

     1  package rules
     2  
     3  import (
     4  	"fmt"
     5  
     6  	C "github.com/yaling888/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  	switch tp {
    16  	case "DOMAIN":
    17  		parsed = NewDomain(payload, target)
    18  	case "DOMAIN-SUFFIX":
    19  		parsed = NewDomainSuffix(payload, target)
    20  	case "DOMAIN-KEYWORD":
    21  		parsed = NewDomainKeyword(payload, target)
    22  	case "GEOSITE":
    23  		parsed, parseErr = NewGEOSITE(payload, target)
    24  	case "GEOIP":
    25  		noResolve := HasNoResolve(params)
    26  		parsed = NewGEOIP(payload, target, noResolve)
    27  	case "IP-CIDR", "IP-CIDR6":
    28  		noResolve := HasNoResolve(params)
    29  		parsed, parseErr = NewIPCIDR(payload, target, WithIPCIDRNoResolve(noResolve))
    30  	case "SRC-IP-CIDR":
    31  		parsed, parseErr = NewIPCIDR(payload, target, WithIPCIDRSourceIP(true), WithIPCIDRNoResolve(true))
    32  	case "SRC-PORT":
    33  		parsed, parseErr = NewPort(payload, target, PortTypeSrc)
    34  	case "DST-PORT":
    35  		parsed, parseErr = NewPort(payload, target, PortTypeDest)
    36  	case "INBOUND-PORT":
    37  		parsed, parseErr = NewPort(payload, target, PortTypeInbound)
    38  	case "PROCESS-NAME":
    39  		parsed, parseErr = NewProcess(payload, target, true)
    40  	case "PROCESS-PATH":
    41  		parsed, parseErr = NewProcess(payload, target, false)
    42  	case "SCRIPT":
    43  		parsed, parseErr = NewScript(payload, target)
    44  	case "USER-AGENT":
    45  		parsed, parseErr = NewUserAgent(payload, target)
    46  	case "IPSET":
    47  		noResolve := HasNoResolve(params)
    48  		parsed, parseErr = NewIPSet(payload, target, noResolve)
    49  	case "MATCH":
    50  		parsed = NewMatch(target)
    51  	default:
    52  		parseErr = fmt.Errorf("unsupported rule type %s", tp)
    53  	}
    54  
    55  	if parseErr != nil {
    56  		return nil, parseErr
    57  	}
    58  
    59  	ruleExtra := &C.RuleExtra{
    60  		Network:      findNetwork(params),
    61  		SourceIPs:    findSourceIPs(params),
    62  		ProcessNames: findProcessName(params),
    63  	}
    64  
    65  	parsed.SetRuleExtra(ruleExtra)
    66  
    67  	return parsed, nil
    68  }