This repository has been archived by the owner on Jan 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler-host.go
533 lines (456 loc) · 11.4 KB
/
handler-host.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
package logx
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"sync"
"time"
"github.com/etcd-io/bbolt"
uuid "github.com/nu7hatch/gouuid"
stringutil "github.com/monstercat/golib/string"
)
const (
MsgTypeDecode = "Decode"
MsgTypeHeartbeat = "Heartbeat"
MsgTypeRegister = "Register"
MsgTypeAuthorization = "Authorization"
)
var (
BucketName = []byte("logx")
ErrCertRequired = errors.New("certificate required")
)
// Host Handler will communicate with the server
// and send all detailed logs to said server.
//
// It will try to send the logs to the server within
// a certain frequency, but at certain times
// (e.g., host panic or signal interruption)
// a Flush() command is provided to flush that data
// directly to the server.
type HostHandler struct {
// This is the endpoint of the host to which
// this logger is connected.
Endpoint string
// Certificate
CertFile, KeyFile string
// Certificate pair for caching purposes
// after loading from the CertFile and KeyFile.
pair tls.Certificate
// Origin machine - the name of the current machine
Machine string
// Service - name of the current 'service', if any.
Service string
// Password to use to login.
Password string
// Duration for which we wait before sending logs
// to the host process.
WaitDuration time.Duration
// Period at which to send the heartbeat.
HeartBeatDuration time.Duration
// Cache to store unsent messages.
// This should be a directory. The system on startup
// will attempt to read this directory for any existing files and
// send them to the host.
CacheFileLocation string
db *bbolt.DB
// List of filenames/Ids that are currently being sent to the server,
// so they do not get sent again.
currentlySending []string
currentlySendingMu sync.RWMutex
// Channel to stop processing
die chan bool
}
// Host Message is messages that are sent to the host.
type HostMessage struct {
Id string
Type string
Time time.Time
Message []byte
Context []byte
// Only used by the register message
Machine string
Service string
}
// Client messages are messsages sent to the client.
type ClientMessage struct {
Type string
Status ClientMessageStatus
Message string `json:"Message,omitempty"`
Id string `json:"Id,omitempty"`
}
type ClientMessageStatus string
const (
ClientMessageStatusFailed ClientMessageStatus = "Failed"
ClientMessageStatusSuccessful ClientMessageStatus = "Successful"
)
// Handle handles incoming logs by storing them
// in files in CacheFileLocation.
func (h *HostHandler) Handle(l Log) (int, error) {
hostLog, ok := l.(HostLog)
if !ok {
return 0, nil
}
if err := h.Store(hostLog); err != nil {
return 0, err
}
byt, err := json.Marshal(hostLog)
if err != nil {
return 0, err
}
return len(byt), nil
}
func (h *HostHandler) StartDb() (err error) {
if h.db != nil {
return nil
}
h.db, err = bbolt.Open(h.CacheFileLocation, 0666, nil)
return
}
func (h *HostHandler) createBucketIfNotExists(tx *bbolt.Tx) (b *bbolt.Bucket, err error) {
b = tx.Bucket(BucketName)
if b != nil {
return
}
return tx.CreateBucket(BucketName)
}
type storedHostLog struct{
BaseHostLog
Id string
Context []byte
}
func (h *HostHandler) Store(l HostLog) error {
b := l.HostLog()
c := l.Context()
var B storedHostLog
B.BaseHostLog = b
ctx, err := json.Marshal(c)
if err != nil {
return err
}
B.Context = ctx
if err := h.StartDb(); err != nil {
return err
}
// Insert
return h.db.Update(func(tx *bbolt.Tx) error {
bucket, err := h.createBucketIfNotExists(tx)
if err != nil {
return err
}
id, err := uuid.NewV4()
if err != nil {
return err
}
B.Id = id.String()
byt, err := json.Marshal(B)
if err != nil {
return err
}
return bucket.Put([]byte(B.Id), byt)
})
}
func (h *HostHandler) initStopChannels() {
h.die = make(chan bool)
}
func (h *HostHandler) Close() {
if h.db != nil {
h.db.Close()
}
close(h.die)
}
func (h *HostHandler) RunForever(errCh chan error) {
defer close(errCh)
if err := h.Startup(); err != nil {
errCh <- err
return
}
defer h.db.Close()
currDelay := 5 * time.Millisecond
now := time.Now()
for {
select {
case <-h.die:
return
case <-time.After(100 * time.Millisecond):
}
// Continually restart!
h.run(errCh)
if time.Now().Sub(now).Seconds() < 10 {
time.Sleep(currDelay)
currDelay *= 2
if currDelay > time.Second {
currDelay = time.Second
}
} else {
currDelay = 5 * time.Millisecond
}
}
}
// Run will run the host handler. As it contains
// an infinite loop, it should be called in a
// go routine.
//
// This function will call startup as well, so
// calling startup is not necessary.
func (h *HostHandler) Run(errCh chan error) {
defer close(errCh)
if err := h.Startup(); err != nil {
errCh <- err
return
}
defer h.db.Close()
h.run(errCh)
}
func (h *HostHandler) run(errCh chan error) {
conn, err := h.connect()
if err != nil {
errCh <- err
return
}
defer conn.Close()
wrCh := make(chan HostMessage)
go h.RunHeartbeat(wrCh)
go h.ReadResponses(conn, errCh)
go h.SendLogs(wrCh, errCh)
// This for loop actually writes all the responses.
for {
select {
case <-h.die:
return
case msg := <-wrCh:
if err := h.sendToHost(conn, msg); err != nil {
errCh <- err
// If the function returns, the wrapping function should
// know to restart!
return
}
}
}
}
// SendLogs sends logs to the host. In general, this function
// should not be called directly, as it is called in the Run function.
//
// It does this by reading the CacheFileLocation for any files containing
// a log or logs.
func (h *HostHandler) SendLogs(wrCh chan HostMessage, errCh chan error) {
for {
select {
case <-h.die:
return
case <-time.After(h.WaitDuration):
h.currentlySendingMu.RLock()
sending := h.currentlySending
h.currentlySendingMu.RUnlock()
err := h.db.View(func(tx *bbolt.Tx) error {
bucket := tx.Bucket(BucketName)
if bucket == nil {
return nil
}
return bucket.ForEach(func(k, v []byte) error {
var l storedHostLog
if err := json.Unmarshal(v, &l); err != nil {
return err
}
if stringutil.StringInList(sending, l.Id) {
return nil
}
h.currentlySendingMu.Lock()
h.currentlySending = append(h.currentlySending, l.Id)
h.currentlySendingMu.Unlock()
wrCh <- HostMessage{
Id: l.Id,
Type: l.Type,
Time: l.Time,
Message: l.Message,
Context: l.Context,
}
return nil
})
})
if err != nil {
errCh <- err
}
}
}
}
// Startup starts up the connection to the server. It does this by
// ensuring the certificate and key are present. If they are not, it
// will try to generate the cert and the key.
//
// Then, it will register itself with the host.
func (h *HostHandler) Startup() error {
h.initStopChannels()
if h.CertFile == "" || h.KeyFile == "" {
return ErrCertRequired
}
// Try to load the key pair. If they don't exist,
// then try to create them!
var err error
h.pair, err = tls.LoadX509KeyPair(h.CertFile, h.KeyFile)
// If it has expired, or is invalid, then create new ones as well!
if err != nil || h.pair.Leaf == nil || h.pair.Leaf.NotAfter.Before(time.Now()) {
cert, key, err := GenerateCerts(time.Hour * 24 * 365)
if err != nil {
return err
}
if err := WriteCertificate(cert, h.CertFile); err != nil {
return err
}
if err := WritePrivateKey(key, h.KeyFile); err != nil {
return err
}
//We cannot load directly, because the X509KeyPair function
//requires PEM data.
h.pair, err = tls.LoadX509KeyPair(h.CertFile, h.KeyFile)
if err != nil {
return err
}
}
// Before registration, start the sql lite database.
if err := h.StartDb(); err != nil {
return err
}
return h.Register()
}
// This function continually reads responses from the server and decodes it. If there
// are any errors, it will send the errors back through the error channel.
// Otherwise, it will process the messages appropriately.
func (h *HostHandler) ReadResponses(conn *tls.Conn, errCh chan error) {
dec := json.NewDecoder(conn)
for {
select {
case <-h.die:
return
case <-time.After(time.Millisecond):
}
var m ClientMessage
if err := dec.Decode(&m); err != nil {
if err == io.EOF {
// TODO: restart connection!
return
}
errCh <- err
}
// Remove from "sending"
h.currentlySendingMu.Lock()
for idx, id := range h.currentlySending {
if id == m.Id {
h.currentlySending[idx] = h.currentlySending[0]
h.currentlySending = h.currentlySending[1:]
break
}
}
h.currentlySendingMu.Unlock()
if m.Status == ClientMessageStatusFailed {
errCh <- errors.New(m.Message)
continue
}
if err := h.Remove(m.Id); err != nil {
errCh <- err
}
}
}
func (h *HostHandler) Remove(id string) error {
return h.db.Update(func(tx *bbolt.Tx) error {
b := tx.Bucket(BucketName)
if b == nil {
return nil
}
return b.Delete([]byte(id))
})
}
func (h *HostHandler) GetLocalLogs() (arr []*BaseHostLog, err error) {
arr = make([]*BaseHostLog, 0, 10)
err = h.db.View(func(tx *bbolt.Tx) error {
b := tx.Bucket(BucketName)
if b == nil {
return nil
}
return b.ForEach(func(k, v []byte) error {
var l BaseHostLog
if err := json.Unmarshal(v, &l); err != nil {
return err
}
arr = append(arr, &l)
return nil
})
})
return
}
// Registration with the host involves sending the origin and the
// service information to the host with a Register Message and a password.
//
// The host will associate a hash of the certificate with the
// provided origin and service, as long as the password is correct.
//
// Any future requests can be made using the same certificate
// and will automatically be associated with said service and origin.
//
// This allows for public key encryption for most of the log messages
// which ensures that services with the password cannot easily
// mimic another service/origin.
func (h *HostHandler) Register() error {
conn, err := h.connect()
if err != nil {
return err
}
defer conn.Close()
msg := HostMessage{
Machine: h.Machine,
Service: h.Service,
Type: MsgTypeRegister,
Message: []byte(h.Password),
}
if err := h.sendToHost(conn, msg); err != nil {
return err
}
// Read response and handle any errors.
dec := json.NewDecoder(conn)
var m ClientMessage
if err := dec.Decode(&m); err != nil {
return err
}
if m.Type != MsgTypeRegister {
return errors.New(fmt.Sprintf("Registration error: Invalid response type from server. Expect %s got %s", MsgTypeRegister, m.Type))
}
if m.Status == ClientMessageStatusFailed {
return errors.New("Registration error: " + m.Message)
}
return nil
}
// A heartbeat is a signal that is sent to the host to tell the host
// that the client process is still alive.
func (h *HostHandler) RunHeartbeat(wrCh chan HostMessage) {
for {
select {
case <-h.die:
return
case <-time.After(h.HeartBeatDuration):
wrCh <- HostMessage{
Machine: h.Machine,
Service: h.Service,
Type: MsgTypeHeartbeat,
}
}
}
}
func (h *HostHandler) connect() (*tls.Conn, error) {
conn, err := tls.Dial("tcp", h.Endpoint, &tls.Config{
Certificates: []tls.Certificate{h.pair},
InsecureSkipVerify: true,
})
if err != nil {
return nil, err
}
return conn, nil
}
func (h *HostHandler) sendToHost(conn *tls.Conn, msg HostMessage) error {
byt, err := json.Marshal(msg)
if err != nil {
return err
}
_, err = conn.Write(byt)
return err
}