forked from BankaiNoJutsu/subspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
executable file
·279 lines (243 loc) · 5.67 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"sort"
"sync"
"time"
)
var (
ErrProfileNotFound = errors.New("profile not found")
)
type Profile struct {
ID string `json:"id"`
Public_Key string `json:"publickey"`
Private_Key string `json:"privatekey"`
Name string `json:"name"`
Platform string `json:"platform"`
Routing string `json:"routing"`
Number int `json:"number"`
Created time.Time `json:"created"`
}
type Data struct {
Type string
Name string
Domain string
Public_Key string
Public_Key_Trim string
Allowed string
AllowedIPs []string
Private_Key string
Port string
Latest_handshake string
Transfer_rx string
Transfer_tx string
Keepalive string
ClientEndpoint string
Preshared_Key string
}
type DynDNS struct {
Domain string
Token string
DynIP string
IP string
Status string
}
func (p Profile) NameClean() string {
return regexp.MustCompile(`[^a-zA-Z0-9]`).ReplaceAllString(p.Name, "")
}
func (p Profile) WireGuardConfigPath() string {
return fmt.Sprintf("%s/clients/%s/%s.conf", datadir, p.Name, p.ID)
}
func (p Profile) WireGuardConfigName() string {
return fmt.Sprintf("%s%s", p.Name, ".conf")
}
func (p Profile) WireGuardPNGPath() string {
return fmt.Sprintf("%s/clients/%s/%s.png", datadir, p.Name, p.ID)
}
func (p Profile) WireGuardPNGName() string {
return fmt.Sprintf("%s%s", p.Name, ".png")
}
type Info struct {
Email string `json:"email"`
Password []byte `json:"password"`
Secret string `json:"secret"`
Configured bool `json:"configure"`
Domain string `json:"domain"`
HashKey string `json:"hash_key"`
BlockKey string `json:"block_key"`
Mail struct {
From string `json:"from"`
Server string `json:"server"`
Port int `json:"port"`
Username string `json:"username"`
Password string `json:"password"`
} `json:"mail"`
Server struct {
ServerConfigured bool `json:"serverconfigure"`
IP_Address string `json:"ip_address"`
Port int `json:"port"`
Network_Adapter string `json:"network_adapter"`
Virtual_IP_Address string `json:"virtual_ip_address"`
CIDR string `json:"cidr"`
DNS string `json:"dns"`
Public_Key string `json:"public_key"`
Config_Path string `json:"config_path"`
} `json:"server"`
DynDNS struct {
Domain string `json:"domain"`
Token string `json:"token"`
} `json:"dyndns"`
}
type Config struct {
mu sync.RWMutex
filename string
Info *Info `json:"info"`
Profiles []*Profile `json:"profiles"`
Modified time.Time `json:"modified"`
}
func NewConfig(filename string) (*Config, error) {
filename = filepath.Join(datadir, filename)
c := &Config{filename: filename}
b, err := ioutil.ReadFile(filename)
// Create new config with defaults
if os.IsNotExist(err) {
c.Info = &Info{
HashKey: RandomString(32),
BlockKey: RandomString(32),
}
return c, c.save()
}
if err != nil {
return nil, err
}
// Open existing config
if err := json.Unmarshal(b, c); err != nil {
return nil, fmt.Errorf("invalid config %q: %s", filename, err)
}
return c, nil
}
func (c *Config) Lock() {
c.mu.Lock()
}
func (c *Config) Unlock() {
c.mu.Unlock()
}
func (c *Config) RLock() {
c.mu.RLock()
}
func (c *Config) RUnlock() {
c.mu.RUnlock()
}
func (c *Config) DeleteProfile(id string) error {
c.Lock()
defer c.Unlock()
var profiles []*Profile
for _, p := range c.Profiles {
if p.ID == id {
continue
}
profiles = append(profiles, p)
}
c.Profiles = profiles
return c.save()
}
func (c *Config) UpdateProfile(id string, fn func(*Profile) error) error {
c.Lock()
defer c.Unlock()
p, err := c.findProfile(id)
if err != nil {
return err
}
if err := fn(p); err != nil {
return err
}
return c.save()
}
func (c *Config) AddProfile(privatekey, publickey, name, platform, routing string) (Profile, error) {
c.Lock()
defer c.Unlock()
var id string
for {
n := RandomString(16)
if _, err := c.findProfile(n); err == ErrProfileNotFound {
id = n
break
}
}
number := 2 // MUST start at 2
for _, p := range c.Profiles {
if p.Number >= number {
number = p.Number + 1
}
}
profile := Profile{
ID: id,
Public_Key: publickey,
Private_Key: privatekey,
Name: name,
Platform: platform,
Routing: routing,
Number: number,
Created: time.Now(),
}
c.Profiles = append(c.Profiles, &profile)
return profile, c.save()
}
func (c *Config) FindProfile(id string) (Profile, error) {
c.RLock()
defer c.RUnlock()
u, err := c.findProfile(id)
if err != nil {
return Profile{}, err
}
return *u, nil
}
func (c *Config) findProfile(id string) (*Profile, error) {
for _, u := range c.Profiles {
if u.ID == id {
return u, nil
}
}
return nil, ErrProfileNotFound
}
func (c *Config) ListProfiles() (profiles []Profile) {
c.RLock()
defer c.RUnlock()
for _, p := range c.listProfiles() {
profiles = append(profiles, *p)
}
return
}
func (c *Config) listProfiles() (profiles []*Profile) {
for _, p := range c.Profiles {
profiles = append(profiles, p)
}
sort.Slice(profiles, func(i, j int) bool { return profiles[i].Created.After(profiles[j].Created) })
return
}
func (c *Config) FindInfo() Info {
c.RLock()
defer c.RUnlock()
return *c.Info
}
func (c *Config) UpdateInfo(fn func(*Info) error) error {
c.Lock()
defer c.Unlock()
if err := fn(c.Info); err != nil {
return err
}
return c.save()
}
func (c *Config) save() error {
b, err := json.MarshalIndent(c, "", " ")
if err != nil {
return err
}
return Overwrite(c.filename, b, 0644)
}