@@ -37,11 +37,39 @@ func (r *BalancingRule) Build() (*router.BalancingRule, error) {
3737 }, nil
3838}
3939
40+ type RuleSet struct {
41+ Identifier string `json:"tag"`
42+ Rules []json.RawMessage `json:"rules"`
43+ }
44+
45+ func (r * RuleSet ) Build () (* router.RoutingRules , error ) {
46+ if r .Identifier == "" {
47+ return nil , newError ("empty rule set tag" )
48+ }
49+ if len (r .Rules ) == 0 {
50+ return nil , newError ("empty rule set" )
51+ }
52+
53+ ruleSet := new (router.RoutingRules )
54+ ruleSet .Identifier = r .Identifier
55+
56+ for _ , rr := range r .Rules {
57+ if rule , err := ParseRule (rr , true ); err != nil {
58+ return nil , err
59+ } else {
60+ ruleSet .Rules = append (ruleSet .Rules , rule )
61+ }
62+ }
63+
64+ return ruleSet , nil
65+ }
66+
4067type RouterConfig struct {
4168 Settings * RouterRulesConfig `json:"settings"` // Deprecated
4269 RuleList []json.RawMessage `json:"rules"`
4370 DomainStrategy * string `json:"domainStrategy"`
4471 Balancers []* BalancingRule `json:"balancers"`
72+ RuleSets []* RuleSet `json:"rule_sets"`
4573}
4674
4775func (c * RouterConfig ) getDomainStrategy () router.Config_DomainStrategy {
@@ -78,7 +106,7 @@ func (c *RouterConfig) Build() (*router.Config, error) {
78106 }
79107
80108 for _ , rawRule := range rawRuleList {
81- rule , err := ParseRule (rawRule )
109+ rule , err := ParseRule (rawRule , false )
82110 if err != nil {
83111 return nil , err
84112 }
@@ -91,6 +119,13 @@ func (c *RouterConfig) Build() (*router.Config, error) {
91119 }
92120 config .BalancingRule = append (config .BalancingRule , balancer )
93121 }
122+ for _ , rawRuleSet := range c .RuleSets {
123+ rawRuleSet , err := rawRuleSet .Build ()
124+ if err != nil {
125+ return nil , err
126+ }
127+ config .RuleSets = append (config .RuleSets , rawRuleSet )
128+ }
94129 return config , nil
95130}
96131
@@ -456,7 +491,7 @@ func toCidrList(ips StringList) ([]*router.GeoIP, error) {
456491 return geoipList , nil
457492}
458493
459- func parseFieldRule (msg json.RawMessage ) (* router.RoutingRule , error ) {
494+ func parseFieldRule (msg json.RawMessage , skipTargetCheck bool ) (* router.RoutingRule , error ) {
460495 type RawFieldRule struct {
461496 RouterRule
462497 Domain * StringList `json:"domain"`
@@ -469,6 +504,7 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
469504 InboundTag * StringList `json:"inboundTag"`
470505 Protocols * StringList `json:"protocol"`
471506 Attributes string `json:"attrs"`
507+ RuleSet string `json:"rule_set"`
472508 }
473509 rawFieldRule := new (RawFieldRule )
474510 err := json .Unmarshal (msg , rawFieldRule )
@@ -477,17 +513,20 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
477513 }
478514
479515 rule := new (router.RoutingRule )
480- switch {
481- case len (rawFieldRule .OutboundTag ) > 0 :
482- rule .TargetTag = & router.RoutingRule_Tag {
483- Tag : rawFieldRule .OutboundTag ,
484- }
485- case len (rawFieldRule .BalancerTag ) > 0 :
486- rule .TargetTag = & router.RoutingRule_BalancingTag {
487- BalancingTag : rawFieldRule .BalancerTag ,
516+
517+ if ! skipTargetCheck {
518+ switch {
519+ case len (rawFieldRule .OutboundTag ) > 0 :
520+ rule .TargetTag = & router.RoutingRule_Tag {
521+ Tag : rawFieldRule .OutboundTag ,
522+ }
523+ case len (rawFieldRule .BalancerTag ) > 0 :
524+ rule .TargetTag = & router.RoutingRule_BalancingTag {
525+ BalancingTag : rawFieldRule .BalancerTag ,
526+ }
527+ default :
528+ return nil , newError ("neither outboundTag nor balancerTag is specified in routing rule" )
488529 }
489- default :
490- return nil , newError ("neither outboundTag nor balancerTag is specified in routing rule" )
491530 }
492531
493532 if rawFieldRule .Domain != nil {
@@ -550,17 +589,19 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
550589 rule .Attributes = rawFieldRule .Attributes
551590 }
552591
592+ rule .RuleSet = rawFieldRule .RuleSet
593+
553594 return rule , nil
554595}
555596
556- func ParseRule (msg json.RawMessage ) (* router.RoutingRule , error ) {
597+ func ParseRule (msg json.RawMessage , skipTargetCheck bool ) (* router.RoutingRule , error ) {
557598 rawRule := new (RouterRule )
558599 err := json .Unmarshal (msg , rawRule )
559600 if err != nil {
560601 return nil , newError ("invalid router rule" ).Base (err )
561602 }
562603 if rawRule .Type == "field" {
563- fieldrule , err := parseFieldRule (msg )
604+ fieldrule , err := parseFieldRule (msg , skipTargetCheck )
564605 if err != nil {
565606 return nil , newError ("invalid field rule" ).Base (err )
566607 }
0 commit comments