2022-05-24 15:04:13 +08:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/Dreamacro/clash/component/geodata"
|
|
|
|
_ "github.com/Dreamacro/clash/component/geodata/standard"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/oschwald/geoip2-golang"
|
2022-08-11 23:56:50 +08:00
|
|
|
"io"
|
2022-06-03 16:50:05 +08:00
|
|
|
"net/http"
|
2022-08-11 23:56:50 +08:00
|
|
|
"os"
|
2022-05-24 15:04:13 +08:00
|
|
|
"runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-06-03 16:50:05 +08:00
|
|
|
if saveFile(data, C.Path.GeoIP()) != nil {
|
|
|
|
return fmt.Errorf("can't save GeoIP database file: %w", err)
|
2022-05-24 15:04:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
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)
|
|
|
|
}
|
|
|
|
|
2022-06-03 16:50:05 +08:00
|
|
|
instance, err := geoip2.FromBytes(data)
|
|
|
|
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()
|
2022-05-24 15:04:13 +08:00
|
|
|
|
2022-06-03 16:50:05 +08:00
|
|
|
if saveFile(data, C.Path.MMDB()) != nil {
|
|
|
|
return fmt.Errorf("can't save MMDB database file: %w", err)
|
2022-05-24 15:04:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-06-03 16:50:05 +08:00
|
|
|
if saveFile(data, C.Path.GeoSite()) != nil {
|
|
|
|
return fmt.Errorf("can't save GeoSite database file: %w", err)
|
2022-05-24 15:04:13 +08:00
|
|
|
}
|
|
|
|
|
2022-06-03 16:50:05 +08:00
|
|
|
return nil
|
2022-05-24 15:04:13 +08:00
|
|
|
}
|
|
|
|
|
2022-06-03 16:50:05 +08:00
|
|
|
func downloadForBytes(url string) ([]byte, error) {
|
|
|
|
resp, err := http.Get(url)
|
2022-05-24 15:04:13 +08:00
|
|
|
if err != nil {
|
2022-06-03 16:50:05 +08:00
|
|
|
return nil, err
|
2022-05-24 15:04:13 +08:00
|
|
|
}
|
2022-06-03 16:50:05 +08:00
|
|
|
defer resp.Body.Close()
|
2022-05-24 15:04:13 +08:00
|
|
|
|
2022-08-11 23:56:50 +08:00
|
|
|
return io.ReadAll(resp.Body)
|
2022-06-03 16:50:05 +08:00
|
|
|
}
|
2022-05-24 15:04:13 +08:00
|
|
|
|
2022-06-03 16:50:05 +08:00
|
|
|
func saveFile(bytes []byte, path string) error {
|
2022-08-11 23:56:50 +08:00
|
|
|
return os.WriteFile(path, bytes, 0o644)
|
2022-05-24 15:04:13 +08:00
|
|
|
}
|