-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
id.go
228 lines (200 loc) · 6.06 KB
/
id.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
package commands
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"sort"
"strings"
version "github.com/ipfs/go-ipfs"
core "github.com/ipfs/go-ipfs/core"
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
cmds "github.com/ipfs/go-ipfs-cmds"
ke "github.com/ipfs/go-ipfs/core/commands/keyencode"
ic "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/host"
peer "github.com/libp2p/go-libp2p-core/peer"
pstore "github.com/libp2p/go-libp2p-core/peerstore"
kb "github.com/libp2p/go-libp2p-kbucket"
identify "github.com/libp2p/go-libp2p/p2p/protocol/identify"
)
const offlineIdErrorMessage = `'ipfs id' currently cannot query information on remote
peers without a running daemon; we are working to fix this.
In the meantime, if you want to query remote peers using 'ipfs id',
please run the daemon:
ipfs daemon &
ipfs id QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
`
type IdOutput struct {
ID string
PublicKey string
Addresses []string
AgentVersion string
ProtocolVersion string
Protocols []string
}
const (
formatOptionName = "format"
idFormatOptionName = "peerid-base"
)
var IDCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Show ipfs node id info.",
ShortDescription: `
Prints out information about the specified peer.
If no peer is specified, prints out information for local peers.
'ipfs id' supports the format option for output with the following keys:
<id> : The peers id.
<aver>: Agent version.
<pver>: Protocol version.
<pubkey>: Public key.
<addrs>: Addresses (newline delimited).
EXAMPLE:
ipfs id Qmece2RkXhsKe5CRooNisBTh4SK119KrXXGmoK6V3kb8aH -f="<addrs>\n"
`,
},
Arguments: []cmds.Argument{
cmds.StringArg("peerid", false, false, "Peer.ID of node to look up."),
},
Options: []cmds.Option{
cmds.StringOption(formatOptionName, "f", "Optional output format."),
cmds.StringOption(idFormatOptionName, "Encoding used for peer IDs: Can either be a multibase encoded CID or a base58btc encoded multihash. Takes {b58mh|base36|k|base32|b...}.").WithDefault("b58mh"),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
keyEnc, err := ke.KeyEncoderFromString(req.Options[idFormatOptionName].(string))
if err != nil {
return err
}
n, err := cmdenv.GetNode(env)
if err != nil {
return err
}
var id peer.ID
if len(req.Arguments) > 0 {
var err error
id, err = peer.Decode(req.Arguments[0])
if err != nil {
return fmt.Errorf("invalid peer id")
}
} else {
id = n.Identity
}
if id == n.Identity {
output, err := printSelf(keyEnc, n)
if err != nil {
return err
}
return cmds.EmitOnce(res, output)
}
// TODO handle offline mode with polymorphism instead of conditionals
if !n.IsOnline {
return errors.New(offlineIdErrorMessage)
}
// We need to actually connect to run identify.
err = n.PeerHost.Connect(req.Context, peer.AddrInfo{ID: id})
switch err {
case nil:
case kb.ErrLookupFailure:
return errors.New(offlineIdErrorMessage)
default:
return err
}
output, err := printPeer(keyEnc, n.Peerstore, id)
if err != nil {
return err
}
return cmds.EmitOnce(res, output)
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *IdOutput) error {
format, found := req.Options[formatOptionName].(string)
if found {
output := format
output = strings.Replace(output, "<id>", out.ID, -1)
output = strings.Replace(output, "<aver>", out.AgentVersion, -1)
output = strings.Replace(output, "<pver>", out.ProtocolVersion, -1)
output = strings.Replace(output, "<pubkey>", out.PublicKey, -1)
output = strings.Replace(output, "<addrs>", strings.Join(out.Addresses, "\n"), -1)
output = strings.Replace(output, "<protocols>", strings.Join(out.Protocols, "\n"), -1)
output = strings.Replace(output, "\\n", "\n", -1)
output = strings.Replace(output, "\\t", "\t", -1)
fmt.Fprint(w, output)
} else {
marshaled, err := json.MarshalIndent(out, "", "\t")
if err != nil {
return err
}
marshaled = append(marshaled, byte('\n'))
fmt.Fprintln(w, string(marshaled))
}
return nil
}),
},
Type: IdOutput{},
}
func printPeer(keyEnc ke.KeyEncoder, ps pstore.Peerstore, p peer.ID) (interface{}, error) {
if p == "" {
return nil, errors.New("attempted to print nil peer")
}
info := new(IdOutput)
info.ID = keyEnc.FormatID(p)
if pk := ps.PubKey(p); pk != nil {
pkb, err := ic.MarshalPublicKey(pk)
if err != nil {
return nil, err
}
info.PublicKey = base64.StdEncoding.EncodeToString(pkb)
}
addrInfo := ps.PeerInfo(p)
addrs, err := peer.AddrInfoToP2pAddrs(&addrInfo)
if err != nil {
return nil, err
}
for _, a := range addrs {
info.Addresses = append(info.Addresses, a.String())
}
sort.Strings(info.Addresses)
protocols, _ := ps.GetProtocols(p) // don't care about errors here.
for _, p := range protocols {
info.Protocols = append(info.Protocols, string(p))
}
sort.Strings(info.Protocols)
if v, err := ps.Get(p, "ProtocolVersion"); err == nil {
if vs, ok := v.(string); ok {
info.ProtocolVersion = vs
}
}
if v, err := ps.Get(p, "AgentVersion"); err == nil {
if vs, ok := v.(string); ok {
info.AgentVersion = vs
}
}
return info, nil
}
// printing self is special cased as we get values differently.
func printSelf(keyEnc ke.KeyEncoder, node *core.IpfsNode) (interface{}, error) {
info := new(IdOutput)
info.ID = keyEnc.FormatID(node.Identity)
pk := node.PrivateKey.GetPublic()
pkb, err := ic.MarshalPublicKey(pk)
if err != nil {
return nil, err
}
info.PublicKey = base64.StdEncoding.EncodeToString(pkb)
if node.PeerHost != nil {
addrs, err := peer.AddrInfoToP2pAddrs(host.InfoFromHost(node.PeerHost))
if err != nil {
return nil, err
}
for _, a := range addrs {
info.Addresses = append(info.Addresses, a.String())
}
sort.Strings(info.Addresses)
info.Protocols = node.PeerHost.Mux().Protocols()
sort.Strings(info.Protocols)
}
info.ProtocolVersion = identify.LibP2PVersion
info.AgentVersion = version.UserAgent
return info, nil
}