43 lines
1.0 KiB
Go
43 lines
1.0 KiB
Go
package app
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
)
|
|
|
|
func loadSingBoxProfilesState() singBoxProfilesState {
|
|
st := singBoxProfilesState{
|
|
Version: singBoxProfilesStateVersion,
|
|
Revision: 0,
|
|
}
|
|
data, err := os.ReadFile(singBoxProfilesStatePath)
|
|
if err != nil {
|
|
return st
|
|
}
|
|
if err := json.Unmarshal(data, &st); err != nil {
|
|
return singBoxProfilesState{Version: singBoxProfilesStateVersion, Revision: 0}
|
|
}
|
|
return normalizeSingBoxProfilesState(st)
|
|
}
|
|
|
|
func saveSingBoxProfilesState(st singBoxProfilesState) error {
|
|
st = normalizeSingBoxProfilesState(st)
|
|
st.Version = singBoxProfilesStateVersion
|
|
st.UpdatedAt = time.Now().UTC().Format(time.RFC3339)
|
|
|
|
data, err := json.MarshalIndent(st, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := os.MkdirAll(filepath.Dir(singBoxProfilesStatePath), 0o755); err != nil {
|
|
return err
|
|
}
|
|
tmp := singBoxProfilesStatePath + ".tmp"
|
|
if err := os.WriteFile(tmp, data, 0o644); err != nil {
|
|
return err
|
|
}
|
|
return os.Rename(tmp, singBoxProfilesStatePath)
|
|
}
|