bootstrap.go 6.86 KB
Newer Older
1 2 3
package commands

import (
4 5
	"bytes"
	"io"
6 7 8 9 10 11 12
	"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"
13
	u "github.com/jbenet/go-ipfs/util"
14 15 16 17 18 19
)

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

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

22
var bootstrapCmd = &cmds.Command{
23 24
	Helptext: cmds.HelpText{
		Tagline: "Show or edit the list of bootstrap peers",
25
		Synopsis: `
26 27 28 29 30 31
ipfs bootstrap list             - Show peers in the bootstrap list
ipfs bootstrap add <peer>...    - Add peers to the bootstrap list
ipfs bootstrap remove <peer>... - Removes peers from the bootstrap list
`,
		ShortDescription: `
Running 'ipfs bootstrap' with no arguments will run 'ipfs bootstrap list'.
32
` + bootstrapSecurityWarning,
33
	},
34

35 36 37
	Run:        bootstrapListCmd.Run,
	Marshalers: bootstrapListCmd.Marshalers,
	Type:       bootstrapListCmd.Type,
38

39 40 41
	Subcommands: map[string]*cmds.Command{
		"list":   bootstrapListCmd,
		"add":    bootstrapAddCmd,
42
		"rm": bootstrapRemoveCmd,
43 44 45 46
	},
}

var bootstrapAddCmd = &cmds.Command{
47 48 49
	Helptext: cmds.HelpText{
		Tagline: "Add peers to the bootstrap list",
		ShortDescription: `Outputs a list of peers that were added (that weren't already
50 51
in the bootstrap list).
` + bootstrapSecurityWarning,
52
	},
53

54
	Arguments: []cmds.Argument{
55
		cmds.StringArg("peer", true, true, peerOptionDesc),
56
	},
57
	Run: func(req cmds.Request) (interface{}, error) {
58 59
		input, err := bootstrapInputToPeers(req.Arguments())
		if err != nil {
60
			return nil, err
61 62 63 64
		}

		filename, err := config.Filename(req.Context().ConfigRoot)
		if err != nil {
65
			return nil, err
66 67
		}

68 69 70 71 72 73
		cfg, err := req.Context().GetConfig()
		if err != nil {
			return nil, err
		}

		added, err := bootstrapAdd(filename, cfg, input)
74
		if err != nil {
75
			return nil, err
76 77
		}

78
		return &BootstrapOutput{added}, nil
79 80
	},
	Type: &BootstrapOutput{},
81
	Marshalers: cmds.MarshalerMap{
82
		cmds.Text: func(res cmds.Response) ([]byte, error) {
83 84 85
			v, ok := res.Output().(*BootstrapOutput)
			if !ok {
				return nil, u.ErrCast()
86
			}
87 88 89 90

			var buf bytes.Buffer
			err := bootstrapWritePeers(&buf, "added ", v.Peers)
			return buf.Bytes(), err
91 92 93 94 95
		},
	},
}

var bootstrapRemoveCmd = &cmds.Command{
96 97 98
	Helptext: cmds.HelpText{
		Tagline: "Removes peers from the bootstrap list",
		ShortDescription: `Outputs the list of peers that were removed.
99
` + bootstrapSecurityWarning,
100
	},
101

102
	Arguments: []cmds.Argument{
103
		cmds.StringArg("peer", true, true, peerOptionDesc),
104
	},
105
	Run: func(req cmds.Request) (interface{}, error) {
106 107
		input, err := bootstrapInputToPeers(req.Arguments())
		if err != nil {
108
			return nil, err
109 110 111 112
		}

		filename, err := config.Filename(req.Context().ConfigRoot)
		if err != nil {
113
			return nil, err
114 115
		}

116 117 118 119 120 121
		cfg, err := req.Context().GetConfig()
		if err != nil {
			return nil, err
		}

		removed, err := bootstrapRemove(filename, cfg, input)
122
		if err != nil {
123
			return nil, err
124 125
		}

126
		return &BootstrapOutput{removed}, nil
127 128
	},
	Type: &BootstrapOutput{},
129
	Marshalers: cmds.MarshalerMap{
130
		cmds.Text: func(res cmds.Response) ([]byte, error) {
131 132 133
			v, ok := res.Output().(*BootstrapOutput)
			if !ok {
				return nil, u.ErrCast()
134
			}
135 136 137 138

			var buf bytes.Buffer
			err := bootstrapWritePeers(&buf, "removed ", v.Peers)
			return buf.Bytes(), err
139 140 141 142 143
		},
	},
}

var bootstrapListCmd = &cmds.Command{
144 145 146 147
	Helptext: cmds.HelpText{
		Tagline:          "Show peers in the bootstrap list",
		ShortDescription: "Peers are output in the format '<multiaddr>/<peerID>'.",
	},
148

149
	Run: func(req cmds.Request) (interface{}, error) {
150 151 152 153 154 155
		cfg, err := req.Context().GetConfig()
		if err != nil {
			return nil, err
		}

		peers := cfg.Bootstrap
156
		return &BootstrapOutput{peers}, nil
157 158
	},
	Type: &BootstrapOutput{},
159 160
	Marshalers: cmds.MarshalerMap{
		cmds.Text: bootstrapMarshaler,
161 162 163
	},
}

164
func bootstrapMarshaler(res cmds.Response) ([]byte, error) {
165 166 167 168
	v, ok := res.Output().(*BootstrapOutput)
	if !ok {
		return nil, u.ErrCast()
	}
169

170 171 172 173 174 175
	var buf bytes.Buffer
	err := bootstrapWritePeers(&buf, "", v.Peers)
	return buf.Bytes(), err
}

func bootstrapWritePeers(w io.Writer, prefix string, peers []*config.BootstrapPeer) error {
176

177 178 179 180 181 182 183 184
	for _, peer := range peers {
		s := prefix + peer.Address + "/" + peer.PeerID + "\n"
		_, err := w.Write([]byte(s))
		if err != nil {
			return err
		}
	}
	return nil
185 186 187
}

func bootstrapInputToPeers(input []interface{}) ([]*config.BootstrapPeer, error) {
188 189 190 191 192 193 194 195 196
	inputAddrs := make([]string, len(input))
	for i, v := range input {
		addr, ok := v.(string)
		if !ok {
			return nil, u.ErrCast()
		}
		inputAddrs[i] = addr
	}

197 198 199 200 201 202 203 204 205
	split := func(addr string) (string, string) {
		idx := strings.LastIndex(addr, "/")
		if idx == -1 {
			return "", addr
		}
		return addr[:idx], addr[idx+1:]
	}

	peers := []*config.BootstrapPeer{}
206
	for _, addr := range inputAddrs {
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
		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 {
240
			if peer.Address == peer2.Address && peer.PeerID == peer2.PeerID {
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
				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
}

260 261
func bootstrapRemove(filename string, cfg *config.Config, toRemove []*config.BootstrapPeer) ([]*config.BootstrapPeer, error) {
	removed := make([]*config.BootstrapPeer, 0, len(toRemove))
262 263 264 265
	keep := make([]*config.BootstrapPeer, 0, len(cfg.Bootstrap))

	for _, peer := range cfg.Bootstrap {
		found := false
266
		for _, peer2 := range toRemove {
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
			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.

`