-
Notifications
You must be signed in to change notification settings - Fork 11
/
peer.go
292 lines (262 loc) · 6.39 KB
/
peer.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
package nymo
import (
"context"
"errors"
"fmt"
"io"
"math/rand"
"sync"
"sync/atomic"
"time"
"unsafe"
"github.com/nymo-net/nymo/pb"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
)
type peer struct {
ctx context.Context
handle PeerHandle
reader io.ReadCloser
writer io.Writer
cohort uint32
peers []*pb.Digest
key []byte
queue chan proto.Message
msgReq sync.Map
msgQ chan struct{}
peerReq sync.Map
msgProc uint32
}
func (p *peer) sendProto(msg proto.Message) {
select {
case <-p.ctx.Done():
case p.queue <- msg:
}
}
func (p *peer) requestMsg(diff []*pb.Digest, u *User) {
count := 0
for _, digest := range diff {
// deal with out-of-cohort message
if !sameCohort(u.cohort, digest.Cohort) {
// should not receive if msg cohort is same as peer
if sameCohort(p.cohort, digest.Cohort) {
continue
}
// 1-ε chance of ignoring an out-of-cohort message
if rand.Float64() >= epsilon {
u.db.IgnoreMessage(digest)
continue
}
}
p.msgReq.Store(*(*[hashSize]byte)(unsafe.Pointer(&digest.Hash[0])), digest.Cohort)
p.sendProto(&pb.RequestMsg{Hash: digest.Hash})
count++
}
for count > 0 {
if _, ok := <-p.msgQ; !ok {
break
}
count--
}
atomic.StoreUint32(&p.msgProc, 0)
p.sendProto(new(pb.MsgListAck))
}
func (p *peer) requestPeer(pred func(uint32) bool) bool {
for i, digest := range p.peers {
if !pred(digest.Cohort) {
continue
}
p.peerReq.Store(truncateHash(digest.Hash), digest)
p.peers = append(p.peers[:i], p.peers[i+1:]...)
p.sendProto(&pb.RequestPeer{
UrlHash: digest.Hash,
Pow: calcPoW(append(p.key, digest.Hash...)),
})
return true
}
return false
}
func (u *User) peerDownlink(p *peer) error {
defer close(p.msgQ)
defer p.reader.Close()
var listTimer unsafe.Pointer
defer func() {
t := (*time.Timer)(atomic.SwapPointer(&listTimer, nil))
if t != nil {
t.Stop()
}
}()
listMsg := func() {
if atomic.SwapPointer(&listTimer, nil) != nil {
p.sendProto(&pb.MsgList{Messages: p.handle.ListMessages(msgListMax)})
}
}
for p.ctx.Err() == nil {
var any anypb.Any
err := recvMessage(p.reader, &any)
if err != nil {
return err
}
n, err := any.UnmarshalNew()
if err != nil {
return err
}
switch msg := n.(type) {
case *pb.RequestMsg:
if len(msg.Hash) != hashSize {
return fmt.Errorf("unexpected hash length")
}
cont := new(pb.MsgContainer)
cont.Msg, cont.Pow = u.db.GetMessage(copyHash(msg.Hash))
p.sendProto(cont)
case *pb.RequestPeer:
if len(msg.UrlHash) != hashTruncate {
return fmt.Errorf("unexpected hash length")
}
if !validatePoW(append(p.key, msg.UrlHash...), msg.Pow) {
return fmt.Errorf("invalid pow")
}
p.sendProto(&pb.ResponsePeer{Address: u.db.GetUrlByHash(truncateHash(msg.UrlHash))})
case *pb.ResponsePeer:
hash := hasher([]byte(msg.Address))
digest, loaded := p.peerReq.LoadAndDelete(truncateHash(hash[:]))
if !loaded {
return fmt.Errorf("unexpected peer response")
}
u.db.AddPeer(msg.Address, digest.(*pb.Digest))
case *pb.PeerList:
if p.peers != nil {
return fmt.Errorf("unexpected peer list")
}
for _, l := range msg.Peers {
if len(l.Hash) != hashTruncate || l.Cohort > cohortNumber {
return fmt.Errorf("unexpected hash length")
}
}
p.peers = p.handle.AddKnownPeers(msg.Peers)
case *pb.MsgList:
if atomic.LoadUint32(&p.msgProc) != 0 {
return fmt.Errorf("unexpected msg list")
}
for _, l := range msg.Messages {
if len(l.Hash) != hashSize || l.Cohort >= cohortNumber {
return fmt.Errorf("unexpected hash length")
}
}
p.msgProc = 1
go p.requestMsg(p.handle.AddKnownMessages(msg.Messages), u)
case *pb.MsgListAck:
if atomic.LoadPointer(&listTimer) != nil {
return errors.New("unexpected peer msg ack")
}
p.handle.AckMessages()
atomic.StorePointer(&listTimer, unsafe.Pointer(time.AfterFunc(u.cfg.ListMessageTime, listMsg)))
case *pb.MsgContainer:
if msg.Msg == nil {
return fmt.Errorf("no msg")
}
// 1. retrieve request
msgHash := hasher(msg.Msg)
expCohort, loaded := p.msgReq.LoadAndDelete(msgHash)
if !loaded {
return fmt.Errorf("unexpected msg response")
}
p.msgQ <- struct{}{}
err := u.db.StoreMessage(msgHash, msg, func() (uint32, error) {
// 2. validate pow
if !validatePoW(msgHash[:], msg.Pow) {
return 0, errors.New("invalid pow")
}
// 3. try decode
var m pb.Message
err := proto.Unmarshal(msg.Msg, &m)
if err != nil {
return 0, err
}
if m.TargetCohort != expCohort.(uint32) {
return 0, errors.New("unexpected pow")
}
// 4. try decrypt and store
if m.TargetCohort == u.cohort {
if rMsg := u.decryptMessage(&m); rMsg != nil {
u.db.StoreDecryptedMessage(rMsg)
}
}
return m.TargetCohort, nil
})
if err != nil {
return err
}
default:
return fmt.Errorf("unknown message type %T", msg)
}
}
return p.ctx.Err()
}
func (u *User) peerUplink(p *peer) {
defer p.reader.Close()
for {
var msg proto.Message
select {
case msg = <-p.queue:
case <-p.ctx.Done():
return
}
any, err := anypb.New(msg)
if err != nil {
panic(err)
}
err = sendMessage(p.writer, any)
if err != nil {
p.handle.Disconnect(err)
return
}
}
}
func (u *User) runPeer(p *peer) *peer {
p.queue = make(chan proto.Message, 10)
p.queue <- &pb.PeerList{Peers: p.handle.ListPeers(peerListMax)}
p.queue <- &pb.MsgList{Messages: p.handle.ListMessages(msgListMax)}
go func() {
p.handle.Disconnect(u.peerDownlink(p))
}()
go u.peerUplink(p)
return p
}
func (u *User) newPeerAsServer(
ctx context.Context, handle PeerHandle,
r io.ReadCloser, w io.Writer,
material []byte, cohort uint32) (*peer, error) {
if err := sendMessage(w, &pb.HandshakeOK{
Cohort: u.cohort,
}); err != nil {
return nil, err
}
return u.runPeer(&peer{
ctx: ctx,
handle: handle,
reader: r,
writer: w,
cohort: cohort,
key: material,
msgQ: make(chan struct{}, 100),
}), nil
}
func (u *User) newPeerAsClient(
ctx context.Context, handle PeerEnumerate,
r io.ReadCloser, w io.Writer,
id [hashTruncate]byte, sKey []byte) (*peer, error) {
var ok pb.HandshakeOK
if err := recvMessage(r, &ok); err != nil {
return nil, err
}
return u.runPeer(&peer{
ctx: ctx,
handle: handle.Connect(id, ok.Cohort),
reader: r,
writer: w,
cohort: ok.Cohort,
key: sKey,
msgQ: make(chan struct{}, 100),
}), nil
}