2022-05-24 07:04:13 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"runtime"
|
2023-04-10 13:03:31 +00:00
|
|
|
|
2023-11-03 13:01:45 +00:00
|
|
|
"github.com/metacubex/mihomo/component/geodata"
|
|
|
|
_ "github.com/metacubex/mihomo/component/geodata/standard"
|
|
|
|
C "github.com/metacubex/mihomo/constant"
|
2023-04-10 13:03:31 +00:00
|
|
|
|
2023-07-14 14:28:24 +00:00
|
|
|
"github.com/oschwald/maxminddb-golang"
|
2022-05-24 07:04:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func UpdateGeoDatabases() error {
|
2022-06-03 08:50:05 +00:00
|
|
|
defer runtime.GC()
|
|
|
|
geoLoader, err := geodata.GetGeoDataLoader("standard")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-24 07:04:13 +00:00
|
|
|
|
|
|
|
if C.GeodataMode {
|
2022-06-03 08:50:05 +00:00
|
|
|
data, err := downloadForBytes(C.GeoIpUrl)
|
|
|
|
if err != nil {
|
2022-05-27 04:16:04 +00:00
|
|
|
return fmt.Errorf("can't download GeoIP database file: %w", err)
|
2022-05-24 07:04:13 +00:00
|
|
|
}
|
|
|
|
|
2022-06-03 08:50:05 +00:00
|
|
|
if _, err = geoLoader.LoadIPByBytes(data, "cn"); err != nil {
|
2022-05-24 07:04:13 +00:00
|
|
|
return fmt.Errorf("invalid GeoIP database file: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-12-03 15:10:09 +00:00
|
|
|
if err = saveFile(data, C.Path.GeoIP()); err != nil {
|
2022-06-03 08:50:05 +00:00
|
|
|
return fmt.Errorf("can't save GeoIP database file: %w", err)
|
2022-05-24 07:04:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2022-06-03 08:50:05 +00:00
|
|
|
data, err := downloadForBytes(C.MmdbUrl)
|
|
|
|
if err != nil {
|
2022-05-24 07:04:13 +00:00
|
|
|
return fmt.Errorf("can't download MMDB database file: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-07-14 14:28:24 +00:00
|
|
|
instance, err := maxminddb.FromBytes(data)
|
2022-06-03 08:50:05 +00:00
|
|
|
if err != nil {
|
2022-05-24 07:04:13 +00:00
|
|
|
return fmt.Errorf("invalid MMDB database file: %s", err)
|
|
|
|
}
|
2022-06-03 08:50:05 +00:00
|
|
|
_ = instance.Close()
|
2023-12-03 15:10:09 +00:00
|
|
|
if err = saveFile(data, C.Path.MMDB()); err != nil {
|
2022-06-03 08:50:05 +00:00
|
|
|
return fmt.Errorf("can't save MMDB database file: %w", err)
|
2022-05-24 07:04:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 08:50:05 +00:00
|
|
|
data, err := downloadForBytes(C.GeoSiteUrl)
|
|
|
|
if err != nil {
|
2022-05-24 07:04:13 +00:00
|
|
|
return fmt.Errorf("can't download GeoSite database file: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-06-03 08:50:05 +00:00
|
|
|
if _, err = geoLoader.LoadSiteByBytes(data, "cn"); err != nil {
|
2022-05-24 07:04:13 +00:00
|
|
|
return fmt.Errorf("invalid GeoSite database file: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-12-03 15:10:09 +00:00
|
|
|
if err = saveFile(data, C.Path.GeoSite()); err != nil {
|
2022-06-03 08:50:05 +00:00
|
|
|
return fmt.Errorf("can't save GeoSite database file: %w", err)
|
2022-05-24 07:04:13 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 10:35:37 +00:00
|
|
|
geodata.ClearCache()
|
|
|
|
|
2022-06-03 08:50:05 +00:00
|
|
|
return nil
|
2022-05-24 07:04:13 +00:00
|
|
|
}
|