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

import (
	"encoding/base64"
	"fmt"
6
	"io"
7 8 9 10 11 12

	ci "github.com/jbenet/go-ipfs/p2p/crypto"
	peer "github.com/jbenet/go-ipfs/p2p/peer"
	errors "github.com/jbenet/go-ipfs/util/debugerror"
)

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 29 30 31 32 33 34 35 36 37
	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.
			},
38 39
			API:     "/ip4/127.0.0.1/tcp/5001",
			Gateway: "/ip4/127.0.0.1/tcp/8080",
40 41
		},

42
		Bootstrap: BootstrapPeerStrings(bootstrapPeers),
43 44
		Datastore: *ds,
		Identity:  identity,
45
		Log: Log{
46 47
			MaxSizeMB:  250,
			MaxBackups: 1,
48
		},
49 50 51 52 53 54 55 56 57 58

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

		// tracking ipfs version used to generate the init folder and adding
		// update checker default setting.
		Version: VersionDefaultValue(),
59 60 61

		Gateway: Gateway{
			RootRedirect: "",
62
			Writable:     false,
63
		},
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	}

	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.
81
func identityConfig(out io.Writer, nbits int) (Identity, error) {
82 83 84 85 86 87
	// TODO guard higher up
	ident := Identity{}
	if nbits < 1024 {
		return ident, errors.New("Bitsize less than 1024 is considered unsafe.")
	}

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

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