bootstrap.go 6.46 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 24
	Helptext: cmds.HelpText{
		Tagline: "Show or edit the list of bootstrap peers",
		ShortDescription: `Running 'ipfs bootstrap' with no arguments will run 'ipfs bootstrap list'.
25
` + bootstrapSecurityWarning,
26
	},
27

28 29
	Run:         bootstrapListCmd.Run,
	Marshallers: bootstrapListCmd.Marshallers,
30 31
	Type:        bootstrapListCmd.Type,

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

var bootstrapAddCmd = &cmds.Command{
40 41 42
	Helptext: cmds.HelpText{
		Tagline: "Add peers to the bootstrap list",
		ShortDescription: `Outputs a list of peers that were added (that weren't already
43 44
in the bootstrap list).
` + bootstrapSecurityWarning,
45
	},
46

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

		filename, err := config.Filename(req.Context().ConfigRoot)
		if err != nil {
58
			return nil, err
59 60
		}

61 62 63 64 65 66
		cfg, err := req.Context().GetConfig()
		if err != nil {
			return nil, err
		}

		added, err := bootstrapAdd(filename, cfg, input)
67
		if err != nil {
68
			return nil, err
69 70
		}

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

94
	Arguments: []cmds.Argument{
95
		cmds.StringArg("peer", true, true, peerOptionDesc),
96
	},
97
	Run: func(req cmds.Request) (interface{}, error) {
98 99
		input, err := bootstrapInputToPeers(req.Arguments())
		if err != nil {
100
			return nil, err
101 102 103 104
		}

		filename, err := config.Filename(req.Context().ConfigRoot)
		if err != nil {
105
			return nil, err
106 107
		}

108 109 110 111 112 113
		cfg, err := req.Context().GetConfig()
		if err != nil {
			return nil, err
		}

		removed, err := bootstrapRemove(filename, cfg, input)
114
		if err != nil {
115
			return nil, err
116 117
		}

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

140
	Run: func(req cmds.Request) (interface{}, error) {
141 142 143 144 145 146
		cfg, err := req.Context().GetConfig()
		if err != nil {
			return nil, err
		}

		peers := cfg.Bootstrap
147
		return &BootstrapOutput{peers}, nil
148 149 150 151 152 153 154 155
	},
	Type: &BootstrapOutput{},
	Marshallers: map[cmds.EncodingType]cmds.Marshaller{
		cmds.Text: bootstrapMarshaller,
	},
}

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

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

		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.

`