mihomo/config/initial.go

148 lines
3.5 KiB
Go
Raw Normal View History

package config
import (
2018-11-21 13:47:46 +08:00
"fmt"
"io"
"net/http"
"os"
2021-10-15 14:11:14 +08:00
"github.com/Dreamacro/clash/component/mmdb"
C "github.com/Dreamacro/clash/constant"
2019-12-20 17:22:24 +08:00
"github.com/Dreamacro/clash/log"
)
2021-10-15 14:11:14 +08:00
func downloadMMDB(path string) (err error) {
resp, err := http.Get("https://cdn.jsdelivr.net/gh/Loyalsoldier/geoip@release/Country.mmdb")
if err != nil {
return
}
defer resp.Body.Close()
2021-10-15 14:11:14 +08:00
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644)
if err != nil {
2019-12-31 12:30:42 +08:00
return err
}
2019-12-31 12:30:42 +08:00
defer f.Close()
_, err = io.Copy(f, resp.Body)
2019-12-31 12:30:42 +08:00
return err
}
2021-10-15 14:11:14 +08:00
func initMMDB() error {
if _, err := os.Stat(C.Path.MMDB()); os.IsNotExist(err) {
log.Infoln("Can't find MMDB, start download")
if err := downloadMMDB(C.Path.MMDB()); err != nil {
return fmt.Errorf("can't download MMDB: %s", err.Error())
}
}
if !mmdb.Verify() {
log.Warnln("MMDB invalid, remove and download")
if err := os.Remove(C.Path.MMDB()); err != nil {
return fmt.Errorf("can't remove invalid MMDB: %s", err.Error())
}
if err := downloadMMDB(C.Path.MMDB()); err != nil {
return fmt.Errorf("can't download MMDB: %s", err.Error())
}
}
return nil
}
//func downloadGeoIP(path string) (err error) {
// resp, err := http.Get("https://cdn.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/geoip.dat")
// if err != nil {
// return
// }
// defer resp.Body.Close()
//
// f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644)
// if err != nil {
// return err
// }
// defer f.Close()
// _, err = io.Copy(f, resp.Body)
//
// return err
//}
2021-07-01 22:49:29 +08:00
func downloadGeoSite(path string) (err error) {
resp, err := http.Get("https://cdn.jsdelivr.net/gh/Loyalsoldier/v2ray-rules-dat@release/geosite.dat")
if err != nil {
return
}
defer resp.Body.Close()
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
2020-04-16 19:12:25 +08:00
}
2021-07-01 22:49:29 +08:00
defer f.Close()
_, err = io.Copy(f, resp.Body)
2020-04-16 19:12:25 +08:00
2021-07-01 22:49:29 +08:00
return err
}
2021-10-15 14:11:14 +08:00
//
//func initGeoIP() error {
// 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 {
// return fmt.Errorf("can't download GeoIP.dat: %s", err.Error())
// }
// log.Infoln("Download GeoIP.dat finish")
// }
//
// return nil
//}
2021-07-01 22:49:29 +08:00
func initGeoSite() error {
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 {
return fmt.Errorf("can't download GeoSite.dat: %s", err.Error())
2020-04-16 19:12:25 +08:00
}
2021-07-01 22:49:29 +08:00
log.Infoln("Download GeoSite.dat finish")
2020-04-16 19:12:25 +08:00
}
return nil
}
// Init prepare necessary files
2018-11-21 13:47:46 +08:00
func Init(dir string) error {
2018-10-14 21:22:58 +08:00
// initial homedir
2018-11-21 13:47:46 +08:00
if _, err := os.Stat(dir); os.IsNotExist(err) {
if err := os.MkdirAll(dir, 0777); err != nil {
2020-08-25 22:19:59 +08:00
return fmt.Errorf("can't create config directory %s: %s", dir, err.Error())
2018-10-14 21:22:58 +08:00
}
}
// initial config.yaml
2018-10-14 21:22:58 +08:00
if _, err := os.Stat(C.Path.Config()); os.IsNotExist(err) {
2020-02-18 13:48:15 +08:00
log.Infoln("Can't find config, create a initial config file")
f, err := os.OpenFile(C.Path.Config(), os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
2020-08-25 22:19:59 +08:00
return fmt.Errorf("can't create file %s: %s", C.Path.Config(), err.Error())
2020-02-18 13:48:15 +08:00
}
f.Write([]byte(`mixed-port: 7890`))
2020-02-18 13:48:15 +08:00
f.Close()
}
2021-10-15 14:11:14 +08:00
//// initial GeoIP
//if err := initGeoIP(); err != nil {
// return fmt.Errorf("can't initial GeoIP: %w", err)
//}
// initial mmdb
if err := initMMDB(); err != nil {
return fmt.Errorf("can't initial MMDB: %w", err)
2021-07-01 22:49:29 +08:00
}
// initial GeoSite
if err := initGeoSite(); err != nil {
return fmt.Errorf("can't initial GeoSite: %w", err)
}
2018-11-21 13:47:46 +08:00
return nil
}