@cryptotaxi247 / kubo / commits / 69d7848d2

go-ipfs-config: move config.Init into config package

Brian Tiger Chow committed Jan 24, 2015 at 02:50 UTC 69d7848d2ed23a5491ee7c0f396650c429457f0d
2 files changed +131 -1
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 }
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 +}