2024-05-17 11:49:09 +08:00
|
|
|
package updater
|
2022-05-24 15:04:13 +08:00
|
|
|
|
|
|
|
import (
|
2024-05-17 11:49:09 +08:00
|
|
|
"errors"
|
2022-05-24 15:04:13 +08:00
|
|
|
"fmt"
|
2024-05-17 11:49:09 +08:00
|
|
|
"os"
|
2022-05-24 15:04:13 +08:00
|
|
|
"runtime"
|
2024-05-17 11:49:09 +08:00
|
|
|
"time"
|
2023-04-10 21:03:31 +08:00
|
|
|
|
2024-05-17 11:49:09 +08:00
|
|
|
"github.com/metacubex/mihomo/common/atomic"
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/component/geodata"
|
|
|
|
_ "github.com/metacubex/mihomo/component/geodata/standard"
|
2024-02-21 21:56:20 +08:00
|
|
|
"github.com/metacubex/mihomo/component/mmdb"
|
2023-11-03 21:01:45 +08:00
|
|
|
C "github.com/metacubex/mihomo/constant"
|
2024-05-17 11:49:09 +08:00
|
|
|
"github.com/metacubex/mihomo/log"
|
2023-04-10 21:03:31 +08:00
|
|
|
|
2023-07-14 22:28:24 +08:00
|
|
|
"github.com/oschwald/maxminddb-golang"
|
2022-05-24 15:04:13 +08:00
|
|
|
)
|
|
|
|
|
2024-05-17 11:49:09 +08:00
|
|
|
var (
|
2024-05-19 15:46:23 +08:00
|
|
|
UpdatingGeo atomic.Bool
|
2024-05-17 11:49:09 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func updateGeoDatabases() error {
|
2022-06-03 16:50:05 +08:00
|
|
|
defer runtime.GC()
|
|
|
|
geoLoader, err := geodata.GetGeoDataLoader("standard")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-24 15:04:13 +08:00
|
|
|
|
|
|
|
if C.GeodataMode {
|
2022-06-03 16:50:05 +08:00
|
|
|
data, err := downloadForBytes(C.GeoIpUrl)
|
|
|
|
if err != nil {
|
2022-05-27 12:16:04 +08:00
|
|
|
return fmt.Errorf("can't download GeoIP database file: %w", err)
|
2022-05-24 15:04:13 +08:00
|
|
|
}
|
|
|
|
|
2022-06-03 16:50:05 +08:00
|
|
|
if _, err = geoLoader.LoadIPByBytes(data, "cn"); err != nil {
|
2022-05-24 15:04:13 +08:00
|
|
|
return fmt.Errorf("invalid GeoIP database file: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-12-03 23:10:09 +08:00
|
|
|
if err = saveFile(data, C.Path.GeoIP()); err != nil {
|
2022-06-03 16:50:05 +08:00
|
|
|
return fmt.Errorf("can't save GeoIP database file: %w", err)
|
2022-05-24 15:04:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2024-03-12 03:14:25 +08:00
|
|
|
defer mmdb.ReloadIP()
|
2022-06-03 16:50:05 +08:00
|
|
|
data, err := downloadForBytes(C.MmdbUrl)
|
|
|
|
if err != nil {
|
2022-05-24 15:04:13 +08:00
|
|
|
return fmt.Errorf("can't download MMDB database file: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-07-14 22:28:24 +08:00
|
|
|
instance, err := maxminddb.FromBytes(data)
|
2022-06-03 16:50:05 +08:00
|
|
|
if err != nil {
|
2022-05-24 15:04:13 +08:00
|
|
|
return fmt.Errorf("invalid MMDB database file: %s", err)
|
|
|
|
}
|
2022-06-03 16:50:05 +08:00
|
|
|
_ = instance.Close()
|
2024-02-21 21:56:20 +08:00
|
|
|
|
2024-03-12 03:14:25 +08:00
|
|
|
mmdb.IPInstance().Reader.Close() // mmdb is loaded with mmap, so it needs to be closed before overwriting the file
|
2023-12-03 23:10:09 +08:00
|
|
|
if err = saveFile(data, C.Path.MMDB()); err != nil {
|
2022-06-03 16:50:05 +08:00
|
|
|
return fmt.Errorf("can't save MMDB database file: %w", err)
|
2022-05-24 15:04:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-12 03:14:25 +08:00
|
|
|
if C.ASNEnable {
|
|
|
|
defer mmdb.ReloadASN()
|
|
|
|
data, err := downloadForBytes(C.ASNUrl)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't download ASN database file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
instance, err := maxminddb.FromBytes(data)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid ASN database file: %s", err)
|
|
|
|
}
|
|
|
|
_ = instance.Close()
|
|
|
|
|
|
|
|
mmdb.ASNInstance().Reader.Close()
|
|
|
|
if err = saveFile(data, C.Path.ASN()); err != nil {
|
|
|
|
return fmt.Errorf("can't save ASN database file: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 16:50:05 +08:00
|
|
|
data, err := downloadForBytes(C.GeoSiteUrl)
|
|
|
|
if err != nil {
|
2022-05-24 15:04:13 +08:00
|
|
|
return fmt.Errorf("can't download GeoSite database file: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-06-03 16:50:05 +08:00
|
|
|
if _, err = geoLoader.LoadSiteByBytes(data, "cn"); err != nil {
|
2022-05-24 15:04:13 +08:00
|
|
|
return fmt.Errorf("invalid GeoSite database file: %s", err)
|
|
|
|
}
|
|
|
|
|
2023-12-03 23:10:09 +08:00
|
|
|
if err = saveFile(data, C.Path.GeoSite()); err != nil {
|
2022-06-03 16:50:05 +08:00
|
|
|
return fmt.Errorf("can't save GeoSite database file: %w", err)
|
2022-05-24 15:04:13 +08:00
|
|
|
}
|
|
|
|
|
2023-03-23 18:35:37 +08:00
|
|
|
geodata.ClearCache()
|
|
|
|
|
2022-06-03 16:50:05 +08:00
|
|
|
return nil
|
2022-05-24 15:04:13 +08:00
|
|
|
}
|
2024-05-17 11:49:09 +08:00
|
|
|
|
2024-05-19 17:30:39 +08:00
|
|
|
var ErrGetDatabaseUpdateSkip = errors.New("GEO database is updating, skip")
|
|
|
|
|
|
|
|
func UpdateGeoDatabases() error {
|
2024-05-17 11:49:09 +08:00
|
|
|
log.Infoln("[GEO] Start updating GEO database")
|
|
|
|
|
|
|
|
if UpdatingGeo.Load() {
|
2024-05-19 17:30:39 +08:00
|
|
|
return ErrGetDatabaseUpdateSkip
|
2024-05-17 11:49:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
UpdatingGeo.Store(true)
|
2024-05-19 15:46:23 +08:00
|
|
|
defer UpdatingGeo.Store(false)
|
2024-05-17 11:49:09 +08:00
|
|
|
|
|
|
|
log.Infoln("[GEO] Updating GEO database")
|
|
|
|
|
|
|
|
if err := updateGeoDatabases(); err != nil {
|
|
|
|
log.Errorln("[GEO] update GEO database error: %s", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getUpdateTime() (err error, time time.Time) {
|
|
|
|
var fileInfo os.FileInfo
|
|
|
|
if C.GeodataMode {
|
|
|
|
fileInfo, err = os.Stat(C.Path.GeoIP())
|
|
|
|
if err != nil {
|
|
|
|
return err, time
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fileInfo, err = os.Stat(C.Path.MMDB())
|
|
|
|
if err != nil {
|
|
|
|
return err, time
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fileInfo.ModTime()
|
|
|
|
}
|
|
|
|
|
2024-05-19 17:30:39 +08:00
|
|
|
func RegisterGeoUpdater(onSuccess func()) {
|
2024-05-17 11:49:09 +08:00
|
|
|
if C.GeoUpdateInterval <= 0 {
|
|
|
|
log.Errorln("[GEO] Invalid update interval: %d", C.GeoUpdateInterval)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2024-05-19 17:30:39 +08:00
|
|
|
ticker := time.NewTicker(time.Duration(C.GeoUpdateInterval) * time.Hour)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
2024-05-17 11:49:09 +08:00
|
|
|
err, lastUpdate := getUpdateTime()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("[GEO] Get GEO database update time error: %s", err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2024-05-19 17:30:39 +08:00
|
|
|
if err := UpdateGeoDatabases(); err != nil {
|
2024-05-17 11:49:09 +08:00
|
|
|
log.Errorln("[GEO] Failed to update GEO database: %s", err.Error())
|
|
|
|
return
|
2024-05-19 17:30:39 +08:00
|
|
|
} else {
|
|
|
|
onSuccess()
|
2024-05-17 11:49:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for range ticker.C {
|
2024-05-19 17:30:39 +08:00
|
|
|
log.Infoln("[GEO] updating database every %d hours", C.GeoUpdateInterval)
|
|
|
|
if err := UpdateGeoDatabases(); err != nil {
|
2024-05-17 11:49:09 +08:00
|
|
|
log.Errorln("[GEO] Failed to update GEO database: %s", err.Error())
|
2024-05-19 17:30:39 +08:00
|
|
|
} else {
|
|
|
|
onSuccess()
|
2024-05-17 11:49:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|