forked from OpenBazaar/openbazaar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
retriever.go
139 lines (126 loc) · 3.61 KB
/
retriever.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
package net
import (
"github.com/OpenBazaar/openbazaar-go/ipfs"
"github.com/OpenBazaar/openbazaar-go/net/service"
"github.com/OpenBazaar/openbazaar-go/pb"
"github.com/OpenBazaar/openbazaar-go/repo"
"github.com/golang/protobuf/proto"
"github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/core"
routing "github.com/ipfs/go-ipfs/routing/dht"
"golang.org/x/net/context"
peer "gx/ipfs/QmRBqJF7hb8ZSpRcMwUt8hNhydWcxGEhtk81HKq6oUwKvs/go-libp2p-peer"
multihash "gx/ipfs/QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku/go-multihash"
ma "gx/ipfs/QmYzDkkgAEmrcNzFCiYo6L1dTX4EAG1gZkbtdbd9trL4vd/go-multiaddr"
"io/ioutil"
"net/http"
"time"
)
type MessageRetriever struct {
db repo.Datastore
node *core.IpfsNode
ctx commands.Context
service *service.OpenBazaarService
prefixLen int
sendAck func(peerId string, pointerID peer.ID) error
}
func NewMessageRetriever(db repo.Datastore, ctx commands.Context, node *core.IpfsNode, service *service.OpenBazaarService, prefixLen int, sendAck func(peerId string, pointerID peer.ID) error) *MessageRetriever {
return &MessageRetriever{
db: db,
node: node,
ctx: ctx,
service: service,
prefixLen: prefixLen,
sendAck: sendAck,
}
}
func (m *MessageRetriever) Run() {
tick := time.NewTicker(time.Hour)
defer tick.Stop()
go m.fetchPointers()
for {
select {
case <-tick.C:
go m.fetchPointers()
}
}
}
func (m *MessageRetriever) fetchPointers() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
mh, _ := multihash.FromB58String(m.node.Identity.Pretty())
peerOut := ipfs.FindPointersAsync(m.node.Routing.(*routing.IpfsDHT), ctx, mh, m.prefixLen)
for p := range peerOut {
if len(p.Addrs) > 0 && !m.db.OfflineMessages().Has(p.Addrs[0].String()) {
// ipfs
if len(p.Addrs[0].Protocols()) == 1 && p.Addrs[0].Protocols()[0].Code == 421 {
go m.fetchIPFS(m.ctx, p.ID, p.Addrs[0])
}
// https
if len(p.Addrs[0].Protocols()) == 2 && p.Addrs[0].Protocols()[0].Code == 421 && p.Addrs[0].Protocols()[1].Code == 443 {
enc, err := p.Addrs[0].ValueForProtocol(421)
if err != nil {
continue
}
mh, err := multihash.FromB58String(enc)
if err != nil {
continue
}
d, err := multihash.Decode(mh)
if err != nil {
continue
}
go m.fetchHTTPS(p.ID, string(d.Digest), p.Addrs[0])
}
}
}
}
func (m *MessageRetriever) fetchIPFS(ctx commands.Context, pid peer.ID, addr ma.Multiaddr) {
ciphertext, err := ipfs.Cat(ctx, addr.String())
if err != nil {
return
}
m.attemptDecrypt(ciphertext, pid)
m.db.OfflineMessages().Put(addr.String())
}
func (m *MessageRetriever) fetchHTTPS(pid peer.ID, url string, addr ma.Multiaddr) {
resp, err := http.Get(url)
if err != nil {
return
}
ciphertext, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
m.attemptDecrypt(ciphertext, pid)
m.db.OfflineMessages().Put(addr.String())
}
func (m *MessageRetriever) attemptDecrypt(ciphertext []byte, pid peer.ID) {
plaintext, err := Decrypt(m.node.PrivateKey, ciphertext)
if err == nil {
env := pb.Envelope{}
err := proto.Unmarshal(plaintext, &env)
if err != nil {
return
}
id, err := peer.IDB58Decode(env.PeerID)
if err != nil {
return
}
// get handler for this msg type.
handler := m.service.HandlerForMsgType(env.Message.MessageType)
if handler == nil {
log.Debug("Got back nil handler from handlerForMsgType")
return
}
// dispatch handler.
_, err = handler(id, env.Message)
if err != nil {
log.Debugf("handle message error: %s", err)
return
}
if env.Message.MessageType != pb.Message_OFFLINE_ACK {
m.sendAck(id.Pretty(), pid)
}
}
}