forked from mandiant/gocrack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
201 lines (171 loc) · 4.99 KB
/
server.go
File metadata and controls
201 lines (171 loc) · 4.99 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
package server
import (
"net/http"
"sync"
"github.com/fireeye/gocrack/server/authentication"
"github.com/fireeye/gocrack/server/filemanager"
"github.com/fireeye/gocrack/server/notifications"
"github.com/fireeye/gocrack/server/rpc"
"github.com/fireeye/gocrack/server/storage"
"github.com/fireeye/gocrack/server/web"
"github.com/fireeye/gocrack/server/workmgr"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
var (
// CompileTime is when this was compiled
CompileTime string
// CompileRev is the git revision hash (sha1)
CompileRev string
)
// Server is a GoCrack server instance
type Server struct {
// unexported or filtered fields below
auth *authentication.AuthWrapper
stor storage.Backend
wg *sync.WaitGroup
cfg *Config
workers *workmgr.WorkerManager
fm *filemanager.Context
stop chan bool
}
// New returns a gocrack server context. It creates the necessary listeners and opens a connection to the storage and authentication backend
func New(cfg *Config) (*Server, error) {
var err error
if err = cfg.validate(); err != nil {
return nil, err
}
if !cfg.Debug {
gin.SetMode(gin.ReleaseMode)
}
stor, err := storage.Open(cfg.Database)
if err != nil {
return nil, err
}
ctx := &Server{
wg: &sync.WaitGroup{},
cfg: cfg,
workers: workmgr.NewWorkerManager(),
stop: make(chan bool, 1),
stor: stor,
}
if ctx.auth, err = authentication.Open(ctx.stor, cfg.Authentication); err != nil {
return nil, err
}
ctx.fm = filemanager.New(ctx.stor, cfg.FileManager)
return ctx, nil
}
// registerNotifications starts the notification engine and subscribes to needed topic. If the engine starts successfully
// it returns a closer function to stop & unsubscribe from topics
func (s *Server) registerNotifications() (func(), error) {
log.Debug().Msg("Starting Notification Engine")
emailer, err := notifications.New(s.cfg.Notification, s.stor)
if err != nil {
return nil, err
}
emTaskStatusHndl, err := s.workers.Subscribe(workmgr.TaskStatusTopic, func(payload interface{}) {
taskStatus, ok := payload.(workmgr.TaskStatusChangeBroadcast)
if !ok {
log.Error().Msg("TaskStatusTopic message is not the correct type")
return
}
if err := emailer.TaskStatusChanged(taskStatus.TaskID, taskStatus.Status); err != nil {
log.Error().Err(err).Msg("Failed to send email regarding task status change")
}
})
if err != nil {
return nil, err
}
emCrackedPwHndl, err := s.workers.Subscribe(workmgr.CrackedTopic, func(payload interface{}) {
crackedPassword, ok := payload.(workmgr.CrackedPasswordBroadcast)
if !ok {
log.Error().Msg("CrackedTopic message is not the correct type")
return
}
if err := emailer.CrackedPassword(crackedPassword.TaskID); err != nil {
log.Error().Err(err).Msg("Failed to send newly cracked password email")
}
})
if err != nil {
return nil, err
}
log.Debug().Msg("Notification Engine Started")
return func() {
log.Debug().Msg("Stopping notification engine")
s.workers.Unsubscribe(emTaskStatusHndl)
s.workers.Unsubscribe(emCrackedPwHndl)
emailer.Stop()
}, nil
}
// Start spawns the API Server as well as the RPC Server and blocks until stop has been called
func (s *Server) Start() error {
rs, err := rpc.NewRPCServer(s.cfg.RPC, s.stor, s.workers)
if err != nil {
return err
}
web, err := web.NewServer(s.cfg.WebServer, s.stor, s.workers, s.auth, s.fm)
if err != nil {
return err
}
if s.cfg.Notification.Enabled {
closer, err := s.registerNotifications()
if err != nil {
return err
}
defer closer()
}
// If any of the goroutines that are running a listener fail, we'll send the err on this channel
errch := make(chan error, 1)
defer close(errch)
s.wg.Add(2)
go func() {
defer s.wg.Done()
log.Info().Str("addr", rs.Address()).Msg("RPC Server listening")
if err := rs.Start(); err != nil {
if err == http.ErrServerClosed {
log.Warn().Msg("RPC server has stopped")
return
}
log.Error().Err(err).Msg("Error while serving rpc server")
errch <- err
}
}()
go func() {
defer s.wg.Done()
log.Info().Str("addr", web.Address()).Msg("HTTP Server listening")
if err := web.Start(); err != nil {
if err == http.ErrServerClosed {
log.Warn().Msg("HTTP server has stopped")
return
}
log.Error().Err(err).Msg("Error while serving HTTP server")
errch <- err
}
}()
// Wait for a fatal error on a listener or a stop message
select {
case fatalError := <-errch:
return fatalError
case <-s.stop:
rs.Stop()
web.Stop()
}
return nil
}
// Stop gracefully stops the GoCrack server and waits for all the goroutines to exit
func (s *Server) Stop() error {
close(s.stop)
s.wg.Wait()
// Stop the work manager after the RPC & API server have exited safefully
s.workers.Stop()
// Stop the storage backend
s.stor.Close()
return nil
}
// Refresh is called on a SIGUSR1 and refreshes the internal server state
func (s *Server) Refresh() error {
if err := s.fm.Refresh(); err != nil {
log.Error().Err(err).Msg("Failed to refresh filemanager")
}
return nil
}