2018-07-31 09:54:16 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2018-11-21 05:47:46 +00:00
|
|
|
"fmt"
|
2022-03-15 14:25:33 +00:00
|
|
|
"github.com/Dreamacro/clash/component/geodata"
|
2022-03-14 17:30:17 +00:00
|
|
|
"github.com/Dreamacro/clash/component/mmdb"
|
2018-07-31 09:54:16 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2022-03-16 14:55:18 +00:00
|
|
|
var initMode = true
|
|
|
|
|
2022-03-14 17:30:17 +00:00
|
|
|
func downloadMMDB(path string) (err error) {
|
2022-05-24 07:04:13 +00:00
|
|
|
resp, err := http.Get(C.MmdbUrl)
|
2022-03-14 17:30:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = io.Copy(f, resp.Body)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-01-26 04:01:14 +00:00
|
|
|
func downloadGeoIP(path string) (err error) {
|
2022-05-24 07:04:13 +00:00
|
|
|
resp, err := http.Get(C.GeoIpUrl)
|
2018-07-31 09:54:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2021-10-10 15:44:09 +00:00
|
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644)
|
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
|
|
|
}
|
|
|
|
|
2021-11-17 08:03:47 +00:00
|
|
|
func downloadGeoSite(path string) (err error) {
|
2022-05-24 07:04:13 +00:00
|
|
|
resp, err := http.Get(C.GeoSiteUrl)
|
2021-11-17 08:03:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0o644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = io.Copy(f, resp.Body)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-05 20:41:34 +00:00
|
|
|
func initGeoSite() error {
|
2021-11-17 08:03:47 +00:00
|
|
|
if _, err := os.Stat(C.Path.GeoSite()); os.IsNotExist(err) {
|
2021-07-01 14:49:29 +00:00
|
|
|
log.Infoln("Can't find GeoSite.dat, start download")
|
2021-11-17 08:03:47 +00:00
|
|
|
if err := downloadGeoSite(C.Path.GeoSite()); err != nil {
|
|
|
|
return fmt.Errorf("can't download GeoSite.dat: %s", err.Error())
|
|
|
|
}
|
|
|
|
log.Infoln("Download GeoSite.dat finish")
|
|
|
|
}
|
2022-03-16 14:55:18 +00:00
|
|
|
if initMode {
|
2022-05-24 07:04:13 +00:00
|
|
|
if err := geodata.Verify(C.GeositeName); err != nil {
|
|
|
|
log.Warnln("GeoSite.dat invalid, remove and download: %s", err)
|
2022-03-16 14:55:18 +00:00
|
|
|
if err := os.Remove(C.Path.GeoSite()); err != nil {
|
|
|
|
return fmt.Errorf("can't remove invalid GeoSite.dat: %s", err.Error())
|
|
|
|
}
|
|
|
|
if err := downloadGeoSite(C.Path.GeoSite()); err != nil {
|
|
|
|
return fmt.Errorf("can't download GeoSite.dat: %s", err.Error())
|
|
|
|
}
|
2022-03-15 14:25:33 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-17 08:03:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-26 04:01:14 +00:00
|
|
|
func initGeoIP() error {
|
2022-03-16 09:29:09 +00:00
|
|
|
if C.GeodataMode {
|
2022-03-15 14:25:33 +00:00
|
|
|
if _, err := os.Stat(C.Path.GeoIP()); os.IsNotExist(err) {
|
2022-03-17 09:41:02 +00:00
|
|
|
log.Infoln("Can't find GeoIP.dat, start download")
|
2022-03-15 14:25:33 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2022-05-24 07:04:13 +00:00
|
|
|
if err := geodata.Verify(C.GeoipName); err != nil {
|
|
|
|
log.Warnln("GeoIP.dat invalid, remove and download: %s", err)
|
2022-03-15 14:25:33 +00:00
|
|
|
if err := os.Remove(C.Path.GeoIP()); err != nil {
|
|
|
|
return fmt.Errorf("can't remove invalid GeoIP.dat: %s", err.Error())
|
|
|
|
}
|
|
|
|
if err := downloadGeoIP(C.Path.GeoIP()); err != nil {
|
|
|
|
return fmt.Errorf("can't download GeoIP.dat: %s", err.Error())
|
|
|
|
}
|
2022-01-26 04:01:14 +00:00
|
|
|
}
|
2022-03-15 14:25:33 +00:00
|
|
|
return nil
|
2022-01-26 04:01:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 17:30:17 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 04:01:14 +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) {
|
2021-10-10 15:44:09 +00:00
|
|
|
if err := os.MkdirAll(dir, 0o777); 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")
|
2021-10-10 15:44:09 +00:00
|
|
|
f, err := os.OpenFile(C.Path.Config(), os.O_CREATE|os.O_WRONLY, 0o644)
|
2020-02-18 05:48:15 +00:00
|
|
|
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
|
|
|
}
|
2021-04-01 07:35:33 +00:00
|
|
|
f.Write([]byte(`mixed-port: 7890`))
|
2020-02-18 05:48:15 +00:00
|
|
|
f.Close()
|
2018-07-31 09:54:16 +00:00
|
|
|
}
|
2022-03-16 13:47:00 +00:00
|
|
|
buf, _ := os.ReadFile(C.Path.Config())
|
|
|
|
rawCfg, err := UnmarshalRawConfig(buf)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln(err.Error())
|
|
|
|
fmt.Printf("configuration file %s test failed\n", C.Path.Config())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if !C.GeodataMode {
|
|
|
|
C.GeodataMode = rawCfg.GeodataMode
|
|
|
|
}
|
2022-05-27 04:16:04 +00:00
|
|
|
C.GeoIpUrl = rawCfg.GeoXUrl.GeoIp
|
|
|
|
C.GeoSiteUrl = rawCfg.GeoXUrl.GeoSite
|
|
|
|
C.MmdbUrl = rawCfg.GeoXUrl.Mmdb
|
2022-01-26 04:01:14 +00:00
|
|
|
// initial GeoIP
|
|
|
|
if err := initGeoIP(); err != nil {
|
|
|
|
return fmt.Errorf("can't initial GeoIP: %w", err)
|
2018-07-31 09:54:16 +00:00
|
|
|
}
|
2021-11-17 08:03:47 +00:00
|
|
|
|
2018-11-21 05:47:46 +00:00
|
|
|
return nil
|
2018-07-31 09:54:16 +00:00
|
|
|
}
|