-
Notifications
You must be signed in to change notification settings - Fork 1
/
env.go
366 lines (311 loc) · 7.97 KB
/
env.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Froxy - HTTP over SSH proxy
//
// Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
// See LICENSE for license terms and conditions
//
// Common environment for all Froxy parts
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"sync"
"syscall"
"github.com/alexpevzner/froxy/internal/sysdep"
)
//
// Environment type
//
type Env struct {
Logger
// Directories
PathSysConfDir string // System-wide configuration directory
PathUserHomeDir string // User home directory
PathUserConfDir string // User-specific configuration directory
PathUserStateDir string // User specific persistent state directory
PathUserLogDir string // User log dir
PathUserKeysDir string // User keys directory
PathUserDesktopDir string // User Desktop folder
PathUserStartupDir string // User Startup folder
PathUserIconsDir string // User icons directory
PathUserLockDir string // User locks
// File paths
PathUserConfFile string // User-specific configuration file
PathUserStateFile string // User-specific persistent state file
PathUserLogFile string // User-specific log file
PathUserLockFile string // User-specific lock file
PathUserDesktopFile string // User-specific desktop entry
PathUserStartupFile string // User-specific startup entry
PathUserIconFile string // Path to icon file
// Persistent state
stateLock sync.RWMutex // State access lock
state *State // Froxy persistent state
// Locks
locksCond *sync.Cond // To synchronize between goroutines
locksFiles map[EnvLock]*os.File // Currently open lock files
}
//
// Global locks
//
type EnvLock string
const (
EnvLockRun = EnvLock("run")
EnvLockState = EnvLock("state")
EnvLockKeys = EnvLock("keys")
)
// ----- Constructor -----
//
// Create new environment
//
func NewEnv() *Env {
env := &Env{
state: &State{},
locksCond: sync.NewCond(&sync.Mutex{}),
locksFiles: make(map[EnvLock]*os.File),
}
// Populate paths
env.PathSysConfDir = sysdep.SysConfDir(PROGRAM_NAME)
env.PathUserHomeDir = sysdep.UserHomeDir()
env.PathUserConfDir = sysdep.UserConfDir(PROGRAM_NAME)
env.PathUserStateDir = env.PathUserConfDir
env.PathUserLogDir = filepath.Join(env.PathUserStateDir, "log")
env.PathUserKeysDir = filepath.Join(env.PathUserStateDir, "keys")
env.PathUserDesktopDir = sysdep.UserDesktopDir()
env.PathUserStartupDir = sysdep.UserStartupDir()
env.PathUserIconsDir = env.PathUserConfDir
env.PathUserLockDir = filepath.Join(env.PathUserStateDir, "lock")
progname := strings.ToLower(PROGRAM_NAME)
env.PathUserConfFile = filepath.Join(env.PathUserConfDir, progname+".cfg")
env.PathUserStateFile = filepath.Join(env.PathUserConfDir, progname+".state")
env.PathUserLogFile = filepath.Join(env.PathUserLogDir, progname+".log")
env.PathUserLockFile = filepath.Join(env.PathUserConfDir, progname+".lock")
env.PathUserDesktopFile = sysdep.UserDesktopFile(PROGRAM_NAME, PROGRAM_ICON_NAME)
env.PathUserStartupFile = sysdep.UserStartupFile(PROGRAM_NAME, PROGRAM_ICON_NAME)
env.PathUserIconFile = filepath.Join(env.PathUserIconsDir, progname+"."+sysdep.IconExt())
// Create directories
done := make(map[string]struct{})
for _, dir := range []string{env.PathUserConfDir,
env.PathUserStateDir,
env.PathUserLogDir,
env.PathUserKeysDir,
env.PathUserLockDir} {
_, ok := done[dir]
if !ok {
done[dir] = struct{}{}
os.MkdirAll(dir, 0700)
}
}
// Load state
env.state.Load(env.PathUserStateFile)
return env
}
// ----- Global locks -----
//
// Acquire the global lock
//
func (env *Env) LockAcquire(lock EnvLock, wait bool) error {
env.locksCond.L.Lock()
defer env.locksCond.L.Unlock()
// Synchronize with other goroutines trying to
// acquire the same lock
for file := env.locksFiles[lock]; file != nil; {
if !wait {
return sysdep.ErrLockIsBusy
}
env.locksCond.Wait()
file = env.locksFiles[lock]
}
// Create a lock file
path := filepath.Join(env.PathUserLockDir, string(lock))
file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
goto EXIT
}
env.locksFiles[lock] = file
// Try to acquire a file lock
env.locksCond.L.Unlock()
err = sysdep.FileLock(file, true, wait)
env.locksCond.L.Lock()
EXIT:
if err != nil {
if file != nil {
file.Close()
env.locksFiles[lock] = nil
}
env.locksCond.Signal()
}
return err
}
//
// Convenience wrapper on LockAcquire() -- acquire lock with wait
//
func (env *Env) LockWait(lock EnvLock) error {
return env.LockAcquire(lock, true)
}
//
// Convenience wrapper on LockAcquire() -- acquire lock without wait
//
func (env *Env) LockTry(lock EnvLock) error {
return env.LockAcquire(lock, false)
}
//
// Release the global lock
//
func (env *Env) LockRelease(lock EnvLock) {
env.locksCond.L.Lock()
file := env.locksFiles[lock]
if file == nil {
panic("internal error")
}
file.Close()
delete(env.locksFiles, lock)
env.locksCond.Signal()
env.locksCond.L.Unlock()
}
// ----- Multiple run avoidance -----
//
// Acquire froxy.lock
//
func (env *Env) FroxyLockAcquire() error {
err := env.LockTry(EnvLockRun)
if err == sysdep.ErrLockIsBusy {
err = ErrFroxyRunning
}
return err
}
//
// Release froxy.lock
//
func (env *Env) FroxyLockRelease() {
env.LockRelease(EnvLockRun)
}
// ----- stdin/stdout/stderr redirection -----
//
// Detach stdin/stdout/stderr from console
//
func (env *Env) Detach() error {
nul, err := syscall.Open(os.DevNull, syscall.O_RDONLY, 0644)
if err != nil {
return fmt.Errorf("Open %q: %s", os.DevNull, err)
}
err = env.Logger.LogToFile(env.PathUserLogFile)
if err != nil {
return err
}
nullfd := uintptr(nul)
return sysdep.StdRedirect(nullfd, nullfd, nullfd)
}
// ----- Persistent configuration -----
//
// Set TCP port
//
func (env *Env) SetPort(port int) {
env.stateLock.Lock()
env.state.Port = port
env.state.Save(env.PathUserStateFile)
env.stateLock.Unlock()
}
//
// Get TCP port
//
func (env *Env) GetPort() int {
env.stateLock.RLock()
port := env.state.Port
if port == 0 {
port = HTTP_SERVER_PORT
}
env.stateLock.RUnlock()
return port
}
//
// Get server parameters
//
func (env *Env) GetServerParams() ServerParams {
env.stateLock.RLock()
s := env.state.Server
env.stateLock.RUnlock()
return s
}
//
// Set server parameters
//
func (env *Env) SetServerParams(s ServerParams) {
env.stateLock.Lock()
env.state.Server = s
env.state.Save(env.PathUserStateFile)
env.stateLock.Unlock()
}
//
// Get sites
//
func (env *Env) GetSites() (sites []SiteParams) {
env.stateLock.RLock()
sites = env.state.Sites
if sites == nil {
sites = make([]SiteParams, 0)
}
env.stateLock.RUnlock()
return
}
//
// Set (add or update) a single site entry
//
// Please note, a site is searched by `host' parameter,
// but if site.Host != host, the existent site will be
// renamed
//
func (env *Env) SetSite(host string, site SiteParams) {
host = strings.ToLower(host)
site.Host = strings.ToLower(site.Host)
// Acquire state lock
env.stateLock.Lock()
defer env.stateLock.Unlock()
// Create a copy of sites list. Router may work with
// previous version, so instead of patching the list,
// we will atomically replace it with the new version
sites := make([]SiteParams, len(env.state.Sites))
copy(sites, env.state.Sites)
// Site already listed?
for i, s := range sites {
if host == s.Host {
if s != site {
sites[i] = site
goto SAVE
}
return // Nothing changed
}
}
// New site
sites = append(sites, site)
SAVE:
env.state.Sites = sites
env.state.Save(env.PathUserStateFile)
}
//
// Del a site
//
func (env *Env) DelSite(host string) {
host = strings.ToLower(host)
// Acquire state lock
env.stateLock.Lock()
defer env.stateLock.Unlock()
// Create a copy of sites list
sites := make([]SiteParams, len(env.state.Sites))
copy(sites, env.state.Sites)
// Find the site
pos := -1
for i, s := range sites {
if host == s.Host {
pos = i
break
}
}
if pos < 0 {
return
}
// Update list and save
copy(sites[pos:], sites[pos+1:])
env.state.Sites = sites[:len(sites)-1]
env.state.Save(env.PathUserStateFile)
}