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

import (
	"encoding/base64"
	"errors"
	"os"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
7
	"path/filepath"
8

9 10
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
11
	config "github.com/jbenet/go-ipfs/config"
12
	ci "github.com/jbenet/go-ipfs/crypto"
13
	spipe "github.com/jbenet/go-ipfs/crypto/spipe"
14
	updates "github.com/jbenet/go-ipfs/updates"
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
	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")
34
	cmdIpfsInit.Flag.String("d", "", "Change default datastore location")
35 36 37
}

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

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

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

54
	fi, err := os.Lstat(filename)
55 56 57 58
	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
59 60 61 62
	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)")
		}
63 64 65
	}
	cfg := new(config.Config)

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

76
	// Construct the data store if missing
77
	if err := os.MkdirAll(dspath, os.ModePerm); err != nil {
78 79 80 81 82 83 84 85 86 87
		return err
	}

	// Check the directory is writeable
	if f, err := os.Create(filepath.Join(dspath, "._check_writeable")); err == nil {
		os.Remove(f.Name())
	} else {
		return errors.New("Datastore '" + dspath + "' is not writeable")
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
88
	cfg.Identity = config.Identity{}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
89

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
90 91 92 93 94
	// setup the node addresses.
	cfg.Addresses = config.Addresses{
		Swarm: "/ip4/0.0.0.0/tcp/4001",
		API:   "/ip4/127.0.0.1/tcp/5001",
	}
95

96 97 98 99 100 101
	// setup the node mount points.
	cfg.Mounts = config.Mounts{
		IPFS: "/ipfs",
		IPNS: "/ipns",
	}

102 103 104 105
	nbits, ok := c.Flag.Lookup("b").Value.Get().(int)
	if !ok {
		return errors.New("failed to get bits flag")
	}
106 107 108
	if nbits < 1024 {
		return errors.New("Bitsize less than 1024 is considered unsafe.")
	}
109

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
110
	u.POut("generating key pair\n")
111
	sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
112 113 114 115
	if err != nil {
		return err
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
116 117
	// currently storing key unencrypted. in the future we need to encrypt it.
	// TODO(security)
118 119 120 121 122
	skbytes, err := sk.Bytes()
	if err != nil {
		return err
	}
	cfg.Identity.PrivKey = base64.StdEncoding.EncodeToString(skbytes)
123

124
	id, err := spipe.IDFromPubKey(pk)
125 126 127 128 129
	if err != nil {
		return err
	}
	cfg.Identity.PeerID = id.Pretty()

130
	// Use these hardcoded bootstrap peers for now.
131
	cfg.Bootstrap = []*config.BootstrapPeer{
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
132
		&config.BootstrapPeer{
133 134 135 136 137 138
			// mars.i.ipfs.io
			PeerID:  "QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
			Address: "/ip4/104.131.131.82/tcp/4001",
		},
	}

139
	// tracking ipfs version used to generate the init folder and adding update checker default setting.
140
	cfg.Version = config.Version{
141
		Check:   "error",
142
		Current: updates.Version,
143 144
	}

145
	err = config.WriteConfigFile(filename, cfg)
146 147 148 149 150
	if err != nil {
		return err
	}
	return nil
}