/
transport.go
100 lines (89 loc) · 2.49 KB
/
transport.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
package kr
import (
"encoding/base64"
"strings"
)
type Transport interface {
Setup(ps *PairingSecret) (err error)
PushAlert(ps *PairingSecret, alertText string, message []byte) (err error)
SendMessage(ps *PairingSecret, message []byte) (err error)
Read(notifier *Notifier, ps *PairingSecret) (ciphertexts [][]byte, err error)
}
type AWSTransport struct{}
func (t AWSTransport) Setup(ps *PairingSecret) (err error) {
_, err = CreateQueue(ps.SQSSendQueueName())
if err != nil {
return
}
_, err = CreateQueue(ps.SQSRecvQueueName())
if err != nil {
return
}
return
}
func (t AWSTransport) PushAlert(ps *PairingSecret, alertText string, message []byte) (err error) {
ctxt, err := ps.EncryptMessage(message)
if err != nil {
return
}
ctxtString := base64.StdEncoding.EncodeToString(ctxt)
go func() {
arn := ps.GetSNSEndpointARN()
if arn != nil {
if pushErr := PushAlertToSNSEndpoint(alertText, ctxtString, *arn, ps.SQSSendQueueName()); pushErr != nil {
log.Error("Push error:", pushErr)
}
}
}()
err = SendToQueue(ps.SQSSendQueueName(), ctxtString)
if err != nil {
return
}
return
}
func (t AWSTransport) SendMessage(ps *PairingSecret, message []byte) (err error) {
ctxt, err := ps.EncryptMessage(message)
if err != nil {
return
}
ctxtString := base64.StdEncoding.EncodeToString(ctxt)
go func() {
ps.Lock()
arn := ps.snsEndpointARN
ps.Unlock()
if arn != nil {
if pushErr := PushToSNSEndpoint(ctxtString, *arn, ps.SQSSendQueueName()); pushErr != nil {
log.Error("Push error:", pushErr)
}
}
}()
err = SendToQueue(ps.SQSSendQueueName(), ctxtString)
if err != nil {
return
}
return
}
func notifyIfSignatureExpiredErr(err error, notifier *Notifier) {
if err == nil || notifier == nil {
return
}
if strings.Contains(err.Error(), "Signature expired") {
notifier.Notify([]byte(Red("Kryptonite ▶ Your system time is out of sync! Kryptonite will not work until you have synchronized your system time. Please run ") + Yellow(NTP_UPDATE_CMD) + Red(" and try again.") + "\r\n"))
}
}
func (t AWSTransport) Read(notifier *Notifier, ps *PairingSecret) (ciphertexts [][]byte, err error) {
ctxtStrings, err := ReceiveAndDeleteFromQueue(ps.SQSRecvQueueName())
notifyIfSignatureExpiredErr(err, notifier)
if err != nil {
return
}
for _, ctxtString := range ctxtStrings {
ctxt, err := base64.StdEncoding.DecodeString(ctxtString)
if err != nil {
log.Error("base64 ciphertext decoding error")
continue
}
ciphertexts = append(ciphertexts, ctxt)
}
return
}