init.go 3.06 KB
Newer Older
1 2 3 4 5 6 7
package main

import (
	"encoding/base64"
	"errors"
	"os"

8 9
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
10
	config "github.com/jbenet/go-ipfs/config"
11
	ci "github.com/jbenet/go-ipfs/crypto"
12
	spipe "github.com/jbenet/go-ipfs/crypto/spipe"
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
	u "github.com/jbenet/go-ipfs/util"
)

var cmdIpfsInit = &commander.Command{
	UsageLine: "init",
	Short:     "Initialize ipfs local configuration",
	Long: `ipfs init

	Initializes ipfs configuration files and generates a
	new keypair.
`,
	Run:  initCmd,
	Flag: *flag.NewFlagSet("ipfs-init", flag.ExitOnError),
}

func init() {
	cmdIpfsInit.Flag.Int("b", 4096, "number of bits for keypair")
	cmdIpfsInit.Flag.String("p", "", "passphrase for encrypting keys")
	cmdIpfsInit.Flag.Bool("f", false, "force overwrite of existing config")
32
	cmdIpfsInit.Flag.String("d", "", "Change default datastore location")
33 34 35
}

func initCmd(c *commander.Command, inp []string) error {
36 37 38 39 40
	configpath, err := getConfigDir(c.Parent)
	if err != nil {
		return err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
41
	u.POut("initializing ipfs node at %s\n", configpath)
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
42
	filename, err := config.Filename(configpath)
43 44 45
	if err != nil {
		return errors.New("Couldn't get home directory path")
	}
46

47 48 49 50 51
	dspath, ok := c.Flag.Lookup("d").Value.Get().(string)
	if !ok {
		return errors.New("failed to parse datastore flag")
	}

52
	fi, err := os.Lstat(filename)
53 54 55 56
	force, ok := c.Flag.Lookup("f").Value.Get().(bool)
	if !ok {
		return errors.New("failed to parse force flag")
	}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
57 58 59 60
	if fi != nil || (err != nil && !os.IsNotExist(err)) {
		if !force {
			return errors.New("ipfs configuration file already exists!\nReinitializing would overwrite your keys.\n(use -f to force overwrite)")
		}
61 62 63
	}
	cfg := new(config.Config)

Brian Tiger Chow's avatar
Brian Tiger Chow committed
64
	cfg.Datastore = config.Datastore{}
65
	if len(dspath) == 0 {
Shanti Bouchez-Mongardé's avatar
Shanti Bouchez-Mongardé committed
66
		dspath, err = config.DataStorePath("")
67 68 69
		if err != nil {
			return err
		}
70 71 72 73
	}
	cfg.Datastore.Path = dspath
	cfg.Datastore.Type = "leveldb"

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
74
	cfg.Identity = config.Identity{}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
75

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
76 77 78 79 80
	// setup the node addresses.
	cfg.Addresses = config.Addresses{
		Swarm: "/ip4/0.0.0.0/tcp/4001",
		API:   "/ip4/127.0.0.1/tcp/5001",
	}
81

82 83 84 85
	nbits, ok := c.Flag.Lookup("b").Value.Get().(int)
	if !ok {
		return errors.New("failed to get bits flag")
	}
86 87 88
	if nbits < 1024 {
		return errors.New("Bitsize less than 1024 is considered unsafe.")
	}
89

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90
	u.POut("generating key pair\n")
91
	sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
92 93 94 95
	if err != nil {
		return err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
96 97
	// currently storing key unencrypted. in the future we need to encrypt it.
	// TODO(security)
98 99 100 101 102
	skbytes, err := sk.Bytes()
	if err != nil {
		return err
	}
	cfg.Identity.PrivKey = base64.StdEncoding.EncodeToString(skbytes)
103

104
	id, err := spipe.IDFromPubKey(pk)
105 106 107 108 109
	if err != nil {
		return err
	}
	cfg.Identity.PeerID = id.Pretty()

110
	// Use these hardcoded bootstrap peers for now.
111
	cfg.Bootstrap = []*config.BootstrapPeer{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
112
		&config.BootstrapPeer{
113 114 115 116 117 118
			// mars.i.ipfs.io
			PeerID:  "QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
			Address: "/ip4/104.131.131.82/tcp/4001",
		},
	}

119
	err = config.WriteConfigFile(filename, cfg)
120 121 122 123 124
	if err != nil {
		return err
	}
	return nil
}