init.go 3.68 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
	peer "github.com/jbenet/go-ipfs/peer"
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
	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")
33
	cmdIpfsInit.Flag.String("d", "", "Change default datastore location")
34 35 36
}

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

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

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

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

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

75
	// Construct the data store if missing
76
	if err := os.MkdirAll(dspath, os.ModePerm); err != nil {
77 78 79 80 81 82 83 84 85 86
		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
87
	cfg.Identity = config.Identity{}
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
88

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

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

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

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

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

123
	id, err := peer.IDFromPubKey(pk)
124 125 126 127
	if err != nil {
		return err
	}
	cfg.Identity.PeerID = id.Pretty()
128
	u.POut("peer identity: %s\n", id.Pretty())
129

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.VersionDefaultValue()
141

142
	err = config.WriteConfigFile(filename, cfg)
143 144 145 146 147
	if err != nil {
		return err
	}
	return nil
}