|
1 | 1 | package conf |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
5 | 8 | "runtime" |
6 | 9 | "strconv" |
7 | 10 | "strings" |
8 | 11 |
|
9 | 12 | "github.com/xtls/xray-core/app/router" |
10 | 13 | "github.com/xtls/xray-core/common/errors" |
11 | 14 | "github.com/xtls/xray-core/common/net" |
| 15 | + "github.com/xtls/xray-core/common/platform" |
12 | 16 | "github.com/xtls/xray-core/common/platform/filesystem" |
13 | 17 | "github.com/xtls/xray-core/common/serial" |
14 | 18 | "google.golang.org/protobuf/proto" |
@@ -597,3 +601,43 @@ func ParseRule(msg json.RawMessage) (*router.RoutingRule, error) { |
597 | 601 | } |
598 | 602 | return fieldrule, nil |
599 | 603 | } |
| 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