Skip to content

Commit 3f9dc5d

Browse files
committed
add BuildDomainMatcherCache to RouterConfig
1 parent c6a90d2 commit 3f9dc5d

2 files changed

Lines changed: 46 additions & 2 deletions

File tree

app/router/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (rr *RoutingRule) BuildCondition() (Condition, error) {
118118
domains := rr.Domain
119119
if runtime.GOOS != "windows" && runtime.GOOS != "wasm" && useCachedMatcher == 0 {
120120
var err error
121-
domains, err = getDomainList(rr.Domain)
121+
domains, err = GetDomainList(rr.Domain)
122122
if err != nil {
123123
return nil, errors.New("failed to build domains from mmap").Base(err)
124124
}
@@ -229,7 +229,7 @@ func GetGeoIPList(ips []*GeoIP) ([]*GeoIP, error) {
229229

230230
}
231231

232-
func getDomainList(domains []*Domain) ([]*Domain, error) {
232+
func GetDomainList(domains []*Domain) ([]*Domain, error) {
233233
domainList := []*Domain{}
234234
for _, domain := range domains {
235235
val := strings.Split(domain.Value, "_")

infra/conf/router.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package conf
22

33
import (
4+
"bytes"
45
"encoding/json"
6+
"fmt"
7+
"os"
58
"runtime"
69
"strconv"
710
"strings"
811

912
"github.com/xtls/xray-core/app/router"
1013
"github.com/xtls/xray-core/common/errors"
1114
"github.com/xtls/xray-core/common/net"
15+
"github.com/xtls/xray-core/common/platform"
1216
"github.com/xtls/xray-core/common/platform/filesystem"
1317
"github.com/xtls/xray-core/common/serial"
1418
"google.golang.org/protobuf/proto"
@@ -597,3 +601,43 @@ func ParseRule(msg json.RawMessage) (*router.RoutingRule, error) {
597601
}
598602
return fieldrule, nil
599603
}
604+
605+
func (c *RouterConfig) BuildDomainMatcherCache() error {
606+
var geosite []*router.GeoSite
607+
608+
matcherFilePath := platform.GetAssetLocation("matcher.cache")
609+
routerConfig, err := c.Build()
610+
611+
if len(routerConfig.Rule) == 0 {
612+
return fmt.Errorf("no routing")
613+
}
614+
615+
for _, rule := range routerConfig.Rule {
616+
domains, err := router.GetDomainList(rule.Domain)
617+
if err != nil {
618+
return errors.New("failed to build domains from mmap").Base(err)
619+
}
620+
// write it with ruleTag key
621+
simpleGeoSite := router.GeoSite{CountryCode: rule.RuleTag, Domain: domains}
622+
623+
geosite = append(geosite, &simpleGeoSite)
624+
}
625+
626+
f, err := os.Create(matcherFilePath)
627+
if err != nil {
628+
return err
629+
}
630+
defer f.Close()
631+
632+
var buf bytes.Buffer
633+
println(len(geosite))
634+
if err := router.SerializeGeoSiteList(geosite, &buf); err != nil {
635+
return err
636+
}
637+
638+
if _, err := f.Write(buf.Bytes()); err != nil {
639+
return err
640+
}
641+
642+
return nil
643+
}

0 commit comments

Comments
 (0)