-
Notifications
You must be signed in to change notification settings - Fork 3
/
shelter.go
297 lines (252 loc) · 8.49 KB
/
shelter.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
// shelter - DNS/DNSSEC misconfiguration checker
//
// Copyright (C) 2014 Rafael Dantas Justo <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
package main
import (
"flag"
"fmt"
"net"
"os"
"os/signal"
"path/filepath"
"runtime"
"syscall"
"time"
"github.com/rafaeljusto/shelter/config"
"github.com/rafaeljusto/shelter/log"
"github.com/rafaeljusto/shelter/model"
"github.com/rafaeljusto/shelter/net/http/client"
"github.com/rafaeljusto/shelter/net/http/rest"
"github.com/rafaeljusto/shelter/net/mail/notification"
"github.com/rafaeljusto/shelter/net/scan"
"github.com/rafaeljusto/shelter/scheduler"
)
const (
copyright string = `Shelter version 0.3, Copyright (C) 2014 Rafael Dantas Justo
Shelter comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome
to redistribute it under certain conditions.
`
)
// List of arguments that can be filled in the program command line
var (
configFilePath string // General configuration path
showVersion *bool // Show system version
)
// We store all listeners to make it easier later to stop all in a system SIGTERM event
var (
restListeners []net.Listener
clientListeners []net.Listener
)
// List of possible return codes of the program. This will be useful later to build a
// command line documentation
const (
NoError = iota
ErrInputParameters
ErrLoadingConfig
ErrListeningRESTInterfaces
ErrStartingRESTServer
ErrListeningClientInterfaces
ErrStartingWebClient
ErrScanTimeFormat
ErrCurrentScanInitialize
ErrNotificationTemplates
)
// We are going to use the initialization function to read command line arguments, load
// the configuration file and register system signals
func init() {
flag.StringVar(&configFilePath, "config", "", "Configuration file")
showVersion = flag.Bool("version", false, "System version")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "%s\nUsage of %s:\n", copyright, os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
if *showVersion {
fmt.Println(copyright)
os.Exit(0)
}
if len(configFilePath) == 0 {
fmt.Println("The configuration file was not informed")
os.Exit(ErrInputParameters)
}
if err := loadSettings(); err != nil {
log.Println("Error loading the configuration file. Details:", err)
os.Exit(ErrLoadingConfig)
}
manageSystemSignals()
}
// The main function of the system is responsable for deploying all system components that
// are enabled. For now we have the REST server and the scan system
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
logPath := filepath.Join(
config.ShelterConfig.BasePath,
config.ShelterConfig.LogFilename,
)
if err := log.SetOutput(logPath); err != nil {
log.Println(err)
return
}
defer log.Close()
if config.ShelterConfig.RESTServer.Enabled {
var err error
restListeners, err = rest.Listen()
if err != nil {
log.Println("Error while aquiring interfaces for REST server. Details:", err)
os.Exit(ErrListeningRESTInterfaces)
}
if err := rest.Start(restListeners); err != nil {
log.Println("Error starting the REST server. Details:", err)
os.Exit(ErrStartingRESTServer)
}
log.Info("REST server started")
}
if config.ShelterConfig.WebClient.Enabled {
var err error
clientListeners, err = client.Listen()
if err != nil {
log.Println("Error while aquiring interfaces for Client server. Details:", err)
os.Exit(ErrListeningClientInterfaces)
}
if err := client.Start(clientListeners); err != nil {
log.Println("Error starting the Client server. Details:", err)
os.Exit(ErrStartingWebClient)
}
log.Info("Web client started")
}
if config.ShelterConfig.Scan.Enabled {
// Attention: Cannot use timezone abbreviations
// http://stackoverflow.com/questions/25368415/golang-timezone-parsing
scanTime, err := time.Parse("15:04:05 -0700", config.ShelterConfig.Scan.Time)
if err != nil {
log.Println("Scan time not in a valid format. Details:", err)
os.Exit(ErrScanTimeFormat)
}
scanTime = scanTime.UTC()
now := time.Now().UTC()
nextExecution := time.Date(
now.Year(),
now.Month(),
now.Day(),
scanTime.Hour(),
scanTime.Minute(),
scanTime.Second(),
scanTime.Nanosecond(),
scanTime.Location(),
)
scheduler.Register(scheduler.Job{
Type: scheduler.JobTypeScan,
NextExecution: nextExecution,
Interval: time.Duration(config.ShelterConfig.Scan.IntervalHours) * time.Hour,
Task: scan.ScanDomains,
})
// Must be called after registering in scheduler, because we retrieve the next execution time
// from it
if err := model.InitializeCurrentScan(); err != nil {
log.Println("Current scan information got an error while initializing. Details:", err)
os.Exit(ErrCurrentScanInitialize)
}
}
if config.ShelterConfig.Notification.Enabled {
if err := notification.LoadTemplates(); err != nil {
log.Println("Error loading notification templates. Details:", err)
os.Exit(ErrNotificationTemplates)
}
// Attention: Cannot use timezone abbreviations
// http://stackoverflow.com/questions/25368415/golang-timezone-parsing
notificationTime, err := time.Parse("15:04:05 -0700", config.ShelterConfig.Scan.Time)
if err != nil {
log.Println("Scan time not in a valid format. Details:", err)
os.Exit(ErrScanTimeFormat)
}
notificationTime = notificationTime.UTC()
now := time.Now().UTC()
nextExecution := time.Date(
now.Year(),
now.Month(),
now.Day(),
notificationTime.Hour(),
notificationTime.Minute(),
notificationTime.Second(),
notificationTime.Nanosecond(),
notificationTime.Location(),
)
scheduler.Register(scheduler.Job{
Type: scheduler.JobTypeNotification,
NextExecution: nextExecution,
Interval: time.Duration(config.ShelterConfig.Notification.IntervalHours) * time.Hour,
Task: notification.Notify,
})
}
scheduler.Start()
select {}
}
// Shelter could receive system signals for OS, so this method catch the signals to create
// smothly actions for each one. For example, when receives a KILL signal, we should wait
// to process all requests before finishing the server
func manageSystemSignals() {
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGHUP)
for {
sig := <-sigs
if sig == syscall.SIGHUP {
if err := loadSettings(); err != nil {
log.Println("Error reloading confirguration file. Details:", err)
}
} else if sig == syscall.SIGTERM {
for _, listener := range restListeners {
if err := listener.Close(); err != nil {
log.Println("Error closing listener. Details:", err)
}
}
restListeners = []net.Listener{}
for _, listener := range clientListeners {
if err := listener.Close(); err != nil {
log.Println("Error closing listener. Details:", err)
}
}
clientListeners = []net.Listener{}
// TODO: Wait the last requests to be processed? On epossibly solution is to
// create a request counter in MUX, we wait while this counter is non-zero. If
// there's a scan running what are we going to do?
os.Exit(NoError)
}
}
}()
}
// loadSettings function is responsable for lading the configuration parameters from a
// file. It will be used when the system starts for the first time and when it receives a
// SIGHUP signal
func loadSettings() error {
// TODO: Possible concurrent access problem while reloading the configuration file. And
// we also should reload many structures that could change with the new configuration
// files, like the network interfaces
if err := config.LoadConfig(configFilePath); err != nil {
return err
}
// Load languages to model. We don't do this in the configuration package, because we
// don't want to create a dependency between the model and the config taht could become
// a cross reference
for _, language := range config.ShelterConfig.Languages {
if err := model.AddLanguage(language); err != nil {
return err
}
}
return nil
}