From ebe33cff1889f68f1189c4feda0085587fa718d2 Mon Sep 17 00:00:00 2001 From: H1JK Date: Sun, 6 Aug 2023 21:54:28 +0800 Subject: [PATCH] fix: GeoIP side-load --- component/mmdb/mmdb.go | 2 +- component/mmdb/patch.go | 35 ++++++++++++----------------------- go.mod | 1 - go.sum | 2 -- 4 files changed, 13 insertions(+), 27 deletions(-) diff --git a/component/mmdb/mmdb.go b/component/mmdb/mmdb.go index 081a31b2..5db8bee9 100644 --- a/component/mmdb/mmdb.go +++ b/component/mmdb/mmdb.go @@ -54,7 +54,7 @@ func Verify() bool { return err == nil } -func DefaultInstance() Reader { +func Instance() Reader { once.Do(func() { mmdbPath := C.Path.MMDB() log.Debugln("Load MMDB file: %s", mmdbPath) diff --git a/component/mmdb/patch.go b/component/mmdb/patch.go index db5cb6a4..4ff02a58 100644 --- a/component/mmdb/patch.go +++ b/component/mmdb/patch.go @@ -2,30 +2,19 @@ package mmdb import ( C "github.com/Dreamacro/clash/constant" - "github.com/Dreamacro/clash/log" - "github.com/oschwald/geoip2-golang" + "github.com/oschwald/maxminddb-golang" ) -var overrideMmdb *geoip2.Reader - -func InstallOverride(override *geoip2.Reader) { - overrideMmdb = overrideMmdb -} - -func Instance() Reader { - once.Do(func() { - mmdb, err := maxminddb.Open(C.Path.MMDB()) - if err != nil { - log.Fatalln("Can't load mmdb: %s", err.Error()) - } - reader = Reader{Reader: mmdb} - if mmdb.Metadata.DatabaseType == "sing-geoip" { - reader.databaseType = typeSing - } else { - reader.databaseType = typeMaxmind - } - }) - - return reader +func InstallOverride(override *maxminddb.Reader) { + newReader := Reader{Reader: override} + switch override.Metadata.DatabaseType { + case "sing-geoip": + reader.databaseType = typeSing + case "Meta-geoip0": + reader.databaseType = typeMetaV0 + default: + reader.databaseType = typeMaxmind + } + reader = newReader } diff --git a/go.mod b/go.mod index 6ca942ee..f355a977 100644 --- a/go.mod +++ b/go.mod @@ -29,7 +29,6 @@ require ( github.com/miekg/dns v1.1.56 github.com/mroth/weightedrand/v2 v2.1.0 github.com/openacid/low v0.1.21 - github.com/oschwald/geoip2-golang v1.9.0 github.com/oschwald/maxminddb-golang v1.12.0 github.com/puzpuzpuz/xsync/v2 v2.5.1 github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97 diff --git a/go.sum b/go.sum index ff49bc6b..be89ca4d 100644 --- a/go.sum +++ b/go.sum @@ -127,8 +127,6 @@ github.com/openacid/low v0.1.21 h1:Tr2GNu4N/+rGRYdOsEHOE89cxUIaDViZbVmKz29uKGo= github.com/openacid/low v0.1.21/go.mod h1:q+MsKI6Pz2xsCkzV4BLj7NR5M4EX0sGz5AqotpZDVh0= github.com/openacid/must v0.1.3/go.mod h1:luPiXCuJlEo3UUFQngVQokV0MPGryeYvtCbQPs3U1+I= github.com/openacid/testkeys v0.1.6/go.mod h1:MfA7cACzBpbiwekivj8StqX0WIRmqlMsci1c37CA3Do= -github.com/oschwald/geoip2-golang v1.9.0 h1:uvD3O6fXAXs+usU+UGExshpdP13GAqp4GBrzN7IgKZc= -github.com/oschwald/geoip2-golang v1.9.0/go.mod h1:BHK6TvDyATVQhKNbQBdrj9eAvuwOMi2zSFXizL3K81Y= github.com/oschwald/maxminddb-golang v1.12.0 h1:9FnTOD0YOhP7DGxGsq4glzpGy5+w7pq50AS6wALUMYs= github.com/oschwald/maxminddb-golang v1.12.0/go.mod h1:q0Nob5lTCqyQ8WT6FYgS1L7PXKVVbgiymefNwIjPzgY= github.com/pierrec/lz4/v4 v4.1.14 h1:+fL8AQEZtz/ijeNnpduH0bROTu0O3NZAlPjQxGn8LwE=