/
config.go
250 lines (216 loc) · 5.89 KB
/
config.go
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package cfg
import (
"embed"
"encoding/json"
"fmt"
"log"
"os"
"path"
"sync"
)
var (
// ConfigDir is the path to the directory storing the application configuration.
ConfigDir string
// DataDir is the path to the directory storing the application data.
DataDir string
)
//go:embed default-config.json
var defaultConfig embed.FS
// Config stores and manages the configuration for the application.
// Although all fields are public, this is only for use by the JSON marshaller.
// All access to the Config should be done through the exported methods.
type Config struct {
sync.RWMutex
Filter struct {
FilterLists []FilterList `json:"filterLists"`
} `json:"filter"`
Certmanager struct {
CAInstalled bool `json:"caInstalled"`
} `json:"certmanager"`
Proxy struct {
Port int `json:"port"`
IgnoredHosts []string `json:"ignoredHosts"`
} `json:"proxy"`
// firstLaunch is true if the application is being run for the first time.
firstLaunch bool
}
type FilterList struct {
Name string `json:"name"`
Type string `json:"type"`
URL string `json:"url"`
Enabled bool `json:"enabled"`
}
func init() {
var err error
ConfigDir, err = getConfigDir()
if err != nil {
log.Fatalf("failed to get config dir: %v", err)
}
stat, err := os.Stat(ConfigDir)
if os.IsNotExist(err) {
if err := os.MkdirAll(ConfigDir, 0755); err != nil {
log.Fatalf("failed to create config dir: %v", err)
}
stat, err = os.Stat(ConfigDir)
}
if err != nil {
log.Fatalf("failed to stat config dir: %v", err)
}
if !stat.IsDir() {
log.Fatalf("config dir is not a directory: %s", ConfigDir)
}
DataDir, err = getDataDir()
if err != nil {
log.Fatalf("failed to get data dir: %v", err)
}
stat, err = os.Stat(DataDir)
if os.IsNotExist(err) {
if err := os.MkdirAll(DataDir, 0755); err != nil {
log.Fatalf("failed to create data dir: %v", err)
}
stat, err = os.Stat(DataDir)
}
if err != nil {
log.Fatalf("failed to stat data dir: %v", err)
}
if !stat.IsDir() {
log.Fatalf("data dir is not a directory: %s", DataDir)
}
}
func NewConfig() (*Config, error) {
c := &Config{}
configFile := path.Join(ConfigDir, "config.json")
var configData []byte
if _, err := os.Stat(configFile); !os.IsNotExist(err) {
configData, err = os.ReadFile(configFile)
if err != nil {
return nil, fmt.Errorf("failed to read config file: %v", err)
}
} else {
configData, err = defaultConfig.ReadFile("default-config.json")
if err != nil {
return nil, fmt.Errorf("failed to read default config file: %v", err)
}
if err := os.WriteFile(configFile, configData, 0644); err != nil {
return nil, fmt.Errorf("failed to write config file: %v", err)
}
c.firstLaunch = true
}
if err := json.Unmarshal(configData, c); err != nil {
return nil, fmt.Errorf("failed to unmarshal config: %v", err)
}
return c, nil
}
// Save saves the config to disk.
// It is not thread-safe, and should only be called if the caller has
// a lock on the config.
func (c *Config) Save() error {
configData, err := json.Marshal(c)
if err != nil {
return fmt.Errorf("failed to marshal config: %w", err)
}
configFile := path.Join(ConfigDir, "config.json")
err = os.WriteFile(configFile, configData, 0644)
if err != nil {
return fmt.Errorf("failed to write config file: %w", err)
}
return nil
}
// GetFilterLists returns the list of enabled filter lists.
func (c *Config) GetFilterLists() []FilterList {
c.RLock()
defer c.RUnlock()
return c.Filter.FilterLists
}
// AddFilterList adds a new filter list to the list of enabled filter lists.
func (c *Config) AddFilterList(list FilterList) string {
c.Lock()
defer c.Unlock()
c.Filter.FilterLists = append(c.Filter.FilterLists, list)
if err := c.Save(); err != nil {
log.Printf("failed to save config: %v", err)
return err.Error()
}
return ""
}
// RemoveFilterList removes a filter list from the list of enabled filter lists.
func (c *Config) RemoveFilterList(url string) string {
c.Lock()
defer c.Unlock()
for i, filterList := range c.Filter.FilterLists {
if filterList.URL == url {
c.Filter.FilterLists = append(c.Filter.FilterLists[:i], c.Filter.FilterLists[i+1:]...)
break
}
}
if err := c.Save(); err != nil {
log.Printf("failed to save config: %v", err)
return err.Error()
}
return ""
}
// ToggleFilterList toggles the enabled state of a filter list.
func (c *Config) ToggleFilterList(url string, enabled bool) string {
c.Lock()
defer c.Unlock()
for i, filterList := range c.Filter.FilterLists {
if filterList.URL == url {
c.Filter.FilterLists[i].Enabled = enabled
break
}
}
if err := c.Save(); err != nil {
log.Printf("failed to save config: %v", err)
return err.Error()
}
return ""
}
// GetPort returns the port the proxy is set to listen on.
func (c *Config) GetPort() int {
c.RLock()
defer c.RUnlock()
return c.Proxy.Port
}
// SetPort sets the port the proxy is set to listen on.
func (c *Config) SetPort(port int) string {
c.Lock()
defer c.Unlock()
c.Proxy.Port = port
if err := c.Save(); err != nil {
log.Printf("failed to save config: %v", err)
return err.Error()
}
return ""
}
// GetIgnoredHosts returns the list of ignored hosts.
func (c *Config) GetIgnoredHosts() []string {
c.RLock()
defer c.RUnlock()
return c.Proxy.IgnoredHosts
}
// SetIgnoredHosts sets the list of ignored hosts.
func (c *Config) SetIgnoredHosts(hosts []string) error {
c.Lock()
defer c.Unlock()
c.Proxy.IgnoredHosts = hosts
if err := c.Save(); err != nil {
log.Printf("failed to save config: %v", err)
return err
}
return nil
}
// GetCAInstalled returns whether the CA is installed.
func (c *Config) GetCAInstalled() bool {
c.RLock()
defer c.RUnlock()
return c.Certmanager.CAInstalled
}
// SetCAInstalled sets whether the CA is installed.
func (c *Config) SetCAInstalled(caInstalled bool) {
c.Lock()
defer c.Unlock()
c.Certmanager.CAInstalled = caInstalled
if err := c.Save(); err != nil {
log.Printf("failed to save config: %v", err)
}
}