2018-07-31 09:54:16 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2018-11-21 05:47:46 +00:00
|
|
|
"fmt"
|
2018-07-31 09:54:16 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2020-04-16 11:12:25 +00:00
|
|
|
"github.com/Dreamacro/clash/component/mmdb"
|
2018-07-31 09:54:16 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2019-12-20 09:22:24 +00:00
|
|
|
"github.com/Dreamacro/clash/log"
|
2018-07-31 09:54:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func downloadMMDB(path string) (err error) {
|
2019-12-31 04:30:42 +00:00
|
|
|
resp, err := http.Get("https://github.com/Dreamacro/maxmind-geoip/releases/latest/download/Country.mmdb")
|
2018-07-31 09:54:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2019-12-31 04:30:42 +00:00
|
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644)
|
2018-07-31 09:54:16 +00:00
|
|
|
if err != nil {
|
2019-12-31 04:30:42 +00:00
|
|
|
return err
|
2018-07-31 09:54:16 +00:00
|
|
|
}
|
2019-12-31 04:30:42 +00:00
|
|
|
defer f.Close()
|
|
|
|
_, err = io.Copy(f, resp.Body)
|
2018-07-31 09:54:16 +00:00
|
|
|
|
2019-12-31 04:30:42 +00:00
|
|
|
return err
|
2018-07-31 09:54:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 11:12:25 +00: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 {
|
2020-08-25 14:19:59 +00:00
|
|
|
return fmt.Errorf("can't download MMDB: %s", err.Error())
|
2020-04-16 11:12:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !mmdb.Verify() {
|
|
|
|
log.Warnln("MMDB invalid, remove and download")
|
|
|
|
if err := os.Remove(C.Path.MMDB()); err != nil {
|
2020-08-25 14:19:59 +00:00
|
|
|
return fmt.Errorf("can't remove invalid MMDB: %s", err.Error())
|
2020-04-16 11:12:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := downloadMMDB(C.Path.MMDB()); err != nil {
|
2020-08-25 14:19:59 +00:00
|
|
|
return fmt.Errorf("can't download MMDB: %s", err.Error())
|
2020-04-16 11:12:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-31 09:54:16 +00:00
|
|
|
// Init prepare necessary files
|
2018-11-21 05:47:46 +00:00
|
|
|
func Init(dir string) error {
|
2018-10-14 13:22:58 +00:00
|
|
|
// initial homedir
|
2018-11-21 05:47:46 +00:00
|
|
|
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
|
|
|
if err := os.MkdirAll(dir, 0777); err != nil {
|
2020-08-25 14:19:59 +00:00
|
|
|
return fmt.Errorf("can't create config directory %s: %s", dir, err.Error())
|
2018-10-14 13:22:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 12:55:26 +00:00
|
|
|
// initial config.yaml
|
2018-10-14 13:22:58 +00:00
|
|
|
if _, err := os.Stat(C.Path.Config()); os.IsNotExist(err) {
|
2020-02-18 05:48:15 +00: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 14:19:59 +00:00
|
|
|
return fmt.Errorf("can't create file %s: %s", C.Path.Config(), err.Error())
|
2020-02-18 05:48:15 +00:00
|
|
|
}
|
|
|
|
f.Write([]byte(`port: 7890`))
|
|
|
|
f.Close()
|
2018-07-31 09:54:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// initial mmdb
|
2020-04-16 11:12:25 +00:00
|
|
|
if err := initMMDB(); err != nil {
|
2020-08-25 14:19:59 +00:00
|
|
|
return fmt.Errorf("can't initial MMDB: %w", err)
|
2018-07-31 09:54:16 +00:00
|
|
|
}
|
2018-11-21 05:47:46 +00:00
|
|
|
return nil
|
2018-07-31 09:54:16 +00:00
|
|
|
}
|