2018-10-14 21:22:58 +08:00
|
|
|
package constant
|
|
|
|
|
|
|
|
import (
|
2023-06-15 22:45:02 +08:00
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
2018-10-14 21:22:58 +08:00
|
|
|
"os"
|
|
|
|
P "path"
|
2019-12-01 13:22:47 +08:00
|
|
|
"path/filepath"
|
2023-06-29 16:40:08 +08:00
|
|
|
"strconv"
|
2022-02-06 01:59:35 +08:00
|
|
|
"strings"
|
2018-10-14 21:22:58 +08:00
|
|
|
)
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
const Name = "mihomo"
|
2018-10-14 21:22:58 +08:00
|
|
|
|
2022-02-06 01:59:35 +08:00
|
|
|
var (
|
|
|
|
GeositeName = "GeoSite.dat"
|
|
|
|
GeoipName = "GeoIP.dat"
|
2024-03-12 03:14:25 +08:00
|
|
|
ASNName = "ASN.mmdb"
|
2022-02-06 01:59:35 +08:00
|
|
|
)
|
|
|
|
|
2018-10-14 21:22:58 +08:00
|
|
|
// Path is used to get the configuration path
|
2023-10-16 09:23:31 +08:00
|
|
|
//
|
2023-11-03 21:01:45 +08:00
|
|
|
// on Unix systems, `$HOME/.config/mihomo`.
|
|
|
|
// on Windows, `%USERPROFILE%/.config/mihomo`.
|
2021-07-11 19:42:54 +08:00
|
|
|
var Path = func() *path {
|
2019-10-14 18:11:22 +08:00
|
|
|
homeDir, err := os.UserHomeDir()
|
2018-10-14 21:22:58 +08:00
|
|
|
if err != nil {
|
2019-10-14 18:11:22 +08:00
|
|
|
homeDir, _ = os.Getwd()
|
2018-10-14 21:22:58 +08:00
|
|
|
}
|
2023-06-29 16:40:08 +08:00
|
|
|
allowUnsafePath, _ := strconv.ParseBool(os.Getenv("SKIP_SAFE_PATH_CHECK"))
|
2019-10-14 18:11:22 +08:00
|
|
|
homeDir = P.Join(homeDir, ".config", Name)
|
2023-10-16 09:23:31 +08:00
|
|
|
|
|
|
|
if _, err = os.Stat(homeDir); err != nil {
|
|
|
|
if configHome, ok := os.LookupEnv("XDG_CONFIG_HOME"); ok {
|
|
|
|
homeDir = P.Join(configHome, Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-11 09:55:49 +08:00
|
|
|
return &path{homeDir: homeDir, configFile: "config.yaml", allowUnsafePath: allowUnsafePath}
|
2021-07-11 19:42:54 +08:00
|
|
|
}()
|
|
|
|
|
|
|
|
type path struct {
|
2023-06-11 09:55:49 +08:00
|
|
|
homeDir string
|
|
|
|
configFile string
|
|
|
|
allowUnsafePath bool
|
2018-10-14 21:22:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetHomeDir is used to set the configuration path
|
|
|
|
func SetHomeDir(root string) {
|
2019-10-14 18:11:22 +08:00
|
|
|
Path.homeDir = root
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetConfig is used to set the configuration file
|
|
|
|
func SetConfig(file string) {
|
|
|
|
Path.configFile = file
|
2018-10-14 21:22:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *path) HomeDir() string {
|
2019-10-14 18:11:22 +08:00
|
|
|
return p.homeDir
|
2018-10-14 21:22:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *path) Config() string {
|
2019-10-14 18:11:22 +08:00
|
|
|
return p.configFile
|
2018-10-14 21:22:58 +08:00
|
|
|
}
|
|
|
|
|
2020-01-30 17:03:11 +08:00
|
|
|
// Resolve return a absolute path or a relative path with homedir
|
|
|
|
func (p *path) Resolve(path string) string {
|
2019-12-01 13:22:47 +08:00
|
|
|
if !filepath.IsAbs(path) {
|
|
|
|
return filepath.Join(p.HomeDir(), path)
|
|
|
|
}
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2023-06-11 09:55:49 +08:00
|
|
|
// IsSafePath return true if path is a subpath of homedir
|
|
|
|
func (p *path) IsSafePath(path string) bool {
|
|
|
|
if p.allowUnsafePath {
|
|
|
|
return true
|
|
|
|
}
|
2023-06-06 09:45:05 +08:00
|
|
|
homedir := p.HomeDir()
|
|
|
|
path = p.Resolve(path)
|
|
|
|
rel, err := filepath.Rel(homedir, path)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return !strings.Contains(rel, "..")
|
|
|
|
}
|
|
|
|
|
2023-06-19 08:32:11 +08:00
|
|
|
func (p *path) GetPathByHash(prefix, name string) string {
|
2023-06-15 22:45:02 +08:00
|
|
|
hash := md5.Sum([]byte(name))
|
|
|
|
filename := hex.EncodeToString(hash[:])
|
|
|
|
return filepath.Join(p.HomeDir(), prefix, filename)
|
|
|
|
}
|
|
|
|
|
2018-10-14 21:22:58 +08:00
|
|
|
func (p *path) MMDB() string {
|
2022-08-11 23:56:50 +08:00
|
|
|
files, err := os.ReadDir(p.homeDir)
|
2022-03-15 01:30:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
for _, fi := range files {
|
|
|
|
if fi.IsDir() {
|
|
|
|
// 目录则直接跳过
|
|
|
|
continue
|
|
|
|
} else {
|
2023-07-14 22:28:24 +08:00
|
|
|
if strings.EqualFold(fi.Name(), "Country.mmdb") ||
|
2023-07-17 10:33:20 +08:00
|
|
|
strings.EqualFold(fi.Name(), "geoip.db") ||
|
|
|
|
strings.EqualFold(fi.Name(), "geoip.metadb") {
|
2022-03-15 01:30:17 +08:00
|
|
|
GeoipName = fi.Name()
|
|
|
|
return P.Join(p.homeDir, fi.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-20 23:24:48 +08:00
|
|
|
return P.Join(p.homeDir, "geoip.metadb")
|
2018-10-14 21:22:58 +08:00
|
|
|
}
|
2021-02-18 23:41:50 +08:00
|
|
|
|
2024-03-12 03:14:25 +08:00
|
|
|
func (p *path) ASN() string {
|
|
|
|
files, err := os.ReadDir(p.homeDir)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
for _, fi := range files {
|
|
|
|
if fi.IsDir() {
|
|
|
|
// 目录则直接跳过
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
if strings.EqualFold(fi.Name(), "ASN.mmdb") {
|
|
|
|
ASNName = fi.Name()
|
|
|
|
return P.Join(p.homeDir, fi.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return P.Join(p.homeDir, ASNName)
|
|
|
|
}
|
|
|
|
|
2021-10-04 19:20:11 +08:00
|
|
|
func (p *path) OldCache() string {
|
2021-02-18 23:41:50 +08:00
|
|
|
return P.Join(p.homeDir, ".cache")
|
|
|
|
}
|
2021-10-04 19:20:11 +08:00
|
|
|
|
|
|
|
func (p *path) Cache() string {
|
|
|
|
return P.Join(p.homeDir, "cache.db")
|
|
|
|
}
|
2021-11-17 16:03:47 +08:00
|
|
|
|
|
|
|
func (p *path) GeoIP() string {
|
2022-08-11 23:56:50 +08:00
|
|
|
files, err := os.ReadDir(p.homeDir)
|
2022-02-06 01:59:35 +08:00
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
for _, fi := range files {
|
|
|
|
if fi.IsDir() {
|
|
|
|
// 目录则直接跳过
|
|
|
|
continue
|
|
|
|
} else {
|
2023-03-29 14:03:13 +08:00
|
|
|
if strings.EqualFold(fi.Name(), "GeoIP.dat") {
|
2022-02-06 01:59:35 +08:00
|
|
|
GeoipName = fi.Name()
|
|
|
|
return P.Join(p.homeDir, fi.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 12:01:14 +08:00
|
|
|
return P.Join(p.homeDir, "GeoIP.dat")
|
2021-11-17 16:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *path) GeoSite() string {
|
2022-08-11 23:56:50 +08:00
|
|
|
files, err := os.ReadDir(p.homeDir)
|
2022-02-06 01:59:35 +08:00
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
for _, fi := range files {
|
|
|
|
if fi.IsDir() {
|
|
|
|
// 目录则直接跳过
|
|
|
|
continue
|
|
|
|
} else {
|
2023-03-29 14:03:13 +08:00
|
|
|
if strings.EqualFold(fi.Name(), "GeoSite.dat") {
|
2022-02-06 01:59:35 +08:00
|
|
|
GeositeName = fi.Name()
|
|
|
|
return P.Join(p.homeDir, fi.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 12:01:14 +08:00
|
|
|
return P.Join(p.homeDir, "GeoSite.dat")
|
2021-11-17 16:03:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *path) GetAssetLocation(file string) string {
|
|
|
|
return P.Join(p.homeDir, file)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *path) GetExecutableFullPath() string {
|
|
|
|
exePath, err := os.Executable()
|
|
|
|
if err != nil {
|
2023-11-03 21:01:45 +08:00
|
|
|
return "mihomo"
|
2021-11-17 16:03:47 +08:00
|
|
|
}
|
|
|
|
res, _ := filepath.EvalSymlinks(exePath)
|
|
|
|
return res
|
|
|
|
}
|