-
Notifications
You must be signed in to change notification settings - Fork 12
/
client_rm_handlers.go
321 lines (252 loc) · 8.22 KB
/
client_rm_handlers.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
package client
import (
"errors"
"fmt"
"time"
"github.com/companyzero/bisonrelay/client/clientdb"
"github.com/companyzero/bisonrelay/client/clientintf"
"github.com/companyzero/bisonrelay/rpc"
"github.com/companyzero/bisonrelay/sw"
"github.com/companyzero/sntrup4591761"
"github.com/davecgh/go-spew/spew"
"github.com/decred/slog"
)
// clearLastHandshakeAttemptTime clears the last handshake attempt time for
// the given user.
func (c *Client) clearLastHandshakeAttemptTime(ru *RemoteUser) error {
return c.dbUpdate(func(tx clientdb.ReadWriteTx) error {
entry, err := c.db.GetAddressBookEntry(tx, ru.id)
if err != nil {
return err
}
entry.LastHandshakeAttempt = time.Time{}
return c.db.UpdateAddressBookEntry(tx, entry)
})
}
// handleRMHandshake handles all handshake messages.
func (c *Client) handleRMHandshake(ru *RemoteUser, msg interface{}) error {
switch msg.(type) {
case rpc.RMHandshakeSYN:
ru.log.Infof("Received handshake SYN. Replying with SYN/ACK.")
c.ntfns.notifyHandshakeStage(ru, "SYN")
return c.sendWithSendQ("synack", rpc.RMHandshakeSYNACK{}, ru.ID())
case rpc.RMHandshakeSYNACK:
ru.log.Infof("Received handshake SYN/ACK. Replying with ACK. User ratchet is fully synced.")
if err := c.clearLastHandshakeAttemptTime(ru); err != nil {
return err
}
c.ntfns.notifyHandshakeStage(ru, "SYNACK")
return c.sendWithSendQ("synack", rpc.RMHandshakeACK{}, ru.ID())
case rpc.RMHandshakeACK:
ru.log.Infof("Received handshake ACK. User ratchet is fully synced.")
if err := c.clearLastHandshakeAttemptTime(ru); err != nil {
return err
}
c.ntfns.notifyHandshakeStage(ru, "ACK")
return nil
default:
return fmt.Errorf("wrong msg payload sent to handleRMHandshake: %T", msg)
}
}
func (c *Client) handleTransitiveMsg(ru *RemoteUser, tm rpc.RMTransitiveMessage) error {
c.ntfns.notifyTransitiveEvent(ru.ID(), tm.For, TEMsgForward)
target, err := c.rul.byID(tm.For)
if err != nil {
ru.log.Warnf("Received message to forward to unknown target %s",
tm.For)
}
ru.log.Infof("Forwarding msg to %s", target)
fwd := rpc.RMTransitiveMessageForward{
From: ru.ID(),
CipherText: tm.CipherText,
Message: tm.Message,
}
go func() {
payEvent := fmt.Sprintf("fwdtransitive.%s", ru.ID())
err := target.sendRM(fwd, payEvent)
if err != nil && !errors.Is(err, clientintf.ErrSubsysExiting) {
ru.log.Errorf("Unable to send transitive msg reply: %v", err)
}
}()
return nil
}
func (c *Client) handleTransitiveMsgFwd(ru *RemoteUser, fwd rpc.RMTransitiveMessageForward) error {
ct := (*sntrup4591761.Ciphertext)(&fwd.CipherText)
pk := (*sntrup4591761.PrivateKey)(&c.localID.privKey)
sk, n := sntrup4591761.Decapsulate(ct, pk)
if n != 1 {
return fmt.Errorf("could not decapsulate shared key from transitive msg fwd")
}
// Decrypt transitive command
cleartext, ok := sw.Open(fwd.Message, sk)
if !ok {
return fmt.Errorf("could not open transitive command")
}
// Get header and command
from, err := c.rul.byID(fwd.From)
var msgVerifier rpc.MessageVerifier
if err != nil {
if !errors.Is(err, userNotFoundError{}) {
return fmt.Errorf("error finding target user %s of transitive "+
"forward: %v", fwd.From, err)
}
// This should only happen during KX.
} else {
msgVerifier = from.verifyMessage
}
h, cmd, err := rpc.DecomposeRM(msgVerifier, cleartext, uint(c.q.MaxMsgSize()))
if err != nil {
return fmt.Errorf("handleRMTransitiveMessageForward: "+
"decompose %v", err)
}
if h.Version != rpc.RMHeaderVersion {
return fmt.Errorf("handleRMTransitiveMessageForward: "+
"header version %v, want %v", h.Version,
rpc.RMHeaderVersion)
}
// Assume DecomposeRM generated the correct type for a given command.
switch p := cmd.(type) {
case rpc.OOBPublicIdentityInvite:
return c.handleTransitiveIDInvite(ru, p)
case rpc.RMTransitiveReset:
return c.handleRMTransitiveReset(ru, fwd.From, p)
case rpc.RMTransitiveResetReply:
return c.handleRMTransitiveResetReply(ru, fwd.From, p)
default:
return fmt.Errorf("unknown command payload type %T for "+
"transitive fwd cmd %q", cmd, h.Command)
}
}
func (c *Client) innerHandleUserRM(ru *RemoteUser, h *rpc.RMHeader,
p interface{}, ts time.Time) error {
// DecomposeRM should have created the correct payload type based on
// the command, so switch on the payload type directly.
switch p := p.(type) {
case rpc.RMPrivateMessage:
if ru.IsIgnored() {
ru.log.Tracef("Ignoring received PM")
return nil
}
if filter, _ := c.FilterPM(ru.ID(), p.Message); filter {
return nil
}
err := c.dbUpdate(func(tx clientdb.ReadWriteTx) error {
return c.db.LogPM(tx, ru.ID(), false, ru.Nick(), p.Message, ts)
})
if err != nil {
return err
}
ru.log.Debugf("Received private message of length %d", len(p.Message))
c.ntfns.notifyOnPM(ru, p, ts)
case rpc.RMHandshakeSYN, rpc.RMHandshakeACK, rpc.RMHandshakeSYNACK:
return c.handleRMHandshake(ru, p)
case rpc.RMProfileUpdate:
return c.handleProfileUpdate(ru, p)
case rpc.RMGroupInvite:
return c.handleGCInvite(ru, p)
case rpc.RMGroupJoin:
return c.handleGCJoin(ru, p)
case rpc.RMGroupList:
return c.handleGCList(ru, p)
case rpc.RMGroupUpgradeVersion:
return c.handleGCUpgradeVersion(ru, p)
case rpc.RMGroupUpdateAdmins:
return c.handleGCUpdateAdmins(ru, p)
case rpc.RMGroupMessage:
if ru.IsIgnored() {
ru.log.Tracef("Ignoring received GC message")
return nil
}
return c.handleGCMessage(ru, p, ts)
case rpc.RMMediateIdentity:
return c.handleMediateID(ru, p)
case rpc.RMBlock:
return c.handleRMBlock(ru, p)
case rpc.RMInvite:
return c.handleRMInvite(ru, p)
case rpc.RMFTList:
return c.handleFTList(ru, p)
case rpc.RMFTListReply:
return c.handleFTListReply(ru, p)
case rpc.RMFTGet:
return c.handleFTGet(ru, p)
case rpc.RMFTGetReply:
return c.handleFTGetReply(ru, p)
case rpc.RMFTGetChunk:
return c.handleFTGetChunk(ru, p)
case rpc.RMFTPayForChunk:
return c.handleFTPayForChunk(ru, p)
case rpc.RMFTGetChunkReply:
return c.handleFTGetChunkReply(ru, p)
case rpc.RMFTSendFile:
return c.handleFTSendFile(ru, p)
case rpc.RMTransitiveMessage:
return c.handleTransitiveMsg(ru, p)
case rpc.RMTransitiveMessageForward:
return c.handleTransitiveMsgFwd(ru, p)
case rpc.RMGetInvoice:
return c.handleGetInvoice(ru, p)
case rpc.RMInvoice:
return c.handleInvoice(ru, p)
case rpc.RMListPosts:
return c.handleListPosts(ru, p)
case rpc.RMListPostsReply:
return c.handleListPostsReply(ru, p)
case rpc.RMGetPost:
return c.handleGetPost(ru, p)
case rpc.RMPostsSubscribe:
return c.handlePostsSubscribe(ru, p)
case rpc.RMPostsSubscribeReply:
return c.handlePostsSubscribeReply(ru, p)
case rpc.RMPostsUnsubscribe:
return c.handlePostsUnsubscribe(ru, p)
case rpc.RMPostsUnsubscribeReply:
return c.handlePostsUnsubscribeReply(ru, p)
case rpc.RMPostShare:
return c.handlePostShare(ru, p)
case rpc.RMPostStatus:
return c.handlePostStatus(ru, p)
case rpc.RMPostStatusReply:
return c.handlePostStatusReply(ru, p)
case rpc.RMReceiveReceipt:
return c.handleReceiveReceipt(ru, p, ts)
case rpc.RMGroupKick:
return c.handleGCKick(ru, p)
case rpc.RMGroupPart:
return c.handleGCPart(ru, p)
case rpc.RMGroupKill:
return c.handleGCKill(ru, p)
case rpc.RMKXSearch:
return c.handleKXSearch(ru, p)
case rpc.RMKXSearchReply:
return c.handleKXSearchReply(ru, p)
case rpc.RMKXSuggestion:
return c.handleKXSuggestion(ru, p)
case rpc.RMFetchResource:
return c.handleFetchResource(ru, p)
case rpc.RMFetchResourceReply:
return c.handleFetchResourceReply(ru, p)
default:
return fmt.Errorf("Received unknown command %q payload %T",
h.Command, p)
}
return nil
}
// handleUserRM is the main handler for remote user RoutedMessages. It decides
// what to do with the given RM from the given user.
func (c *Client) handleUserRM(ru *RemoteUser, h *rpc.RMHeader, p interface{}, ts time.Time) {
ru.log.Tracef("Starting to handle %T", p)
c.ntfns.notifyRMReceived(ru, h, p, ts)
c.gcmq.RMReceived(ru.ID(), ts)
err := c.innerHandleUserRM(ru, h, p, ts)
if err != nil {
ru.log.Errorf("Error handling %q (payload %T) from user: %v",
h.Command, p, err)
if ru.log.Level() <= slog.LevelDebug {
ru.logPayloads.Debugf("Payload for error %v: %s",
err, spew.Sdump(p))
}
} else {
ru.log.Tracef("Finished handling %T", p)
}
}