-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
167 lines (145 loc) · 4.2 KB
/
main.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
package main
import (
"crypto/subtle"
"github.com/gin-gonic/gin"
"github.com/jessevdk/go-flags"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
ginlogrus "github.com/toorop/gin-logrus"
"net/http"
"notdeadyet/config"
"notdeadyet/notify"
"notdeadyet/watching"
"os"
)
type Options struct {
ConfigFile string `short:"c" long:"config-file" description:"The config file to load, can be a .toml, .yml or .json"`
LogLevel string `short:"l" long:"log-level" description:"The log level" choice:"debug" choice:"info" choice:"warn" default:"info"`
LogJson bool `short:"j" long:"log-json" description:"Write logs as json"`
}
var (
options Options
flagsParser = flags.NewParser(&options, flags.Default)
cfg config.Config
receivers []notify.Receiver
watchers []*watching.Watcher
)
func main() {
// Parse CLI options.
if _, err := flagsParser.Parse(); err != nil {
if flagsErr, ok := err.(*flags.Error); ok {
if flagsErr.Type == flags.ErrHelp {
os.Exit(0)
} else {
os.Exit(1)
}
} else {
log.Fatal(err)
}
}
// Configure logging.
logLevel, err := log.ParseLevel(options.LogLevel)
if err != nil {
log.WithError(err).Fatal("Unknown log level.")
}
log.SetLevel(logLevel)
if options.LogJson {
log.SetFormatter(&log.JSONFormatter{})
}
// Specify configuration file location.
if options.ConfigFile != "" {
viper.SetConfigFile(options.ConfigFile)
} else {
viper.SetConfigName("config")
viper.AddConfigPath("/etc/notdeadyet/")
viper.AddConfigPath(".")
}
// Read configuration file.
log.Info("Parsing configuration...")
config.SetDefaults(viper.GetViper())
if err := viper.ReadInConfig(); err != nil {
log.WithField("File", viper.ConfigFileUsed()).WithError(err).Fatal("Cannot read config file.")
}
// Unmarshal configuration.
if err := viper.Unmarshal(&cfg); err != nil {
log.WithField("File", viper.ConfigFileUsed()).WithError(err).Fatal("Error unmarshalling config file.")
}
log.WithField("File", viper.ConfigFileUsed()).Debug("Configuration parsed.")
// Create receivers.
initReceivers()
// Create and start watchers.
initWatchers()
// Configure gin mode.
if log.IsLevelEnabled(log.DebugLevel) {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
// Configure the HTTP server engine.
g := gin.New()
g.Use(ginlogrus.Logger(log.StandardLogger()), gin.Recovery())
g.GET("/", handleIndex)
g.GET("/im-alive/:token", handleLiveSign)
g.POST("/im-alive/:token", handleLiveSign)
// Run HTTP server.
log.Info("Listening...")
if err := g.Run(cfg.Listen); err != nil {
log.WithError(err).Fatal("Starting HTTP server failed.")
}
}
func initReceivers() {
for _, rc := range cfg.Receivers.PushoverReceivers {
r, err := notify.NewPushoverReceiver(&rc)
if err != nil {
log.WithField("receiver", rc.Name).WithError(err).Fatal("Cannot create pushover receiver.")
}
receivers = append(receivers, r)
}
}
func initWatchers() {
for _, ac := range cfg.Apps {
// Filter receivers.
filteredReceivers := make([]notify.Receiver, 0)
for _, receiverName := range ac.NotificationReceivers {
var receiver notify.Receiver
for _, r := range receivers {
if r.Config().Name == receiverName {
receiver = r
break
}
}
if receiver == nil {
log.WithField("app", ac.Name).Fatalf("Receiver \"%s\" does not exist.", receiverName)
}
filteredReceivers = append(filteredReceivers, receiver)
}
// Create watcher.
w, err := watching.NewWatcher(ac, filteredReceivers)
if err != nil {
log.WithField("app", ac.Name).WithError(err).Fatal("Cannot create app watcher.")
}
w.Start()
watchers = append(watchers, w)
}
}
func handleIndex(c *gin.Context) {
c.String(http.StatusOK, "Not Dead Yet? - The dead man's switch monitoring daemon.")
}
func handleLiveSign(c *gin.Context) {
// Find the app.
token := c.Param("token")
var foundWatcher *watching.Watcher
for _, w := range watchers {
// Simple protection against brute force attacks.
if subtle.ConstantTimeCompare([]byte(w.App.Token), []byte(token)) == 1 {
foundWatcher = w
break
}
}
if foundWatcher == nil {
c.String(http.StatusNotFound, "Unknown token.")
return
}
foundWatcher.HandleLiveSign()
c.String(http.StatusOK, "Got it. Waiting 'till you die.")
}