-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
240 lines (210 loc) · 7.05 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
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
package main
import (
"fmt"
"net"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/lunarway/strong-duckling/internal/daemon"
"github.com/lunarway/strong-duckling/internal/http"
"github.com/lunarway/strong-duckling/internal/metrics"
"github.com/lunarway/strong-duckling/internal/strongswan"
"github.com/lunarway/strong-duckling/internal/tcpchecker"
"github.com/lunarway/strong-duckling/internal/vici"
"github.com/lunarway/strong-duckling/internal/whooping"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
var (
version = ""
)
func main() {
var exitCode int
defer func() {
os.Exit(exitCode)
}()
flags := kingpin.New("strong-duckling", "A small sidekick to strongswan VPN")
listenAddress := flags.Flag("listen", "Address on which to expose metrics.").String()
whoopingAddress := flags.Flag("whooping", "Address on which to start whooping.").String()
tcpCheckerAddresses := flags.Flag("tcp-checker", "TCP address to check. Supports <address>:<port> or <name>:<address>:<port>").Strings()
enableReinitiator := flags.Flag("enable-reinitiator", "Enables re-initiation of connections when expected Security Associations are missing").Bool()
log.AddFlags(flags)
flags.HelpFlag.Short('h')
flags.Version(version)
socket := flags.Flag("vici-socket", "VICI (charon.vici) socket to connect to. Usually /var/run/charon.vici").String()
kingpin.MustParse(flags.Parse(os.Args[1:]))
if *enableReinitiator && len(*socket) == 0 {
log.Errorf("--enable-reinitiator requires --vici-socket to be set up")
os.Exit(1)
}
whooper := whooping.Whooper{}
httpServer := http.Define()
if *listenAddress != "" {
whooper.RegisterListener(httpServer, fmt.Sprintf("http://localhost%s", *listenAddress))
metrics.Register(httpServer)
}
prometheusReporter, err := metrics.NewPrometheusReporter(prometheus.DefaultRegisterer, log.Base().With("name", "prometheusReporter"))
if err != nil {
log.Errorf("Failed to register metrics: %v", err)
os.Exit(1)
}
componentDone := make(chan error)
shutdown := make(chan struct{})
var shutdownWg sync.WaitGroup
if whoopingAddress != nil && *whoopingAddress != "" {
logger := log.With("name", "whooper")
whoopDaemon := daemon.New(daemon.Configuration{
Reporter: prometheusReporter.Daemon(logger, "whopper"),
Interval: 1 * time.Second,
Tick: func() {
whooper.Whoop(*whoopingAddress, fmt.Sprintf("http://localhost%s", *listenAddress))
},
})
shutdownWg.Add(1)
go func() {
defer shutdownWg.Done()
whoopDaemon.Loop(shutdown)
}()
}
for _, tcpCheckerAddress := range *tcpCheckerAddresses {
values := strings.Split(tcpCheckerAddress, ":")
var name, address, portStr string
if len(values) == 3 {
name = values[0]
address = values[1]
portStr = values[2]
} else if len(values) == 2 {
address = values[0]
portStr = values[1]
name = fmt.Sprintf("%s:%s", address, portStr)
} else {
log.Errorf("Could not understand tcp-checker %s", tcpCheckerAddress)
os.Exit(1)
}
port, err := strconv.ParseInt(portStr, 10, 32)
if err != nil {
log.Errorf("Could not parse port %s as integer in tcp-checker %s. Error: %s", portStr, tcpCheckerAddress, err)
os.Exit(1)
}
logger := log.
With("type", "tcpchecker").
With("name", name).
With("address", address).
With("port", port)
logger.Infof("Start checking address %s:%v", address, port)
tcpCheckerDaemon := daemon.New(daemon.Configuration{
Reporter: prometheusReporter.Daemon(logger, "tcpchecker"),
Interval: 1 * time.Second,
Tick: func() {
tcpchecker.Check(name, address, int(port), tcpchecker.CompositeReporter(tcpchecker.LogReporter(logger), prometheusReporter.TcpChecker()))
},
})
shutdownWg.Add(1)
go func() {
defer shutdownWg.Done()
tcpCheckerDaemon.Loop(shutdown)
}()
}
if *listenAddress != "" {
go func() {
// no shutdown mechanism in place for the HTTP server
componentDone <- http.Start(httpServer, *listenAddress)
}()
}
shutdownWg.Add(1)
go func() {
defer shutdownWg.Done()
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
select {
case sig := <-sigs:
log.Infof("Received os signal '%s'. Terminating...", sig)
componentDone <- nil
case <-shutdown:
}
}()
if len(*socket) != 0 {
ikeSAStatusReceivers := []strongswan.IKESAStatusReceiver{
prometheusReporter.StrongSwan(),
}
if *enableReinitiator {
reinitiatorClient := viciClient(&shutdownWg, shutdown, componentDone, log.With("viciClient", "reinitiator"), *socket)
reinitiatorClient.ReadTimeout = 5 * time.Minute
ikeSAStatusReceivers = append(ikeSAStatusReceivers, strongswan.NewReinitiator(reinitiatorClient, log.Base().With("name", "reinitiator")))
}
client := viciClient(&shutdownWg, shutdown, componentDone, log.With("viciClient", "collector"), *socket)
client.ReadTimeout = 60 * time.Second
d := daemon.New(daemon.Configuration{
Reporter: prometheusReporter.Daemon(log.Base().With("name", "strongswan"), "strongswan"),
Interval: 2 * time.Second,
Tick: func() {
strongswan.Collect(client, ikeSAStatusReceivers)
},
})
shutdownWg.Add(1)
go func() {
defer shutdownWg.Done()
d.Loop(shutdown)
log.Infof("vici strongswan checker daemon stopped. Terminating...")
}()
}
log.Infof("Strong duckling version %s", version)
prometheusReporter.Info(version)
// this is blocking until some component fails of a signal is received
reason := <-componentDone
close(shutdown)
log.Info("waiting for all components to shutdown")
shutdownWg.Wait()
if reason != nil {
log.Errorf("exited due to error: %v", reason)
exitCode = 1
} else {
log.Info("exited due to a component shutting down")
}
}
// viciClient returns a listening vici.ClientConn controlled by provided life
// cycle channels.
func viciClient(shutdownWg *sync.WaitGroup, shutdown chan struct{}, componentDone chan error, log log.Logger, socket string) *vici.ClientConn {
conn, err := net.Dial("unix", socket)
if err != nil {
log.Errorf("Failed to establish socket connection to vici on '%s': %v", socket, err)
os.Exit(1)
}
client := vici.NewClientConn(conn)
shutdownWg.Add(1)
go func() {
defer shutdownWg.Done()
log.Info("vici client shutdown listener started")
defer log.Info("vici client shutdown listener stopped")
<-shutdown
log.Info("Closing vici client listener")
err := client.Close()
if err != nil {
log.Errorf("Controlled close of vici client failed: %v", err)
}
}()
shutdownWg.Add(1)
go func() {
defer shutdownWg.Done()
log.Infof("vici client listening on %s", socket)
defer log.Info("vici client lister Go routine stopped")
err := client.Listen()
if err != nil {
// we don't know if Listen stopped due to a controlled shutdown or due
// to an underlying error. Log the error in the former case or report
// the component done if the shutdown is unexpected
select {
case componentDone <- fmt.Errorf("vici client listener stopped unexpectedly: %w", err):
return
default:
log.Infof("vici client listener stopped: %v", err)
}
}
}()
return client
}