mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-28 17:03:58 +08:00
add in a default file hash and cleaned up init functiona bit
This commit is contained in:
145
cmd/ipfs/init.go
145
cmd/ipfs/init.go
@ -1,16 +1,22 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
|
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gonuts/flag"
|
||||||
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
|
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/commander"
|
||||||
config "github.com/jbenet/go-ipfs/config"
|
config "github.com/jbenet/go-ipfs/config"
|
||||||
|
core "github.com/jbenet/go-ipfs/core"
|
||||||
ci "github.com/jbenet/go-ipfs/crypto"
|
ci "github.com/jbenet/go-ipfs/crypto"
|
||||||
|
imp "github.com/jbenet/go-ipfs/importer"
|
||||||
|
chunk "github.com/jbenet/go-ipfs/importer/chunk"
|
||||||
peer "github.com/jbenet/go-ipfs/peer"
|
peer "github.com/jbenet/go-ipfs/peer"
|
||||||
|
updates "github.com/jbenet/go-ipfs/updates"
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -33,6 +39,66 @@ func init() {
|
|||||||
cmdIpfsInit.Flag.String("d", "", "Change default datastore location")
|
cmdIpfsInit.Flag.String("d", "", "Change default datastore location")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var defaultPeers = []*config.BootstrapPeer{
|
||||||
|
&config.BootstrapPeer{
|
||||||
|
// mars.i.ipfs.io
|
||||||
|
PeerID: "QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
|
||||||
|
Address: "/ip4/104.131.131.82/tcp/4001",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func datastoreConfig(dspath string) (config.Datastore, error) {
|
||||||
|
ds := config.Datastore{}
|
||||||
|
if len(dspath) == 0 {
|
||||||
|
var err error
|
||||||
|
dspath, err = config.DataStorePath("")
|
||||||
|
if err != nil {
|
||||||
|
return ds, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ds.Path = dspath
|
||||||
|
ds.Type = "leveldb"
|
||||||
|
|
||||||
|
// Construct the data store if missing
|
||||||
|
if err := os.MkdirAll(dspath, os.ModePerm); err != nil {
|
||||||
|
return ds, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check the directory is writeable
|
||||||
|
if f, err := os.Create(filepath.Join(dspath, "._check_writeable")); err == nil {
|
||||||
|
os.Remove(f.Name())
|
||||||
|
} else {
|
||||||
|
return ds, errors.New("Datastore '" + dspath + "' is not writeable")
|
||||||
|
}
|
||||||
|
|
||||||
|
return ds, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func identityConfig(nbits int) (config.Identity, error) {
|
||||||
|
ident := config.Identity{}
|
||||||
|
fmt.Println("generating key pair...")
|
||||||
|
sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
|
||||||
|
if err != nil {
|
||||||
|
return ident, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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.IDFromPubKey(pk)
|
||||||
|
if err != nil {
|
||||||
|
return ident, err
|
||||||
|
}
|
||||||
|
ident.PeerID = id.Pretty()
|
||||||
|
|
||||||
|
return ident, nil
|
||||||
|
}
|
||||||
|
|
||||||
func initCmd(c *commander.Command, inp []string) error {
|
func initCmd(c *commander.Command, inp []string) error {
|
||||||
configpath, err := getConfigDir(c.Parent)
|
configpath, err := getConfigDir(c.Parent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -62,29 +128,11 @@ func initCmd(c *commander.Command, inp []string) error {
|
|||||||
}
|
}
|
||||||
cfg := new(config.Config)
|
cfg := new(config.Config)
|
||||||
|
|
||||||
cfg.Datastore = config.Datastore{}
|
// setup the datastore
|
||||||
if len(dspath) == 0 {
|
cfg.Datastore, err = datastoreConfig(dspath)
|
||||||
dspath, err = config.DataStorePath("")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
cfg.Datastore.Path = dspath
|
|
||||||
cfg.Datastore.Type = "leveldb"
|
|
||||||
|
|
||||||
// Construct the data store if missing
|
|
||||||
if err := os.MkdirAll(dspath, os.ModePerm); err != nil {
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg.Identity = config.Identity{}
|
|
||||||
|
|
||||||
// setup the node addresses.
|
// setup the node addresses.
|
||||||
cfg.Addresses = config.Addresses{
|
cfg.Addresses = config.Addresses{
|
||||||
@ -106,42 +154,47 @@ func initCmd(c *commander.Command, inp []string) error {
|
|||||||
return errors.New("Bitsize less than 1024 is considered unsafe.")
|
return errors.New("Bitsize less than 1024 is considered unsafe.")
|
||||||
}
|
}
|
||||||
|
|
||||||
u.POut("generating key pair\n")
|
cfg.Identity, err = identityConfig(nbits)
|
||||||
sk, pk, err := ci.GenerateKeyPair(ci.RSA, nbits)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// currently storing key unencrypted. in the future we need to encrypt it.
|
|
||||||
// TODO(security)
|
|
||||||
skbytes, err := sk.Bytes()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
cfg.Identity.PrivKey = base64.StdEncoding.EncodeToString(skbytes)
|
|
||||||
|
|
||||||
id, err := peer.IDFromPubKey(pk)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
cfg.Identity.PeerID = id.Pretty()
|
|
||||||
u.POut("peer identity: %s\n", id.Pretty())
|
|
||||||
|
|
||||||
// Use these hardcoded bootstrap peers for now.
|
// Use these hardcoded bootstrap peers for now.
|
||||||
cfg.Bootstrap = []*config.BootstrapPeer{
|
cfg.Bootstrap = defaultPeers
|
||||||
&config.BootstrapPeer{
|
|
||||||
// mars.i.ipfs.io
|
|
||||||
PeerID: "QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
|
|
||||||
Address: "/ip4/104.131.131.82/tcp/4001",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// tracking ipfs version used to generate the init folder and adding update checker default setting.
|
// tracking ipfs version used to generate the init folder
|
||||||
cfg.Version = config.VersionDefaultValue()
|
// and adding update checker default setting.
|
||||||
|
cfg.Version = config.Version{
|
||||||
|
Check: "error",
|
||||||
|
Current: updates.Version,
|
||||||
|
}
|
||||||
|
|
||||||
err = config.WriteConfigFile(filename, cfg)
|
err = config.WriteConfigFile(filename, cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nd, err := core.NewIpfsNode(cfg, false)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer nd.Close()
|
||||||
|
|
||||||
|
// Set up default file
|
||||||
|
msg := `Hello and Welcome to IPFS!
|
||||||
|
If you're seeing this, that means that you have successfully
|
||||||
|
installed ipfs and are now interfacing with the wonderful
|
||||||
|
world of DAGs and hashes!
|
||||||
|
`
|
||||||
|
reader := bytes.NewBufferString(msg)
|
||||||
|
|
||||||
|
defnd, err := imp.BuildDagFromReader(reader, nd.DAG, nd.Pinning.GetManual(), chunk.DefaultSplitter)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
k, _ := defnd.Key()
|
||||||
|
fmt.Printf("Default file key: %s\n", k)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -32,6 +32,7 @@ type Pinner interface {
|
|||||||
Pin(*mdag.Node, bool) error
|
Pin(*mdag.Node, bool) error
|
||||||
Unpin(util.Key, bool) error
|
Unpin(util.Key, bool) error
|
||||||
Flush() error
|
Flush() error
|
||||||
|
GetManual() ManualPinner
|
||||||
}
|
}
|
||||||
|
|
||||||
// ManualPinner is for manually editing the pin structure
|
// ManualPinner is for manually editing the pin structure
|
||||||
@ -263,3 +264,7 @@ func (p *pinner) PinWithMode(k util.Key, mode PinMode) {
|
|||||||
p.indirPin.Increment(k)
|
p.indirPin.Increment(k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *pinner) GetManual() ManualPinner {
|
||||||
|
return p
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user