diff --git a/constant/path.go b/constant/path.go index 1594441c..462da13d 100644 --- a/constant/path.go +++ b/constant/path.go @@ -37,12 +37,13 @@ var Path = func() *path { } } - return &path{homeDir: homeDir, configFile: "config.yaml", allowUnsafePath: allowUnsafePath} + return &path{homeDir: homeDir, configFile: "config.yaml", cacheFile: "cache.db", allowUnsafePath: allowUnsafePath} }() type path struct { homeDir string configFile string + cacheFile string allowUnsafePath bool } @@ -51,6 +52,11 @@ func SetHomeDir(root string) { Path.homeDir = root } +// SetCache is used to set the cache file path +func SetCache(file string) { + Path.cacheFile = file +} + // SetConfig is used to set the configuration file func SetConfig(file string) { Path.configFile = file @@ -138,7 +144,7 @@ func (p *path) OldCache() string { } func (p *path) Cache() string { - return P.Join(p.homeDir, "cache.db") + return p.cacheFile } func (p *path) GeoIP() string { diff --git a/main.go b/main.go index 685fc89f..7dd3606f 100644 --- a/main.go +++ b/main.go @@ -31,6 +31,7 @@ var ( testConfig bool geodataMode bool homeDir string + cacheFile string configFile string configString string configBytes []byte @@ -45,6 +46,7 @@ func init() { flag.StringVar(&homeDir, "d", os.Getenv("CLASH_HOME_DIR"), "set configuration directory") flag.StringVar(&configFile, "f", os.Getenv("CLASH_CONFIG_FILE"), "specify configuration file") flag.StringVar(&configString, "config", os.Getenv("CLASH_CONFIG_STRING"), "specify base64-encoded configuration string") + flag.StringVar(&cacheFile, "cache", os.Getenv("CLASH_CACHE_FILE"), "specify cache file") flag.StringVar(&externalUI, "ext-ui", os.Getenv("CLASH_OVERRIDE_EXTERNAL_UI_DIR"), "override external ui directory") flag.StringVar(&externalController, "ext-ctl", os.Getenv("CLASH_OVERRIDE_EXTERNAL_CONTROLLER"), "override external controller address") flag.StringVar(&externalControllerUnix, "ext-ctl-unix", os.Getenv("CLASH_OVERRIDE_EXTERNAL_CONTROLLER_UNIX"), "override external controller unix address") @@ -88,6 +90,16 @@ func main() { C.SetHomeDir(homeDir) } + if cacheFile != "" { + if !filepath.IsAbs(cacheFile) { + currentDir, _ := os.Getwd() + cacheFile = filepath.Join(currentDir, cacheFile) + } + } else { + cacheFile = filepath.Join(C.Path.HomeDir(), C.Path.Cache()) + } + C.SetCache(cacheFile) + if geodataMode { geodata.SetGeodataMode(true) }