package config import ( "io/ioutil" "os" "github.com/Dreamacro/clash/constant" ) type OverrideSlot int const ( OverrideSlotPersist OverrideSlot = iota OverrideSlotSession ) const defaultPersistOverride = `{"dns":{"enable": false}, "redir-port": 0, "tproxy-port": 0}` const defaultSessionOverride = `{}` var sessionOverride = defaultSessionOverride func overridePersistPath() string { return constant.Path.Resolve("override.json") } func ReadOverride(slot OverrideSlot) string { switch slot { case OverrideSlotPersist: file, err := os.OpenFile(overridePersistPath(), os.O_RDONLY, 0600) if err != nil { return defaultPersistOverride } buf, err := ioutil.ReadAll(file) if err != nil { return defaultPersistOverride } return string(buf) case OverrideSlotSession: return sessionOverride } return "" } func WriteOverride(slot OverrideSlot, content string) { switch slot { case OverrideSlotPersist: file, err := os.OpenFile(overridePersistPath(), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0600) if err != nil { return } _, err = file.Write([]byte(content)) case OverrideSlotSession: sessionOverride = content } } func ClearOverride(slot OverrideSlot) { switch slot { case OverrideSlotPersist: _ = os.Remove(overridePersistPath()) case OverrideSlotSession: sessionOverride = defaultSessionOverride } }