Skip to content

Commit bb56c9f

Browse files
committed
Add template.start_rules
1 parent a335a10 commit bb56c9f

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

option/template.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type _Template struct {
5858

5959
// Route
6060
DisableDefaultRules bool `json:"disable_default_rules,omitempty"`
61+
StartRules []option.Rule `json:"start_rules,omitempty"`
6162
PreRules []option.Rule `json:"pre_rules,omitempty"`
6263
CustomRules []option.Rule `json:"custom_rules,omitempty"`
6364
EnableJSDelivr bool `json:"enable_jsdelivr,omitempty"`

template/render_route.go

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
func (t *Template) renderRoute(metadata M.Metadata, options *option.Options) error {
1212
if options.Route == nil {
1313
options.Route = &option.RouteOptions{
14+
Rules: t.StartRules,
1415
RuleSet: t.renderRuleSet(t.CustomRuleSet),
1516
}
1617
}
@@ -19,51 +20,48 @@ func (t *Template) renderRoute(metadata M.Metadata, options *option.Options) err
1920
}
2021
disableRuleAction := t.DisableRuleAction || (metadata.Version != nil && metadata.Version.LessThan(semver.ParseVersion("1.11.0-alpha.7")))
2122
if disableRuleAction {
22-
options.Route.Rules = []option.Rule{
23-
{
24-
Type: C.RuleTypeLogical,
25-
LogicalOptions: option.LogicalRule{
26-
RawLogicalRule: option.RawLogicalRule{
27-
Mode: C.LogicalTypeOr,
28-
Rules: []option.Rule{
29-
{
30-
Type: C.RuleTypeDefault,
31-
DefaultOptions: option.DefaultRule{
32-
RawDefaultRule: option.RawDefaultRule{
33-
Port: []uint16{53},
34-
},
23+
options.Route.Rules = append(options.Route.Rules, option.Rule{
24+
Type: C.RuleTypeLogical,
25+
LogicalOptions: option.LogicalRule{
26+
RawLogicalRule: option.RawLogicalRule{
27+
Mode: C.LogicalTypeOr,
28+
Rules: []option.Rule{
29+
{
30+
Type: C.RuleTypeDefault,
31+
DefaultOptions: option.DefaultRule{
32+
RawDefaultRule: option.RawDefaultRule{
33+
Port: []uint16{53},
3534
},
3635
},
37-
{
38-
Type: C.RuleTypeDefault,
39-
DefaultOptions: option.DefaultRule{
40-
RawDefaultRule: option.RawDefaultRule{
41-
Protocol: []string{C.ProtocolDNS},
42-
},
36+
},
37+
{
38+
Type: C.RuleTypeDefault,
39+
DefaultOptions: option.DefaultRule{
40+
RawDefaultRule: option.RawDefaultRule{
41+
Protocol: []string{C.ProtocolDNS},
4342
},
4443
},
4544
},
4645
},
47-
RuleAction: option.RuleAction{
48-
Action: C.RuleActionTypeRoute,
49-
RouteOptions: option.RouteActionOptions{
50-
Outbound: DNSTag,
51-
},
46+
},
47+
RuleAction: option.RuleAction{
48+
Action: C.RuleActionTypeRoute,
49+
RouteOptions: option.RouteActionOptions{
50+
Outbound: DNSTag,
5251
},
5352
},
5453
},
55-
}
54+
})
5655
} else {
57-
options.Route.Rules = []option.Rule{
58-
{
59-
Type: C.RuleTypeDefault,
60-
DefaultOptions: option.DefaultRule{
61-
RuleAction: option.RuleAction{
62-
Action: C.RuleActionTypeSniff,
63-
},
56+
options.Route.Rules = append(options.Route.Rules, option.Rule{
57+
Type: C.RuleTypeDefault,
58+
DefaultOptions: option.DefaultRule{
59+
RuleAction: option.RuleAction{
60+
Action: C.RuleActionTypeSniff,
6461
},
6562
},
66-
{
63+
},
64+
option.Rule{
6765
Type: C.RuleTypeLogical,
6866
LogicalOptions: option.LogicalRule{
6967
RawLogicalRule: option.RawLogicalRule{
@@ -91,8 +89,7 @@ func (t *Template) renderRoute(metadata M.Metadata, options *option.Options) err
9189
Action: C.RuleActionTypeHijackDNS,
9290
},
9391
},
94-
},
95-
}
92+
})
9693
}
9794
directTag := t.DirectTag
9895
defaultTag := t.DefaultTag

0 commit comments

Comments
 (0)