2018-10-14 21:22:58 +08:00
|
|
|
package constant
|
|
|
|
|
|
|
|
import (
|
2022-02-06 01:59:35 +08:00
|
|
|
"io/ioutil"
|
2018-10-14 21:22:58 +08:00
|
|
|
"os"
|
|
|
|
P "path"
|
2019-12-01 13:22:47 +08:00
|
|
|
"path/filepath"
|
2022-02-06 01:59:35 +08:00
|
|
|
"strings"
|
2018-10-14 21:22:58 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const Name = "clash"
|
|
|
|
|
2022-02-06 01:59:35 +08:00
|
|
|
var (
|
|
|
|
GeositeName = "GeoSite.dat"
|
|
|
|
GeoipName = "GeoIP.dat"
|
|
|
|
)
|
|
|
|
|
2018-10-14 21:22:58 +08:00
|
|
|
// Path is used to get the configuration path
|
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)
|
2021-07-11 19:42:54 +08:00
|
|
|
return &path{homeDir: homeDir, configFile: "config.yaml"}
|
|
|
|
}()
|
|
|
|
|
|
|
|
type path struct {
|
|
|
|
homeDir string
|
|
|
|
configFile string
|
2021-11-17 16:03:47 +08:00
|
|
|
scriptDir 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
|
|
|
|
}
|
|
|
|
|
2018-10-14 21:22:58 +08:00
|
|
|
func (p *path) MMDB() string {
|
2022-03-15 01:30:17 +08:00
|
|
|
files, err := ioutil.ReadDir(p.homeDir)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
for _, fi := range files {
|
|
|
|
if fi.IsDir() {
|
|
|
|
// 目录则直接跳过
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
if strings.EqualFold(fi.Name(), "Country.mmdb") {
|
|
|
|
GeoipName = fi.Name()
|
|
|
|
return P.Join(p.homeDir, fi.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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")
|
|
|
|
}
|
2021-11-17 16:03:47 +08:00
|
|
|
|
|
|
|
func (p *path) GeoIP() string {
|
2022-02-06 01:59:35 +08:00
|
|
|
files, err := ioutil.ReadDir(p.homeDir)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
for _, fi := range files {
|
|
|
|
if fi.IsDir() {
|
|
|
|
// 目录则直接跳过
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
if strings.EqualFold(fi.Name(), "GeoIP.dat") {
|
|
|
|
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-02-06 01:59:35 +08:00
|
|
|
files, err := ioutil.ReadDir(p.homeDir)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
for _, fi := range files {
|
|
|
|
if fi.IsDir() {
|
|
|
|
// 目录则直接跳过
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
if strings.EqualFold(fi.Name(), "GeoSite.dat") {
|
|
|
|
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) ScriptDir() string {
|
|
|
|
if len(p.scriptDir) != 0 {
|
|
|
|
return p.scriptDir
|
|
|
|
}
|
|
|
|
if dir, err := os.MkdirTemp("", Name+"-"); err == nil {
|
|
|
|
p.scriptDir = dir
|
|
|
|
} else {
|
|
|
|
p.scriptDir = P.Join(os.TempDir(), Name)
|
|
|
|
_ = os.MkdirAll(p.scriptDir, 0o644)
|
|
|
|
}
|
|
|
|
return p.scriptDir
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *path) Script() string {
|
|
|
|
return P.Join(p.ScriptDir(), "clash_script.py")
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|