Skip to content

Commit 55ea2ee

Browse files
committed
read domain matcher path form config
1 parent 3f9dc5d commit 55ea2ee

6 files changed

Lines changed: 48 additions & 31 deletions

File tree

app/router/config.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"strings"
88

99
"github.com/xtls/xray-core/common/errors"
10-
"github.com/xtls/xray-core/common/platform"
1110
"github.com/xtls/xray-core/common/platform/filesystem"
1211
"github.com/xtls/xray-core/features/outbound"
1312
"github.com/xtls/xray-core/features/routing"
@@ -33,7 +32,7 @@ func (r *Rule) Apply(ctx routing.Context) bool {
3332
return r.Condition.Apply(ctx)
3433
}
3534

36-
func (rr *RoutingRule) BuildCondition() (Condition, error) {
35+
func (rr *RoutingRule) BuildCondition(cachedDMPath string) (Condition, error) {
3736
conds := NewConditionChan()
3837

3938
if len(rr.InboundTag) > 0 {
@@ -110,22 +109,20 @@ func (rr *RoutingRule) BuildCondition() (Condition, error) {
110109
}
111110

112111
if len(rr.Domain) > 0 {
113-
useCachedMatcher := platform.NewEnvFlag(platform.UseCachedMatcher).GetValueAsInt(0)
114-
115112
var matcher *DomainMatcher
116113
var err error
117114

118115
domains := rr.Domain
119-
if runtime.GOOS != "windows" && runtime.GOOS != "wasm" && useCachedMatcher == 0 {
116+
if runtime.GOOS != "windows" && runtime.GOOS != "wasm" && cachedDMPath == "" {
120117
var err error
121118
domains, err = GetDomainList(rr.Domain)
122119
if err != nil {
123120
return nil, errors.New("failed to build domains from mmap").Base(err)
124121
}
125122
}
126123

127-
if useCachedMatcher != 0 {
128-
matcher, err = GetDomainMathcerWithRuleTag(rr.RuleTag)
124+
if cachedDMPath != "" {
125+
matcher, err = GetDomainMathcerWithRuleTag(cachedDMPath, rr.RuleTag)
129126
if err != nil {
130127
return nil, errors.New("failed to build domain condition from cached MphDomainMatcher").Base(err)
131128
}
@@ -276,15 +273,14 @@ func GetDomainList(domains []*Domain) ([]*Domain, error) {
276273
return domainList, nil
277274
}
278275

279-
func GetDomainMathcerWithRuleTag(ruleTag string) (*DomainMatcher, error) {
280-
file := "matcher.cache"
281-
bs, err := filesystem.ReadAsset(file)
276+
func GetDomainMathcerWithRuleTag(cachedDMPath string, ruleTag string) (*DomainMatcher, error) {
277+
bs, err := filesystem.ReadFile(cachedDMPath)
282278
if err != nil {
283-
return nil, errors.New("failed to load file: ", file).Base(err)
279+
return nil, errors.New("failed to load file: ", cachedDMPath).Base(err)
284280
}
285281
g, err := LoadGeoSiteMatcher(bs, ruleTag)
286282
if err != nil {
287-
return nil, errors.New("failed to load file:", file).Base(err)
283+
return nil, errors.New("failed to load file:", cachedDMPath).Base(err)
288284
}
289285
return &DomainMatcher{
290286
Matchers: g,

app/router/config.pb.go

Lines changed: 25 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/router/config.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,6 @@ message Config {
160160
DomainStrategy domain_strategy = 1;
161161
repeated RoutingRule rule = 2;
162162
repeated BalancingRule balancing_rule = 3;
163+
164+
string domainMatcherPath = 4;
163165
}

app/router/router.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func (r *Router) Init(ctx context.Context, config *Config, d dns.Client, ohm out
5555

5656
r.rules = make([]*Rule, 0, len(config.Rule))
5757
for _, rule := range config.Rule {
58-
cond, err := rule.BuildCondition()
58+
cond, err := rule.BuildCondition(config.DomainMatcherPath)
5959
if err != nil {
6060
return err
6161
}
@@ -129,7 +129,7 @@ func (r *Router) ReloadRules(config *Config, shouldAppend bool) error {
129129
if r.RuleExists(rule.GetRuleTag()) {
130130
return errors.New("duplicate ruleTag ", rule.GetRuleTag())
131131
}
132-
cond, err := rule.BuildCondition()
132+
cond, err := rule.BuildCondition(config.DomainMatcherPath)
133133
if err != nil {
134134
return err
135135
}

common/platform/platform.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ const (
1717
UseFreedomSplice = "xray.buf.splice"
1818
UseVmessPadding = "xray.vmess.padding"
1919
UseCone = "xray.cone.disabled"
20-
UseCachedMatcher = "xray.cached.matcher"
2120

2221
BufferSize = "xray.ray.buffer.size"
2322
BrowserDialerAddress = "xray.browser.dialer"

infra/conf/router.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ type RouterConfig struct {
7878
RuleList []json.RawMessage `json:"rules"`
7979
DomainStrategy *string `json:"domainStrategy"`
8080
Balancers []*BalancingRule `json:"balancers"`
81+
82+
DomainMatcherPath *string `json:"domainMatcherPath"`
8183
}
8284

8385
func (c *RouterConfig) getDomainStrategy() router.Config_DomainStrategy {
@@ -120,6 +122,14 @@ func (c *RouterConfig) Build() (*router.Config, error) {
120122
}
121123
config.BalancingRule = append(config.BalancingRule, balancer)
122124
}
125+
126+
if c.DomainMatcherPath != nil {
127+
path := *c.DomainMatcherPath
128+
if val := strings.Split(path, "assets:"); len(val) == 2 {
129+
path = platform.GetAssetLocation(val[1])
130+
}
131+
config.DomainMatcherPath = path
132+
}
123133
return config, nil
124134
}
125135

@@ -630,7 +640,7 @@ func (c *RouterConfig) BuildDomainMatcherCache() error {
630640
defer f.Close()
631641

632642
var buf bytes.Buffer
633-
println(len(geosite))
643+
634644
if err := router.SerializeGeoSiteList(geosite, &buf); err != nil {
635645
return err
636646
}

0 commit comments

Comments
 (0)