github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/cmd/ipfs/init.go (about) 1 package main 2 3 import ( 4 "encoding/base64" 5 "errors" 6 "os" 7 "path/filepath" 8 9 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag" 10 "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 u "github.com/jbenet/go-ipfs/util" 15 ) 16 17 var cmdIpfsInit = &commander.Command{ 18 UsageLine: "init", 19 Short: "Initialize ipfs local configuration", 20 Long: `ipfs init 21 22 Initializes ipfs configuration files and generates a 23 new keypair. 24 `, 25 Run: initCmd, 26 Flag: *flag.NewFlagSet("ipfs-init", flag.ExitOnError), 27 } 28 29 func init() { 30 cmdIpfsInit.Flag.Int("b", 4096, "number of bits for keypair") 31 cmdIpfsInit.Flag.String("p", "", "passphrase for encrypting keys") 32 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 configpath, err := getConfigDir(c.Parent) 38 if err != nil { 39 return err 40 } 41 42 u.POut("initializing ipfs node at %s\n", configpath) 43 filename, err := config.Filename(configpath) 44 if err != nil { 45 return errors.New("Couldn't get home directory path") 46 } 47 48 dspath, ok := c.Flag.Lookup("d").Value.Get().(string) 49 if !ok { 50 return errors.New("failed to parse datastore flag") 51 } 52 53 fi, err := os.Lstat(filename) 54 force, ok := c.Flag.Lookup("f").Value.Get().(bool) 55 if !ok { 56 return errors.New("failed to parse force flag") 57 } 58 if fi != nil || (err != nil && !os.IsNotExist(err)) { 59 if !force { 60 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) 64 65 cfg.Datastore = config.Datastore{} 66 if len(dspath) == 0 { 67 dspath, err = config.DataStorePath("") 68 if err != nil { 69 return err 70 } 71 } 72 cfg.Datastore.Path = dspath 73 cfg.Datastore.Type = "leveldb" 74 75 // Construct the data store if missing 76 if err := os.MkdirAll(dspath, os.ModePerm); err != nil { 77 return err 78 } 79 80 // Check the directory is writeable 81 if f, err := os.Create(filepath.Join(dspath, "._check_writeable")); err == nil { 82 os.Remove(f.Name()) 83 } else { 84 return errors.New("Datastore '" + dspath + "' is not writeable") 85 } 86 87 cfg.Identity = config.Identity{} 88 89 // setup the node addresses. 90 cfg.Addresses = config.Addresses{ 91 Swarm: "/ip4/0.0.0.0/tcp/4001", 92 API: "/ip4/127.0.0.1/tcp/5001", 93 } 94 95 // setup the node mount points. 96 cfg.Mounts = config.Mounts{ 97 IPFS: "/ipfs", 98 IPNS: "/ipns", 99 } 100 101 nbits, ok := c.Flag.Lookup("b").Value.Get().(int) 102 if !ok { 103 return errors.New("failed to get bits flag") 104 } 105 if nbits < 1024 { 106 return errors.New("Bitsize less than 1024 is considered unsafe.") 107 } 108 109 u.POut("generating key pair\n") 110 sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits) 111 if err != nil { 112 return err 113 } 114 115 // currently storing key unencrypted. in the future we need to encrypt it. 116 // TODO(security) 117 skbytes, err := sk.Bytes() 118 if err != nil { 119 return err 120 } 121 cfg.Identity.PrivKey = base64.StdEncoding.EncodeToString(skbytes) 122 123 id, err := peer.IDFromPubKey(pk) 124 if err != nil { 125 return err 126 } 127 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{ 132 &config.BootstrapPeer{ 133 // mars.i.ipfs.io 134 PeerID: "QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", 135 Address: "/ip4/104.131.131.82/tcp/4001", 136 }, 137 } 138 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 if err != nil { 144 return err 145 } 146 return nil 147 }