Compare commits

...

4 Commits

Author SHA1 Message Date
A1lo f2778f49f0
Merge 6b25c7e500 into 51504c624c 2024-04-29 11:08:28 +08:00
dependabot[bot] 51504c624c Bump github.com/quic-go/quic-go from 0.42.0 to 0.43.0
Bumps [github.com/quic-go/quic-go](https://github.com/quic-go/quic-go) from 0.42.0 to 0.43.0.
- [Release notes](https://github.com/quic-go/quic-go/releases)
- [Changelog](https://github.com/quic-go/quic-go/blob/master/Changelog.md)
- [Commits](https://github.com/quic-go/quic-go/compare/v0.42.0...v0.43.0)

---
updated-dependencies:
- dependency-name: github.com/quic-go/quic-go
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-04-28 21:54:54 -04:00
Kobe Arthur Scofield 98a2e2c7a1
Adding notes for critical build file (#3308)
* Adding notes for critical build file

* Got trapped by Tabs and fix it
2024-04-28 19:13:59 -04:00
Allo 6b25c7e500
refactor: temporarily cache geo data 2024-04-21 21:16:14 +08:00
7 changed files with 158 additions and 65 deletions

View File

@ -1,5 +1,11 @@
name: Build and Release
# NOTE: This Github Actions file depends on the Makefile.
# Building the correct package requires the correct binaries generated by the Makefile. To
# ensure the correct output, the Makefile must accept the appropriate input and compile the
# correct file with the correct name. If you need to modify this file, please ensure it won't
# disrupt the Makefile.
on:
workflow_dispatch:
release:

View File

@ -2,6 +2,17 @@ NAME = xray
VERSION=$(shell git describe --always --dirty)
# NOTE: This MAKEFILE can be used to build Xray-core locally and in Automatic workflows. It is \
provided for convinience in automatic building and functions as a part of it.
# NOTE: If you need to modify this file, please be aware that:\
- This file is not the main Makefile; it only accepts environment variables and builds the \
binary.\
- Automatic building expects the correct binaries to be built by this Makefile. If you \
intend to propose a change to this Makefile, carefully review the file below and ensure \
that the change will not accidently break the automatic building:\
.github/workflows/release.yml \
Otherwise it is recommended to contact the project maintainers.
LDFLAGS = -X github.com/xtls/xray-core/core.build=$(VERSION) -s -w -buildid=
PARAMS = -trimpath -ldflags "$(LDFLAGS)" -v
MAIN = ./main

2
go.mod
View File

@ -12,7 +12,7 @@ require (
github.com/miekg/dns v1.1.59
github.com/pelletier/go-toml v1.9.5
github.com/pires/go-proxyproto v0.7.0
github.com/quic-go/quic-go v0.42.0
github.com/quic-go/quic-go v0.43.0
github.com/refraction-networking/utls v1.6.4
github.com/sagernet/sing v0.3.8
github.com/sagernet/sing-shadowsocks v0.2.6

4
go.sum
View File

@ -114,8 +114,8 @@ github.com/prometheus/client_golang v0.8.0/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXP
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/common v0.0.0-20180801064454-c7de2306084e/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/quic-go/quic-go v0.42.0 h1:uSfdap0eveIl8KXnipv9K7nlwZ5IqLlYOpJ58u5utpM=
github.com/quic-go/quic-go v0.42.0/go.mod h1:132kz4kL3F9vxhW3CtQJLDVwcFe5wdWeJXXijhsO57M=
github.com/quic-go/quic-go v0.43.0 h1:sjtsTKWX0dsHpuMJvLxGqoQdtgJnbAPWY+W+5vjYW/g=
github.com/quic-go/quic-go v0.43.0/go.mod h1:132kz4kL3F9vxhW3CtQJLDVwcFe5wdWeJXXijhsO57M=
github.com/refraction-networking/utls v1.6.4 h1:aeynTroaYn7y+mFtqv8D0bQ4bw0y9nJHneGxJ7lvRDM=
github.com/refraction-networking/utls v1.6.4/go.mod h1:2VL2xfiqgFAZtJKeUTlf+PSYFs3Eu7km0gCtXJ3m8zs=
github.com/riobard/go-bloom v0.0.0-20200614022211-cdc8013cb5b3 h1:f/FNXud6gA3MNr8meMVVGxhp+QBTqY91tM8HjEuMjGg=

View File

@ -73,8 +73,11 @@ func (c *NameServerConfig) Build() (*dns.NameServer, error) {
var domains []*dns.NameServer_PriorityDomain
var originalRules []*dns.NameServer_OriginalRule
cache := NewGeoCache()
defer cache.Clear()
for _, rule := range c.Domains {
parsedDomain, err := parseDomainRule(rule)
parsedDomain, err := cache.ParseDomainRule(rule)
if err != nil {
return nil, newError("invalid domain rule: ", rule).Base(err)
}
@ -91,7 +94,7 @@ func (c *NameServerConfig) Build() (*dns.NameServer, error) {
})
}
geoipList, err := ToCidrList(c.ExpectIPs)
geoipList, err := cache.ToCidrList(c.ExpectIPs)
if err != nil {
return nil, newError("invalid IP rule: ", c.ExpectIPs).Base(err)
}
@ -209,6 +212,9 @@ func (m *HostsWrapper) Build() ([]*dns.Config_HostMapping, error) {
}
sort.Strings(domains)
cache := newSiteCache()
defer cache.reset()
for _, domain := range domains {
switch {
case strings.HasPrefix(domain, "domain:"):
@ -226,7 +232,7 @@ func (m *HostsWrapper) Build() ([]*dns.Config_HostMapping, error) {
if len(listName) == 0 {
return nil, newError("empty geosite rule: ", domain)
}
geositeList, err := loadGeositeWithAttr("geosite.dat", listName)
geositeList, err := cache.loadGeositeWithAttr("geosite.dat", listName)
if err != nil {
return nil, newError("failed to load geosite: ", listName).Base(err)
}
@ -287,7 +293,7 @@ func (m *HostsWrapper) Build() ([]*dns.Config_HostMapping, error) {
}
filename := kv[0]
list := kv[1]
geositeList, err := loadGeositeWithAttr(filename, list)
geositeList, err := cache.loadGeositeWithAttr(filename, list)
if err != nil {
return nil, newError("failed to load domain list: ", list, " from ", filename).Base(err)
}

View File

@ -116,8 +116,11 @@ func (c *RouterConfig) Build() (*router.Config, error) {
}
}
cache := NewGeoCache()
defer cache.Clear()
for _, rawRule := range rawRuleList {
rule, err := ParseRule(rawRule)
rule, err := cache.ParseRule(rawRule)
if err != nil {
return nil, err
}
@ -195,39 +198,80 @@ func ParseIP(s string) (*router.CIDR, error) {
}
}
func loadGeoIP(code string) ([]*router.CIDR, error) {
return loadIP("geoip.dat", code)
}
var (
FileCache = make(map[string][]byte)
IPCache = make(map[string]*router.GeoIP)
SiteCache = make(map[string]*router.GeoSite)
)
func loadFile(file string) ([]byte, error) {
if FileCache[file] == nil {
bs, err := filesystem.ReadAsset(file)
if err != nil {
return nil, newError("failed to open file: ", file).Base(err)
}
if len(bs) == 0 {
return nil, newError("empty file: ", file)
}
// Do not cache file, may save RAM when there
// are many files, but consume CPU each time.
return bs, nil
FileCache[file] = bs
bs, err := filesystem.ReadAsset(file)
if err != nil {
return nil, newError("failed to open file: ", file).Base(err)
}
return FileCache[file], nil
if len(bs) == 0 {
return nil, newError("empty file: ", file)
}
return bs, nil
}
func loadIP(file, code string) ([]*router.CIDR, error) {
type ipCache struct {
fileContent map[string][]byte
geoIPs map[string]*router.GeoIP
}
type siteCache struct {
fileContent map[string][]byte
geoSites map[string]*router.GeoSite
}
// GeoCache is a cache for GeoIP and GeoSite.
type GeoCache struct {
ipCache *ipCache
siteCache *siteCache
}
// NewGeoCache creates a new GeoCache.
func NewGeoCache() *GeoCache {
return &GeoCache{
ipCache: newIPCache(),
siteCache: newSiteCache(),
}
}
// Clear clears the cache.
func (c *GeoCache) Clear() {
if c.ipCache.reset() || c.siteCache.reset() {
// only trigger GC if there is something to release
runtime.GC()
}
}
// newIPCache creates a new ipCache.
// after use, call close() to release memory.
func newIPCache() *ipCache {
return &ipCache{
fileContent: make(map[string][]byte),
geoIPs: make(map[string]*router.GeoIP),
}
}
// reset remove the content of ipCache.
// returns true if the cache is not empty.
func (c *ipCache) reset() bool {
if len(c.fileContent) == 0 && len(c.geoIPs) == 0 {
return false
}
c.fileContent = make(map[string][]byte)
c.geoIPs = make(map[string]*router.GeoIP)
return true
}
func (c *ipCache) loadIP(file, code string) ([]*router.CIDR, error) {
index := file + ":" + code
if IPCache[index] == nil {
bs, err := loadFile(file)
if err != nil {
return nil, newError("failed to load file: ", file).Base(err)
if c.geoIPs[index] == nil {
bs := c.fileContent[file]
if bs == nil {
var err error
bs, err = loadFile(file)
if err != nil {
return nil, newError("failed to load file: ", file).Base(err)
}
c.fileContent[file] = bs
}
bs = find(bs, []byte(code))
if bs == nil {
@ -237,19 +281,43 @@ func loadIP(file, code string) ([]*router.CIDR, error) {
if err := proto.Unmarshal(bs, &geoip); err != nil {
return nil, newError("error unmarshal IP in ", file, ": ", code).Base(err)
}
defer runtime.GC() // or debug.FreeOSMemory()
return geoip.Cidr, nil // do not cache geoip
IPCache[index] = &geoip
c.geoIPs[index] = &geoip
return geoip.Cidr, nil
}
return IPCache[index].Cidr, nil
return c.geoIPs[index].Cidr, nil
}
func loadSite(file, code string) ([]*router.Domain, error) {
// newSiteCache creates a new siteCache.
// after use, call close() to release memory.
func newSiteCache() *siteCache {
return &siteCache{
fileContent: make(map[string][]byte),
geoSites: make(map[string]*router.GeoSite),
}
}
// reset remove the content of siteCache.
// returns true if the cache is not empty.
func (c *siteCache) reset() bool {
if len(c.fileContent) == 0 && len(c.geoSites) == 0 {
return false
}
c.fileContent = make(map[string][]byte)
c.geoSites = make(map[string]*router.GeoSite)
return true
}
func (c *siteCache) loadSite(file, code string) ([]*router.Domain, error) {
index := file + ":" + code
if SiteCache[index] == nil {
bs, err := loadFile(file)
if err != nil {
return nil, newError("failed to load file: ", file).Base(err)
if c.geoSites[index] == nil {
bs := c.fileContent[file]
if bs == nil {
var err error
bs, err = loadFile(file)
if err != nil {
return nil, newError("failed to load file: ", file).Base(err)
}
c.fileContent[file] = bs
}
bs = find(bs, []byte(code))
if bs == nil {
@ -259,11 +327,10 @@ func loadSite(file, code string) ([]*router.Domain, error) {
if err := proto.Unmarshal(bs, &geosite); err != nil {
return nil, newError("error unmarshal Site in ", file, ": ", code).Base(err)
}
defer runtime.GC() // or debug.FreeOSMemory()
return geosite.Domain, nil // do not cache geosite
SiteCache[index] = &geosite
c.geoSites[index] = &geosite
return geosite.Domain, nil
}
return SiteCache[index].Domain, nil
return c.geoSites[index].Domain, nil
}
func DecodeVarint(buf []byte) (x uint64, n int) {
@ -358,14 +425,14 @@ func parseAttrs(attrs []string) *AttributeList {
return al
}
func loadGeositeWithAttr(file string, siteWithAttr string) ([]*router.Domain, error) {
func (c *siteCache) loadGeositeWithAttr(file string, siteWithAttr string) ([]*router.Domain, error) {
parts := strings.Split(siteWithAttr, "@")
if len(parts) == 0 {
return nil, newError("empty site")
}
country := strings.ToUpper(parts[0])
attrs := parseAttrs(parts[1:])
domains, err := loadSite(file, country)
domains, err := c.loadSite(file, country)
if err != nil {
return nil, err
}
@ -384,10 +451,10 @@ func loadGeositeWithAttr(file string, siteWithAttr string) ([]*router.Domain, er
return filteredDomains, nil
}
func parseDomainRule(domain string) ([]*router.Domain, error) {
func (c *GeoCache) ParseDomainRule(domain string) ([]*router.Domain, error) {
if strings.HasPrefix(domain, "geosite:") {
country := strings.ToUpper(domain[8:])
domains, err := loadGeositeWithAttr("geosite.dat", country)
domains, err := c.siteCache.loadGeositeWithAttr("geosite.dat", country)
if err != nil {
return nil, newError("failed to load geosite: ", country).Base(err)
}
@ -411,7 +478,7 @@ func parseDomainRule(domain string) ([]*router.Domain, error) {
}
filename := kv[0]
country := kv[1]
domains, err := loadGeositeWithAttr(filename, country)
domains, err := c.siteCache.loadGeositeWithAttr(filename, country)
if err != nil {
return nil, newError("failed to load external sites: ", country, " from ", filename).Base(err)
}
@ -454,7 +521,7 @@ func parseDomainRule(domain string) ([]*router.Domain, error) {
return []*router.Domain{domainRule}, nil
}
func ToCidrList(ips StringList) ([]*router.GeoIP, error) {
func (c *GeoCache) ToCidrList(ips StringList) ([]*router.GeoIP, error) {
var geoipList []*router.GeoIP
var customCidrs []*router.CIDR
@ -469,7 +536,7 @@ func ToCidrList(ips StringList) ([]*router.GeoIP, error) {
if len(country) == 0 {
return nil, newError("empty country name in rule")
}
geoip, err := loadGeoIP(strings.ToUpper(country))
geoip, err := c.ipCache.loadIP("geoip.dat", strings.ToUpper(country))
if err != nil {
return nil, newError("failed to load GeoIP: ", country).Base(err)
}
@ -509,7 +576,7 @@ func ToCidrList(ips StringList) ([]*router.GeoIP, error) {
country = country[1:]
isReverseMatch = true
}
geoip, err := loadIP(filename, strings.ToUpper(country))
geoip, err := c.ipCache.loadIP(filename, strings.ToUpper(country))
if err != nil {
return nil, newError("failed to load IPs: ", country, " from ", filename).Base(err)
}
@ -539,7 +606,7 @@ func ToCidrList(ips StringList) ([]*router.GeoIP, error) {
return geoipList, nil
}
func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
func (c *GeoCache) parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
type RawFieldRule struct {
RouterRule
Domain *StringList `json:"domain"`
@ -581,7 +648,7 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
if rawFieldRule.Domain != nil {
for _, domain := range *rawFieldRule.Domain {
rules, err := parseDomainRule(domain)
rules, err := c.ParseDomainRule(domain)
if err != nil {
return nil, newError("failed to parse domain rule: ", domain).Base(err)
}
@ -591,7 +658,7 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
if rawFieldRule.Domains != nil {
for _, domain := range *rawFieldRule.Domains {
rules, err := parseDomainRule(domain)
rules, err := c.ParseDomainRule(domain)
if err != nil {
return nil, newError("failed to parse domain rule: ", domain).Base(err)
}
@ -600,7 +667,7 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
}
if rawFieldRule.IP != nil {
geoipList, err := ToCidrList(*rawFieldRule.IP)
geoipList, err := c.ToCidrList(*rawFieldRule.IP)
if err != nil {
return nil, err
}
@ -616,7 +683,7 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
}
if rawFieldRule.SourceIP != nil {
geoipList, err := ToCidrList(*rawFieldRule.SourceIP)
geoipList, err := c.ToCidrList(*rawFieldRule.SourceIP)
if err != nil {
return nil, err
}
@ -652,14 +719,14 @@ func parseFieldRule(msg json.RawMessage) (*router.RoutingRule, error) {
return rule, nil
}
func ParseRule(msg json.RawMessage) (*router.RoutingRule, error) {
func (c *GeoCache) ParseRule(msg json.RawMessage) (*router.RoutingRule, error) {
rawRule := new(RouterRule)
err := json.Unmarshal(msg, rawRule)
if err != nil {
return nil, newError("invalid router rule").Base(err)
}
if rawRule.Type == "" || strings.EqualFold(rawRule.Type, "field") {
fieldrule, err := parseFieldRule(msg)
fieldrule, err := c.parseFieldRule(msg)
if err != nil {
return nil, newError("invalid field rule").Base(err)
}

View File

@ -44,7 +44,10 @@ func TestToCidrList(t *testing.T) {
"ext-ip:geoiptestrouter.dat:!ca",
})
_, err := ToCidrList(ips)
cache := NewGeoCache()
defer cache.Clear()
_, err := cache.ToCidrList(ips)
if err != nil {
t.Fatalf("Failed to parse geoip list, got %s", err)
}