bootstrap.go 4.11 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
package main

import (
	"fmt"
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
	config "github.com/jbenet/go-ipfs/config"
	"strings"
)

var cmdIpfsBootstrap = &commander.Command{
	UsageLine: "bootstrap",
	Short:     "Show a list of bootstrapped addresses.",
	Long: `ipfs bootstrap - show, or manipulate bootstrap node addresses

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.

Running 'ipfs bootstrap' with no arguments will run 'ipfs bootstrap list'.

Commands:

  list               Show the boostrap list.
  add <address>      Add a node's address to the bootstrap list.
  remove <address>   Remove an address from the bootstrap list.

`,
	Run: bootstrapCmd,
	Subcommands: []*commander.Command{
		cmdIpfsBootstrapRemove,
		cmdIpfsBootstrapAdd,
	},
	Flag: *flag.NewFlagSet("ipfs-bootstrap", flag.ExitOnError),
}

var cmdIpfsBootstrapRemove = &commander.Command{
	UsageLine: "remove",
	Run:       IpfsBootstrapRemoveCmd,
	Flag:      *flag.NewFlagSet("ipfs-bootstrap-remove", flag.ExitOnError),
}

var cmdIpfsBootstrapAdd = &commander.Command{
	UsageLine: "add",
	Run:       IpfsBootstrapAddCmd,
	Flag:      *flag.NewFlagSet("ipfs-bootstrap-add", flag.ExitOnError),
}

func IpfsBootstrapRemoveCmd(c *commander.Command, inp []string) error {

	if len(inp) == 0 {
		fmt.Println("No peer specified.")
		return nil
	}

	if strings.Contains(inp[0], "/") {

		var pID = inp[0][len(inp[0])-46:]
		var ip = strings.TrimSuffix(inp[0], pID)
		maddr, err := ma.NewMultiaddr(strings.TrimSuffix(ip, "/"))
		var address, _ = maddr.String()
		if err != nil {
			return err
		}

		peer := config.BootstrapPeer{
			Address: address,
			PeerID:  pID,
		}

		configpath, _ := config.Filename("~/.go-ipfs/config")
		var cfg config.Config
		readErr := config.ReadConfigFile(configpath, &cfg)
		if readErr != nil {
			return readErr
		}

		i := 0
		for _, v := range cfg.Bootstrap {
			if v.PeerID == peer.PeerID && v.Address == peer.Address {
				continue
			}
			cfg.Bootstrap[i] = v
			i++
		}
		cfg.Bootstrap = cfg.Bootstrap[:i]

		writeErr := config.WriteConfigFile(configpath, cfg)
		if writeErr != nil {
			return writeErr
		}
	}

	if !strings.Contains(inp[0], "/") {

		var peerID = inp[0]

		configpath, _ := config.Filename("~/.go-ipfs/config")
		var cfg config.Config
		readErr := config.ReadConfigFile(configpath, &cfg)
		if readErr != nil {
			return readErr
		}

		i := 0
		for _, v := range cfg.Bootstrap {
			if v.PeerID == peerID {
				continue
			}
			cfg.Bootstrap[i] = v
			i++
		}
		cfg.Bootstrap = cfg.Bootstrap[:i]

		writeErr := config.WriteConfigFile(configpath, cfg)
		if writeErr != nil {
			return writeErr
		}

	}
	return nil
}

func IpfsBootstrapAddCmd(c *commander.Command, inp []string) error {

	if len(inp) == 0 {
		fmt.Println("No peer specified.")
		return nil
	}

	var pID = inp[0][len(inp[0])-46:]
	var ip = strings.TrimSuffix(inp[0], pID)
	maddr, err := ma.NewMultiaddr(strings.TrimSuffix(ip, "/"))
	var address, _ = maddr.String()
	if err != nil {
		return err
	}

	peer := config.BootstrapPeer{
		Address: address,
		PeerID:  pID,
	}

	configpath, _ := config.Filename("~/.go-ipfs/config")
	var cfg config.Config
	readErr := config.ReadConfigFile(configpath, &cfg)
	if readErr != nil {
		return readErr
	}

	addedPeer := append(cfg.Bootstrap, &peer)
	cfg.Bootstrap = addedPeer

	writeErr := config.WriteConfigFile(configpath, cfg)
	if writeErr != nil {
		return writeErr
	}
	return nil
	return nil
}
func bootstrapCmd(c *commander.Command, inp []string) error {

	configpath, _ := config.Filename("~/.go-ipfs/config")
	var cfg config.Config
	config.ReadConfigFile(configpath, &cfg)

	for i := range cfg.Bootstrap {
		s := []string{cfg.Bootstrap[i].Address, "/", cfg.Bootstrap[i].PeerID, "\n"}
		fmt.Printf(strings.Join(s, ""))
	}

	return nil

}