bootstrap.go 6.67 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
var bootstrapCmd = &cmds.Command{
22 23
	Helptext: cmds.HelpText{
		Tagline: "Show or edit the list of bootstrap peers",
24 25 26 27 28 29 30
    Synopsis: `
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'.
31
` + bootstrapSecurityWarning,
32
	},
33

34 35
	Run:         bootstrapListCmd.Run,
	Marshallers: bootstrapListCmd.Marshallers,
36 37
	Type:        bootstrapListCmd.Type,

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

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

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

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

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

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

77
		return &BootstrapOutput{added}, nil
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
	},
	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{
94 95 96
	Helptext: cmds.HelpText{
		Tagline: "Removes peers from the bootstrap list",
		ShortDescription: `Outputs the list of peers that were removed.
97
` + bootstrapSecurityWarning,
98
	},
99

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

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

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

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

124
		return &BootstrapOutput{removed}, nil
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
	},
	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{
141 142 143 144
	Helptext: cmds.HelpText{
		Tagline:          "Show peers in the bootstrap list",
		ShortDescription: "Peers are output in the format '<multiaddr>/<peerID>'.",
	},
145

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

		peers := cfg.Bootstrap
153
		return &BootstrapOutput{peers}, nil
154 155 156 157 158 159 160 161
	},
	Type: &BootstrapOutput{},
	Marshallers: map[cmds.EncodingType]cmds.Marshaller{
		cmds.Text: bootstrapMarshaller,
	},
}

func bootstrapMarshaller(res cmds.Response) ([]byte, error) {
162 163 164 165
	v, ok := res.Output().(*BootstrapOutput)
	if !ok {
		return nil, u.ErrCast()
	}
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

	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 {
188
			return nil, u.ErrCast()
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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
		}

		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.

`