bootstrap.go 6.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
package commands

import (
	"fmt"
	"strings"

	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
	mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"

	cmds "github.com/jbenet/go-ipfs/commands"
	config "github.com/jbenet/go-ipfs/config"
12
	u "github.com/jbenet/go-ipfs/util"
13 14 15 16 17 18
)

type BootstrapOutput struct {
	Peers []*config.BootstrapPeer
}

19
var peerOptionDesc = "A peer to add to the bootstrap list (in the format '<multiaddr>/<peerID>')"
20

21 22 23
var bootstrapCmd = &cmds.Command{
	Description: "Show or edit the list of bootstrap peers",
	Help: `Running 'ipfs bootstrap' with no arguments will run 'ipfs bootstrap list'.
24
` + bootstrapSecurityWarning,
25

26 27
	Run:         bootstrapListCmd.Run,
	Marshallers: bootstrapListCmd.Marshallers,
28 29
	Type:        bootstrapListCmd.Type,

30 31 32 33 34 35 36 37
	Subcommands: map[string]*cmds.Command{
		"list":   bootstrapListCmd,
		"add":    bootstrapAddCmd,
		"remove": bootstrapRemoveCmd,
	},
}

var bootstrapAddCmd = &cmds.Command{
38 39 40 41 42
	Description: "Add peers to the bootstrap list",
	Help: `Outputs a list of peers that were added (that weren't already
in the bootstrap list).
` + bootstrapSecurityWarning,

43
	Arguments: []cmds.Argument{
44
		cmds.StringArg("peer", true, true, peerOptionDesc),
45
	},
46
	Run: func(req cmds.Request) (interface{}, error) {
47 48
		input, err := bootstrapInputToPeers(req.Arguments())
		if err != nil {
49
			return nil, err
50 51 52 53
		}

		filename, err := config.Filename(req.Context().ConfigRoot)
		if err != nil {
54
			return nil, err
55 56 57 58
		}

		added, err := bootstrapAdd(filename, req.Context().Config, input)
		if err != nil {
59
			return nil, err
60 61
		}

62
		return &BootstrapOutput{added}, nil
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
	},
	Type: &BootstrapOutput{},
	Marshallers: map[cmds.EncodingType]cmds.Marshaller{
		cmds.Text: func(res cmds.Response) ([]byte, error) {
			v := res.Output().(*BootstrapOutput)
			s := fmt.Sprintf("Added %v peers to the bootstrap list:\n", len(v.Peers))
			marshalled, err := bootstrapMarshaller(res)
			if err != nil {
				return nil, err
			}
			return append([]byte(s), marshalled...), nil
		},
	},
}

var bootstrapRemoveCmd = &cmds.Command{
79 80 81 82
	Description: "Removes peers from the bootstrap list",
	Help: `Outputs the list of peers that were removed.
` + bootstrapSecurityWarning,

83
	Arguments: []cmds.Argument{
84
		cmds.StringArg("peer", true, true, peerOptionDesc),
85
	},
86
	Run: func(req cmds.Request) (interface{}, error) {
87 88
		input, err := bootstrapInputToPeers(req.Arguments())
		if err != nil {
89
			return nil, err
90 91 92 93
		}

		filename, err := config.Filename(req.Context().ConfigRoot)
		if err != nil {
94
			return nil, err
95 96 97 98
		}

		removed, err := bootstrapRemove(filename, req.Context().Config, input)
		if err != nil {
99
			return nil, err
100 101
		}

102
		return &BootstrapOutput{removed}, nil
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	},
	Type: &BootstrapOutput{},
	Marshallers: map[cmds.EncodingType]cmds.Marshaller{
		cmds.Text: func(res cmds.Response) ([]byte, error) {
			v := res.Output().(*BootstrapOutput)
			s := fmt.Sprintf("Removed %v peers from the bootstrap list:\n", len(v.Peers))
			marshalled, err := bootstrapMarshaller(res)
			if err != nil {
				return nil, err
			}
			return append([]byte(s), marshalled...), nil
		},
	},
}

var bootstrapListCmd = &cmds.Command{
119
	Description: "Show peers in the bootstrap list",
120 121 122
	Help: `Peers are output in the format '<multiaddr>/<peerID>'.
`,

123
	Run: func(req cmds.Request) (interface{}, error) {
124
		peers := req.Context().Config.Bootstrap
125
		return &BootstrapOutput{peers}, nil
126 127 128 129 130 131 132 133
	},
	Type: &BootstrapOutput{},
	Marshallers: map[cmds.EncodingType]cmds.Marshaller{
		cmds.Text: bootstrapMarshaller,
	},
}

func bootstrapMarshaller(res cmds.Response) ([]byte, error) {
134 135 136 137
	v, ok := res.Output().(*BootstrapOutput)
	if !ok {
		return nil, u.ErrCast()
	}
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

	s := ""
	for _, peer := range v.Peers {
		s += fmt.Sprintf("%s/%s\n", peer.Address, peer.PeerID)
	}

	return []byte(s), nil
}

func bootstrapInputToPeers(input []interface{}) ([]*config.BootstrapPeer, error) {
	split := func(addr string) (string, string) {
		idx := strings.LastIndex(addr, "/")
		if idx == -1 {
			return "", addr
		}
		return addr[:idx], addr[idx+1:]
	}

	peers := []*config.BootstrapPeer{}
	for _, v := range input {
		addr, ok := v.(string)
		if !ok {
160
			return nil, u.ErrCast()
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
		}

		addrS, peeridS := split(addr)

		// make sure addrS parses as a multiaddr.
		if len(addrS) > 0 {
			maddr, err := ma.NewMultiaddr(addrS)
			if err != nil {
				return nil, err
			}

			addrS = maddr.String()
		}

		// make sure idS parses as a peer.ID
		_, err := mh.FromB58String(peeridS)
		if err != nil {
			return nil, err
		}

		// construct config entry
		peers = append(peers, &config.BootstrapPeer{
			Address: addrS,
			PeerID:  peeridS,
		})
	}
	return peers, nil
}

func bootstrapAdd(filename string, cfg *config.Config, peers []*config.BootstrapPeer) ([]*config.BootstrapPeer, error) {
	added := make([]*config.BootstrapPeer, 0, len(peers))

	for _, peer := range peers {
		duplicate := false
		for _, peer2 := range cfg.Bootstrap {
			if peer.Address == peer2.Address {
				duplicate = true
				break
			}
		}

		if !duplicate {
			cfg.Bootstrap = append(cfg.Bootstrap, peer)
			added = append(added, peer)
		}
	}

	err := config.WriteConfigFile(filename, cfg)
	if err != nil {
		return nil, err
	}

	return added, nil
}

func bootstrapRemove(filename string, cfg *config.Config, peers []*config.BootstrapPeer) ([]*config.BootstrapPeer, error) {
	removed := make([]*config.BootstrapPeer, 0, len(peers))
	keep := make([]*config.BootstrapPeer, 0, len(cfg.Bootstrap))

	for _, peer := range cfg.Bootstrap {
		found := false
		for _, peer2 := range peers {
			if peer.Address == peer2.Address && peer.PeerID == peer2.PeerID {
				found = true
				removed = append(removed, peer)
				break
			}
		}

		if !found {
			keep = append(keep, peer)
		}
	}
	cfg.Bootstrap = keep

	err := config.WriteConfigFile(filename, cfg)
	if err != nil {
		return nil, err
	}

	return removed, nil
}

const bootstrapSecurityWarning = `
SECURITY WARNING:

The bootstrap command manipulates the "bootstrap list", which contains
the addresses of bootstrap nodes. These are the *trusted peers* from
which to learn about other peers in the network. Only edit this list
if you understand the risks of adding or removing nodes from this list.

`