clash/constant/path.go

163 lines
3.2 KiB
Go
Raw Normal View History

2018-10-14 13:22:58 +00:00
package constant
import (
2023-06-15 14:45:02 +00:00
"crypto/md5"
"encoding/hex"
2018-10-14 13:22:58 +00:00
"os"
P "path"
"path/filepath"
2023-06-29 08:40:08 +00:00
"strconv"
2022-02-05 17:59:35 +00:00
"strings"
2018-10-14 13:22:58 +00:00
)
const Name = "clash"
2022-02-05 17:59:35 +00:00
var (
GeositeName = "GeoSite.dat"
GeoipName = "GeoIP.dat"
)
2018-10-14 13:22:58 +00:00
// Path is used to get the configuration path
var Path = func() *path {
homeDir, err := os.UserHomeDir()
2018-10-14 13:22:58 +00:00
if err != nil {
homeDir, _ = os.Getwd()
2018-10-14 13:22:58 +00:00
}
2023-06-29 08:40:08 +00:00
allowUnsafePath, _ := strconv.ParseBool(os.Getenv("SKIP_SAFE_PATH_CHECK"))
homeDir = P.Join(homeDir, ".config", Name)
return &path{homeDir: homeDir, configFile: "config.yaml", allowUnsafePath: allowUnsafePath}
}()
type path struct {
homeDir string
configFile string
allowUnsafePath bool
2018-10-14 13:22:58 +00:00
}
// SetHomeDir is used to set the configuration path
func SetHomeDir(root string) {
Path.homeDir = root
}
// SetConfig is used to set the configuration file
func SetConfig(file string) {
Path.configFile = file
2018-10-14 13:22:58 +00:00
}
func (p *path) HomeDir() string {
return p.homeDir
2018-10-14 13:22:58 +00:00
}
func (p *path) Config() string {
return p.configFile
2018-10-14 13:22:58 +00:00
}
2020-01-30 09:03:11 +00:00
// Resolve return a absolute path or a relative path with homedir
func (p *path) Resolve(path string) string {
if !filepath.IsAbs(path) {
return filepath.Join(p.HomeDir(), path)
}
return path
}
// IsSafePath return true if path is a subpath of homedir
func (p *path) IsSafePath(path string) bool {
if p.allowUnsafePath {
return true
}
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 00:32:11 +00:00
func (p *path) GetPathByHash(prefix, name string) string {
2023-06-15 14:45:02 +00:00
hash := md5.Sum([]byte(name))
filename := hex.EncodeToString(hash[:])
return filepath.Join(p.HomeDir(), prefix, filename)
}
2018-10-14 13:22:58 +00:00
func (p *path) MMDB() string {
2022-08-11 15:56:50 +00:00
files, err := os.ReadDir(p.homeDir)
2022-03-14 17:30:17 +00:00
if err != nil {
return ""
}
for _, fi := range files {
if fi.IsDir() {
// 目录则直接跳过
continue
} else {
2023-07-14 14:28:24 +00:00
if strings.EqualFold(fi.Name(), "Country.mmdb") ||
strings.EqualFold(fi.Name(), "geoip.db") ||
strings.EqualFold(fi.Name(), "geoip.metadb") {
2022-03-14 17:30:17 +00:00
GeoipName = fi.Name()
return P.Join(p.homeDir, fi.Name())
}
}
}
2023-07-20 15:24:48 +00:00
return P.Join(p.homeDir, "geoip.metadb")
2018-10-14 13:22:58 +00:00
}
2021-10-04 11:20:11 +00:00
func (p *path) OldCache() string {
return P.Join(p.homeDir, ".cache")
}
2021-10-04 11:20:11 +00:00
func (p *path) Cache() string {
return P.Join(p.homeDir, "cache.db")
}
2021-11-17 08:03:47 +00:00
func (p *path) GeoIP() string {
2022-08-11 15:56:50 +00:00
files, err := os.ReadDir(p.homeDir)
2022-02-05 17:59:35 +00:00
if err != nil {
return ""
}
for _, fi := range files {
if fi.IsDir() {
// 目录则直接跳过
continue
} else {
2023-03-29 06:03:13 +00:00
if strings.EqualFold(fi.Name(), "GeoIP.dat") {
2022-02-05 17:59:35 +00:00
GeoipName = fi.Name()
return P.Join(p.homeDir, fi.Name())
}
}
}
return P.Join(p.homeDir, "GeoIP.dat")
2021-11-17 08:03:47 +00:00
}
func (p *path) GeoSite() string {
2022-08-11 15:56:50 +00:00
files, err := os.ReadDir(p.homeDir)
2022-02-05 17:59:35 +00:00
if err != nil {
return ""
}
for _, fi := range files {
if fi.IsDir() {
// 目录则直接跳过
continue
} else {
2023-03-29 06:03:13 +00:00
if strings.EqualFold(fi.Name(), "GeoSite.dat") {
2022-02-05 17:59:35 +00:00
GeositeName = fi.Name()
return P.Join(p.homeDir, fi.Name())
}
}
}
return P.Join(p.homeDir, "GeoSite.dat")
2021-11-17 08:03:47 +00: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 {
return "clash"
}
res, _ := filepath.EvalSymlinks(exePath)
return res
}