-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.go
More file actions
260 lines (227 loc) · 5.89 KB
/
Copy pathsettings.go
File metadata and controls
260 lines (227 loc) · 5.89 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
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
251
252
253
254
255
256
257
258
259
260
package settings
import (
"errors"
"fmt"
"io/fs"
"log/slog"
"os"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/knadh/koanf/parsers/json"
"github.com/knadh/koanf/providers/rawbytes"
"github.com/knadh/koanf/v2"
"github.com/getlantern/radiance/common/atomicfile"
"github.com/getlantern/radiance/events"
"github.com/getlantern/radiance/internal"
)
// Keys for various settings.
const (
CountryCodeKey = "country_code"
LocaleKey = "locale"
DeviceIDKey = "device_id"
DataPathKey = "data_path"
LogPathKey = "log_path"
EmailKey = "email"
UserLevelKey = "user_level"
TokenKey = "token"
UserIDKey = "user_id"
DevicesKey = "devices"
LogLevelKey = "log_level"
LoginResponseKey = "login_response"
SmartRoutingKey = "smart_routing"
AdBlockKey = "ad_block"
UnboundedKey = "unbounded"
filePathKey = "file_path"
settingsFileName = "local.json"
)
type settings struct {
k *koanf.Koanf
readOnly atomic.Bool
initialized bool
watcher *internal.FileWatcher
mu sync.Mutex
}
var k = &settings{
k: koanf.New("."),
}
var ErrReadOnly = errors.New("read-only")
// InitSettings initializes the config for user settings, which can be used by both the tunnel process and
// the main application process to read user preferences like locale.
func InitSettings(fileDir string) error {
k.mu.Lock()
defer k.mu.Unlock()
if k.initialized {
return nil
}
if err := initialize(fileDir); err != nil {
return fmt.Errorf("initializing settings: %w", err)
}
k.initialized = true
return nil
}
func initialize(fileDir string) error {
k.k = koanf.New(".")
if err := os.MkdirAll(fileDir, 0755); err != nil {
return fmt.Errorf("failed to create data directory: %v", err)
}
filePath := filepath.Join(fileDir, settingsFileName)
switch err := loadSettings(filePath); {
case errors.Is(err, fs.ErrNotExist):
slog.Warn("settings file not found", "path", filePath) // file may not have been created yet
if err := setDefaults(filePath); err != nil {
return fmt.Errorf("setting default settings: %w", err)
}
return save()
case err != nil:
return fmt.Errorf("loading settings: %w", err)
}
return nil
}
func setDefaults(filePath string) error {
// We need to set the file path first because the save function reads it as soon as we set any key.
if err := k.k.Set(filePathKey, filePath); err != nil {
return fmt.Errorf("failed to set file path: %w", err)
}
if err := k.k.Set(LocaleKey, "fa-IR"); err != nil {
return fmt.Errorf("failed to set default locale: %w", err)
}
if err := k.k.Set(UserLevelKey, "free"); err != nil {
return fmt.Errorf("failed to set default user level: %w", err)
}
return nil
}
func loadSettings(path string) error {
contents, err := atomicfile.ReadFile(path)
if err != nil {
return fmt.Errorf("loading settings (read-only): %w", err)
}
kk := koanf.New(".")
if err := kk.Load(rawbytes.Provider(contents), json.Parser()); err != nil {
return fmt.Errorf("parsing settings: %w", err)
}
k.k = kk
return nil
}
func SetReadOnly(readOnly bool) {
k.readOnly.Store(readOnly)
}
func StartWatching() error {
k.mu.Lock()
defer k.mu.Unlock()
if !k.initialized {
return errors.New("settings not initialized")
}
if k.watcher != nil {
return errors.New("settings file watcher already started")
}
path := k.k.String(filePathKey)
watcher := internal.NewFileWatcher(path, func() {
if err := loadSettings(path); err != nil {
slog.Error("reloading settings file", "error", err)
}
})
if err := watcher.Start(); err != nil {
return fmt.Errorf("starting settings file watcher: %w", err)
}
k.watcher = watcher
// reload settings once at start in case there were changes before we started watching
if err := loadSettings(path); err != nil && !errors.Is(err, fs.ErrNotExist) {
return err
}
return nil
}
// StopWatching stops watching the settings file for changes. This is only relevant in read-only mode.
func StopWatching() {
k.mu.Lock()
defer k.mu.Unlock()
if k.watcher != nil {
k.watcher.Close()
k.watcher = nil
}
}
func Get(key string) any {
return k.k.Get(key)
}
func GetString(key string) string {
return k.k.String(key)
}
func GetBool(key string) bool {
return k.k.Bool(key)
}
func GetInt(key string) int {
return k.k.Int(key)
}
func GetInt64(key string) int64 {
return k.k.Int64(key)
}
func GetFloat64(key string) float64 {
return k.k.Float64(key)
}
func GetStringSlice(key string) []string {
return k.k.Strings(key)
}
func GetDuration(key string) time.Duration {
return k.k.Duration(key)
}
func GetStruct(key string, out any) error {
return k.k.Unmarshal(key, out)
}
func Set(key string, value any) error {
if k.readOnly.Load() {
return ErrReadOnly
}
err := k.k.Set(key, value)
if err != nil {
return fmt.Errorf("could not set key %s: %w", key, err)
}
return save()
}
func save() error {
if k.readOnly.Load() {
return ErrReadOnly
}
if GetString(filePathKey) == "" {
return errors.New("settings file path is not set")
}
out, err := k.k.Marshal(json.Parser())
if err != nil {
return fmt.Errorf("could not marshal koanf file: %w", err)
}
err = atomicfile.WriteFile(GetString(filePathKey), out, 0644)
if err != nil {
return fmt.Errorf("could not write koanf file: %w", err)
}
return nil
}
// Reset clears the current settings in memory primarily for testing purposes.
func Reset() {
k.mu.Lock()
defer k.mu.Unlock()
if !k.readOnly.Load() {
if k.watcher != nil {
k.watcher.Close()
k.watcher = nil
}
k.k = koanf.New(".")
k.initialized = false
}
}
func IsPro() bool {
return strings.ToLower(GetString(UserLevelKey)) == "pro"
}
// Device is a machine registered to a user account (e.g. an Android phone or a Windows desktop).
type Device struct {
ID string
Name string
}
func Devices() ([]Device, error) {
devices := []Device{}
err := GetStruct(DevicesKey, &devices)
return devices, err
}
type UserChangeEvent struct {
events.Event
}