Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IPFS Bootstrap Command #137

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 178 additions & 0 deletions cmd/ipfs/bootstrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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.

`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about:

    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.

`,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was fixed

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),
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

config.BootstrapPeer already exists

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was fixed


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will break your config when you write back out. Please just use the functions provided in the config package.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was fixed


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

if len(inp) == 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i was a bit confused by this. I'll look into this more.

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

}
1 change: 1 addition & 0 deletions cmd/ipfs/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Use "ipfs help <command>" for more information about a command.
cmdIpfsCommands,
cmdIpfsMount,
cmdIpfsInit,
cmdIpfsBootstrap,
},
Flag: *flag.NewFlagSet("ipfs", flag.ExitOnError),
}
Expand Down