This repository has been archived by the owner on Dec 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
576 lines (467 loc) · 13.6 KB
/
server.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
package incognitomail
import (
"errors"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
"github.com/valyala/gorpc"
"golang.org/x/net/websocket"
"gopkg.in/tylerb/graceful.v1"
)
// Server holds all the data required to run the server.
type Server struct {
lockFileHandle *os.File
persistence *IncognitoData
mailSystemWriter MailSystemHandleWriter
commandCh chan interface{}
signalCh chan os.Signal
httpServer *graceful.Server
rpcServer *gorpc.Server
started bool
finishCh chan bool
}
// MailSystemHandleWriter has methods for adding and removing mappings from the mail system.
type MailSystemHandleWriter interface {
AddHandle(string, string) (string, error)
RemoveHandle(string) error
}
type newHandleCommand struct {
source string
accountSecret string
resultCh chan string
errorCh chan error
}
type newAccountCommand struct {
source string
target string
resultCh chan string
errorCh chan error
}
type deleteHandleCommand struct {
source string
handle string
secret string
resultCh chan string
errorCh chan error
}
type deleteAccountCommand struct {
source string
secret string
resultCh chan string
errorCh chan error
}
type terminateCommand struct{}
const (
accountSecretSize = 64
handleSize = 18
commandQueue = 10
httpServerTimeout = 10 * time.Second
httpServerTCPKeepAliveTimeout = 3 * time.Minute
)
var (
// ErrServerNotStarted is used whenever an action is taken that expects a started server, but the server is actually not started.
ErrServerNotStarted = errors.New("server not started")
// ErrLockFileAlreadyExists is used only when trying to acquire a lock file, but the lock file already exists.
ErrLockFileAlreadyExists = errors.New("lock file already exists")
// ErrLockFileNotFound is used whenever an action requires a lock file to be used, but the lock file handle is nil.
ErrLockFileNotFound = errors.New("could not find lock file")
// ErrEmptyCommand is used when the server receives an empty command.
ErrEmptyCommand = errors.New("empty command received")
// ErrUnknownCommand is used when the server receives an unknown/garbage command.
ErrUnknownCommand = errors.New("unknown command received")
// ErrWrongCommand is used when a known command is received, but is malformed.
ErrWrongCommand = errors.New("wrong command usage")
// ErrInvalidPermission is used when a command has been received from the websocket, but the server shouldn't execute it.
ErrInvalidPermission = errors.New("invalid permission to do this")
)
func mailSystemWriterFromConfig() MailSystemHandleWriter {
switch Config.General.MailSystem {
case "postfix":
return NewPostfixWriter()
}
return nil
}
// NewServer returns an IncognitoMailServer object ready for use.
func NewServer() (*Server, error) {
server := &Server{
mailSystemWriter: mailSystemWriterFromConfig(),
commandCh: make(chan interface{}, commandQueue),
signalCh: make(chan os.Signal, 1),
}
data, err := OpenIncognitoData()
if err != nil {
return nil, err
}
server.persistence = data
err = server.getLockFile()
if err != nil {
return nil, err
}
return server, nil
}
func (s *Server) getLockFile() error {
if s.lockFileHandle != nil {
return ErrLockFileAlreadyExists
}
err := os.MkdirAll(filepath.Dir(Config.General.LockFilePath), os.FileMode(0755))
if err != nil {
return err
}
file, err := os.OpenFile(Config.General.LockFilePath, os.O_CREATE|os.O_RDWR, os.FileMode(0644))
if err != nil {
return err
}
err = syscall.Flock(int(file.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
if err != nil {
return err
}
// Writing the PID in the lock file according to File System Hierarchy (FHS) standards
fmt.Fprintf(file, "%10d\n", os.Getpid())
s.lockFileHandle = file
return nil
}
func (s *Server) removeLockFile() error {
if s.lockFileHandle == nil {
return ErrLockFileNotFound
}
err := syscall.Flock(int(s.lockFileHandle.Fd()), syscall.LOCK_UN|syscall.LOCK_NB)
if err != nil {
return err
}
s.lockFileHandle.Close()
err = os.Remove(Config.General.LockFilePath)
if err != nil {
// If the lock file stays in the system, we won't have a problem when executing the program again, so just log the occurrence.
log.Printf("[DEBUG] Could not remove lock file in %s\n", Config.General.LockFilePath)
}
s.lockFileHandle = nil
return nil
}
// startRPCListener starts the RPC service for communication between the running incognito server and any other processes.
func (s *Server) startRPCListener() error {
d := gorpc.NewDispatcher()
d.AddService("IncognitoRPCService", s)
server := gorpc.NewUnixServer(Config.General.UnixSockPath, d.NewHandlerFunc())
err := server.Start()
if err != nil {
return err
}
s.rpcServer = server
return nil
}
// Start begins listening for websocket connections (external requests) and RPC calls (internal requests).
func (s *Server) Start() {
if s.started {
return
}
s.started = true
signal.Notify(s.signalCh, syscall.SIGINT, syscall.SIGTERM)
go handleSignals(s)
err := s.startRPCListener()
if err != nil {
log.Fatal(err)
}
go handleCommands(s)
mux := http.NewServeMux()
// We listen for websocket connection this way to avoid receiving an 403 when connecting from the localhost (or anything that passes a "null" Origin header)
mux.HandleFunc(Config.General.ListenPath, func(w http.ResponseWriter, req *http.Request) {
server := websocket.Server{Handler: websocket.Handler(func(ws *websocket.Conn) {
var args string
err := websocket.Message.Receive(ws, &args)
if err != nil {
log.Printf("[DEBUG] Error receiving command from websocket: %s\n", err)
websocket.Message.Send(ws, "error receiving command")
return
}
result, err := s.SendCommand("websocket", args)
if err != nil {
websocket.Message.Send(ws, "error "+err.Error())
return
}
websocket.Message.Send(ws, result)
})}
server.ServeHTTP(w, req)
})
srv := &graceful.Server{
Timeout: httpServerTimeout,
TCPKeepAlive: httpServerTCPKeepAliveTimeout,
Server: &http.Server{
Addr: Config.General.ListenAddress,
Handler: mux,
},
}
s.httpServer = srv
if Config.General.TLSCertFile != "" && Config.General.TLSKeyFile != "" {
err = srv.ListenAndServeTLS(Config.General.TLSCertFile, Config.General.TLSKeyFile)
} else {
err = srv.ListenAndServe()
}
if err != nil {
log.Fatal(err)
}
}
// stopAllButHTTPServer is an utility for stopping everything else besides the http server (read the docs for handleSignals for an explanation).
func (s *Server) stopAllButHTTPServer() {
s.rpcServer.Stop()
s.commandCh <- terminateCommand{}
s.persistence.Close()
s.removeLockFile()
}
// Stop will stop everything from a running server
func (s *Server) Stop() {
s.stopAllButHTTPServer()
s.httpServer.Stop(httpServerTimeout)
// Waiting for the http server to stop
<-s.httpServer.StopChan()
}
// Wait blocks until the server has finished executing. If the server wasn't started, it returns an error instead.
func (s *Server) Wait() error {
if s.finishCh == nil {
return ErrServerNotStarted
}
<-s.finishCh
return nil
}
// SendCommand is executed for every message received either by the websocket or RPC interface. Builds a well-defined command to send to the goroutine listening for commands to execute.
func (s *Server) SendCommand(source, args string) (string, error) {
c := strings.Fields(args)
if len(c) == 0 {
return "", ErrEmptyCommand
}
command := c[0]
// c[1:] works even if len(c) == 1, and in this case it's just an empty slice
extra := c[1:]
// It is important to make buffered channels, because we'll send values and close them afterwards
resultCh := make(chan string, 1)
errorCh := make(chan error, 1)
switch command {
case "new":
if len(extra) != 2 {
return "", ErrWrongCommand
}
switch extra[0] {
case "handle":
s.commandCh <- newHandleCommand{
source: source,
accountSecret: extra[1],
resultCh: resultCh,
errorCh: errorCh,
}
case "account":
s.commandCh <- newAccountCommand{
source: source,
target: extra[1],
resultCh: resultCh,
errorCh: errorCh,
}
default:
log.Printf("[DEBUG] received unknown 'new' option: %s\n", args)
return "", ErrWrongCommand
}
case "delete":
if len(extra) < 1 {
return "", ErrWrongCommand
}
switch extra[0] {
case "handle":
if len(extra) != 3 {
return "", ErrWrongCommand
}
s.commandCh <- deleteHandleCommand{
source: source,
handle: extra[1],
secret: extra[2],
resultCh: resultCh,
errorCh: errorCh,
}
case "account":
if len(extra) != 2 {
return "", ErrWrongCommand
}
s.commandCh <- deleteAccountCommand{
source: source,
secret: extra[1],
resultCh: resultCh,
errorCh: errorCh,
}
}
default:
log.Printf("[DEBUG] received unknown command %s\n", args)
return "", ErrUnknownCommand
}
return <-resultCh, <-errorCh
}
// Receives any command that needs to be executed, and executes them.
func handleCommands(s *Server) {
for {
command := <-s.commandCh
var res string
var err error
var resCh chan string
var errCh chan error
switch t := command.(type) {
case terminateCommand:
log.Println("[INFO] Terminating server")
return
case newHandleCommand:
res, err = s.NewHandle(t.accountSecret)
resCh = t.resultCh
errCh = t.errorCh
case newAccountCommand:
// New accounts should be created locally only, at least at first
if t.source == "websocket" {
err = ErrInvalidPermission
res = ""
} else {
res, err = s.NewAccount(t.target)
}
resCh = t.resultCh
errCh = t.errorCh
case deleteHandleCommand:
res = ""
if t.source == "websocket" {
err = ErrInvalidPermission
} else {
err = s.DeleteHandle(t.secret, t.handle)
if err == nil {
res = "success"
}
}
resCh = t.resultCh
errCh = t.errorCh
case deleteAccountCommand:
res = ""
if t.source == "websocket" {
err = ErrInvalidPermission
} else {
err = s.DeleteAccount(t.secret)
if err == nil {
res = "success"
}
}
resCh = t.resultCh
errCh = t.errorCh
default:
log.Printf("[DEBUG] unrecognized command %v\n", t)
continue
}
errCh <- err
resCh <- res
close(errCh)
close(resCh)
}
}
func handleSignals(s *Server) {
<-s.signalCh
// Upon receiving a signal, just stop. s.httpServer will also receive the signal, so stop everything but s.httpServer
s.stopAllButHTTPServer()
}
// NewHandle creates a new handle for the account with the given secret.
func (s *Server) NewHandle(accountSecret string) (string, error) {
target, err := s.persistence.GetAccountTarget(accountSecret)
if err != nil {
return "", err
}
var newHandle string
// We'll keep looping until we find a handle that hasn't been used
for {
newHandle, err = generateRandomString(handleSize)
if err != nil {
return "", err
}
if !s.persistence.HasHandleGlobal(newHandle) {
break
}
}
err = s.persistence.NewAccountHandle(accountSecret, newHandle)
if err != nil {
return "", err
}
// fullHandle will have the domain attached, so it's the complete incognito email
fullHandle, err := s.mailSystemWriter.AddHandle(newHandle, target)
if err != nil {
return "", err
}
return fullHandle, nil
}
// NewAccount creates a new account with the given target email address and returns the secret.
func (s *Server) NewAccount(target string) (string, error) {
var secret string
var err error
// We'll keep looping until we find an unused secret
for {
secret, err = generateRandomString(accountSecretSize)
if err != nil {
return "", err
}
if !s.persistence.HasAccount(secret) {
break
}
}
err = s.persistence.NewAccount(secret, target)
if err != nil {
return "", err
}
return secret, nil
}
// DeleteHandle deletes the given handle from the account with the given secret. If the account does not exist, it returns an error.
func (s *Server) DeleteHandle(secret, handle string) error {
exists := s.persistence.HasAccount(secret)
if !exists {
return ErrAccountNotFound
}
s.persistence.DeleteAccountHandle(secret, handle)
s.mailSystemWriter.RemoveHandle(handle)
return nil
}
// DeleteAccount deletes all data from the account with the given secret. If the account does not exist, it returns an error.
func (s *Server) DeleteAccount(secret string) error {
exists := s.persistence.HasAccount(secret)
if !exists {
return ErrAccountNotFound
}
// Listing all handles for this account and removing them from the mail system
handles, err := s.persistence.ListAccountHandles(secret)
if err != nil {
return err
}
for _, handle := range handles {
err := s.mailSystemWriter.RemoveHandle(handle)
if err != nil {
return err
}
}
// Only after removing all handles from the mail system, delete from persistence system
s.persistence.DeleteAccount(secret)
return nil
}
// ListHandles returns all handles from the account with the given secret.
func (s *Server) ListHandles(secret string) ([]string, error) {
exists := s.persistence.HasAccount(secret)
if !exists {
return nil, ErrAccountNotFound
}
// Listing all handles for this account and removing them from the mail system
handles, err := s.persistence.ListAccountHandles(secret)
if err != nil {
return nil, err
}
return handles, nil
}
// CreateRPCServiceClient creates and returns a reasy to use RPC dispatcher client.
func CreateRPCServiceClient() *gorpc.DispatcherClient {
// Using an empty server struct is not a problem, we only want the methods
s := &Server{}
d := gorpc.NewDispatcher()
d.AddService("IncognitoRPCService", s)
c := gorpc.NewUnixClient(Config.General.UnixSockPath)
c.Start()
dc := d.NewServiceClient("IncognitoRPCService", c)
return dc
}