@cryptotaxi247 / kubo / commits / a2c2c4877

Implement `ipfs bootstrap` command.

This set of commits implements the bootstrap commands, following from the description in #117

Siraj Ravel committed Sep 29, 2014 at 09:54 UTC a2c2c4877379c0bf6fc9cc8388ea6434b2c46107
2 files changed +179
cmd/ipfs/bootstrap.go new
+178
@@ -0,0 +1,178 @@
1 +package main
2 +
3 +import (
4 + "fmt"
5 + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
6 + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
7 + ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
8 + config "github.com/jbenet/go-ipfs/config"
9 + "strings"
10 +)
11 +
12 +var cmdIpfsBootstrap = &commander.Command{
13 + UsageLine: "bootstrap",
14 + Short: "Show a list of bootstrapped addresses.",
15 + Long: `ipfs bootstrap - show, or manipulate bootstrap node addresses
16 +
17 +SECURITY WARNING:
18 +
19 +The bootstrap command manipulates the "bootstrap list", which contains
20 +the addresses of bootstrap nodes. These are the *trusted peers* from
21 +which to learn about other peers in the network. Only edit this list
22 +if you understand the risks of adding or removing nodes from this list.
23 +
24 +Running 'ipfs bootstrap' with no arguments will run 'ipfs bootstrap list'.
25 +
26 +Commands:
27 +
28 + list Show the boostrap list.
29 + add <address> Add a node's address to the bootstrap list.
30 + remove <address> Remove an address from the bootstrap list.
31 +
32 +`,
33 + Run: bootstrapCmd,
34 + Subcommands: []*commander.Command{
35 + cmdIpfsBootstrapRemove,
36 + cmdIpfsBootstrapAdd,
37 + },
38 + Flag: *flag.NewFlagSet("ipfs-bootstrap", flag.ExitOnError),
39 +}
40 +
41 +var cmdIpfsBootstrapRemove = &commander.Command{
42 + UsageLine: "remove",
43 + Run: IpfsBootstrapRemoveCmd,
44 + Flag: *flag.NewFlagSet("ipfs-bootstrap-remove", flag.ExitOnError),
45 +}
46 +
47 +var cmdIpfsBootstrapAdd = &commander.Command{
48 + UsageLine: "add",
49 + Run: IpfsBootstrapAddCmd,
50 + Flag: *flag.NewFlagSet("ipfs-bootstrap-add", flag.ExitOnError),
51 +}
52 +
53 +func IpfsBootstrapRemoveCmd(c *commander.Command, inp []string) error {
54 +
55 + if len(inp) == 0 {
56 + fmt.Println("No peer specified.")
57 + return nil
58 + }
59 +
60 + if strings.Contains(inp[0], "/") {
61 +
62 + var pID = inp[0][len(inp[0])-46:]
63 + var ip = strings.TrimSuffix(inp[0], pID)
64 + maddr, err := ma.NewMultiaddr(strings.TrimSuffix(ip, "/"))
65 + var address, _ = maddr.String()
66 + if err != nil {
67 + return err
68 + }
69 +
70 + peer := config.BootstrapPeer{
71 + Address: address,
72 + PeerID: pID,
73 + }
74 +
75 + configpath, _ := config.Filename("~/.go-ipfs/config")
76 + var cfg config.Config
77 + readErr := config.ReadConfigFile(configpath, &cfg)
78 + if readErr != nil {
79 + return readErr
80 + }
81 +
82 + i := 0
83 + for _, v := range cfg.Bootstrap {
84 + if v.PeerID == peer.PeerID && v.Address == peer.Address {
85 + continue
86 + }
87 + cfg.Bootstrap[i] = v
88 + i++
89 + }
90 + cfg.Bootstrap = cfg.Bootstrap[:i]
91 +
92 + writeErr := config.WriteConfigFile(configpath, cfg)
93 + if writeErr != nil {
94 + return writeErr
95 + }
96 + }
97 +
98 + if !strings.Contains(inp[0], "/") {
99 +
100 + var peerID = inp[0]
101 +
102 + configpath, _ := config.Filename("~/.go-ipfs/config")
103 + var cfg config.Config
104 + readErr := config.ReadConfigFile(configpath, &cfg)
105 + if readErr != nil {
106 + return readErr
107 + }
108 +
109 + i := 0
110 + for _, v := range cfg.Bootstrap {
111 + if v.PeerID == peerID {
112 + continue
113 + }
114 + cfg.Bootstrap[i] = v
115 + i++
116 + }
117 + cfg.Bootstrap = cfg.Bootstrap[:i]
118 +
119 + writeErr := config.WriteConfigFile(configpath, cfg)
120 + if writeErr != nil {
121 + return writeErr
122 + }
123 +
124 + }
125 + return nil
126 +}
127 +
128 +func IpfsBootstrapAddCmd(c *commander.Command, inp []string) error {
129 +
130 + if len(inp) == 0 {
131 + fmt.Println("No peer specified.")
132 + return nil
133 + }
134 +
135 + var pID = inp[0][len(inp[0])-46:]
136 + var ip = strings.TrimSuffix(inp[0], pID)
137 + maddr, err := ma.NewMultiaddr(strings.TrimSuffix(ip, "/"))
138 + var address, _ = maddr.String()
139 + if err != nil {
140 + return err
141 + }
142 +
143 + peer := config.BootstrapPeer{
144 + Address: address,
145 + PeerID: pID,
146 + }
147 +
148 + configpath, _ := config.Filename("~/.go-ipfs/config")
149 + var cfg config.Config
150 + readErr := config.ReadConfigFile(configpath, &cfg)
151 + if readErr != nil {
152 + return readErr
153 + }
154 +
155 + addedPeer := append(cfg.Bootstrap, &peer)
156 + cfg.Bootstrap = addedPeer
157 +
158 + writeErr := config.WriteConfigFile(configpath, cfg)
159 + if writeErr != nil {
160 + return writeErr
161 + }
162 + return nil
163 + return nil
164 +}
165 +func bootstrapCmd(c *commander.Command, inp []string) error {
166 +
167 + configpath, _ := config.Filename("~/.go-ipfs/config")
168 + var cfg config.Config
169 + config.ReadConfigFile(configpath, &cfg)
170 +
171 + for i := range cfg.Bootstrap {
172 + s := []string{cfg.Bootstrap[i].Address, "/", cfg.Bootstrap[i].PeerID, "\n"}
173 + fmt.Printf(strings.Join(s, ""))
174 + }
175 +
176 + return nil
177 +
178 +}
cmd/ipfs/ipfs.go
+1
@@ -51,6 +51,7 @@ Use "ipfs help <command>" for more information about a command.
51 cmdIpfsMount,
52 cmdIpfsInit,
53 cmdIpfsServe,
54 + cmdIpfsBootstrap,
55 },
56 Flag: *flag.NewFlagSet("ipfs", flag.ExitOnError),
57 }