@cryptotaxi247 / kubo / commits / 0de12b5d3

implement ipfs id in the new commands library

Jeromy committed Nov 16, 2014 at 14:54 UTC 0de12b5d3645bd41e4abbc5e821a5a6347eb69cf
5 files changed +128 -5
core/commands2/id.go new
+99
@@ -0,0 +1,99 @@
1 +package commands
2 +
3 +import (
4 + "encoding/base64"
5 + "encoding/json"
6 + "errors"
7 + "time"
8 +
9 + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
10 +
11 + b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
12 +
13 + cmds "github.com/jbenet/go-ipfs/commands"
14 + "github.com/jbenet/go-ipfs/peer"
15 + kb "github.com/jbenet/go-ipfs/routing/kbucket"
16 + u "github.com/jbenet/go-ipfs/util"
17 +)
18 +
19 +type IdOutput struct {
20 + ID string
21 + PublicKey string
22 + Addresses []string
23 + AgentVersion string
24 + ProtocolVersion string
25 +}
26 +
27 +var idCmd = &cmds.Command{
28 + Helptext: cmds.HelpText{
29 + Tagline: "Show IPFS Node IF info",
30 + ShortDescription: `Prints out information about the specified peer,
31 + if no peer is specified, prints out local peers info.`,
32 + },
33 + Arguments: nil,
34 + Run: func(req cmds.Request) (interface{}, error) {
35 + node, err := req.Context().GetNode()
36 + if err != nil {
37 + return nil, err
38 + }
39 +
40 + if len(req.Arguments()) == 0 {
41 + return printPeer(node.Identity)
42 + }
43 +
44 + pid, ok := req.Arguments()[0].(string)
45 + if !ok {
46 + return nil, errors.New("Improperly formatted peer id")
47 + }
48 +
49 + id := peer.ID(b58.Decode(pid))
50 + ctx, _ := context.WithTimeout(context.TODO(), time.Second*5)
51 + p, err := node.Routing.FindPeer(ctx, id)
52 + if err == kb.ErrLookupFailure {
53 + return nil, errors.New(`ID command fails when run without daemon, we are working to fix this
54 + In the meantime, please run the daemon if you want to use 'ipfs id'`)
55 + }
56 + if err != nil {
57 + return nil, err
58 + }
59 + return printPeer(p)
60 + },
61 + Marshalers: cmds.MarshalerMap{
62 + cmds.Text: func(res cmds.Response) ([]byte, error) {
63 + val, ok := res.Output().(*IdOutput)
64 + if !ok {
65 + return nil, u.ErrCast()
66 + }
67 +
68 + return json.MarshalIndent(val, "", "\t")
69 + },
70 + },
71 + Type: &IdOutput{},
72 +}
73 +
74 +func printPeer(p peer.Peer) (interface{}, error) {
75 + if p == nil {
76 + return nil, errors.New("Attempted to print nil peer!")
77 + }
78 + info := new(IdOutput)
79 +
80 + info.ID = p.ID().String()
81 + if p.PubKey() == nil {
82 + return nil, errors.New(`peer publickey not populated on offline runs,
83 + please run the daemon to use ipfs id!`)
84 + }
85 + pkb, err := p.PubKey().Bytes()
86 + if err != nil {
87 + return nil, err
88 + }
89 + info.PublicKey = base64.StdEncoding.EncodeToString(pkb)
90 + for _, a := range p.Addresses() {
91 + info.Addresses = append(info.Addresses, a.String())
92 + }
93 +
94 + agent, protocol := p.GetVersions()
95 + info.AgentVersion = agent
96 + info.ProtocolVersion = protocol
97 +
98 + return info, nil
99 +}
core/commands2/root.go
+2
@@ -32,6 +32,7 @@ Tool commands:
32 update Download and apply go-ipfs updates
33 version Show ipfs version information
34 commands List all available commands
35 + id Show info about ipfs peers
36
37 Advanced Commands:
38
@@ -77,6 +78,7 @@ var rootSubcommands = map[string]*cmds.Command{
78 "update": UpdateCmd,
79 "object": objectCmd,
80 "refs": refsCmd,
81 + "id": idCmd,
82 }
83
84 func init() {
core/core.go
+3
@@ -19,6 +19,7 @@ import (
19 merkledag "github.com/jbenet/go-ipfs/merkledag"
20 namesys "github.com/jbenet/go-ipfs/namesys"
21 inet "github.com/jbenet/go-ipfs/net"
22 + handshake "github.com/jbenet/go-ipfs/net/handshake"
23 mux "github.com/jbenet/go-ipfs/net/mux"
24 netservice "github.com/jbenet/go-ipfs/net/service"
25 path "github.com/jbenet/go-ipfs/path"
@@ -217,6 +218,8 @@ func initIdentity(cfg *config.Identity, peers peer.Peerstore, online bool) (peer
218 return nil, err
219 }
220
221 + peer.SetVersions(handshake.ClientVersion, handshake.IpfsVersion.String())
222 +
223 // when not online, don't need to parse private keys (yet)
224 if online {
225 skb, err := base64.StdEncoding.DecodeString(cfg.PrivKey)
net/handshake/handshake1.go
+5 -5
@@ -10,13 +10,13 @@ import (
10 semver "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
11 )
12
13 -// ipfsVersion holds the current protocol version for a client running this code
14 -var ipfsVersion *semver.Version
15 -var clientVersion = "go-ipfs/" + config.CurrentVersionNumber
13 +// IpfsVersion holds the current protocol version for a client running this code
14 +var IpfsVersion *semver.Version
15 +var ClientVersion = "go-ipfs/" + config.CurrentVersionNumber
16
17 func init() {
18 var err error
19 - ipfsVersion, err = semver.NewVersion("0.0.1")
19 + IpfsVersion, err = semver.NewVersion("0.0.1")
20 if err != nil {
21 panic(fmt.Errorf("invalid protocol version: %v", err))
22 }
@@ -24,7 +24,7 @@ func init() {
24
25 // Handshake1Msg returns the current protocol version as a protobuf message
26 func Handshake1Msg() *pb.Handshake1 {
27 - return NewHandshake1(ipfsVersion.String(), clientVersion)
27 + return NewHandshake1(IpfsVersion.String(), ClientVersion)
28 }
29
30 // ErrVersionMismatch is returned when two clients don't share a protocol version
peer/peer.go
+19
@@ -99,6 +99,9 @@ type Peer interface {
99 GetType() Type
100 SetType(Type)
101
102 + //Get/Set Agent and Protocol Versions
103 + GetVersions() (agent, protocol string)
104 + SetVersions(agent, protocol string)
105 // Update with the data of another peer instance
106 Update(Peer) error
107
@@ -137,6 +140,9 @@ type peer struct {
140 // within that package, map from ID to latency value.
141 latency time.Duration
142
143 + protocolVersion string
144 + agentVersion string
145 +
146 // typ can be Local, Remote, or Unspecified (default)
147 typ Type
148
@@ -372,6 +378,19 @@ func (p *peer) Update(other Peer) error {
378 return nil
379 }
380
381 +func (p *peer) GetVersions() (agent, protocol string) {
382 + p.RLock()
383 + defer p.RUnlock()
384 + return p.agentVersion, p.protocolVersion
385 +}
386 +
387 +func (p *peer) SetVersions(agent, protocol string) {
388 + p.Lock()
389 + defer p.Unlock()
390 + p.agentVersion = agent
391 + p.protocolVersion = protocol
392 +}
393 +
394 // WithKeyPair returns a Peer object with given keys.
395 func WithKeyPair(sk ic.PrivKey, pk ic.PubKey) (Peer, error) {
396 if sk == nil && pk == nil {