From 0de12b5d3645bd41e4abbc5e821a5a6347eb69cf Mon Sep 17 00:00:00 2001 From: Jeromy <jeromyj@gmail.com> Date: Sun, 16 Nov 2014 14:54:05 -0800 Subject: [PATCH] implement ipfs id in the new commands library --- core/commands2/id.go | 99 +++++++++++++++++++++++++++++++++++++ core/commands2/root.go | 2 + core/core.go | 3 ++ net/handshake/handshake1.go | 10 ++-- peer/peer.go | 19 +++++++ 5 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 core/commands2/id.go diff --git a/core/commands2/id.go b/core/commands2/id.go new file mode 100644 index 000000000..88a602d8b --- /dev/null +++ b/core/commands2/id.go @@ -0,0 +1,99 @@ +package commands + +import ( + "encoding/base64" + "encoding/json" + "errors" + "time" + + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" + + b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58" + + cmds "github.com/jbenet/go-ipfs/commands" + "github.com/jbenet/go-ipfs/peer" + kb "github.com/jbenet/go-ipfs/routing/kbucket" + u "github.com/jbenet/go-ipfs/util" +) + +type IdOutput struct { + ID string + PublicKey string + Addresses []string + AgentVersion string + ProtocolVersion string +} + +var idCmd = &cmds.Command{ + Helptext: cmds.HelpText{ + Tagline: "Show IPFS Node IF info", + ShortDescription: `Prints out information about the specified peer, + if no peer is specified, prints out local peers info.`, + }, + Arguments: nil, + Run: func(req cmds.Request) (interface{}, error) { + node, err := req.Context().GetNode() + if err != nil { + return nil, err + } + + if len(req.Arguments()) == 0 { + return printPeer(node.Identity) + } + + pid, ok := req.Arguments()[0].(string) + if !ok { + return nil, errors.New("Improperly formatted peer id") + } + + id := peer.ID(b58.Decode(pid)) + ctx, _ := context.WithTimeout(context.TODO(), time.Second*5) + p, err := node.Routing.FindPeer(ctx, id) + if err == kb.ErrLookupFailure { + return nil, errors.New(`ID command fails when run without daemon, we are working to fix this + In the meantime, please run the daemon if you want to use 'ipfs id'`) + } + if err != nil { + return nil, err + } + return printPeer(p) + }, + Marshalers: cmds.MarshalerMap{ + cmds.Text: func(res cmds.Response) ([]byte, error) { + val, ok := res.Output().(*IdOutput) + if !ok { + return nil, u.ErrCast() + } + + return json.MarshalIndent(val, "", "\t") + }, + }, + Type: &IdOutput{}, +} + +func printPeer(p peer.Peer) (interface{}, error) { + if p == nil { + return nil, errors.New("Attempted to print nil peer!") + } + info := new(IdOutput) + + info.ID = p.ID().String() + if p.PubKey() == nil { + return nil, errors.New(`peer publickey not populated on offline runs, + please run the daemon to use ipfs id!`) + } + pkb, err := p.PubKey().Bytes() + if err != nil { + return nil, err + } + info.PublicKey = base64.StdEncoding.EncodeToString(pkb) + for _, a := range p.Addresses() { + info.Addresses = append(info.Addresses, a.String()) + } + + agent, protocol := p.GetVersions() + info.AgentVersion = agent + info.ProtocolVersion = protocol + + return info, nil +} diff --git a/core/commands2/root.go b/core/commands2/root.go index f71b75955..e84746253 100644 --- a/core/commands2/root.go +++ b/core/commands2/root.go @@ -32,6 +32,7 @@ Tool commands: update Download and apply go-ipfs updates version Show ipfs version information commands List all available commands + id Show info about ipfs peers Advanced Commands: @@ -77,6 +78,7 @@ var rootSubcommands = map[string]*cmds.Command{ "update": UpdateCmd, "object": objectCmd, "refs": refsCmd, + "id": idCmd, } func init() { diff --git a/core/core.go b/core/core.go index 3c3da7ad6..153dd5833 100644 --- a/core/core.go +++ b/core/core.go @@ -19,6 +19,7 @@ import ( merkledag "github.com/jbenet/go-ipfs/merkledag" namesys "github.com/jbenet/go-ipfs/namesys" inet "github.com/jbenet/go-ipfs/net" + handshake "github.com/jbenet/go-ipfs/net/handshake" mux "github.com/jbenet/go-ipfs/net/mux" netservice "github.com/jbenet/go-ipfs/net/service" path "github.com/jbenet/go-ipfs/path" @@ -217,6 +218,8 @@ func initIdentity(cfg *config.Identity, peers peer.Peerstore, online bool) (peer return nil, err } + peer.SetVersions(handshake.ClientVersion, handshake.IpfsVersion.String()) + // when not online, don't need to parse private keys (yet) if online { skb, err := base64.StdEncoding.DecodeString(cfg.PrivKey) diff --git a/net/handshake/handshake1.go b/net/handshake/handshake1.go index 306c93390..99f8e2ac7 100644 --- a/net/handshake/handshake1.go +++ b/net/handshake/handshake1.go @@ -10,13 +10,13 @@ import ( semver "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/coreos/go-semver/semver" ) -// ipfsVersion holds the current protocol version for a client running this code -var ipfsVersion *semver.Version -var clientVersion = "go-ipfs/" + config.CurrentVersionNumber +// IpfsVersion holds the current protocol version for a client running this code +var IpfsVersion *semver.Version +var ClientVersion = "go-ipfs/" + config.CurrentVersionNumber func init() { var err error - ipfsVersion, err = semver.NewVersion("0.0.1") + IpfsVersion, err = semver.NewVersion("0.0.1") if err != nil { panic(fmt.Errorf("invalid protocol version: %v", err)) } @@ -24,7 +24,7 @@ func init() { // Handshake1Msg returns the current protocol version as a protobuf message func Handshake1Msg() *pb.Handshake1 { - return NewHandshake1(ipfsVersion.String(), clientVersion) + return NewHandshake1(IpfsVersion.String(), ClientVersion) } // ErrVersionMismatch is returned when two clients don't share a protocol version diff --git a/peer/peer.go b/peer/peer.go index f38a2df21..32751e7b0 100644 --- a/peer/peer.go +++ b/peer/peer.go @@ -99,6 +99,9 @@ type Peer interface { GetType() Type SetType(Type) + //Get/Set Agent and Protocol Versions + GetVersions() (agent, protocol string) + SetVersions(agent, protocol string) // Update with the data of another peer instance Update(Peer) error @@ -137,6 +140,9 @@ type peer struct { // within that package, map from ID to latency value. latency time.Duration + protocolVersion string + agentVersion string + // typ can be Local, Remote, or Unspecified (default) typ Type @@ -372,6 +378,19 @@ func (p *peer) Update(other Peer) error { return nil } +func (p *peer) GetVersions() (agent, protocol string) { + p.RLock() + defer p.RUnlock() + return p.agentVersion, p.protocolVersion +} + +func (p *peer) SetVersions(agent, protocol string) { + p.Lock() + defer p.Unlock() + p.agentVersion = agent + p.protocolVersion = protocol +} + // WithKeyPair returns a Peer object with given keys. func WithKeyPair(sk ic.PrivKey, pk ic.PubKey) (Peer, error) { if sk == nil && pk == nil { -- GitLab