From 7c8f451892258afe80e211867a118cd1d6aba63c Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Mon, 9 Sep 2024 16:08:48 +0800 Subject: [PATCH] chore: cleanup geo internal code --- adapter/provider/provider.go | 2 +- component/geodata/init.go | 143 +++++++++++++++++++++---------- component/geodata/utils.go | 16 ---- component/http/http.go | 15 +++- component/mmdb/mmdb.go | 84 +++++------------- component/mmdb/patch_android.go | 8 +- component/updater/update_core.go | 4 +- component/updater/update_geo.go | 75 ++++++++++------ component/updater/utils.go | 3 +- config/config.go | 20 ++--- constant/geodata.go | 12 --- constant/http.go | 5 -- hub/executor/executor.go | 18 ++-- main.go | 5 +- rules/common/geoip.go | 22 ++--- rules/common/ipasn.go | 1 - 16 files changed, 219 insertions(+), 214 deletions(-) delete mode 100644 constant/geodata.go delete mode 100644 constant/http.go diff --git a/adapter/provider/provider.go b/adapter/provider/provider.go index 923cddba..107b621b 100644 --- a/adapter/provider/provider.go +++ b/adapter/provider/provider.go @@ -130,7 +130,7 @@ func (pp *proxySetProvider) getSubscriptionInfo() { ctx, cancel := context.WithTimeout(context.Background(), time.Second*90) defer cancel() resp, err := mihomoHttp.HttpRequestWithProxy(ctx, pp.Vehicle().(*resource.HTTPVehicle).Url(), - http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil, pp.Vehicle().Proxy()) + http.MethodGet, nil, nil, pp.Vehicle().Proxy()) if err != nil { return } diff --git a/component/geodata/init.go b/component/geodata/init.go index 834567a4..08ec1b94 100644 --- a/component/geodata/init.go +++ b/component/geodata/init.go @@ -6,8 +6,10 @@ import ( "io" "net/http" "os" + "sync" "time" + "github.com/metacubex/mihomo/common/atomic" mihomoHttp "github.com/metacubex/mihomo/component/http" "github.com/metacubex/mihomo/component/mmdb" C "github.com/metacubex/mihomo/constant" @@ -18,12 +20,79 @@ var ( initGeoSite bool initGeoIP int initASN bool + + initGeoSiteMutex sync.Mutex + initGeoIPMutex sync.Mutex + initASNMutex sync.Mutex + + geoIpEnable atomic.Bool + geoSiteEnable atomic.Bool + asnEnable atomic.Bool + + geoIpUrl string + mmdbUrl string + geoSiteUrl string + asnUrl string ) +func GeoIpUrl() string { + return geoIpUrl +} + +func SetGeoIpUrl(url string) { + geoIpUrl = url +} + +func MmdbUrl() string { + return mmdbUrl +} + +func SetMmdbUrl(url string) { + mmdbUrl = url +} + +func GeoSiteUrl() string { + return geoSiteUrl +} + +func SetGeoSiteUrl(url string) { + geoSiteUrl = url +} + +func ASNUrl() string { + return asnUrl +} + +func SetASNUrl(url string) { + asnUrl = url +} + +func downloadToPath(url string, path string) (err error) { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*90) + defer cancel() + resp, err := mihomoHttp.HttpRequest(ctx, url, http.MethodGet, nil, nil) + if err != nil { + return + } + defer resp.Body.Close() + + f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644) + if err != nil { + return err + } + defer f.Close() + _, err = io.Copy(f, resp.Body) + + return err +} + func InitGeoSite() error { + geoSiteEnable.Store(true) + initGeoSiteMutex.Lock() + defer initGeoSiteMutex.Unlock() if _, err := os.Stat(C.Path.GeoSite()); os.IsNotExist(err) { log.Infoln("Can't find GeoSite.dat, start download") - if err := downloadGeoSite(C.Path.GeoSite()); err != nil { + if err := downloadToPath(GeoSiteUrl(), C.Path.GeoSite()); err != nil { return fmt.Errorf("can't download GeoSite.dat: %s", err.Error()) } log.Infoln("Download GeoSite.dat finish") @@ -35,7 +104,7 @@ func InitGeoSite() error { if err := os.Remove(C.Path.GeoSite()); err != nil { return fmt.Errorf("can't remove invalid GeoSite.dat: %s", err.Error()) } - if err := downloadGeoSite(C.Path.GeoSite()); err != nil { + if err := downloadToPath(GeoSiteUrl(), C.Path.GeoSite()); err != nil { return fmt.Errorf("can't download GeoSite.dat: %s", err.Error()) } } @@ -44,49 +113,14 @@ func InitGeoSite() error { return nil } -func downloadGeoSite(path string) (err error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*90) - defer cancel() - resp, err := mihomoHttp.HttpRequest(ctx, C.GeoSiteUrl, http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil) - if err != nil { - return - } - defer resp.Body.Close() - - f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644) - if err != nil { - return err - } - defer f.Close() - _, err = io.Copy(f, resp.Body) - - return err -} - -func downloadGeoIP(path string) (err error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*90) - defer cancel() - resp, err := mihomoHttp.HttpRequest(ctx, C.GeoIpUrl, http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil) - if err != nil { - return - } - defer resp.Body.Close() - - f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644) - if err != nil { - return err - } - defer f.Close() - _, err = io.Copy(f, resp.Body) - - return err -} - func InitGeoIP() error { - if C.GeodataMode { + geoIpEnable.Store(true) + initGeoIPMutex.Lock() + defer initGeoIPMutex.Unlock() + if GeodataMode() { if _, err := os.Stat(C.Path.GeoIP()); os.IsNotExist(err) { log.Infoln("Can't find GeoIP.dat, start download") - if err := downloadGeoIP(C.Path.GeoIP()); err != nil { + if err := downloadToPath(GeoIpUrl(), C.Path.GeoIP()); err != nil { return fmt.Errorf("can't download GeoIP.dat: %s", err.Error()) } log.Infoln("Download GeoIP.dat finish") @@ -99,7 +133,7 @@ func InitGeoIP() error { if err := os.Remove(C.Path.GeoIP()); err != nil { return fmt.Errorf("can't remove invalid GeoIP.dat: %s", err.Error()) } - if err := downloadGeoIP(C.Path.GeoIP()); err != nil { + if err := downloadToPath(GeoIpUrl(), C.Path.GeoIP()); err != nil { return fmt.Errorf("can't download GeoIP.dat: %s", err.Error()) } } @@ -110,7 +144,7 @@ func InitGeoIP() error { if _, err := os.Stat(C.Path.MMDB()); os.IsNotExist(err) { log.Infoln("Can't find MMDB, start download") - if err := mmdb.DownloadMMDB(C.Path.MMDB()); err != nil { + if err := downloadToPath(MmdbUrl(), C.Path.MMDB()); err != nil { return fmt.Errorf("can't download MMDB: %s", err.Error()) } } @@ -121,7 +155,7 @@ func InitGeoIP() error { if err := os.Remove(C.Path.MMDB()); err != nil { return fmt.Errorf("can't remove invalid MMDB: %s", err.Error()) } - if err := mmdb.DownloadMMDB(C.Path.MMDB()); err != nil { + if err := downloadToPath(MmdbUrl(), C.Path.MMDB()); err != nil { return fmt.Errorf("can't download MMDB: %s", err.Error()) } } @@ -131,9 +165,12 @@ func InitGeoIP() error { } func InitASN() error { + asnEnable.Store(true) + initASNMutex.Lock() + defer initASNMutex.Unlock() if _, err := os.Stat(C.Path.ASN()); os.IsNotExist(err) { log.Infoln("Can't find ASN.mmdb, start download") - if err := mmdb.DownloadASN(C.Path.ASN()); err != nil { + if err := downloadToPath(ASNUrl(), C.Path.ASN()); err != nil { return fmt.Errorf("can't download ASN.mmdb: %s", err.Error()) } log.Infoln("Download ASN.mmdb finish") @@ -145,7 +182,7 @@ func InitASN() error { if err := os.Remove(C.Path.ASN()); err != nil { return fmt.Errorf("can't remove invalid ASN: %s", err.Error()) } - if err := mmdb.DownloadASN(C.Path.ASN()); err != nil { + if err := downloadToPath(ASNUrl(), C.Path.ASN()); err != nil { return fmt.Errorf("can't download ASN: %s", err.Error()) } } @@ -153,3 +190,15 @@ func InitASN() error { } return nil } + +func GeoIpEnable() bool { + return geoIpEnable.Load() +} + +func GeoSiteEnable() bool { + return geoSiteEnable.Load() +} + +func ASNEnable() bool { + return asnEnable.Load() +} diff --git a/component/geodata/utils.go b/component/geodata/utils.go index 04c4293f..47966249 100644 --- a/component/geodata/utils.go +++ b/component/geodata/utils.go @@ -13,8 +13,6 @@ import ( var ( geoMode bool - AutoUpdate bool - UpdateInterval int geoLoaderName = "memconservative" geoSiteMatcher = "succinct" ) @@ -25,14 +23,6 @@ func GeodataMode() bool { return geoMode } -func GeoAutoUpdate() bool { - return AutoUpdate -} - -func GeoUpdateInterval() int { - return UpdateInterval -} - func LoaderName() string { return geoLoaderName } @@ -44,12 +34,6 @@ func SiteMatcherName() string { func SetGeodataMode(newGeodataMode bool) { geoMode = newGeodataMode } -func SetGeoAutoUpdate(newAutoUpdate bool) { - AutoUpdate = newAutoUpdate -} -func SetGeoUpdateInterval(newGeoUpdateInterval int) { - UpdateInterval = newGeoUpdateInterval -} func SetLoader(newLoader string) { if newLoader == "memc" { diff --git a/component/http/http.go b/component/http/http.go index 21d65d2e..3fc06da3 100644 --- a/component/http/http.go +++ b/component/http/http.go @@ -12,10 +12,21 @@ import ( "time" "github.com/metacubex/mihomo/component/ca" - C "github.com/metacubex/mihomo/constant" "github.com/metacubex/mihomo/listener/inner" ) +var ( + ua string +) + +func UA() string { + return ua +} + +func SetUA(UA string) { + ua = UA +} + func HttpRequest(ctx context.Context, url, method string, header map[string][]string, body io.Reader) (*http.Response, error) { return HttpRequestWithProxy(ctx, url, method, header, body, "") } @@ -35,7 +46,7 @@ func HttpRequestWithProxy(ctx context.Context, url, method string, header map[st } if _, ok := header["User-Agent"]; !ok { - req.Header.Set("User-Agent", C.UA) + req.Header.Set("User-Agent", UA()) } if err != nil { diff --git a/component/mmdb/mmdb.go b/component/mmdb/mmdb.go index 81156bc6..81644b00 100644 --- a/component/mmdb/mmdb.go +++ b/component/mmdb/mmdb.go @@ -1,15 +1,9 @@ package mmdb import ( - "context" - "io" - "net/http" - "os" "sync" - "time" mihomoOnce "github.com/metacubex/mihomo/common/once" - mihomoHttp "github.com/metacubex/mihomo/component/http" C "github.com/metacubex/mihomo/constant" "github.com/metacubex/mihomo/log" @@ -25,26 +19,26 @@ const ( ) var ( - IPreader IPReader - ASNreader ASNReader - IPonce sync.Once - ASNonce sync.Once + ipReader IPReader + asnReader ASNReader + ipOnce sync.Once + asnOnce sync.Once ) func LoadFromBytes(buffer []byte) { - IPonce.Do(func() { + ipOnce.Do(func() { mmdb, err := maxminddb.FromBytes(buffer) if err != nil { log.Fatalln("Can't load mmdb: %s", err.Error()) } - IPreader = IPReader{Reader: mmdb} + ipReader = IPReader{Reader: mmdb} switch mmdb.Metadata.DatabaseType { case "sing-geoip": - IPreader.databaseType = typeSing + ipReader.databaseType = typeSing case "Meta-geoip0": - IPreader.databaseType = typeMetaV0 + ipReader.databaseType = typeMetaV0 default: - IPreader.databaseType = typeMaxmind + ipReader.databaseType = typeMaxmind } }) } @@ -58,83 +52,45 @@ func Verify(path string) bool { } func IPInstance() IPReader { - IPonce.Do(func() { + ipOnce.Do(func() { mmdbPath := C.Path.MMDB() log.Infoln("Load MMDB file: %s", mmdbPath) mmdb, err := maxminddb.Open(mmdbPath) if err != nil { log.Fatalln("Can't load MMDB: %s", err.Error()) } - IPreader = IPReader{Reader: mmdb} + ipReader = IPReader{Reader: mmdb} switch mmdb.Metadata.DatabaseType { case "sing-geoip": - IPreader.databaseType = typeSing + ipReader.databaseType = typeSing case "Meta-geoip0": - IPreader.databaseType = typeMetaV0 + ipReader.databaseType = typeMetaV0 default: - IPreader.databaseType = typeMaxmind + ipReader.databaseType = typeMaxmind } }) - return IPreader -} - -func DownloadMMDB(path string) (err error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*90) - defer cancel() - resp, err := mihomoHttp.HttpRequest(ctx, C.MmdbUrl, http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil) - if err != nil { - return - } - defer resp.Body.Close() - - f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644) - if err != nil { - return err - } - defer f.Close() - _, err = io.Copy(f, resp.Body) - - return err + return ipReader } func ASNInstance() ASNReader { - ASNonce.Do(func() { + asnOnce.Do(func() { ASNPath := C.Path.ASN() log.Infoln("Load ASN file: %s", ASNPath) asn, err := maxminddb.Open(ASNPath) if err != nil { log.Fatalln("Can't load ASN: %s", err.Error()) } - ASNreader = ASNReader{Reader: asn} + asnReader = ASNReader{Reader: asn} }) - return ASNreader -} - -func DownloadASN(path string) (err error) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*90) - defer cancel() - resp, err := mihomoHttp.HttpRequest(ctx, C.ASNUrl, http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil) - if err != nil { - return - } - defer resp.Body.Close() - - f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644) - if err != nil { - return err - } - defer f.Close() - _, err = io.Copy(f, resp.Body) - - return err + return asnReader } func ReloadIP() { - mihomoOnce.Reset(&IPonce) + mihomoOnce.Reset(&ipOnce) } func ReloadASN() { - mihomoOnce.Reset(&ASNonce) + mihomoOnce.Reset(&asnOnce) } diff --git a/component/mmdb/patch_android.go b/component/mmdb/patch_android.go index 147a3324..dfa96cac 100644 --- a/component/mmdb/patch_android.go +++ b/component/mmdb/patch_android.go @@ -8,11 +8,11 @@ func InstallOverride(override *maxminddb.Reader) { newReader := IPReader{Reader: override} switch override.Metadata.DatabaseType { case "sing-geoip": - IPreader.databaseType = typeSing + ipReader.databaseType = typeSing case "Meta-geoip0": - IPreader.databaseType = typeMetaV0 + ipReader.databaseType = typeMetaV0 default: - IPreader.databaseType = typeMaxmind + ipReader.databaseType = typeMaxmind } - IPreader = newReader + ipReader = newReader } diff --git a/component/updater/update_core.go b/component/updater/update_core.go index 9f13af9c..2aab7833 100644 --- a/component/updater/update_core.go +++ b/component/updater/update_core.go @@ -237,7 +237,7 @@ const MaxPackageFileSize = 32 * 1024 * 1024 func downloadPackageFile() (err error) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*90) defer cancel() - resp, err := mihomoHttp.HttpRequest(ctx, packageURL, http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil) + resp, err := mihomoHttp.HttpRequest(ctx, packageURL, http.MethodGet, nil, nil) if err != nil { return fmt.Errorf("http request failed: %w", err) } @@ -418,7 +418,7 @@ func copyFile(src, dst string) error { func getLatestVersion() (version string, err error) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() - resp, err := mihomoHttp.HttpRequest(ctx, versionURL, http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil) + resp, err := mihomoHttp.HttpRequest(ctx, versionURL, http.MethodGet, nil, nil) if err != nil { return "", fmt.Errorf("get Latest Version fail: %w", err) } diff --git a/component/updater/update_geo.go b/component/updater/update_geo.go index d2d1e28d..6246b1c6 100644 --- a/component/updater/update_geo.go +++ b/component/updater/update_geo.go @@ -20,12 +20,31 @@ import ( ) var ( + autoUpdate bool + updateInterval int + updatingGeo atomic.Bool ) +func GeoAutoUpdate() bool { + return autoUpdate +} + +func GeoUpdateInterval() int { + return updateInterval +} + +func SetGeoAutoUpdate(newAutoUpdate bool) { + autoUpdate = newAutoUpdate +} + +func SetGeoUpdateInterval(newGeoUpdateInterval int) { + updateInterval = newGeoUpdateInterval +} + func UpdateMMDB() (err error) { defer mmdb.ReloadIP() - data, err := downloadForBytes(C.MmdbUrl) + data, err := downloadForBytes(geodata.MmdbUrl()) if err != nil { return fmt.Errorf("can't download MMDB database file: %w", err) } @@ -44,7 +63,7 @@ func UpdateMMDB() (err error) { func UpdateASN() (err error) { defer mmdb.ReloadASN() - data, err := downloadForBytes(C.ASNUrl) + data, err := downloadForBytes(geodata.ASNUrl()) if err != nil { return fmt.Errorf("can't download ASN database file: %w", err) } @@ -65,7 +84,7 @@ func UpdateASN() (err error) { func UpdateGeoIp() (err error) { defer geodata.ClearGeoIPCache() geoLoader, err := geodata.GetGeoDataLoader("standard") - data, err := downloadForBytes(C.GeoIpUrl) + data, err := downloadForBytes(geodata.GeoIpUrl()) if err != nil { return fmt.Errorf("can't download GeoIP database file: %w", err) } @@ -81,7 +100,7 @@ func UpdateGeoIp() (err error) { func UpdateGeoSite() (err error) { defer geodata.ClearGeoSiteCache() geoLoader, err := geodata.GetGeoDataLoader("standard") - data, err := downloadForBytes(C.GeoSiteUrl) + data, err := downloadForBytes(geodata.GeoSiteUrl()) if err != nil { return fmt.Errorf("can't download GeoSite database file: %w", err) } @@ -101,29 +120,33 @@ func updateGeoDatabases() error { b, _ := batch.New[interface{}](context.Background()) - if C.GeodataMode { - b.Go("UpdateGeoIp", func() (_ interface{}, err error) { - err = UpdateGeoIp() - return - }) - } else { - b.Go("UpdateMMDB", func() (_ interface{}, err error) { - err = UpdateMMDB() - return - }) + if geodata.GeoIpEnable() { + if geodata.GeodataMode() { + b.Go("UpdateGeoIp", func() (_ interface{}, err error) { + err = UpdateGeoIp() + return + }) + } else { + b.Go("UpdateMMDB", func() (_ interface{}, err error) { + err = UpdateMMDB() + return + }) + } } - if C.ASNEnable { + if geodata.ASNEnable() { b.Go("UpdateASN", func() (_ interface{}, err error) { err = UpdateASN() return }) } - b.Go("UpdateGeoSite", func() (_ interface{}, err error) { - err = UpdateGeoSite() - return - }) + if geodata.GeoSiteEnable() { + b.Go("UpdateGeoSite", func() (_ interface{}, err error) { + err = UpdateGeoSite() + return + }) + } if e := b.Wait(); e != nil { return e.Err @@ -156,7 +179,7 @@ func UpdateGeoDatabases() error { func getUpdateTime() (err error, time time.Time) { var fileInfo os.FileInfo - if C.GeodataMode { + if geodata.GeodataMode() { fileInfo, err = os.Stat(C.Path.GeoIP()) if err != nil { return err, time @@ -172,13 +195,13 @@ func getUpdateTime() (err error, time time.Time) { } func RegisterGeoUpdater() { - if C.GeoUpdateInterval <= 0 { - log.Errorln("[GEO] Invalid update interval: %d", C.GeoUpdateInterval) + if updateInterval <= 0 { + log.Errorln("[GEO] Invalid update interval: %d", updateInterval) return } go func() { - ticker := time.NewTicker(time.Duration(C.GeoUpdateInterval) * time.Hour) + ticker := time.NewTicker(time.Duration(updateInterval) * time.Hour) defer ticker.Stop() err, lastUpdate := getUpdateTime() @@ -188,8 +211,8 @@ func RegisterGeoUpdater() { } log.Infoln("[GEO] last update time %s", lastUpdate) - if lastUpdate.Add(time.Duration(C.GeoUpdateInterval) * time.Hour).Before(time.Now()) { - log.Infoln("[GEO] Database has not been updated for %v, update now", time.Duration(C.GeoUpdateInterval)*time.Hour) + if lastUpdate.Add(time.Duration(updateInterval) * time.Hour).Before(time.Now()) { + log.Infoln("[GEO] Database has not been updated for %v, update now", time.Duration(updateInterval)*time.Hour) if err := UpdateGeoDatabases(); err != nil { log.Errorln("[GEO] Failed to update GEO database: %s", err.Error()) return @@ -197,7 +220,7 @@ func RegisterGeoUpdater() { } for range ticker.C { - log.Infoln("[GEO] updating database every %d hours", C.GeoUpdateInterval) + log.Infoln("[GEO] updating database every %d hours", updateInterval) if err := UpdateGeoDatabases(); err != nil { log.Errorln("[GEO] Failed to update GEO database: %s", err.Error()) } diff --git a/component/updater/utils.go b/component/updater/utils.go index 0eecfc6c..d9da48c3 100644 --- a/component/updater/utils.go +++ b/component/updater/utils.go @@ -9,7 +9,6 @@ import ( "time" mihomoHttp "github.com/metacubex/mihomo/component/http" - C "github.com/metacubex/mihomo/constant" "golang.org/x/exp/constraints" ) @@ -17,7 +16,7 @@ import ( func downloadForBytes(url string) ([]byte, error) { ctx, cancel := context.WithTimeout(context.Background(), time.Second*90) defer cancel() - resp, err := mihomoHttp.HttpRequest(ctx, url, http.MethodGet, http.Header{"User-Agent": {C.UA}}, nil) + resp, err := mihomoHttp.HttpRequest(ctx, url, http.MethodGet, nil, nil) if err != nil { return nil, err } diff --git a/config/config.go b/config/config.go index 4490ebf6..26518758 100644 --- a/config/config.go +++ b/config/config.go @@ -22,6 +22,7 @@ import ( "github.com/metacubex/mihomo/component/cidr" "github.com/metacubex/mihomo/component/fakeip" "github.com/metacubex/mihomo/component/geodata" + mihomoHttp "github.com/metacubex/mihomo/component/http" P "github.com/metacubex/mihomo/component/process" "github.com/metacubex/mihomo/component/resolver" "github.com/metacubex/mihomo/component/sniffer" @@ -433,7 +434,7 @@ func DefaultRawConfig() *RawConfig { Mode: T.Rule, GeoAutoUpdate: false, GeoUpdateInterval: 24, - GeodataMode: C.GeodataMode, + GeodataMode: geodata.GeodataMode(), GeodataLoader: "memconservative", UnifiedDelay: false, Authentication: []string{}, @@ -681,19 +682,16 @@ func ParseRawConfig(rawCfg *RawConfig) (*Config, error) { } func parseGeneral(cfg *RawConfig) (*General, error) { + updater.SetGeoAutoUpdate(cfg.GeoAutoUpdate) + updater.SetGeoUpdateInterval(cfg.GeoUpdateInterval) geodata.SetGeodataMode(cfg.GeodataMode) - geodata.SetGeoAutoUpdate(cfg.GeoAutoUpdate) - geodata.SetGeoUpdateInterval(cfg.GeoUpdateInterval) geodata.SetLoader(cfg.GeodataLoader) geodata.SetSiteMatcher(cfg.GeositeMatcher) - C.GeoAutoUpdate = cfg.GeoAutoUpdate - C.GeoUpdateInterval = cfg.GeoUpdateInterval - C.GeoIpUrl = cfg.GeoXUrl.GeoIp - C.GeoSiteUrl = cfg.GeoXUrl.GeoSite - C.MmdbUrl = cfg.GeoXUrl.Mmdb - C.ASNUrl = cfg.GeoXUrl.ASN - C.GeodataMode = cfg.GeodataMode - C.UA = cfg.GlobalUA + geodata.SetGeoIpUrl(cfg.GeoXUrl.GeoIp) + geodata.SetGeoSiteUrl(cfg.GeoXUrl.GeoSite) + geodata.SetMmdbUrl(cfg.GeoXUrl.Mmdb) + geodata.SetASNUrl(cfg.GeoXUrl.ASN) + mihomoHttp.SetUA(cfg.GlobalUA) if cfg.KeepAliveIdle != 0 { N.KeepAliveIdle = time.Duration(cfg.KeepAliveIdle) * time.Second diff --git a/constant/geodata.go b/constant/geodata.go deleted file mode 100644 index cd3f74e3..00000000 --- a/constant/geodata.go +++ /dev/null @@ -1,12 +0,0 @@ -package constant - -var ( - ASNEnable bool - GeodataMode bool - GeoAutoUpdate bool - GeoUpdateInterval int - GeoIpUrl string - MmdbUrl string - GeoSiteUrl string - ASNUrl string -) diff --git a/constant/http.go b/constant/http.go deleted file mode 100644 index 8e321f6b..00000000 --- a/constant/http.go +++ /dev/null @@ -1,5 +0,0 @@ -package constant - -var ( - UA string -) diff --git a/hub/executor/executor.go b/hub/executor/executor.go index c83b254c..fddcc42e 100644 --- a/hub/executor/executor.go +++ b/hub/executor/executor.go @@ -17,6 +17,7 @@ import ( "github.com/metacubex/mihomo/component/ca" "github.com/metacubex/mihomo/component/dialer" G "github.com/metacubex/mihomo/component/geodata" + mihomoHttp "github.com/metacubex/mihomo/component/http" "github.com/metacubex/mihomo/component/iface" "github.com/metacubex/mihomo/component/profile" "github.com/metacubex/mihomo/component/profile/cachefile" @@ -81,6 +82,7 @@ func ParseWithBytes(buf []byte) (*config.Config, error) { func ApplyConfig(cfg *config.Config, force bool) { mux.Lock() defer mux.Unlock() + log.SetLevel(cfg.General.LogLevel) tunnel.OnSuspend() @@ -115,8 +117,6 @@ func ApplyConfig(cfg *config.Config, force bool) { tunnel.OnRunning() hcCompatibleProvider(cfg.Providers) initExternalUI() - - log.SetLevel(cfg.General.LogLevel) } func initInnerTcp() { @@ -157,13 +157,13 @@ func GetGeneral() *config.General { Interface: dialer.DefaultInterface.Load(), RoutingMark: int(dialer.DefaultRoutingMark.Load()), GeoXUrl: config.GeoXUrl{ - GeoIp: C.GeoIpUrl, - Mmdb: C.MmdbUrl, - ASN: C.ASNUrl, - GeoSite: C.GeoSiteUrl, + GeoIp: G.GeoIpUrl(), + Mmdb: G.MmdbUrl(), + ASN: G.ASNUrl(), + GeoSite: G.GeoSiteUrl(), }, - GeoAutoUpdate: G.GeoAutoUpdate(), - GeoUpdateInterval: G.GeoUpdateInterval(), + GeoAutoUpdate: updater.GeoAutoUpdate(), + GeoUpdateInterval: updater.GeoUpdateInterval(), GeodataMode: G.GeodataMode(), GeodataLoader: G.LoaderName(), GeositeMatcher: G.SiteMatcherName(), @@ -171,7 +171,7 @@ func GetGeneral() *config.General { FindProcessMode: tunnel.FindProcessMode(), Sniffing: tunnel.IsSniffing(), GlobalClientFingerprint: tlsC.GetGlobalFingerprint(), - GlobalUA: C.UA, + GlobalUA: mihomoHttp.UA(), } return general diff --git a/main.go b/main.go index 26f1d2c0..8910a006 100644 --- a/main.go +++ b/main.go @@ -11,6 +11,7 @@ import ( "strings" "syscall" + "github.com/metacubex/mihomo/component/geodata" "github.com/metacubex/mihomo/component/updater" "github.com/metacubex/mihomo/config" C "github.com/metacubex/mihomo/constant" @@ -78,7 +79,7 @@ func main() { } if geodataMode { - C.GeodataMode = true + geodata.SetGeodataMode(true) } if configString != "" { @@ -140,7 +141,7 @@ func main() { log.Fatalln("Parse config error: %s", err.Error()) } - if C.GeoAutoUpdate { + if updater.GeoAutoUpdate() { updater.RegisterGeoUpdater() } diff --git a/rules/common/geoip.go b/rules/common/geoip.go index 7d6871c7..61fae504 100644 --- a/rules/common/geoip.go +++ b/rules/common/geoip.go @@ -46,7 +46,7 @@ func (g *GEOIP) Match(metadata *C.Metadata) (bool, string) { return g.isLan(ip), g.adapter } - if C.GeodataMode { + if geodata.GeodataMode() { if g.isSourceIP { if slices.Contains(metadata.SrcGeoIP, g.country) { return true, g.adapter @@ -102,7 +102,7 @@ func (g *GEOIP) MatchIp(ip netip.Addr) bool { return g.isLan(ip) } - if C.GeodataMode { + if geodata.GeodataMode() { matcher, err := g.getIPMatcher() if err != nil { return false @@ -124,7 +124,7 @@ func (g dnsFallbackFilter) MatchIp(ip netip.Addr) bool { return false } - if C.GeodataMode { + if geodata.GeodataMode() { matcher, err := g.getIPMatcher() if err != nil { return false @@ -170,7 +170,7 @@ func (g *GEOIP) GetCountry() string { } func (g *GEOIP) GetIPMatcher() (router.IPMatcher, error) { - if C.GeodataMode { + if geodata.GeodataMode() { return g.getIPMatcher() } return nil, errors.New("not geodata mode") @@ -193,10 +193,6 @@ func (g *GEOIP) GetRecodeSize() int { } func NewGEOIP(country string, adapter string, isSrc, noResolveIP bool) (*GEOIP, error) { - if err := geodata.InitGeoIP(); err != nil { - log.Errorln("can't initial GeoIP: %s", err) - return nil, err - } country = strings.ToLower(country) geoip := &GEOIP{ @@ -206,11 +202,17 @@ func NewGEOIP(country string, adapter string, isSrc, noResolveIP bool) (*GEOIP, noResolveIP: noResolveIP, isSourceIP: isSrc, } - if !C.GeodataMode || country == "lan" { + + if country == "lan" { return geoip, nil } - if C.GeodataMode { + if err := geodata.InitGeoIP(); err != nil { + log.Errorln("can't initial GeoIP: %s", err) + return nil, err + } + + if geodata.GeodataMode() { geoIPMatcher, err := geoip.getIPMatcher() // test load if err != nil { return nil, err diff --git a/rules/common/ipasn.go b/rules/common/ipasn.go index bcff4e72..813923ac 100644 --- a/rules/common/ipasn.go +++ b/rules/common/ipasn.go @@ -63,7 +63,6 @@ func (a *ASN) GetASN() string { } func NewIPASN(asn string, adapter string, isSrc, noResolveIP bool) (*ASN, error) { - C.ASNEnable = true if err := geodata.InitASN(); err != nil { log.Errorln("can't initial ASN: %s", err) return nil, err