init.go 2.5 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 "github.com/ipfs/go-ipfs/p2p/crypto"
	peer "github.com/ipfs/go-ipfs/p2p/peer"
11 12
)

13
func Init(out io.Writer, nBitsForKeypair int) (*Config, error) {
14 15 16 17 18
	ds, err := datastoreConfig()
	if err != nil {
		return nil, err
	}

19
	identity, err := identityConfig(out, nBitsForKeypair)
20 21 22 23 24 25 26 27 28
	if err != nil {
		return nil, err
	}

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

29
	snr, err := initSNRConfig()
30 31 32 33
	if err != nil {
		return nil, err
	}

34 35 36 37 38 39 40 41
	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.
42
				"/ip6/::/tcp/4001",
43
			},
44 45
			API:     "/ip4/127.0.0.1/tcp/5001",
			Gateway: "/ip4/127.0.0.1/tcp/8080",
46 47
		},

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
48 49 50 51
		Bootstrap:        BootstrapPeerStrings(bootstrapPeers),
		SupernodeRouting: *snr,
		Datastore:        *ds,
		Identity:         identity,
Jeromy's avatar
Jeromy committed
52 53 54 55
		Discovery: Discovery{MDNS{
			Enabled:  true,
			Interval: 10,
		}},
56
		Log: Log{
57 58
			MaxSizeMB:  250,
			MaxBackups: 1,
59
		},
60 61 62 63 64 65 66

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

67 68 69 70
		Ipns: Ipns{
			ResolveCacheSize: 128,
		},

71 72 73
		// tracking ipfs version used to generate the init folder and adding
		// update checker default setting.
		Version: VersionDefaultValue(),
74 75 76

		Gateway: Gateway{
			RootRedirect: "",
77
			Writable:     false,
78
		},
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	}

	return conf, nil
}

func datastoreConfig() (*Datastore, error) {
	dspath, err := DataStorePath("")
	if err != nil {
		return nil, err
	}
	return &Datastore{
		Path: dspath,
		Type: "leveldb",
	}, nil
}

// identityConfig initializes a new identity.
96
func identityConfig(out io.Writer, nbits int) (Identity, error) {
97 98 99 100 101 102
	// TODO guard higher up
	ident := Identity{}
	if nbits < 1024 {
		return ident, errors.New("Bitsize less than 1024 is considered unsafe.")
	}

103
	fmt.Fprintf(out, "generating %v-bit RSA keypair...", nbits)
104 105 106 107
	sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
	if err != nil {
		return ident, err
	}
108
	fmt.Fprintf(out, "done\n")
109 110 111 112 113 114 115 116 117 118 119 120 121 122

	// 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()
123
	fmt.Fprintf(out, "peer identity: %s\n", ident.PeerID)
124 125
	return ident, nil
}