-
Notifications
You must be signed in to change notification settings - Fork 12
/
routedrpc.go
1124 lines (878 loc) · 28.6 KB
/
routedrpc.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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package rpc
import (
"bytes"
"compress/zlib"
"crypto/sha256"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"time"
"github.com/companyzero/bisonrelay/ratchet"
"github.com/companyzero/bisonrelay/zkidentity"
"github.com/decred/dcrd/crypto/blake256"
)
// Header that describes the payload that follows.
const (
RMHeaderVersion = 1
// maxRMEncodedSize is the absolute maximum value of a a decrompressed
// routed message (plus its header). This is the upper bound used when
// decompressing RMs to avoid decompressing bombs and should be larger
// then the max actual RM payload and low enough to not cause DoS in
// clients in case of such attacks.
maxRMDecompressSize = MaxMsgSize
// Use NoCompression by default
RMDefaultCompressionLevel = zlib.NoCompression
)
type RMHeader struct {
Version uint64 `json:"version"`
Timestamp int64 `json:"timestamp"`
Command string `json:"command"`
Tag uint32 `json:"tag"`
Signature zkidentity.FixedSizeSignature `json:"signature,omitempty"`
}
// Private message to other client
const (
RMCPrivateMessage = "pm"
RMPrivateMessageModeNormal = 0
RMPrivateMessageModeMe = 1 // XXX not rigged up yet
)
type RMPrivateMessage struct {
Mode uint32 `json:"mode"`
Message string `json:"message"`
}
type RMBlock struct {
}
const RMCBlock = "block"
// RMMediateIdentity as target to send a RMInvite on the caller's behalf. This
// should kick of an autokx.
type RMMediateIdentity struct {
Identity [zkidentity.IdentitySize]byte `json:"identity"`
}
const RMCMediateIdentity = "mediateidentity"
// XXX does RMCMediateIdentity need a reply?
// RMInvite request an invite for third party.
type RMInvite struct {
Invitee zkidentity.PublicIdentity `json:"invitee"` // XXX why aren't we using Identity here?
}
const RMCInvite = "invite"
// RMKXSearchRefType identifies the type of a reference used in a kx search
// message.
type RMKXSearchRefType string
const (
KXSRTPostAuthor RMKXSearchRefType = "postauthor"
)
// RMKXSearchRef identifies a specific reference that is being used to search
// for a user.
type RMKXSearchRef struct {
Type RMKXSearchRefType `json:"type"`
Ref string `json:"ref"`
}
// RMKXSearch is sent when a user wishes to perform a transitive/recursive
// KX search for someone.
type RMKXSearch struct {
Refs []RMKXSearchRef `json:"refs"`
}
const RMCKXSearch = "kxsearch"
// RMKXSearchReply is sent with a list of candidates the user might attempt
// to use to connect to target.
type RMKXSearchReply struct {
TargetID zkidentity.ShortID `json:"target_id"`
IDs []zkidentity.ShortID `json:"ids"`
}
const RMCKXSearchReply = "kxsearchreply"
// RMTransitiveReset ask proxy to forward reset message to another client.
const RMCTransitiveReset = "transitivereset"
type RMTransitiveReset struct {
HalfKX ratchet.KeyExchange `json:"halfkx"` // Half ratchet
}
// RMTransitiveResetReply ask proxy to forward reset message reply to another
// client.
const RMCTransitiveResetReply = "transitiveresetreply"
type RMTransitiveResetReply struct {
FullKX ratchet.KeyExchange `json:"fullkx"` // Full ratchet
}
const RMCGetInvoice = "getinvoice"
type RMGetInvoice struct {
PayScheme string
MilliAtoms uint64
Tag uint32
}
const RMCInvoice = "invoice"
type RMInvoice struct {
Invoice string
Tag uint32
Error *string `json:"error,omitempty"`
}
const RMCKXSuggestion = "kxsuggestion"
type RMKXSuggestion struct {
Target zkidentity.PublicIdentity
}
// ComposeCompressedRM creates a blobified message that has a header and a
// payload that can then be encrypted and transmitted to the other side. The
// contents are zlib compressed with the specified level.
func ComposeCompressedRM(from *zkidentity.FullIdentity, rm interface{}, zlibLevel int) ([]byte, error) {
h := RMHeader{
Version: RMHeaderVersion,
Timestamp: time.Now().Unix(),
}
switch rm.(type) {
case RMPrivateMessage:
h.Command = RMCPrivateMessage
case OOBPublicIdentityInvite:
h.Command = OOBCPublicIdentityInvite // XXX this if overloaded
case RMBlock:
h.Command = RMCBlock
case RMInvite:
h.Command = RMCInvite
case RMMediateIdentity:
h.Command = RMCMediateIdentity
case RMTransitiveReset:
h.Command = RMCTransitiveReset
case RMTransitiveResetReply:
h.Command = RMCTransitiveResetReply
case RMGetInvoice:
h.Command = RMCGetInvoice
case RMInvoice:
h.Command = RMCInvoice
case RMTransitiveMessage:
h.Command = RMCTransitiveMessage
case RMTransitiveMessageReply:
h.Command = RMCTransitiveMessageReply
case RMTransitiveMessageForward:
h.Command = RMCTransitiveMessageForward
case RMKXSearch:
h.Command = RMCKXSearch
case RMKXSearchReply:
h.Command = RMCKXSearchReply
case RMKXSuggestion:
h.Command = RMCKXSuggestion
// Group chat
case RMGroupInvite:
h.Command = RMCGroupInvite
case RMGroupJoin:
h.Command = RMCGroupJoin
case RMGroupPart:
h.Command = RMCGroupPart
case RMGroupKill:
h.Command = RMCGroupKill
case RMGroupKick:
h.Command = RMCGroupKick
case RMGroupUpgradeVersion:
h.Command = RMCGroupUpgradeVersion
case RMGroupUpdateAdmins:
h.Command = RMGCGroupUpdateAdmins
case RMGroupList:
h.Command = RMCGroupList
case RMGroupMessage:
h.Command = RMCGroupMessage
// File transfer
case RMFTList:
h.Command = RMCFTList
case RMFTListReply:
h.Command = RMCFTListReply
case RMFTGet:
h.Command = RMCFTGet
case RMFTGetReply:
h.Command = RMCFTGetReply
case RMFTGetChunk:
h.Command = RMCFTGetChunk
case RMFTGetChunkReply:
h.Command = RMCFTGetChunkReply
case RMFTPayForChunk:
h.Command = RMCFTPayForChunk
case RMFTSendFile:
h.Command = RMCFTSendFile
// User
case RMUser:
h.Command = RMCUser
case RMUserReply:
h.Command = RMCUserReply
// Post
case RMListPosts:
h.Command = RMCListPosts
case RMListPostsReply:
h.Command = RMCListPostsReply
case RMGetPost:
h.Command = RMCGetPost
case RMPostShare:
h.Command = RMCPostShare
case RMPostsSubscribe:
h.Command = RMCPostsSubscribe
case RMPostsSubscribeReply:
h.Command = RMCPostsSubscribeReply
case RMPostsUnsubscribe:
h.Command = RMCPostsUnsubscribe
case RMPostsUnsubscribeReply:
h.Command = RMCPostsUnsubscribeReply
case RMPostGet:
h.Command = RMCPostGet
case RMPostGetReply:
h.Command = RMCPostGetReply
case RMPostStatus:
h.Command = RMCPostStatus
case RMPostStatusReply:
h.Command = RMCPostStatusReply
// Purely transitive commands
default:
return nil, fmt.Errorf("unknown routed message type: %T", rm)
}
// Encode payload
payload, err := json.Marshal(rm)
if err != nil {
return nil, err
}
// Sign payload
h.Signature = from.SignMessage(payload)
// Create payload
// Create header, note that the encoder appends a '\n'
mb := &bytes.Buffer{}
w, err := zlib.NewWriterLevel(mb, zlibLevel)
if err != nil {
return nil, err
}
e := json.NewEncoder(w)
err = e.Encode(h)
if err != nil {
return nil, err
}
n, err := w.Write(payload)
if err != nil {
return nil, err
}
if n != len(payload) {
return nil, fmt.Errorf("assert: n(%v) != len(%v)",
n, len(payload))
}
err = w.Close()
if err != nil {
return nil, err
}
log.Debugf("Composed compressed %s: payload size %d, "+
"final size %d", h.Command, len(payload), mb.Len())
return mb.Bytes(), nil
}
// ComposeRM creates a blobified message that has a header and a
// payload that can then be encrypted and transmitted to the other side.
func ComposeRM(from *zkidentity.FullIdentity, rm interface{}) ([]byte, error) {
return ComposeCompressedRM(from, rm, RMDefaultCompressionLevel)
}
func DecomposeRM(id *zkidentity.PublicIdentity, mb []byte) (*RMHeader, interface{}, error) {
// Decompress everything
cr, err := zlib.NewReader(bytes.NewReader(mb))
if err != nil {
return nil, nil, err
}
lr := &limitedReader{R: cr, N: maxRMDecompressSize}
all, err := io.ReadAll(lr)
closeErr := cr.Close()
if err != nil {
return nil, nil, fmt.Errorf("zlib read err: %w", err)
}
if closeErr != nil {
return nil, nil, fmt.Errorf("zlib close err: %w", closeErr)
}
var h RMHeader
d := json.NewDecoder(bytes.NewReader(all))
err = d.Decode(&h)
if err != nil {
return nil, nil, fmt.Errorf("header decode err: %w", err)
}
offset := int(d.InputOffset() + 1)
if len(all) < offset {
return nil, nil, fmt.Errorf("invalid message length: %v",
len(all))
}
if all[offset-1] != '\n' {
return nil, nil, fmt.Errorf("not \\n")
}
pmd := json.NewDecoder(bytes.NewReader(all[offset:]))
var payload interface{}
switch h.Command {
case RMCPrivateMessage:
var pm RMPrivateMessage
err = pmd.Decode(&pm)
payload = pm
case OOBCPublicIdentityInvite: // XXX this is overloaded
var pii OOBPublicIdentityInvite
err = pmd.Decode(&pii)
payload = pii
case RMCBlock:
var block RMBlock
err = pmd.Decode(&block)
payload = block
case RMCInvite:
var invite RMInvite
err = pmd.Decode(&invite)
payload = invite
case RMCMediateIdentity:
var mediateIdentity RMMediateIdentity
err = pmd.Decode(&mediateIdentity)
payload = mediateIdentity
case RMCTransitiveReset:
var transitiveReset RMTransitiveReset
err = pmd.Decode(&transitiveReset)
payload = transitiveReset
case RMCTransitiveResetReply:
var transitiveResetReply RMTransitiveResetReply
err = pmd.Decode(&transitiveResetReply)
payload = transitiveResetReply
case RMCGetInvoice:
var getinv RMGetInvoice
err = pmd.Decode(&getinv)
payload = getinv
case RMCInvoice:
var inv RMInvoice
err = pmd.Decode(&inv)
payload = inv
case RMCTransitiveMessage:
var transitiveMessage RMTransitiveMessage
err = pmd.Decode(&transitiveMessage)
payload = transitiveMessage
case RMCTransitiveMessageReply:
var transitiveMessageReply RMTransitiveMessageReply
err = pmd.Decode(&transitiveMessageReply)
payload = transitiveMessageReply
case RMCTransitiveMessageForward:
var transitiveMessageForward RMTransitiveMessageForward
err = pmd.Decode(&transitiveMessageForward)
payload = transitiveMessageForward
case RMCKXSearch:
var kxs RMKXSearch
err = pmd.Decode(&kxs)
payload = kxs
case RMCKXSearchReply:
var kxsr RMKXSearchReply
err = pmd.Decode(&kxsr)
payload = kxsr
case RMCKXSuggestion:
var kxsg RMKXSuggestion
err = pmd.Decode(&kxsg)
payload = kxsg
// Group vhat
case RMCGroupInvite:
var groupInvite RMGroupInvite
err = pmd.Decode(&groupInvite)
payload = groupInvite
case RMCGroupJoin:
var groupJoin RMGroupJoin
err = pmd.Decode(&groupJoin)
payload = groupJoin
case RMCGroupPart:
var groupPart RMGroupPart
err = pmd.Decode(&groupPart)
payload = groupPart
case RMCGroupKill:
var groupKill RMGroupKill
err = pmd.Decode(&groupKill)
payload = groupKill
case RMCGroupKick:
var groupKick RMGroupKick
err = pmd.Decode(&groupKick)
payload = groupKick
case RMCGroupUpgradeVersion:
var groupUpVers RMGroupUpgradeVersion
err = pmd.Decode(&groupUpVers)
payload = groupUpVers
case RMGCGroupUpdateAdmins:
var groupUpPerms RMGroupUpdateAdmins
err = pmd.Decode(&groupUpPerms)
payload = groupUpPerms
case RMCGroupList:
var groupList RMGroupList
err = pmd.Decode(&groupList)
payload = groupList
// File transfer
case RMCFTList:
var ftList RMFTList
err = pmd.Decode(&ftList)
payload = ftList
case RMCFTListReply:
var ftListReply RMFTListReply
err = pmd.Decode(&ftListReply)
payload = ftListReply
case RMCFTGet:
var ftGet RMFTGet
err = pmd.Decode(&ftGet)
payload = ftGet
case RMCFTGetReply:
var ftGetReply RMFTGetReply
err = pmd.Decode(&ftGetReply)
payload = ftGetReply
case RMCFTGetChunk:
var ftGetChunk RMFTGetChunk
err = pmd.Decode(&ftGetChunk)
payload = ftGetChunk
case RMCFTGetChunkReply:
var ftGetChunkReply RMFTGetChunkReply
err = pmd.Decode(&ftGetChunkReply)
payload = ftGetChunkReply
case RMCFTPayForChunk:
var ftPayForChunk RMFTPayForChunk
err = pmd.Decode(&ftPayForChunk)
payload = ftPayForChunk
case RMCFTSendFile:
var ftSendFile RMFTSendFile
err = pmd.Decode(&ftSendFile)
payload = ftSendFile
case RMCGroupMessage:
var groupMessage RMGroupMessage
err = pmd.Decode(&groupMessage)
payload = groupMessage
// User
case RMCUser:
var user RMUser
err = pmd.Decode(&user)
payload = user
case RMCUserReply:
var userReply RMUserReply
err = pmd.Decode(&userReply)
payload = userReply
// Post
case RMCListPosts:
var listPosts RMListPosts
err = pmd.Decode(&listPosts)
payload = listPosts
case RMCListPostsReply:
var listPostsReply RMListPostsReply
err = pmd.Decode(&listPostsReply)
payload = listPostsReply
case RMCGetPost:
var getPost RMGetPost
err = pmd.Decode(&getPost)
payload = getPost
case RMCPostShare:
var postShare RMPostShare
err = pmd.Decode(&postShare)
payload = postShare
case RMCPostsSubscribe:
var postsSubscribe RMPostsSubscribe
err = pmd.Decode(&postsSubscribe)
payload = postsSubscribe
case RMCPostsSubscribeReply:
var postsSubscribeReply RMPostsSubscribeReply
err = pmd.Decode(&postsSubscribeReply)
payload = postsSubscribeReply
case RMCPostsUnsubscribe:
var postsUnsubscribe RMPostsUnsubscribe
err = pmd.Decode(&postsUnsubscribe)
payload = postsUnsubscribe
case RMCPostsUnsubscribeReply:
var postsUnsubscribeReply RMPostsUnsubscribeReply
err = pmd.Decode(&postsUnsubscribeReply)
payload = postsUnsubscribeReply
case RMCPostGet:
var postGet RMPostGet
err = pmd.Decode(&postGet)
payload = postGet
case RMCPostGetReply:
var postGetReply RMPostGetReply
err = pmd.Decode(&postGetReply)
payload = postGetReply
case RMCPostStatus:
var postStatus RMPostStatus
err = pmd.Decode(&postStatus)
payload = postStatus
case RMCPostStatusReply:
var postStatusReply RMPostStatusReply
err = pmd.Decode(&postStatusReply)
payload = postStatusReply
// Purely transitive commands
default:
return nil, nil, fmt.Errorf("unknown routed message command: %v",
h.Command)
}
if err != nil {
return nil, nil, fmt.Errorf("decode command %v: %v", h.Command,
err)
}
// Verify signature if an identity was provided
if id != nil {
if !id.VerifyMessage(all[offset:], h.Signature) {
return nil, nil, fmt.Errorf("message authentication " +
"failed")
}
}
return &h, payload, nil
}
// RMTransitiveMessage is a request to forward a message
type RMTransitiveMessage struct {
// For is the invitee identity and the corresponding public key that
// was used to encrypt the InviteBlob.
For zkidentity.ShortID `json:"for"`
// CipherText contains a sntrup4591761 encapsulated shared key that is
// used to encrypt the message. This ciphertext is decrypted by the
// intended final recipient.
CipherText zkidentity.FixedSizeSntrupCiphertext `json:"ciphertext,omitempty"`
// Message is an encrypted json encoded structure.
Message []byte `json:"message,omitempty"`
}
const RMCTransitiveMessage = "transitivemessage"
// RMTransitiveMessageReply is a reply to a transitive message.
type RMTransitiveMessageReply struct {
// For is the intended recipient that needs Message routed.
For zkidentity.ShortID `json:"for"`
// Error is set if the other side encountered an error.
Error *string `json:"error,omitempty"`
}
const RMCTransitiveMessageReply = "transitivemessagereply"
// RMTransitiveMessageForward forwards a transitive message to a user.
type RMTransitiveMessageForward struct {
// From is the sender identity. This is used as a hint to verify the
// signature and identity inside Message.
From zkidentity.ShortID `json:"from"`
// CipherText contains a sntrup4591761 encapsulated shared key that is
// used to encrypt the InviteBlob.
CipherText zkidentity.FixedSizeSntrupCiphertext `json:"ciphertext"`
// Message is an encrypted json encoded structure.
Message []byte `json:"message"`
}
const RMCTransitiveMessageForward = "tmessageforward"
// RMGroupInvite invites a user to a group chat.
type RMGroupInvite struct {
ID zkidentity.ShortID `json:"id"` // group id
Name string `json:"name"` // requested group name
Token uint64 `json:"token"` // invite token
Description string `json:"description"` // group description
Expires int64 `json:"expires"` // unix time when this invite expires
Version uint8 `json:"version"` // version the GC is running on
}
const RMCGroupInvite = "groupinvite"
// RMGroupJoin instructs inviter that a user did or did not join the group.
type RMGroupJoin struct {
// XXX who sent this?
ID zkidentity.ShortID `json:"id"` // group id
Token uint64 `json:"token"` // invite token, implicitly identifies sender
Error string `json:"error"` // accept or deny Invite
}
const RMCGroupJoin = "groupjoin"
// RMGroupPart is sent to tell the group chat that a user has departed.
type RMGroupPart struct {
// XXX who sent this?
ID zkidentity.ShortID `json:"id"` // group id
Reason string `json:"reason"` // reason to depart group
}
const RMCGroupPart = "grouppart"
// RMGroupKill, sender is implicit to CRPC
type RMGroupKill struct {
// XXX who sent this?
ID zkidentity.ShortID `json:"id"` // group id
Reason string `json:"reason"` // reason to disassemble group
}
const RMCGroupKill = "groupkill"
// RMGroupKick kicks a naughty member out of the group chat.
type RMGroupKick struct {
Member [zkidentity.IdentitySize]byte `json:"member"` // kickee
Reason string `json:"reason"` // why member was kicked
Parted bool `json:"parted"` // kicked/parted
NewGroupList RMGroupList `json:"newgrouplist"` // new GroupList
}
const RMCGroupKick = "groupkick"
type RMGroupUpgradeVersion struct {
NewGroupList RMGroupList `json:"newgrouplist"`
}
const RMCGroupUpgradeVersion = "groupupversion"
// RMGroupUpdateAdmins updates the list of extra admins in the GC.
type RMGroupUpdateAdmins struct {
Reason string `json:"reason"`
NewGroupList RMGroupList `json:"newgrouplist"`
}
const RMGCGroupUpdateAdmins = "groupupdateadmins"
// RMGroupList, currently we detect spoofing by ensuring the origin of the
// message. This may not be sufficient and we may have to add a signature of
// sorts. For now roll with this assumption.
type RMGroupList struct {
ID zkidentity.ShortID `json:"id"` // group id
Name string `json:"name"`
Generation uint64 `json:"generation"` // incremented every time list changes
Timestamp int64 `json:"timestamp"` // unix time last generation changed
Version uint8 `json:"version"` // version of the rules for GC op
// all participants, [0] is administrator
// receiver must check [0] == originator
Members []zkidentity.ShortID `json:"members"`
// Version 1 fields.
// ExtraAdmins are additional admins. Members[0] is still considered
// an admin in version 1 GCs.
ExtraAdmins []zkidentity.ShortID `json:"extra_admins"`
}
const RMCGroupList = "grouplist"
// RMGroupMessage is a message to a group.
type RMGroupMessage struct {
ID zkidentity.ShortID `json:"id"` // group name
Generation uint64 `json:"generation"` // Generation used
Message string `json:"message"` // Actual message
Mode MessageMode `json:"mode"` // 0 regular mode, 1 /me
}
const RMCGroupMessage = "groupmessage"
// RMFTList asks other side for a list of files. Directories are constants that
// describe which directories it should access. Currently only "global" and
// "shared" are allowed.
type RMFTList struct {
Directories []string `json:"directories"` // Which directories to obtain
Filter string `json:"filter,omitempty"` // Filter list by this regex
Tag uint32 `json:"tag"` // Tag to copy in replies
}
const (
RMCFTList = "ftls"
RMFTDGlobal = "global" // Globally accessible files
RMFTDShared = "shared" // Files shared between two users
)
type FileManifest struct {
Index uint64 `json:"index"`
Size uint64 `json:"size"`
Hash []byte `json:"hash"`
}
type FileMetadata struct {
Version uint64 `json:"version"`
Cost uint64 `json:"cost"`
Size uint64 `json:"size"`
Directory string `json:"directory"`
Filename string `json:"filename"`
Description string `json:"description"`
Hash string `json:"hash"`
Manifest []FileManifest `json:"manifest"` // len == number of chunks
Signature string `json:"signature"`
Attributes map[string]string `json:"attributes,omitempty"`
}
const FileMetadataVersion = 1
// MetadataHash calculates the hash of the metadata info. Note that the specific
// information that is hashed depends on the version of the metadata.
func (fm *FileMetadata) MetadataHash() [32]byte {
h := sha256.New()
var b [32]byte
writeUint64 := func(i uint64) {
binary.LittleEndian.PutUint64(b[:], i)
h.Write(b[:])
}
writeStr := func(s string) {
h.Write([]byte(s))
}
writeUint64(fm.Version)
writeUint64(fm.Size)
writeStr(fm.Filename)
writeStr(fm.Hash)
writeStr(fm.Signature)
// In the future, add new fields conditional on the metadata version so
// that old versions will still calculate the same hash.
copy(b[:], h.Sum(nil))
return b
}
type RMFTListReply struct {
Global []FileMetadata `json:"global,omitempty"`
Shared []FileMetadata `json:"shared,omitempty"`
Tag uint32
Error *string `json:"error,omitempty"`
}
const RMCFTListReply = "ftlsreply"
// RMFTGet attempts to retrieve a file from another user
type RMFTGet struct {
Directory string `json:"directory"` // Which directory **DEPRECATED
Filename string `json:"filename"` // Which file **DEPRECATED
Tag uint32 `json:"tag"` // Tag to copy in replies
FileID string `json:"file_id"` // Equals metadata hash
}
const RMCFTGet = "ftget"
// RMFTGetReply file metadata get reply
type RMFTGetReply struct {
Metadata FileMetadata `json:"metadata"`
Tag uint32 `json:"tag"`
Error *string `json:"error,omitempty"`
}
const RMCFTGetReply = "ftgetreply"
// RMFTGetChunk attempts to retrieve a file chunk from another user.
type RMFTGetChunk struct {
FileID string `json:"file_id"`
Hash []byte `json:"hash"` // Chunk to retrieve
Index int `json:"index"`
Tag uint32 `json:"tag"` // Tag to copy in replies
}
const RMCFTGetChunk = "ftgetchunk"
// RMFTGetChunkReply chunked file get reply
type RMFTGetChunkReply struct {
FileID string `json:"file_id"`
Index int `json:"index"`
Chunk []byte `json:"chunk"` // Actual data, needs to be hashed to verify
Tag uint32 `json:"tag"`
Error *string `json:"error,omitempty"`
}
const RMCFTGetChunkReply = "ftgetchunkreply"
type RMFTPayForChunk struct {
Tag uint32 `json:"tag"`
FileID string `json:"file_id"`
Invoice string `json:"invoice"`
Index int `json:"index"`
Hash []byte `json:"hash"`
Error *string `json:"error,omitempty"`
}
const RMCFTPayForChunk = "ftpayforchunk"
type RMFTSendFile struct {
Metadata FileMetadata `json:"metadata"`
}
const RMCFTSendFile = "ftsendfile"
// RMUser retrieves user attributes such as status, profile etc. Attributes is a
// key value store that is used to describe the user attributes.
type RMUser struct{}
type RMUserReply struct {
Identity [sha256.Size]byte `json:"identity"`
Attributes map[string]string `json:"attributes"`
}
const RMCUser = "user"
const RMCUserReply = "userreply"
const (
RMUDescription = "description" // User description
RMUAway = "away" // User away message
RMUProfilePicture = "profilepicture" // User profile picture
)
type RMListPosts struct{}
const RMCListPosts = "listposts"
type PostListItem struct {
ID zkidentity.ShortID `json:"id"`
Title string `json:"title"`
}
type RMListPostsReply struct {
Posts []PostListItem `json:"posts"`
}
const RMCListPostsReply = "listpostsreply"
type RMGetPost struct {
ID zkidentity.ShortID `json:"id"`
IncludeStatus bool `json:"include_status"`
}
const RMCGetPost = "getpost"
// RMPostStatusReply sets attributes such as status on a post. Attributes is a
// key value store that is used to describe the update attributes.
type RMPostStatus struct {
Link string `json:"link"` // Link to post
Attributes map[string]string `json:"attributes"`
}
type RMPostStatusReply struct {
Error *string `json:"error,omitempty"`
}
const RMCPostStatus = "poststatus"
const RMCPostStatusReply = "poststatusreply"
const (
RMPSHeart = "heart" // Heart a post
RMPSComment = "comment" // Comment on a post
RMPSHeartYes = "1" // +1 heart
RMPSHeartNo = "0" // -1 heart
)
// RMPostSubscribe subscribes to new posts from a user.
type RMPostsSubscribe struct {
// GetPost is an optional post to send to the client, assuming
// subscribing to the posts worked.
GetPost *zkidentity.ShortID `json:"get_post,omitempty"`
// IncludeStatus also sends the post status updates if GetPost != nil.
IncludeStatus bool `json:"include_status,omitempty"`
}
const RMCPostsSubscribe = "postssubscribe"
type RMPostsSubscribeReply struct {
Error *string `json:"error,omitempty"`
}
const RMCPostsSubscribeReply = "postssubscribereply"
// RMPostUnsubscribe unsubscribes to new posts from a user.
type RMPostsUnsubscribe struct{}
const RMCPostsUnsubscribe = "postsunsubscribe"
type RMPostsUnsubscribeReply struct {
Error *string `json:"error,omitempty"`
}