-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingbox_rules.go
More file actions
112 lines (105 loc) · 2.93 KB
/
singbox_rules.go
File metadata and controls
112 lines (105 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package common
import (
"strings"
"time"
"github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common/json/badoption"
)
// ToOptions converts SmartRoutingRules to sing-box options, including required outbounds, rules, and rulesets.
func (srs SmartRoutingRules) ToOptions(urlTestInterval, urlTestIdleTimeout time.Duration) ([]option.Outbound, []option.Rule, []option.RuleSet) {
outbounds := []option.Outbound{}
rules := []option.Rule{}
rulesets := []option.RuleSet{}
for _, sr := range srs {
rs := sr.RuleSets.ToOptions()
tags := sr.RuleSets.Tags()
detour := "sr-" + sr.Category
if len(sr.Outbounds) == 1 && sr.Outbounds[0] == "direct" {
detour = "direct"
}
rule := option.Rule{
Type: constant.RuleTypeDefault,
DefaultOptions: option.DefaultRule{
RawDefaultRule: option.RawDefaultRule{
RuleSet: tags,
},
RuleAction: option.RuleAction{
Action: constant.RuleActionTypeRoute,
RouteOptions: option.RouteActionOptions{
Outbound: detour,
},
},
},
}
rules = append(rules, rule)
rulesets = append(rulesets, rs...)
if detour != "direct" {
outbounds = append(outbounds, option.Outbound{
Type: constant.TypeURLTest,
Tag: detour,
Options: &option.URLTestOutboundOptions{
Outbounds: sr.Outbounds,
URL: "https://google.com/generate_204",
Interval: badoption.Duration(urlTestInterval),
IdleTimeout: badoption.Duration(urlTestIdleTimeout),
},
})
}
}
return outbounds, rules, rulesets
}
// ToOptions converts AdBlockRules to sing-box options, including the rule and rulesets.
func (ab AdBlockRules) ToOptions() (option.Rule, []option.RuleSet) {
rulesets := (RuleSets)(ab).ToOptions()
tags := (RuleSets)(ab).Tags()
rule := option.Rule{
Type: constant.RuleTypeDefault,
DefaultOptions: option.DefaultRule{
RawDefaultRule: option.RawDefaultRule{
RuleSet: tags,
},
RuleAction: option.RuleAction{
Action: constant.RuleActionTypeReject,
},
},
}
return rule, rulesets
}
// Tags returns the tags of all RuleSets.
func (rs RuleSets) Tags() []string {
tags := []string{}
for _, rule := range rs {
tags = append(tags, rule.Tag)
}
return tags
}
// ToOptions converts RuleSets to sing-box RuleSet options.
func (rs RuleSets) ToOptions() []option.RuleSet {
rulesets := []option.RuleSet{}
for _, rule := range rs {
format := rule.Format
if format == "" {
if strings.HasSuffix(rule.URL, ".srs") {
format = constant.RuleSetFormatBinary
} else {
format = constant.RuleSetFormatSource
}
}
detour := rule.DownloadDetour
if detour == "" {
detour = "direct"
}
rulesets = append(rulesets, option.RuleSet{
Type: constant.RuleSetTypeRemote,
Tag: rule.Tag,
Format: format,
RemoteOptions: option.RemoteRuleSet{
URL: rule.URL,
DownloadDetour: detour,
UpdateInterval: badoption.Duration(24 * time.Hour),
},
})
}
return rulesets
}