-
Notifications
You must be signed in to change notification settings - Fork 22
/
unit_inbox.go
222 lines (198 loc) · 6.12 KB
/
unit_inbox.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
package api
import (
"encoding/base64"
"encoding/json"
"errors"
"github.com/evilsocket/islazy/log"
"github.com/evilsocket/pwngrid/crypto"
"github.com/evilsocket/pwngrid/models"
"github.com/go-chi/chi"
"io/ioutil"
"net/http"
"strconv"
"time"
)
var (
ErrRecNotFound = errors.New("recipient not found")
ErrMessageNotFound = errors.New("message not found")
ErrInvalidKey = errors.New("invalid public key")
ErrInvalidSignature = errors.New("can't verify signature")
ErrDecoding = errors.New("error decoding data")
)
func (api *API) GetInbox(w http.ResponseWriter, r *http.Request) {
unit := Authenticate(w, r)
if unit == nil {
return
}
page, err := pageNum(r)
if err != nil {
ERROR(w, http.StatusUnprocessableEntity, err)
return
}
messages, total, pages := unit.GetPagedInbox(page)
JSON(w, http.StatusOK, map[string]interface{}{
"records": total,
"pages": pages,
"messages": messages,
})
}
// we need this because the models.Message structure doesn't not export data and signature for fast listing.
type fullMessage struct {
ID uint `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at"`
SeenAt *time.Time `json:"seen_at"`
Sender string `json:"sender"`
SenderName string `json:"sender_name"`
Data string `json:"data"`
Signature string `json:"signature"`
}
func (api *API) GetInboxMessage(w http.ResponseWriter, r *http.Request) {
unit := Authenticate(w, r)
if unit == nil {
return
}
msgIDParam := chi.URLParam(r, "msg_id")
msgID, err := strconv.Atoi(msgIDParam)
if err != nil {
ERROR(w, http.StatusUnprocessableEntity, err)
return
} else if message := unit.GetInboxMessage(msgID); message == nil {
ERROR(w, http.StatusNotFound, ErrMessageNotFound)
return
} else {
JSON(w, http.StatusOK, fullMessage{
ID: message.ID,
CreatedAt: message.CreatedAt,
UpdatedAt: message.UpdatedAt,
DeletedAt: message.DeletedAt,
SeenAt: message.SeenAt,
Sender: message.Sender,
SenderName: message.SenderName,
Data: message.Data,
Signature: message.Signature,
})
}
}
func (api *API) MarkInboxMessage(w http.ResponseWriter, r *http.Request) {
unit := Authenticate(w, r)
if unit == nil {
return
}
now := time.Now()
markAs := chi.URLParam(r, "mark")
msgIDParam := chi.URLParam(r, "msg_id")
msgID, err := strconv.Atoi(msgIDParam)
if err != nil {
ERROR(w, http.StatusUnprocessableEntity, err)
return
} else if message := unit.GetInboxMessage(msgID); message == nil {
ERROR(w, http.StatusNotFound, ErrMessageNotFound)
return
} else if markAs == "seen" {
if err := models.UpdateFields(message, map[string]interface{}{"seen_at": &now}).Error; err != nil {
ERROR(w, http.StatusUnprocessableEntity, err)
return
}
} else if markAs == "unseen" {
if err := models.UpdateFields(message, map[string]interface{}{"seen_at": nil}).Error; err != nil {
ERROR(w, http.StatusUnprocessableEntity, err)
return
}
} else if markAs == "deleted" {
if err := models.UpdateFields(message, map[string]interface{}{"deleted_at": &now}).Error; err != nil {
ERROR(w, http.StatusUnprocessableEntity, err)
return
}
} else if markAs == "restored" {
if err := models.UpdateFields(message, map[string]interface{}{"deleted_at": nil}).Error; err != nil {
ERROR(w, http.StatusUnprocessableEntity, err)
return
}
} else {
ERROR(w, http.StatusNotFound, ErrEmpty)
return
}
JSON(w, http.StatusOK, map[string]bool{
"success": true,
})
}
func (api *API) SendMessageTo(w http.ResponseWriter, r *http.Request) {
// authenticate source unit
srcUnit := Authenticate(w, r)
if srcUnit == nil {
return
}
// get dest unit by fingerprint
dstUnitFingerprint := chi.URLParam(r, "fingerprint")
dstUnit := models.FindUnitByFingerprint(dstUnitFingerprint)
if dstUnit == nil {
ERROR(w, http.StatusNotFound, ErrRecNotFound)
return
}
// read the message and signature from the source unit
client := clientIP(r)
body, err := ioutil.ReadAll(r.Body)
if err != nil {
ERROR(w, http.StatusUnprocessableEntity, err)
return
}
var message Message
if err = json.Unmarshal(body, &message); err != nil {
log.Debug("error while decoding message from %s: %v", srcUnit.Identity(), err)
log.Debug("%s", body)
ERROR(w, http.StatusUnprocessableEntity, err)
return
}
if err := models.ValidateMessage(message.Data, message.Signature); err != nil {
log.Warning("client %s sent a broken message structure: %v", srcUnit.Identity(), err)
ERROR(w, http.StatusUnprocessableEntity, err)
return
}
// parse source unit key
srcKeys, err := crypto.FromPublicPEM(srcUnit.PublicKey)
if err != nil {
log.Warning("error decoding key from %s: %v", srcUnit.Identity(), err)
log.Debug("%s", srcUnit.PublicKey)
ERROR(w, http.StatusUnprocessableEntity, ErrInvalidKey)
return
}
// decode data, signature and verify SIGN(SHA256(data))
data, err := base64.StdEncoding.DecodeString(message.Data)
if err != nil {
log.Warning("error decoding message from %s: %v", srcUnit.Identity(), err)
log.Debug("%s", message.Data)
ERROR(w, http.StatusUnprocessableEntity, ErrDecoding)
return
}
signature, err := base64.StdEncoding.DecodeString(message.Signature)
if err != nil {
log.Warning("error decoding signature from %s: %v", srcUnit.Identity(), err)
log.Debug("%s", message.Signature)
ERROR(w, http.StatusUnprocessableEntity, ErrDecoding)
return
}
if err := srcKeys.VerifyMessage(data, signature); err != nil {
log.Warning("error verifying signature from %s: %v", srcUnit.Identity(), err)
log.Debug("%s", message.Signature)
ERROR(w, http.StatusUnprocessableEntity, ErrInvalidSignature)
return
}
msg := models.Message{
SenderID: srcUnit.ID,
Sender: srcUnit.Fingerprint,
SenderName: srcUnit.Name,
ReceiverID: dstUnit.ID,
Data: message.Data,
Signature: message.Signature,
}
if err := models.Create(&msg).Error; err != nil {
log.Warning("error creating msg %v from %s: %v", msg, client, err)
ERROR(w, http.StatusInternalServerError, ErrEmpty)
return
}
JSON(w, http.StatusOK, map[string]interface{}{
"success": true,
})
}