mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-11-16 19:56:51 +08:00
30f1b29257
# Conflicts: # .github/workflows/codeql-analysis.yml # .github/workflows/linter.yml # .github/workflows/release.yml # Makefile # README.md # adapter/outbound/vless.go # component/geodata/memconservative/cache.go # component/geodata/router/condition.go # component/geodata/router/condition_geoip.go # component/geodata/standard/standard.go # component/geodata/utils.go # config/config.go # config/initial.go # constant/metadata.go # constant/path.go # constant/rule.go # constant/rule_extra.go # dns/client.go # dns/filters.go # dns/resolver.go # go.mod # go.sum # hub/executor/executor.go # hub/route/configs.go # listener/listener.go # listener/tproxy/tproxy_linux_iptables.go # listener/tun/dev/dev.go # listener/tun/dev/dev_darwin.go # listener/tun/dev/dev_linux.go # listener/tun/dev/dev_windows.go # listener/tun/dev/wintun/config.go # listener/tun/dev/wintun/dll_windows.go # listener/tun/dev/wintun/session_windows.go # listener/tun/dev/wintun/wintun_windows.go # listener/tun/ipstack/commons/dns.go # listener/tun/ipstack/gvisor/tun.go # listener/tun/ipstack/gvisor/tundns.go # listener/tun/ipstack/gvisor/utils.go # listener/tun/ipstack/stack_adapter.go # listener/tun/ipstack/system/dns.go # listener/tun/ipstack/system/tcp.go # listener/tun/ipstack/system/tun.go # listener/tun/tun_adapter.go # main.go # rule/common/base.go # rule/common/domain.go # rule/common/domain_keyword.go # rule/common/domain_suffix.go # rule/common/final.go # rule/common/geoip.go # rule/common/geosite.go # rule/common/ipcidr.go # rule/common/port.go # rule/parser.go # rule/process.go # test/go.mod # test/go.sum # transport/vless/xtls.go # tunnel/tunnel.go
169 lines
4.3 KiB
Go
169 lines
4.3 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/Dreamacro/clash/component/geodata"
|
|
"github.com/Dreamacro/clash/component/mmdb"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
"github.com/Dreamacro/clash/log"
|
|
)
|
|
|
|
var initMode = true
|
|
|
|
func downloadMMDB(path string) (err error) {
|
|
resp, err := http.Get("https://raw.githubusercontents.com/Loyalsoldier/geoip/release/Country.mmdb")
|
|
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
|
|
}
|
|
|
|
func downloadGeoIP(path string) (err error) {
|
|
resp, err := http.Get("https://raw.githubusercontents.com/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, 0o644)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
_, err = io.Copy(f, resp.Body)
|
|
|
|
return err
|
|
}
|
|
|
|
func downloadGeoSite(path string) (err error) {
|
|
resp, err := http.Get("https://raw.githubusercontents.com/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, 0o644)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
_, err = io.Copy(f, resp.Body)
|
|
|
|
return err
|
|
}
|
|
|
|
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())
|
|
}
|
|
log.Infoln("Download GeoSite.dat finish")
|
|
}
|
|
if initMode {
|
|
if !geodata.Verify(C.GeositeName) {
|
|
log.Warnln("GeoSite.dat invalid, remove and download")
|
|
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())
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func initGeoIP() error {
|
|
if C.GeodataMode {
|
|
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")
|
|
}
|
|
|
|
if !geodata.Verify(C.GeoipName) {
|
|
log.Warnln("GeoIP.dat invalid, remove and download")
|
|
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())
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
// Init prepare necessary files
|
|
func Init(dir string) error {
|
|
// initial homedir
|
|
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
|
if err := os.MkdirAll(dir, 0o777); err != nil {
|
|
return fmt.Errorf("can't create config directory %s: %s", dir, err.Error())
|
|
}
|
|
}
|
|
|
|
// initial config.yaml
|
|
if _, err := os.Stat(C.Path.Config()); os.IsNotExist(err) {
|
|
log.Infoln("Can't find config, create a initial config file")
|
|
f, err := os.OpenFile(C.Path.Config(), os.O_CREATE|os.O_WRONLY, 0o644)
|
|
if err != nil {
|
|
return fmt.Errorf("can't create file %s: %s", C.Path.Config(), err.Error())
|
|
}
|
|
f.Write([]byte(`mixed-port: 7890`))
|
|
f.Close()
|
|
}
|
|
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
|
|
}
|
|
// initial GeoIP
|
|
if err := initGeoIP(); err != nil {
|
|
return fmt.Errorf("can't initial GeoIP: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|