mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-12-24 12:12:43 +08:00
Some checks failed
Build / build (map[abi:1 debian:loongarch64 goarch:loong64 goos:linux output:loong64-abi1 rpm:loongarch64]) (push) Has been cancelled
Build / build (map[abi:2 debian:loong64 goarch:loong64 goos:linux output:loong64-abi2 rpm:loong64]) (push) Has been cancelled
Build / build (map[debian:amd64 goamd64:v3 goarch:amd64 goos:linux output:amd64 pacman:x86_64 rpm:x86_64]) (push) Has been cancelled
Build / build (map[debian:arm64 goarch:arm64 goos:linux output:arm64 pacman:aarch64 rpm:aarch64]) (push) Has been cancelled
Build / build (map[debian:armel goarch:arm goarm:6 goos:linux output:armv6 rpm:armv6hl]) (push) Has been cancelled
Build / build (map[debian:armhf goarch:arm goarm:7 goos:linux output:armv7 pacman:armv7hl rpm:armv7hl]) (push) Has been cancelled
Build / build (map[debian:i386 go386:sse2 goarch:386 goos:linux output:386 rpm:i386]) (push) Has been cancelled
Build / build (map[debian:mips64el goarch:mips64le goos:linux output:mips64le rpm:mips64el]) (push) Has been cancelled
Build / build (map[debian:ppc64el goarch:ppc64le goos:linux output:ppc64le rpm:ppc64le]) (push) Has been cancelled
Build / build (map[debian:riscv64 goarch:riscv64 goos:linux output:riscv64 rpm:riscv64]) (push) Has been cancelled
Build / build (map[debian:s390x goarch:s390x goos:linux output:s390x rpm:s390x]) (push) Has been cancelled
Build / build (map[go386:softfloat goarch:386 goos:linux output:386-softfloat]) (push) Has been cancelled
Build / build (map[goamd64:v1 goarch:amd64 goos:darwin goversion:1.20 output:amd64-compatible-go120]) (push) Has been cancelled
Build / build (map[goamd64:v1 goarch:amd64 goos:darwin goversion:1.22 output:amd64-compatible-go122]) (push) Has been cancelled
Build / build (map[goamd64:v1 goarch:amd64 goos:darwin output:amd64-compatible]) (push) Has been cancelled
Build / build (map[goamd64:v1 goarch:amd64 goos:freebsd output:amd64-compatible]) (push) Has been cancelled
Build / build (map[goamd64:v1 goarch:amd64 goos:linux goversion:1.20 output:amd64-compatible-go120 test:test]) (push) Has been cancelled
Build / build (map[goamd64:v1 goarch:amd64 goos:linux goversion:1.23 output:amd64-compatible-go123 test:test]) (push) Has been cancelled
Build / build (map[goamd64:v1 goarch:amd64 goos:linux output:amd64-compatible test:test]) (push) Has been cancelled
Build / build (map[goamd64:v1 goarch:amd64 goos:windows goversion:1.20 output:amd64-compatible-go120]) (push) Has been cancelled
Build / build (map[goamd64:v1 goarch:amd64 goos:windows goversion:1.21 output:amd64-compatible-go121]) (push) Has been cancelled
Build / build (map[goamd64:v1 goarch:amd64 goos:windows goversion:1.22 output:amd64-compatible-go122]) (push) Has been cancelled
Build / build (map[goamd64:v1 goarch:amd64 goos:windows goversion:1.23 output:amd64-compatible-go123]) (push) Has been cancelled
Build / build (map[goamd64:v1 goarch:amd64 goos:windows output:amd64-compatible]) (push) Has been cancelled
Build / build (map[goamd64:v3 goarch:amd64 goos:darwin goversion:1.20 output:amd64-go120]) (push) Has been cancelled
Build / build (map[goamd64:v3 goarch:amd64 goos:darwin goversion:1.22 output:amd64-go122]) (push) Has been cancelled
Build / build (map[goamd64:v3 goarch:amd64 goos:darwin output:amd64]) (push) Has been cancelled
Build / build (map[goamd64:v3 goarch:amd64 goos:freebsd output:amd64]) (push) Has been cancelled
Build / build (map[goamd64:v3 goarch:amd64 goos:linux goversion:1.20 output:amd64-go120]) (push) Has been cancelled
Build / build (map[goamd64:v3 goarch:amd64 goos:linux goversion:1.23 output:amd64-go123]) (push) Has been cancelled
Build / build (map[goamd64:v3 goarch:amd64 goos:windows goversion:1.20 output:amd64-go120]) (push) Has been cancelled
Build / build (map[goamd64:v3 goarch:amd64 goos:windows goversion:1.21 output:amd64-go121]) (push) Has been cancelled
Build / build (map[goamd64:v3 goarch:amd64 goos:windows goversion:1.22 output:amd64-go122]) (push) Has been cancelled
Build / build (map[goamd64:v3 goarch:amd64 goos:windows goversion:1.23 output:amd64-go123]) (push) Has been cancelled
Build / build (map[goamd64:v3 goarch:amd64 goos:windows output:amd64]) (push) Has been cancelled
Build / build (map[goarch:386 goos:android ndk:i686-linux-android34 output:386]) (push) Has been cancelled
Build / build (map[goarch:386 goos:freebsd output:386]) (push) Has been cancelled
Build / build (map[goarch:386 goos:linux goversion:1.20 output:386-go120]) (push) Has been cancelled
Build / build (map[goarch:386 goos:linux goversion:1.23 output:386-go123]) (push) Has been cancelled
Build / build (map[goarch:386 goos:windows goversion:1.20 output:386-go120]) (push) Has been cancelled
Build / build (map[goarch:386 goos:windows goversion:1.21 output:386-go121]) (push) Has been cancelled
Build / build (map[goarch:386 goos:windows goversion:1.22 output:386-go122]) (push) Has been cancelled
Build / build (map[goarch:386 goos:windows goversion:1.23 output:386-go123]) (push) Has been cancelled
Build / build (map[goarch:386 goos:windows output:386]) (push) Has been cancelled
Build / build (map[goarch:amd64 goos:android ndk:x86_64-linux-android34 output:amd64]) (push) Has been cancelled
Build / build (map[goarch:arm goarm:5 goos:linux output:armv5]) (push) Has been cancelled
Build / build (map[goarch:arm goos:android ndk:armv7a-linux-androideabi34 output:armv7]) (push) Has been cancelled
Build / build (map[goarch:arm64 goos:android ndk:aarch64-linux-android34 output:arm64-v8]) (push) Has been cancelled
Build / build (map[goarch:arm64 goos:darwin goversion:1.20 output:arm64-go120]) (push) Has been cancelled
Build / build (map[goarch:arm64 goos:darwin goversion:1.22 output:arm64-go122]) (push) Has been cancelled
Build / build (map[goarch:arm64 goos:darwin output:arm64]) (push) Has been cancelled
Build / build (map[goarch:arm64 goos:freebsd output:arm64]) (push) Has been cancelled
Build / build (map[goarch:arm64 goos:windows output:arm64]) (push) Has been cancelled
Build / build (map[goarch:mips gomips:hardfloat goos:linux output:mips-hardfloat]) (push) Has been cancelled
Build / build (map[goarch:mips gomips:softfloat goos:linux output:mips-softfloat]) (push) Has been cancelled
Build / build (map[goarch:mips64 goos:linux output:mips64]) (push) Has been cancelled
Build / build (map[goarch:mipsle gomips:hardfloat goos:linux output:mipsle-hardfloat]) (push) Has been cancelled
Build / build (map[goarch:mipsle gomips:softfloat goos:linux output:mipsle-softfloat]) (push) Has been cancelled
Build / Upload-Prerelease (push) Has been cancelled
Build / Upload-Release (push) Has been cancelled
Build / Docker (push) Has been cancelled
Test / test (1.20, macos-13) (push) Has been cancelled
Test / test (1.20, macos-latest) (push) Has been cancelled
Test / test (1.20, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.20, ubuntu-latest) (push) Has been cancelled
Test / test (1.20, windows-latest) (push) Has been cancelled
Test / test (1.21, macos-13) (push) Has been cancelled
Test / test (1.21, macos-latest) (push) Has been cancelled
Test / test (1.21, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.21, ubuntu-latest) (push) Has been cancelled
Test / test (1.21, windows-latest) (push) Has been cancelled
Test / test (1.22, macos-13) (push) Has been cancelled
Test / test (1.22, macos-latest) (push) Has been cancelled
Test / test (1.22, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.22, ubuntu-latest) (push) Has been cancelled
Test / test (1.22, windows-latest) (push) Has been cancelled
Test / test (1.23, macos-13) (push) Has been cancelled
Test / test (1.23, macos-latest) (push) Has been cancelled
Test / test (1.23, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.23, ubuntu-latest) (push) Has been cancelled
Test / test (1.23, windows-latest) (push) Has been cancelled
Test / test (1.24, macos-13) (push) Has been cancelled
Test / test (1.24, macos-latest) (push) Has been cancelled
Test / test (1.24, ubuntu-24.04-arm) (push) Has been cancelled
Test / test (1.24, ubuntu-latest) (push) Has been cancelled
Test / test (1.24, windows-latest) (push) Has been cancelled
Trigger CMFA Update / trigger-CMFA-update (push) Has been cancelled
223 lines
4.7 KiB
Go
223 lines
4.7 KiB
Go
package constant
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
P "path"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/metacubex/mihomo/common/utils"
|
|
"github.com/metacubex/mihomo/constant/features"
|
|
)
|
|
|
|
const Name = "mihomo"
|
|
|
|
var (
|
|
GeositeName = "GeoSite.dat"
|
|
GeoipName = "GeoIP.dat"
|
|
ASNName = "ASN.mmdb"
|
|
)
|
|
|
|
// Path is used to get the configuration path
|
|
//
|
|
// on Unix systems, `$HOME/.config/mihomo`.
|
|
// on Windows, `%USERPROFILE%/.config/mihomo`.
|
|
var Path = func() *path {
|
|
homeDir, err := os.UserHomeDir()
|
|
if err != nil {
|
|
homeDir, _ = os.Getwd()
|
|
}
|
|
allowUnsafePath, _ := strconv.ParseBool(os.Getenv("SKIP_SAFE_PATH_CHECK"))
|
|
homeDir = P.Join(homeDir, ".config", Name)
|
|
|
|
if _, err = os.Stat(homeDir); err != nil {
|
|
if configHome, ok := os.LookupEnv("XDG_CONFIG_HOME"); ok {
|
|
homeDir = P.Join(configHome, Name)
|
|
}
|
|
}
|
|
|
|
var safePaths []string
|
|
for _, safePath := range filepath.SplitList(os.Getenv("SAFE_PATHS")) {
|
|
safePath = strings.TrimSpace(safePath)
|
|
if len(safePath) == 0 {
|
|
continue
|
|
}
|
|
safePaths = append(safePaths, safePath)
|
|
}
|
|
|
|
return &path{homeDir: homeDir, configFile: "config.yaml", allowUnsafePath: allowUnsafePath, safePaths: safePaths}
|
|
}()
|
|
|
|
type path struct {
|
|
homeDir string
|
|
configFile string
|
|
allowUnsafePath bool
|
|
safePaths []string
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
func (p *path) HomeDir() string {
|
|
return p.homeDir
|
|
}
|
|
|
|
func (p *path) Config() string {
|
|
return p.configFile
|
|
}
|
|
|
|
// 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 (or in the SAFE_PATHS environment variable)
|
|
func (p *path) IsSafePath(path string) bool {
|
|
if p.allowUnsafePath || features.CMFA {
|
|
return true
|
|
}
|
|
path = p.Resolve(path)
|
|
for _, safePath := range p.SafePaths() {
|
|
if rel, err := filepath.Rel(safePath, path); err == nil {
|
|
if filepath.IsLocal(rel) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (p *path) SafePaths() []string {
|
|
return append([]string{p.homeDir}, p.safePaths...) // add homedir to safePaths
|
|
}
|
|
|
|
func (p *path) ErrNotSafePath(path string) error {
|
|
return ErrNotSafePath{Path: path, SafePaths: p.SafePaths()}
|
|
}
|
|
|
|
type ErrNotSafePath struct {
|
|
Path string
|
|
SafePaths []string
|
|
}
|
|
|
|
func (e ErrNotSafePath) Error() string {
|
|
return fmt.Sprintf("path is not subpath of home directory or SAFE_PATHS: %s \n allowed paths: %s", e.Path, e.SafePaths)
|
|
}
|
|
|
|
func (p *path) GetPathByHash(prefix, name string) string {
|
|
hash := utils.MakeHash([]byte(name))
|
|
filename := hash.String()
|
|
return filepath.Join(p.HomeDir(), prefix, filename)
|
|
}
|
|
|
|
func (p *path) MMDB() 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(), "Country.mmdb") ||
|
|
strings.EqualFold(fi.Name(), "geoip.db") ||
|
|
strings.EqualFold(fi.Name(), "geoip.metadb") {
|
|
GeoipName = fi.Name()
|
|
return P.Join(p.homeDir, fi.Name())
|
|
}
|
|
}
|
|
}
|
|
return P.Join(p.homeDir, "geoip.metadb")
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func (p *path) OldCache() string {
|
|
return P.Join(p.homeDir, ".cache")
|
|
}
|
|
|
|
func (p *path) Cache() string {
|
|
return P.Join(p.homeDir, "cache.db")
|
|
}
|
|
|
|
func (p *path) GeoIP() 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(), "GeoIP.dat") {
|
|
GeoipName = fi.Name()
|
|
return P.Join(p.homeDir, fi.Name())
|
|
}
|
|
}
|
|
}
|
|
return P.Join(p.homeDir, "GeoIP.dat")
|
|
}
|
|
|
|
func (p *path) GeoSite() 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(), "GeoSite.dat") {
|
|
GeositeName = fi.Name()
|
|
return P.Join(p.homeDir, fi.Name())
|
|
}
|
|
}
|
|
}
|
|
return P.Join(p.homeDir, "GeoSite.dat")
|
|
}
|
|
|
|
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 "mihomo"
|
|
}
|
|
res, _ := filepath.EvalSymlinks(exePath)
|
|
return res
|
|
}
|