/
peers.go
338 lines (288 loc) · 8.83 KB
/
peers.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
package lib
import (
"context"
"fmt"
"strings"
"github.com/qri-io/qri/config"
"github.com/qri-io/qri/p2p"
"github.com/qri-io/qri/profile"
"github.com/qri-io/qri/repo"
peer "github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
)
// PeerMethods extends a lib.Instance with business logic for peer-to-peer
// interaction
type PeerMethods struct {
d dispatcher
}
// Name returns the name of this method group
func (m PeerMethods) Name() string { return "peer" }
// Attributes defines attributes for each method
func (m PeerMethods) Attributes() map[string]AttributeSet {
return map[string]AttributeSet{
"list": {Endpoint: AEPeers, HTTPVerb: "POST"},
"info": {Endpoint: AEPeer, HTTPVerb: "POST"},
"connect": {Endpoint: AEConnect, HTTPVerb: "POST"},
"disconnect": {Endpoint: AEDisconnect, HTTPVerb: "POST"},
"connections": {Endpoint: AEConnections, HTTPVerb: "POST"},
"connectedqriprofiles": {Endpoint: AEConnectedQriProfiles, HTTPVerb: "POST"},
}
}
// PeerListParams defines parameters for the List method
type PeerListParams struct {
Limit, Offset int
// Cached == true will return offline peers from the repo
// as well as online peers, default is to list connected peers only
Cached bool
}
// List lists Peers on the qri network
func (m PeerMethods) List(ctx context.Context, p *PeerListParams) ([]*config.ProfilePod, error) {
got, _, err := m.d.Dispatch(ctx, dispatchMethodName(m, "list"), p)
if res, ok := got.([]*config.ProfilePod); ok {
return res, err
}
return nil, dispatchReturnError(got, err)
}
// PeerInfoParams defines parameters for the Info method
type PeerInfoParams struct {
Peername string
ProfileID string
// Verbose adds network details from the p2p Peerstore
Verbose bool
}
// Info shows peer profile details
func (m PeerMethods) Info(ctx context.Context, p *PeerInfoParams) (*config.ProfilePod, error) {
got, _, err := m.d.Dispatch(ctx, dispatchMethodName(m, "info"), p)
if res, ok := got.(*config.ProfilePod); ok {
return res, err
}
return nil, dispatchReturnError(got, err)
}
// Connect attempts to create a connection with a peer for a given peer.ID
func (m PeerMethods) Connect(ctx context.Context, p *ConnectParamsPod) (*config.ProfilePod, error) {
got, _, err := m.d.Dispatch(ctx, dispatchMethodName(m, "connect"), p)
if res, ok := got.(*config.ProfilePod); ok {
return res, err
}
return nil, dispatchReturnError(got, err)
}
// Disconnect explicitly closes a peer connection
func (m PeerMethods) Disconnect(ctx context.Context, p *ConnectParamsPod) error {
_, _, err := m.d.Dispatch(ctx, dispatchMethodName(m, "disconnect"), p)
return err
}
// ConnectionsParams defines parameters for the Connections method
type ConnectionsParams struct {
Limit int
Offset int
}
// Connections lists PeerID's we're currently connected to. If running
// IPFS this will also return connected IPFS nodes
func (m PeerMethods) Connections(ctx context.Context, p *ConnectionsParams) ([]string, error) {
got, _, err := m.d.Dispatch(ctx, dispatchMethodName(m, "connections"), p)
if res, ok := got.([]string); ok {
return res, err
}
return nil, dispatchReturnError(got, err)
}
// ConnectedQriProfiles lists profiles we're currently connected to
func (m PeerMethods) ConnectedQriProfiles(ctx context.Context, p *ConnectionsParams) ([]*config.ProfilePod, error) {
got, _, err := m.d.Dispatch(ctx, dispatchMethodName(m, "connectedqriprofiles"), p)
if res, ok := got.([]*config.ProfilePod); ok {
return res, err
}
return nil, dispatchReturnError(got, err)
}
// ConnectParamsPod defines parameters for defining a connection
// to a peer as plain-old-data
type ConnectParamsPod struct {
Peername string
ProfileID string
NetworkID string
Multiaddr string
}
// NewConnectParamsPod attempts to turn a string into peer connection parameters
func NewConnectParamsPod(s string) *ConnectParamsPod {
pcpod := &ConnectParamsPod{}
if strings.HasPrefix(s, "/ipfs/") {
pcpod.NetworkID = s
} else if maddr, err := ma.NewMultiaddr(s); err == nil {
pcpod.Multiaddr = maddr.String()
} else if len(s) == 46 && strings.HasPrefix(s, "Qm") {
pcpod.ProfileID = s
} else {
pcpod.Peername = s
}
return pcpod
}
func (p ConnectParamsPod) String() string {
if p.Peername != "" {
return p.Peername
}
if p.ProfileID != "" {
return p.ProfileID
}
if p.Multiaddr != "" {
return p.Multiaddr
}
if p.NetworkID != "" {
return p.NetworkID
}
return ""
}
// Decode turns plain-old-data into it's rich types
func (p ConnectParamsPod) Decode() (cp p2p.PeerConnectionParams, err error) {
cp.Peername = p.Peername
if p.NetworkID != "" {
id := strings.TrimPrefix(p.NetworkID, "/ipfs/")
if len(id) == len(p.NetworkID) {
err = fmt.Errorf("network IDs must have a network prefix (eg. /ipfs/)")
return
}
if cp.PeerID, err = peer.IDB58Decode(id); err != nil {
err = fmt.Errorf("invalid networkID: %s", err.Error())
return
}
}
if p.ProfileID != "" {
if cp.ProfileID, err = profile.IDB58Decode(p.ProfileID); err != nil {
err = fmt.Errorf("invalid profileID: %s", err.Error())
return
}
}
if p.Multiaddr != "" {
if cp.Multiaddr, err = ma.NewMultiaddr(p.Multiaddr); err != nil {
err = fmt.Errorf("invalid multiaddr: %s", err.Error())
}
}
return
}
// peerImpl holds the method implementations for PeerMethods
type peerImpl struct{}
// List lists Peers on the qri network
func (peerImpl) List(scope scope, p *PeerListParams) ([]*config.ProfilePod, error) {
res := []*config.ProfilePod{}
if scope.Node() == nil || !scope.Node().Online {
return nil, fmt.Errorf("error: not connected, run `qri connect` in another window")
}
var err error
if p.Limit <= 0 {
p.Limit = DefaultPageSize
}
// requesting user is hardcoded as node owner
u := scope.ActiveProfile()
res, err = p2p.ListPeers(scope.Node(), u.ID, p.Offset, p.Limit, !p.Cached)
if err != nil {
return nil, err
}
return res, nil
}
// Info shows peer profile details
func (peerImpl) Info(scope scope, p *PeerInfoParams) (*config.ProfilePod, error) {
res := config.ProfilePod{}
// TODO: Move most / all of this to p2p package, perhaps.
r := scope.Repo()
var pid profile.ID
var err error
if p.ProfileID != "" {
pid, err = profile.IDB58Decode(p.ProfileID)
if err != nil {
return nil, err
}
}
profiles, err := r.Profiles().List()
if err != nil {
log.Debug(err.Error())
return nil, err
}
for _, pro := range profiles {
if pro.ID == pid || pro.Peername == p.Peername {
if p.Verbose && len(pro.PeerIDs) > 0 {
// TODO - grab more than just the first peerID
pinfo := scope.Node().PeerInfo(pro.PeerIDs[0])
pro.NetworkAddrs = pinfo.Addrs
}
prof, err := pro.Encode()
if err != nil {
return nil, err
}
res = *prof
connected := scope.Node().ConnectedQriProfiles()
// If the requested profileID is in the list of connected peers, set Online flag.
if _, ok := connected[pro.ID]; ok {
res.Online = true
}
// If the requested profileID is myself and I'm Online, set Online flag.
if peer.ID(pro.ID) == scope.Node().ID && scope.Node().Online {
res.Online = true
}
return &res, nil
}
}
return nil, repo.ErrNotFound
}
// Connect attempts to create a connection with a peer for a given peer.ID
func (peerImpl) Connect(scope scope, p *ConnectParamsPod) (*config.ProfilePod, error) {
pcp, err := p.Decode()
if err != nil {
return nil, err
}
prof, err := scope.Node().ConnectToPeer(scope.Context(), pcp)
if err != nil {
return nil, err
}
pro, err := prof.Encode()
if err != nil {
return nil, err
}
return pro, nil
}
// Disconnect explicitly closes a peer connection
func (peerImpl) Disconnect(scope scope, p *ConnectParamsPod) error {
pcp, err := p.Decode()
if err != nil {
return err
}
return scope.Node().DisconnectFromPeer(scope.Context(), pcp)
}
// Connections lists PeerID's we're currently connected to. If running
// IPFS this will also return connected IPFS nodes
func (peerImpl) Connections(scope scope, p *ConnectionsParams) ([]string, error) {
// TODO (ramfox): limit and offset not currently used
// ensure valid limit value
if p.Limit <= 0 {
p.Limit = 25
}
// ensure valid offset value
if p.Offset < 0 {
p.Offset = 0
}
return scope.Node().ConnectedPeers(), nil
}
// ConnectedQriProfiles lists profiles we're currently connected to
func (peerImpl) ConnectedQriProfiles(scope scope, p *ConnectionsParams) ([]*config.ProfilePod, error) {
// TODO (ramfox): offset not currently used
// ensure valid limit value
if p.Limit <= 0 {
p.Limit = 25
}
// ensure valid offset value
if p.Offset < 0 {
p.Offset = 0
}
connected := scope.Node().ConnectedQriProfiles()
build := make([]*config.ProfilePod, intMin(len(connected), p.Limit))
for _, pro := range connected {
build = append(build, pro)
if len(build) >= p.Limit {
break
}
}
return build, nil
}
func intMin(a, b int) int {
if a < b {
return a
}
return b
}