@cryptotaxi247 / kubo / commits / 1ea9481a6

move config.Init into config package

Brian Tiger Chow committed Jan 24, 2015 at 02:50 UTC 1ea9481a63c0d9e55a1ae3e567bd9ad05963ba5a
4 files changed +134 -130
cmd/ipfs/init.go
+2 -98
@@ -2,17 +2,13 @@ package main
2
3 import (
4 "bytes"
5 - "encoding/base64"
5 "fmt"
6
7 context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
8 cmds "github.com/jbenet/go-ipfs/commands"
9 core "github.com/jbenet/go-ipfs/core"
11 - corecmds "github.com/jbenet/go-ipfs/core/commands"
10 coreunix "github.com/jbenet/go-ipfs/core/coreunix"
11 ipns "github.com/jbenet/go-ipfs/fuse/ipns"
14 - ci "github.com/jbenet/go-ipfs/p2p/crypto"
15 - peer "github.com/jbenet/go-ipfs/p2p/peer"
12 config "github.com/jbenet/go-ipfs/repo/config"
13 fsrepo "github.com/jbenet/go-ipfs/repo/fsrepo"
14 u "github.com/jbenet/go-ipfs/util"
@@ -88,14 +84,12 @@ func initWithDefaults(repoRoot string) error {
84 }
85
86 func doInit(repoRoot string, force bool, nBitsForKeypair int) (interface{}, error) {
91 -
87 u.POut("initializing ipfs node at %s\n", repoRoot)
88
89 if fsrepo.IsInitialized(repoRoot) && !force {
90 return nil, errRepoExists
91 }
97 -
98 - conf, err := initConfig(nBitsForKeypair)
92 + conf, err := config.Init(nBitsForKeypair)
93 if err != nil {
94 return nil, err
95 }
@@ -107,16 +101,15 @@ func doInit(repoRoot string, force bool, nBitsForKeypair int) (interface{}, erro
101 if err := fsrepo.Init(repoRoot, conf); err != nil {
102 return nil, err
103 }
104 +
105 err = addTheWelcomeFile(repoRoot)
106 if err != nil {
107 return nil, err
108 }
114 -
109 err = initializeIpnsKeyspace(repoRoot)
110 if err != nil {
111 return nil, err
112 }
119 -
113 return nil, nil
114 }
115
@@ -167,92 +160,3 @@ func initializeIpnsKeyspace(repoRoot string) error {
160
161 return ipns.InitializeKeyspace(nd, nd.PrivateKey)
162 }
170 -
171 -func datastoreConfig() (*config.Datastore, error) {
172 - dspath, err := config.DataStorePath("")
173 - if err != nil {
174 - return nil, err
175 - }
176 - return &config.Datastore{
177 - Path: dspath,
178 - Type: "leveldb",
179 - }, nil
180 -}
181 -
182 -func initConfig(nBitsForKeypair int) (*config.Config, error) {
183 - ds, err := datastoreConfig()
184 - if err != nil {
185 - return nil, err
186 - }
187 -
188 - identity, err := identityConfig(nBitsForKeypair)
189 - if err != nil {
190 - return nil, err
191 - }
192 -
193 - bootstrapPeers, err := corecmds.DefaultBootstrapPeers()
194 - if err != nil {
195 - return nil, err
196 - }
197 -
198 - conf := &config.Config{
199 -
200 - // setup the node's default addresses.
201 - // Note: two swarm listen addrs, one tcp, one utp.
202 - Addresses: config.Addresses{
203 - Swarm: []string{
204 - "/ip4/0.0.0.0/tcp/4001",
205 - // "/ip4/0.0.0.0/udp/4002/utp", // disabled for now.
206 - },
207 - API: "/ip4/127.0.0.1/tcp/5001",
208 - },
209 -
210 - Bootstrap: bootstrapPeers,
211 - Datastore: *ds,
212 - Identity: identity,
213 -
214 - // setup the node mount points.
215 - Mounts: config.Mounts{
216 - IPFS: "/ipfs",
217 - IPNS: "/ipns",
218 - },
219 -
220 - // tracking ipfs version used to generate the init folder and adding
221 - // update checker default setting.
222 - Version: config.VersionDefaultValue(),
223 - }
224 -
225 - return conf, nil
226 -}
227 -
228 -// identityConfig initializes a new identity.
229 -func identityConfig(nbits int) (config.Identity, error) {
230 - // TODO guard higher up
231 - ident := config.Identity{}
232 - if nbits < 1024 {
233 - return ident, debugerror.New("Bitsize less than 1024 is considered unsafe.")
234 - }
235 -
236 - fmt.Printf("generating %v-bit RSA keypair...", nbits)
237 - sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
238 - if err != nil {
239 - return ident, err
240 - }
241 - fmt.Printf("done\n")
242 -
243 - // currently storing key unencrypted. in the future we need to encrypt it.
244 - // TODO(security)
245 - skbytes, err := sk.Bytes()
246 - if err != nil {
247 - return ident, err
248 - }
249 - ident.PrivKey = base64.StdEncoding.EncodeToString(skbytes)
250 -
251 - id, err := peer.IDFromPublicKey(pk)
252 - if err != nil {
253 - return ident, err
254 - }
255 - ident.PeerID = id.Pretty()
256 - fmt.Printf("peer identity: %s\n", ident.PeerID)
257 - return ident, nil
258 -}
core/commands/bootstrap.go
+1 -31
@@ -12,24 +12,6 @@ import (
12 errors "github.com/jbenet/go-ipfs/util/debugerror"
13 )
14
15 -// DefaultBootstrapAddresses are the hardcoded bootstrap addresses
16 -// for ipfs. they are nodes run by the ipfs team. docs on these later.
17 -// As with all p2p networks, bootstrap is an important security concern.
18 -//
19 -// Note: this is here -- and not inside cmd/ipfs/init.go -- because of an
20 -// import dependency issue. TODO: move this into a config/default/ package.
21 -var DefaultBootstrapAddresses = []string{
22 - "/ip4/104.131.131.82/tcp/4001/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", // mars.i.ipfs.io
23 - "/ip4/104.236.176.52/tcp/4001/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z", // neptune (to be neptune.i.ipfs.io)
24 - "/ip4/104.236.179.241/tcp/4001/QmSoLpPVmHKQ4XTPdz8tjDFgdeRFkpV8JgYq8JVJ69RrZm", // pluto (to be pluto.i.ipfs.io)
25 - "/ip4/162.243.248.213/tcp/4001/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm", // uranus (to be uranus.i.ipfs.io)
26 - "/ip4/128.199.219.111/tcp/4001/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu", // saturn (to be saturn.i.ipfs.io)
27 - "/ip4/104.236.76.40/tcp/4001/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64", // venus (to be venus.i.ipfs.io)
28 - "/ip4/178.62.158.247/tcp/4001/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd", // earth (to be earth.i.ipfs.io)
29 - "/ip4/178.62.61.185/tcp/4001/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3", // mercury (to be mercury.i.ipfs.io)
30 - "/ip4/104.236.151.122/tcp/4001/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx", // jupiter (to be jupiter.i.ipfs.io)
31 -}
32 -
15 type BootstrapOutput struct {
16 Peers []config.BootstrapPeer
17 }
@@ -99,7 +81,7 @@ in the bootstrap list).
81
82 if deflt {
83 // parse separately for meaningful, correct error.
102 - defltPeers, err := DefaultBootstrapPeers()
84 + defltPeers, err := config.DefaultBootstrapPeers()
85 if err != nil {
86 res.SetError(err, cmds.ErrNormal)
87 return
@@ -311,18 +293,6 @@ func bootstrapRemoveAll(r repo.Repo, cfg *config.Config) ([]config.BootstrapPeer
293 return removed, nil
294 }
295
314 -// DefaultBootstrapPeers returns the (parsed) set of default bootstrap peers.
315 -// if it fails, it returns a meaningful error for the user.
316 -// This is here (and not inside cmd/ipfs/init) because of module dependency problems.
317 -func DefaultBootstrapPeers() ([]config.BootstrapPeer, error) {
318 - ps, err := config.ParseBootstrapPeers(DefaultBootstrapAddresses)
319 - if err != nil {
320 - return nil, errors.Errorf(`failed to parse hardcoded bootstrap peers: %s
321 -This is a problem with the ipfs codebase. Please report it to the dev team.`, err)
322 - }
323 - return ps, nil
324 -}
325 -
296 const bootstrapSecurityWarning = `
297 SECURITY WARNING:
298
repo/config/bootstrap_peers.go
+32 -1
@@ -1,19 +1,50 @@
1 package config
2
3 import (
4 - "errors"
4 "strings"
5
6 ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
7 mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
8 +
9 + errors "github.com/jbenet/go-ipfs/util/debugerror"
10 )
11
12 +// DefaultBootstrapAddresses are the hardcoded bootstrap addresses
13 +// for ipfs. they are nodes run by the ipfs team. docs on these later.
14 +// As with all p2p networks, bootstrap is an important security concern.
15 +//
16 +// Note: this is here -- and not inside cmd/ipfs/init.go -- because of an
17 +// import dependency issue. TODO: move this into a config/default/ package.
18 +var DefaultBootstrapAddresses = []string{
19 + "/ip4/104.131.131.82/tcp/4001/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", // mars.i.ipfs.io
20 + "/ip4/104.236.176.52/tcp/4001/QmSoLnSGccFuZQJzRadHn95W2CrSFmZuTdDWP8HXaHca9z", // neptune (to be neptune.i.ipfs.io)
21 + "/ip4/104.236.179.241/tcp/4001/QmSoLpPVmHKQ4XTPdz8tjDFgdeRFkpV8JgYq8JVJ69RrZm", // pluto (to be pluto.i.ipfs.io)
22 + "/ip4/162.243.248.213/tcp/4001/QmSoLueR4xBeUbY9WZ9xGUUxunbKWcrNFTDAadQJmocnWm", // uranus (to be uranus.i.ipfs.io)
23 + "/ip4/128.199.219.111/tcp/4001/QmSoLSafTMBsPKadTEgaXctDQVcqN88CNLHXMkTNwMKPnu", // saturn (to be saturn.i.ipfs.io)
24 + "/ip4/104.236.76.40/tcp/4001/QmSoLV4Bbm51jM9C4gDYZQ9Cy3U6aXMJDAbzgu2fzaDs64", // venus (to be venus.i.ipfs.io)
25 + "/ip4/178.62.158.247/tcp/4001/QmSoLer265NRgSp2LA3dPaeykiS1J6DifTC88f5uVQKNAd", // earth (to be earth.i.ipfs.io)
26 + "/ip4/178.62.61.185/tcp/4001/QmSoLMeWqB7YGVLJN3pNLQpmmEk35v6wYtsMGLzSr5QBU3", // mercury (to be mercury.i.ipfs.io)
27 + "/ip4/104.236.151.122/tcp/4001/QmSoLju6m7xTh3DuokvT3886QRYqxAzb1kShaanJgW36yx", // jupiter (to be jupiter.i.ipfs.io)
28 +}
29 +
30 // BootstrapPeer is a peer used to bootstrap the network.
31 type BootstrapPeer struct {
32 Address string
33 PeerID string // until multiaddr supports ipfs, use another field.
34 }
35
36 +// DefaultBootstrapPeers returns the (parsed) set of default bootstrap peers.
37 +// if it fails, it returns a meaningful error for the user.
38 +// This is here (and not inside cmd/ipfs/init) because of module dependency problems.
39 +func DefaultBootstrapPeers() ([]BootstrapPeer, error) {
40 + ps, err := ParseBootstrapPeers(DefaultBootstrapAddresses)
41 + if err != nil {
42 + return nil, errors.Errorf(`failed to parse hardcoded bootstrap peers: %s
43 +This is a problem with the ipfs codebase. Please report it to the dev team.`, err)
44 + }
45 + return ps, nil
46 +}
47 +
48 func (bp *BootstrapPeer) String() string {
49 return bp.Address + "/" + bp.PeerID
50 }
repo/config/init.go new
+99
@@ -0,0 +1,99 @@
1 +package config
2 +
3 +import (
4 + "encoding/base64"
5 + "fmt"
6 +
7 + ci "github.com/jbenet/go-ipfs/p2p/crypto"
8 + peer "github.com/jbenet/go-ipfs/p2p/peer"
9 + errors "github.com/jbenet/go-ipfs/util/debugerror"
10 +)
11 +
12 +func Init(nBitsForKeypair int) (*Config, error) {
13 + ds, err := datastoreConfig()
14 + if err != nil {
15 + return nil, err
16 + }
17 +
18 + identity, err := identityConfig(nBitsForKeypair)
19 + if err != nil {
20 + return nil, err
21 + }
22 +
23 + bootstrapPeers, err := DefaultBootstrapPeers()
24 + if err != nil {
25 + return nil, err
26 + }
27 +
28 + conf := &Config{
29 +
30 + // setup the node's default addresses.
31 + // Note: two swarm listen addrs, one tcp, one utp.
32 + Addresses: Addresses{
33 + Swarm: []string{
34 + "/ip4/0.0.0.0/tcp/4001",
35 + // "/ip4/0.0.0.0/udp/4002/utp", // disabled for now.
36 + },
37 + API: "/ip4/127.0.0.1/tcp/5001",
38 + },
39 +
40 + Bootstrap: bootstrapPeers,
41 + Datastore: *ds,
42 + Identity: identity,
43 +
44 + // setup the node mount points.
45 + Mounts: Mounts{
46 + IPFS: "/ipfs",
47 + IPNS: "/ipns",
48 + },
49 +
50 + // tracking ipfs version used to generate the init folder and adding
51 + // update checker default setting.
52 + Version: VersionDefaultValue(),
53 + }
54 +
55 + return conf, nil
56 +}
57 +
58 +func datastoreConfig() (*Datastore, error) {
59 + dspath, err := DataStorePath("")
60 + if err != nil {
61 + return nil, err
62 + }
63 + return &Datastore{
64 + Path: dspath,
65 + Type: "leveldb",
66 + }, nil
67 +}
68 +
69 +// identityConfig initializes a new identity.
70 +func identityConfig(nbits int) (Identity, error) {
71 + // TODO guard higher up
72 + ident := Identity{}
73 + if nbits < 1024 {
74 + return ident, errors.New("Bitsize less than 1024 is considered unsafe.")
75 + }
76 +
77 + fmt.Printf("generating %v-bit RSA keypair...", nbits)
78 + sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
79 + if err != nil {
80 + return ident, err
81 + }
82 + fmt.Printf("done\n")
83 +
84 + // currently storing key unencrypted. in the future we need to encrypt it.
85 + // TODO(security)
86 + skbytes, err := sk.Bytes()
87 + if err != nil {
88 + return ident, err
89 + }
90 + ident.PrivKey = base64.StdEncoding.EncodeToString(skbytes)
91 +
92 + id, err := peer.IDFromPublicKey(pk)
93 + if err != nil {
94 + return ident, err
95 + }
96 + ident.PeerID = id.Pretty()
97 + fmt.Printf("peer identity: %s\n", ident.PeerID)
98 + return ident, nil
99 +}