-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
131 lines (108 loc) · 2.62 KB
/
config.go
File metadata and controls
131 lines (108 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"encoding/json"
"fmt"
"os"
"sync"
"github.com/astaxie/beego/logs"
)
type ModeType int
const (
ModeLocal ModeType = iota
ModeDomain
ModeAuto
ModeProxy
)
type Remote struct {
Name string `json:"name"`
Address string `json:"address"`
Protocol string `json:"protocol"`
Auth bool `json:"auth"`
User string `json:"user"`
Password string `json:"password"`
}
type Config struct {
Address string `json:"address"`
Port int `json:"port"`
Mode ModeType `json:"mode"` // 0: local, 1: domain, 2: auto, 3: proxy
TestUrl string `json:"testUrl"`
RemoteName string `json:"remote"`
RemoteList []Remote `json:"remoteList"`
DomainList []string `json:"domainList"`
}
var configCache = Config{
Address: "0.0.0.0",
Port: 8080,
Mode: ModeLocal,
TestUrl: "https://google.com/",
RemoteName: "",
DomainList: make([]string, 0),
RemoteList: make([]Remote, 0),
}
var configFilePath string
var configLock sync.Mutex
func configSyncToFile() error {
configLock.Lock()
defer configLock.Unlock()
value, err := json.MarshalIndent(configCache, "\t", " ")
if err != nil {
logs.Error("json marshal config fail, %s", err.Error())
return err
}
return SaveToFile(configFilePath, value)
}
func ConfigGet() *Config {
return &configCache
}
func DomainListSave(domain []string) error {
configCache.DomainList = domain
return configSyncToFile()
}
func RemoteListSave(remote []Remote) error {
configCache.RemoteList = remote
return configSyncToFile()
}
func ModeSave(mode ModeType) error {
configCache.Mode = mode
return configSyncToFile()
}
func TestUrlSave(test string) error {
configCache.TestUrl = test
return configSyncToFile()
}
func RemoteSave(remote string) error {
configCache.RemoteName = remote
return configSyncToFile()
}
func ListenAddressSave(addr string) error {
configCache.Address = addr
return configSyncToFile()
}
func ListenPortSave(port int) error {
configCache.Port = port
return configSyncToFile()
}
func ConfigInit() {
configFilePath = fmt.Sprintf("%s%c%s", ConfigDirGet(), os.PathSeparator, "config.json")
_, err := os.Stat(configFilePath)
if err != nil {
err = configSyncToFile()
if err != nil {
logs.Error("default config save to app data dir fail, %s", err.Error())
return
}
}
value, err := os.ReadFile(configFilePath)
if err != nil {
logs.Error("read config file from app data dir fail, %s", err.Error())
configSyncToFile()
return
}
var config Config
err = json.Unmarshal(value, &config)
if err != nil {
logs.Error("json unmarshal config fail, %s", err.Error())
configSyncToFile()
}
configCache = config
}