swarm.go 4.15 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4 5
package commands

import (
	"bytes"
	"fmt"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6
	"path"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7 8

	cmds "github.com/jbenet/go-ipfs/commands"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9
	peer "github.com/jbenet/go-ipfs/p2p/peer"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10
	errors "github.com/jbenet/go-ipfs/util/debugerror"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11 12 13

	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14 15 16 17 18 19
)

type stringList struct {
	Strings []string
}

20
var SwarmCmd = &cmds.Command{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21 22 23 24 25 26 27 28 29 30 31 32 33
	Helptext: cmds.HelpText{
		Tagline: "swarm inspection tool",
		Synopsis: `
ipfs swarm peers             - List peers with open connections
ipfs swarm connect <address> - Open connection to a given peer
`,
		ShortDescription: `
ipfs swarm is a tool to manipulate the network swarm. The swarm is the
component that opens, listens for, and maintains connections to other
ipfs peers in the internet.
`,
	},
	Subcommands: map[string]*cmds.Command{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34 35
		"peers":   swarmPeersCmd,
		"connect": swarmConnectCmd,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
	},
}

var swarmPeersCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "List peers with open connections",
		ShortDescription: `
ipfs swarm peers lists the set of peers this node is connected to.
`,
	},
	Run: func(req cmds.Request) (interface{}, error) {

		log.Debug("ipfs swarm peers")
		n, err := req.Context().GetNode()
		if err != nil {
			return nil, err
		}

		if n.Network == nil {
			return nil, errNotOnline
		}

58
		conns := n.Network.Conns()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
59 60
		addrs := make([]string, len(conns))
		for i, c := range conns {
61
			pid := c.RemotePeer()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62 63 64 65 66 67 68 69 70 71 72 73
			addr := c.RemoteMultiaddr()
			addrs[i] = fmt.Sprintf("%s/%s", addr, pid)
		}

		return &stringList{addrs}, nil
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: stringListMarshaler,
	},
	Type: &stringList{},
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
var swarmConnectCmd = &cmds.Command{
	Helptext: cmds.HelpText{
		Tagline: "Open connection to a given peer",
		ShortDescription: `
'ipfs swarm connect' opens a connection to a peer address. The address format
is an ipfs multiaddr:

ipfs swarm connect /ip4/104.131.131.82/tcp/4001/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ
`,
	},
	Arguments: []cmds.Argument{
		cmds.StringArg("address", true, true, "address of peer to connect to"),
	},
	Run: func(req cmds.Request) (interface{}, error) {
		ctx := context.TODO()

		log.Debug("ipfs swarm connect")
		n, err := req.Context().GetNode()
		if err != nil {
			return nil, err
		}

96
		addrs := req.Arguments()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
97 98 99 100 101 102 103 104 105 106 107 108

		if n.Network == nil {
			return nil, errNotOnline
		}

		peers, err := peersWithAddresses(n.Peerstore, addrs)
		if err != nil {
			return nil, err
		}

		output := make([]string, len(peers))
		for i, p := range peers {
109
			output[i] = "connect " + p.Pretty()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

			err := n.Network.DialPeer(ctx, p)
			if err != nil {
				output[i] += " failure: " + err.Error()
			} else {
				output[i] += " success"
			}
		}

		return &stringList{output}, nil
	},
	Marshalers: cmds.MarshalerMap{
		cmds.Text: stringListMarshaler,
	},
	Type: &stringList{},
}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
127 128 129 130 131 132 133 134 135 136 137 138 139
func stringListMarshaler(res cmds.Response) ([]byte, error) {
	list, ok := res.Output().(*stringList)
	if !ok {
		return nil, errors.New("failed to cast []string")
	}

	var buf bytes.Buffer
	for _, s := range list.Strings {
		buf.Write([]byte(s))
		buf.Write([]byte("\n"))
	}
	return buf.Bytes(), nil
}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
140 141 142 143 144 145 146 147 148 149 150 151

// splitAddresses is a function that takes in a slice of string peer addresses
// (multiaddr + peerid) and returns slices of multiaddrs and peerids.
func splitAddresses(addrs []string) (maddrs []ma.Multiaddr, pids []peer.ID, err error) {

	maddrs = make([]ma.Multiaddr, len(addrs))
	pids = make([]peer.ID, len(addrs))
	for i, addr := range addrs {
		a, err := ma.NewMultiaddr(path.Dir(addr))
		if err != nil {
			return nil, nil, cmds.ClientError("invalid peer address: " + err.Error())
		}
152
		id, err := peer.IDB58Decode(path.Base(addr))
153 154 155 156
		if err != nil {
			return nil, nil, err
		}
		pids[i] = id
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
157 158 159 160 161 162 163
		maddrs[i] = a
	}
	return
}

// peersWithAddresses is a function that takes in a slice of string peer addresses
// (multiaddr + peerid) and returns a slice of properly constructed peers
164
func peersWithAddresses(ps peer.Peerstore, addrs []string) ([]peer.ID, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
165 166 167 168 169
	maddrs, pids, err := splitAddresses(addrs)
	if err != nil {
		return nil, err
	}

170 171
	for i, p := range pids {
		ps.AddAddress(p, maddrs[i])
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
172
	}
173
	return pids, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
174
}