init.go 2.33 KB
Newer Older
1 2 3 4
package config

import (
	"encoding/base64"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
	"errors"
6
	"fmt"
7
	"io"
8

9 10
	ci "gx/ipfs/QmccGfZs3rzku8Bv6sTPH3bMUKD1EVod8srgRjt5csdmva/go-libp2p/p2p/crypto"
	peer "gx/ipfs/QmccGfZs3rzku8Bv6sTPH3bMUKD1EVod8srgRjt5csdmva/go-libp2p/p2p/peer"
11 12
)

13 14
func Init(out io.Writer, nBitsForKeypair int) (*Config, error) {
	identity, err := identityConfig(out, nBitsForKeypair)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
	if err != nil {
		return nil, err
	}

	bootstrapPeers, err := DefaultBootstrapPeers()
	if err != nil {
		return nil, err
	}

	conf := &Config{

		// setup the node's default addresses.
		// Note: two swarm listen addrs, one tcp, one utp.
		Addresses: Addresses{
			Swarm: []string{
				"/ip4/0.0.0.0/tcp/4001",
				// "/ip4/0.0.0.0/udp/4002/utp", // disabled for now.
32
				"/ip6/::/tcp/4001",
33
			},
34 35
			API:     "/ip4/127.0.0.1/tcp/5001",
			Gateway: "/ip4/127.0.0.1/tcp/8080",
36 37
		},

Jeromy's avatar
Jeromy committed
38 39
		Bootstrap: BootstrapPeerStrings(bootstrapPeers),
		Identity:  identity,
Jeromy's avatar
Jeromy committed
40 41 42 43
		Discovery: Discovery{MDNS{
			Enabled:  true,
			Interval: 10,
		}},
44 45 46 47 48 49 50

		// setup the node mount points.
		Mounts: Mounts{
			IPFS: "/ipfs",
			IPNS: "/ipns",
		},

51 52 53 54
		Ipns: Ipns{
			ResolveCacheSize: 128,
		},

55 56
		Gateway: Gateway{
			RootRedirect: "",
57
			Writable:     false,
58
			PathPrefixes: []string{},
59
		},
60 61 62 63 64 65 66 67 68 69 70
	}

	return conf, nil
}

func datastoreConfig() (*Datastore, error) {
	dspath, err := DataStorePath("")
	if err != nil {
		return nil, err
	}
	return &Datastore{
rht's avatar
rht committed
71 72 73 74 75
		Path:               dspath,
		Type:               "leveldb",
		StorageMax:         "10GB",
		StorageGCWatermark: 90, // 90%
		GCPeriod:           "1h",
76 77 78 79
	}, nil
}

// identityConfig initializes a new identity.
80
func identityConfig(out io.Writer, nbits int) (Identity, error) {
81 82 83 84 85 86
	// TODO guard higher up
	ident := Identity{}
	if nbits < 1024 {
		return ident, errors.New("Bitsize less than 1024 is considered unsafe.")
	}

87
	fmt.Fprintf(out, "generating %v-bit RSA keypair...", nbits)
88 89 90 91
	sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
	if err != nil {
		return ident, err
	}
92
	fmt.Fprintf(out, "done\n")
93 94 95 96 97 98 99 100 101 102 103 104 105 106

	// currently storing key unencrypted. in the future we need to encrypt it.
	// TODO(security)
	skbytes, err := sk.Bytes()
	if err != nil {
		return ident, err
	}
	ident.PrivKey = base64.StdEncoding.EncodeToString(skbytes)

	id, err := peer.IDFromPublicKey(pk)
	if err != nil {
		return ident, err
	}
	ident.PeerID = id.Pretty()
107
	fmt.Fprintf(out, "peer identity: %s\n", ident.PeerID)
108 109
	return ident, nil
}