github.com/yaling888/clash@v1.53.0/config/updateGeo.go (about) 1 package config 2 3 import ( 4 "fmt" 5 "os" 6 "runtime" 7 8 "github.com/oschwald/geoip2-golang" 9 10 "github.com/yaling888/clash/component/geodata" 11 C "github.com/yaling888/clash/constant" 12 ) 13 14 func UpdateGeoDatabases() error { 15 var ( 16 tmpMMDB = C.Path.Resolve("temp_country.mmdb") 17 tmpGeoSite = C.Path.Resolve("temp_geosite.dat") 18 ) 19 20 if err := downloadMMDB(tmpMMDB); err != nil { 21 return fmt.Errorf("can't download MMDB database file: %w", err) 22 } 23 24 if err := verifyMMDB(tmpMMDB); err != nil { 25 _ = os.Remove(tmpMMDB) 26 return fmt.Errorf("invalid MMDB database file, %w", err) 27 } 28 29 if err := os.Rename(tmpMMDB, C.Path.MMDB()); err != nil { 30 return fmt.Errorf("can't rename MMDB database file: %w", err) 31 } 32 33 if err := downloadGeoSite(tmpGeoSite); err != nil { 34 return fmt.Errorf("can't download GeoSite database file: %w", err) 35 } 36 37 if err := verifyGeoSite(tmpGeoSite); err != nil { 38 _ = os.Remove(tmpGeoSite) 39 return fmt.Errorf("invalid GeoSite database file, %w", err) 40 } 41 42 if err := os.Rename(tmpGeoSite, C.Path.GeoSite()); err != nil { 43 return fmt.Errorf("can't rename GeoSite database file: %w", err) 44 } 45 46 return nil 47 } 48 49 func verifyMMDB(path string) error { 50 instance, err := geoip2.Open(path) 51 if err == nil { 52 _ = instance.Close() 53 } 54 return err 55 } 56 57 func verifyGeoSite(path string) error { 58 geoLoader, err := geodata.GetGeoDataLoader("standard") 59 if err != nil { 60 return err 61 } 62 63 _, err = geoLoader.LoadSite(path, "gfw") 64 65 runtime.GC() 66 67 return err 68 }