2018-10-14 21:22:58 +08:00
|
|
|
package constant
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
P "path"
|
2019-12-01 13:22:47 +08:00
|
|
|
"path/filepath"
|
2023-04-16 20:14:36 +08:00
|
|
|
"strings"
|
2018-10-14 21:22:58 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const Name = "clash"
|
|
|
|
|
|
|
|
// Path is used to get the configuration path
|
2023-08-28 22:47:56 +08:00
|
|
|
//
|
|
|
|
// on Unix systems, `$HOME/.config/clash`.
|
|
|
|
// on Windows, `%USERPROFILE%/.config/clash`.
|
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
|
|
|
}
|
2019-02-27 01:02:43 +08:00
|
|
|
|
2019-10-14 18:11:22 +08:00
|
|
|
homeDir = P.Join(homeDir, ".config", Name)
|
2023-08-28 22:47:56 +08:00
|
|
|
|
|
|
|
if _, err = os.Stat(homeDir); err != nil {
|
|
|
|
if configHome, ok := os.LookupEnv("XDG_CONFIG_HOME"); ok {
|
|
|
|
homeDir = P.Join(configHome, Name)
|
|
|
|
}
|
|
|
|
}
|
2021-07-11 19:42:54 +08:00
|
|
|
return &path{homeDir: homeDir, configFile: "config.yaml"}
|
|
|
|
}()
|
|
|
|
|
|
|
|
type path struct {
|
|
|
|
homeDir string
|
|
|
|
configFile string
|
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-04-16 20:14:36 +08:00
|
|
|
// IsSubPath return true if path is a subpath of homedir
|
|
|
|
func (p *path) IsSubPath(path string) bool {
|
|
|
|
homedir := p.HomeDir()
|
|
|
|
path = p.Resolve(path)
|
|
|
|
rel, err := filepath.Rel(homedir, path)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return !strings.Contains(rel, "..")
|
|
|
|
}
|
|
|
|
|
2018-10-14 21:22:58 +08:00
|
|
|
func (p *path) MMDB() string {
|
2019-10-14 18:11:22 +08:00
|
|
|
return P.Join(p.homeDir, "Country.mmdb")
|
2018-10-14 21:22:58 +08:00
|
|
|
}
|
2021-02-18 23:41:50 +08:00
|
|
|
|
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")
|
|
|
|
}
|