github.com/sagernet/sing-box@v1.9.0-rc.20/route/router_rule.go (about)

     1  package route
     2  
     3  import (
     4  	C "github.com/sagernet/sing-box/constant"
     5  	"github.com/sagernet/sing-box/option"
     6  	"github.com/sagernet/sing/common"
     7  )
     8  
     9  func hasRule(rules []option.Rule, cond func(rule option.DefaultRule) bool) bool {
    10  	for _, rule := range rules {
    11  		switch rule.Type {
    12  		case C.RuleTypeDefault:
    13  			if cond(rule.DefaultOptions) {
    14  				return true
    15  			}
    16  		case C.RuleTypeLogical:
    17  			if hasRule(rule.LogicalOptions.Rules, cond) {
    18  				return true
    19  			}
    20  		}
    21  	}
    22  	return false
    23  }
    24  
    25  func hasDNSRule(rules []option.DNSRule, cond func(rule option.DefaultDNSRule) bool) bool {
    26  	for _, rule := range rules {
    27  		switch rule.Type {
    28  		case C.RuleTypeDefault:
    29  			if cond(rule.DefaultOptions) {
    30  				return true
    31  			}
    32  		case C.RuleTypeLogical:
    33  			if hasDNSRule(rule.LogicalOptions.Rules, cond) {
    34  				return true
    35  			}
    36  		}
    37  	}
    38  	return false
    39  }
    40  
    41  func hasHeadlessRule(rules []option.HeadlessRule, cond func(rule option.DefaultHeadlessRule) bool) bool {
    42  	for _, rule := range rules {
    43  		switch rule.Type {
    44  		case C.RuleTypeDefault:
    45  			if cond(rule.DefaultOptions) {
    46  				return true
    47  			}
    48  		case C.RuleTypeLogical:
    49  			if hasHeadlessRule(rule.LogicalOptions.Rules, cond) {
    50  				return true
    51  			}
    52  		}
    53  	}
    54  	return false
    55  }
    56  
    57  func isGeoIPRule(rule option.DefaultRule) bool {
    58  	return len(rule.SourceGeoIP) > 0 && common.Any(rule.SourceGeoIP, notPrivateNode) || len(rule.GeoIP) > 0 && common.Any(rule.GeoIP, notPrivateNode)
    59  }
    60  
    61  func isGeoIPDNSRule(rule option.DefaultDNSRule) bool {
    62  	return len(rule.SourceGeoIP) > 0 && common.Any(rule.SourceGeoIP, notPrivateNode) || len(rule.GeoIP) > 0 && common.Any(rule.GeoIP, notPrivateNode)
    63  }
    64  
    65  func isGeositeRule(rule option.DefaultRule) bool {
    66  	return len(rule.Geosite) > 0
    67  }
    68  
    69  func isGeositeDNSRule(rule option.DefaultDNSRule) bool {
    70  	return len(rule.Geosite) > 0
    71  }
    72  
    73  func isProcessRule(rule option.DefaultRule) bool {
    74  	return len(rule.ProcessName) > 0 || len(rule.ProcessPath) > 0 || len(rule.PackageName) > 0 || len(rule.User) > 0 || len(rule.UserID) > 0
    75  }
    76  
    77  func isProcessDNSRule(rule option.DefaultDNSRule) bool {
    78  	return len(rule.ProcessName) > 0 || len(rule.ProcessPath) > 0 || len(rule.PackageName) > 0 || len(rule.User) > 0 || len(rule.UserID) > 0
    79  }
    80  
    81  func isProcessHeadlessRule(rule option.DefaultHeadlessRule) bool {
    82  	return len(rule.ProcessName) > 0 || len(rule.ProcessPath) > 0 || len(rule.PackageName) > 0
    83  }
    84  
    85  func notPrivateNode(code string) bool {
    86  	return code != "private"
    87  }
    88  
    89  func isWIFIRule(rule option.DefaultRule) bool {
    90  	return len(rule.WIFISSID) > 0 || len(rule.WIFIBSSID) > 0
    91  }
    92  
    93  func isWIFIDNSRule(rule option.DefaultDNSRule) bool {
    94  	return len(rule.WIFISSID) > 0 || len(rule.WIFIBSSID) > 0
    95  }
    96  
    97  func isWIFIHeadlessRule(rule option.DefaultHeadlessRule) bool {
    98  	return len(rule.WIFISSID) > 0 || len(rule.WIFIBSSID) > 0
    99  }
   100  
   101  func isIPCIDRHeadlessRule(rule option.DefaultHeadlessRule) bool {
   102  	return len(rule.IPCIDR) > 0 || rule.IPSet != nil
   103  }