mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-26 23:53:19 +08:00
@ -28,7 +28,6 @@ var initCmd = &cmds.Command{
|
|||||||
|
|
||||||
Options: []cmds.Option{
|
Options: []cmds.Option{
|
||||||
cmds.IntOption("bits", "b", "Number of bits to use in the generated RSA private key (defaults to 4096)"),
|
cmds.IntOption("bits", "b", "Number of bits to use in the generated RSA private key (defaults to 4096)"),
|
||||||
cmds.StringOption("passphrase", "p", "Passphrase for encrypting the private key"),
|
|
||||||
cmds.BoolOption("force", "f", "Overwrite existing config (if it exists)"),
|
cmds.BoolOption("force", "f", "Overwrite existing config (if it exists)"),
|
||||||
|
|
||||||
// TODO need to decide whether to expose the override as a file or a
|
// TODO need to decide whether to expose the override as a file or a
|
||||||
@ -49,6 +48,7 @@ var initCmd = &cmds.Command{
|
|||||||
res.SetError(err, cmds.ErrNormal)
|
res.SetError(err, cmds.ErrNormal)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bitsOptFound {
|
if !bitsOptFound {
|
||||||
nBitsForKeypair = nBitsForKeypairDefault
|
nBitsForKeypair = nBitsForKeypairDefault
|
||||||
}
|
}
|
||||||
@ -75,28 +75,26 @@ func initWithDefaults(out io.Writer, repoRoot string) error {
|
|||||||
return debugerror.Wrap(err)
|
return debugerror.Wrap(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func writef(out io.Writer, format string, ifs ...interface{}) error {
|
|
||||||
_, err := out.Write([]byte(fmt.Sprintf(format, ifs...)))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func doInit(out io.Writer, repoRoot string, force bool, nBitsForKeypair int) error {
|
func doInit(out io.Writer, repoRoot string, force bool, nBitsForKeypair int) error {
|
||||||
if err := writef(out, "initializing ipfs node at %s\n", repoRoot); err != nil {
|
if _, err := fmt.Fprintf(out, "initializing ipfs node at %s\n", repoRoot); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if fsrepo.IsInitialized(repoRoot) && !force {
|
if fsrepo.IsInitialized(repoRoot) && !force {
|
||||||
return errRepoExists
|
return errRepoExists
|
||||||
}
|
}
|
||||||
|
|
||||||
conf, err := config.Init(out, nBitsForKeypair)
|
conf, err := config.Init(out, nBitsForKeypair)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if fsrepo.IsInitialized(repoRoot) {
|
if fsrepo.IsInitialized(repoRoot) {
|
||||||
if err := fsrepo.Remove(repoRoot); err != nil {
|
if err := fsrepo.Remove(repoRoot); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := fsrepo.Init(repoRoot, conf); err != nil {
|
if err := fsrepo.Init(repoRoot, conf); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -111,10 +109,12 @@ func doInit(out io.Writer, repoRoot string, force bool, nBitsForKeypair int) err
|
|||||||
func addDefaultAssets(out io.Writer, repoRoot string) error {
|
func addDefaultAssets(out io.Writer, repoRoot string) error {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
r := fsrepo.At(repoRoot)
|
r := fsrepo.At(repoRoot)
|
||||||
if err := r.Open(); err != nil { // NB: repo is owned by the node
|
if err := r.Open(); err != nil { // NB: repo is owned by the node
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
nd, err := core.NewIPFSNode(ctx, core.Offline(r))
|
nd, err := core.NewIPFSNode(ctx, core.Offline(r))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -130,6 +130,7 @@ func addDefaultAssets(out io.Writer, repoRoot string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
k := u.B58KeyDecode(s)
|
k := u.B58KeyDecode(s)
|
||||||
if err := dirb.AddChild(fname, k); err != nil {
|
if err := dirb.AddChild(fname, k); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -141,15 +142,21 @@ func addDefaultAssets(out io.Writer, repoRoot string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := nd.Pinning.Pin(dir, true); err != nil {
|
if err := nd.Pinning.Pin(dir, true); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := nd.Pinning.Flush(); err != nil {
|
if err := nd.Pinning.Flush(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
writef(out, "to get started, enter:\n")
|
if _, err = fmt.Fprintf(out, "to get started, enter:\n"); err != nil {
|
||||||
return writef(out, "\n\tipfs cat /ipfs/%s/readme\n\n", dkey)
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = fmt.Fprintf(out, "\n\tipfs cat /ipfs/%s/readme\n\n", dkey)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func initializeIpnsKeyspace(repoRoot string) error {
|
func initializeIpnsKeyspace(repoRoot string) error {
|
||||||
|
@ -17,6 +17,16 @@ type UpdateOutput struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var UpdateCmd = &cmds.Command{
|
var UpdateCmd = &cmds.Command{
|
||||||
|
Helptext: cmds.HelpText{
|
||||||
|
Tagline: "Downloads and installs updates for IPFS (disabled)",
|
||||||
|
ShortDescription: `ipfs update is disabled until we can deploy the binaries to you over ipfs itself.
|
||||||
|
|
||||||
|
please use 'go get -u github.com/jbenet/go-ipfs/cmd/ipfs' until then.`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: unexported until we can deploy the binaries over ipfs
|
||||||
|
var updateCmd = &cmds.Command{
|
||||||
Helptext: cmds.HelpText{
|
Helptext: cmds.HelpText{
|
||||||
Tagline: "Downloads and installs updates for IPFS",
|
Tagline: "Downloads and installs updates for IPFS",
|
||||||
ShortDescription: "ipfs update is a utility command used to check for updates and apply them.",
|
ShortDescription: "ipfs update is a utility command used to check for updates and apply them.",
|
||||||
|
@ -1,52 +0,0 @@
|
|||||||
package core
|
|
||||||
|
|
||||||
import (
|
|
||||||
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
|
||||||
fsds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/fs"
|
|
||||||
ktds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/keytransform"
|
|
||||||
lds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/leveldb"
|
|
||||||
syncds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
|
|
||||||
ldbopts "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/syndtr/goleveldb/leveldb/opt"
|
|
||||||
|
|
||||||
config "github.com/jbenet/go-ipfs/repo/config"
|
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
|
||||||
ds2 "github.com/jbenet/go-ipfs/util/datastore2"
|
|
||||||
"github.com/jbenet/go-ipfs/util/debugerror"
|
|
||||||
)
|
|
||||||
|
|
||||||
func makeDatastore(cfg config.Datastore) (ds2.ThreadSafeDatastoreCloser, error) {
|
|
||||||
if len(cfg.Type) == 0 {
|
|
||||||
return nil, debugerror.Errorf("config datastore.type required")
|
|
||||||
}
|
|
||||||
|
|
||||||
switch cfg.Type {
|
|
||||||
case "leveldb":
|
|
||||||
return makeLevelDBDatastore(cfg)
|
|
||||||
|
|
||||||
case "memory":
|
|
||||||
return ds2.CloserWrap(syncds.MutexWrap(ds.NewMapDatastore())), nil
|
|
||||||
|
|
||||||
case "fs":
|
|
||||||
log.Warning("using fs.Datastore at .datastore for testing.")
|
|
||||||
d, err := fsds.NewDatastore(".datastore") // for testing!!
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
ktd := ktds.Wrap(d, u.B58KeyConverter)
|
|
||||||
return ds2.CloserWrap(syncds.MutexWrap(ktd)), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, debugerror.Errorf("Unknown datastore type: %s", cfg.Type)
|
|
||||||
}
|
|
||||||
|
|
||||||
func makeLevelDBDatastore(cfg config.Datastore) (ds2.ThreadSafeDatastoreCloser, error) {
|
|
||||||
if len(cfg.Path) == 0 {
|
|
||||||
return nil, debugerror.Errorf("config datastore.path required for leveldb")
|
|
||||||
}
|
|
||||||
|
|
||||||
ds, err := lds.NewDatastore(cfg.Path, &lds.Options{
|
|
||||||
// TODO don't import ldbopts. Get from go-datastore.leveldb
|
|
||||||
Compression: ldbopts.NoCompression,
|
|
||||||
})
|
|
||||||
return ds, debugerror.Wrap(err)
|
|
||||||
}
|
|
@ -1,78 +0,0 @@
|
|||||||
// package http implements an http server that serves static content from ipfs
|
|
||||||
package http
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
mux "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/gorilla/mux"
|
|
||||||
ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
|
|
||||||
manet "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr-net"
|
|
||||||
mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
|
|
||||||
|
|
||||||
core "github.com/jbenet/go-ipfs/core"
|
|
||||||
)
|
|
||||||
|
|
||||||
type handler struct {
|
|
||||||
ipfs
|
|
||||||
}
|
|
||||||
|
|
||||||
// Serve starts the http server
|
|
||||||
func Serve(address ma.Multiaddr, node *core.IpfsNode) error {
|
|
||||||
r := mux.NewRouter()
|
|
||||||
handler := &handler{&ipfsHandler{node}}
|
|
||||||
|
|
||||||
r.HandleFunc("/ipfs/", handler.postHandler).Methods("POST")
|
|
||||||
r.PathPrefix("/ipfs/").Handler(handler).Methods("GET")
|
|
||||||
|
|
||||||
http.Handle("/", r)
|
|
||||||
|
|
||||||
_, host, err := manet.DialArgs(address)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return http.ListenAndServe(host, nil)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
||||||
path := r.URL.Path[5:]
|
|
||||||
|
|
||||||
nd, err := i.ResolvePath(path)
|
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
dr, err := i.NewDagReader(nd)
|
|
||||||
if err != nil {
|
|
||||||
// TODO: return json object containing the tree data if it's a directory (err == ErrIsDir)
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
io.Copy(w, dr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *handler) postHandler(w http.ResponseWriter, r *http.Request) {
|
|
||||||
nd, err := i.NewDagFromReader(r.Body)
|
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
k, err := i.AddNodeToDAG(nd)
|
|
||||||
if err != nil {
|
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO: return json representation of list instead
|
|
||||||
w.WriteHeader(http.StatusCreated)
|
|
||||||
w.Write([]byte(mh.Multihash(k).B58String()))
|
|
||||||
}
|
|
@ -1,108 +0,0 @@
|
|||||||
package http
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
dag "github.com/jbenet/go-ipfs/merkledag"
|
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
|
||||||
)
|
|
||||||
|
|
||||||
type test struct {
|
|
||||||
url string
|
|
||||||
code int
|
|
||||||
reqbody string
|
|
||||||
respbody string
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestServeHTTP(t *testing.T) {
|
|
||||||
testhandler := &handler{&testIpfsHandler{}}
|
|
||||||
tests := []test{
|
|
||||||
{"/ipfs/", http.StatusInternalServerError, "", ""},
|
|
||||||
{"/ipfs/hash", http.StatusOK, "", "some fine data"},
|
|
||||||
{"/ipfs/hash2", http.StatusInternalServerError, "", ""},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
req, _ := http.NewRequest("GET", test.url, nil)
|
|
||||||
resp := httptest.NewRecorder()
|
|
||||||
testhandler.ServeHTTP(resp, req)
|
|
||||||
|
|
||||||
if resp.Code != test.code {
|
|
||||||
t.Error("expected status code", test.code, "received", resp.Code)
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp.Body.String() != test.respbody {
|
|
||||||
t.Error("expected body:", test.respbody)
|
|
||||||
t.Error("received body:", resp.Body)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPostHandler(t *testing.T) {
|
|
||||||
testhandler := &handler{&testIpfsHandler{}}
|
|
||||||
tests := []test{
|
|
||||||
{"/ifps/", http.StatusInternalServerError, "", ""},
|
|
||||||
{"/ipfs/", http.StatusInternalServerError, "something that causes an error in adding to DAG", ""},
|
|
||||||
{"/ipfs/", http.StatusCreated, "some fine data", "jSQBpNSebeYbPBjs1vp"},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range tests {
|
|
||||||
req, _ := http.NewRequest("POST", test.url, strings.NewReader(test.reqbody))
|
|
||||||
resp := httptest.NewRecorder()
|
|
||||||
testhandler.postHandler(resp, req)
|
|
||||||
|
|
||||||
if resp.Code != test.code {
|
|
||||||
t.Error("expected status code", test.code, "received", resp.Code)
|
|
||||||
}
|
|
||||||
|
|
||||||
if resp.Body.String() != test.respbody {
|
|
||||||
t.Error("expected body:", test.respbody)
|
|
||||||
t.Error("received body:", resp.Body)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type testIpfsHandler struct{}
|
|
||||||
|
|
||||||
func (i *testIpfsHandler) ResolvePath(path string) (*dag.Node, error) {
|
|
||||||
if path == "/hash" {
|
|
||||||
return &dag.Node{Data: []byte("some fine data")}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if path == "/hash2" {
|
|
||||||
return &dag.Node{Data: []byte("data that breaks dagreader")}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, errors.New("")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *testIpfsHandler) NewDagFromReader(r io.Reader) (*dag.Node, error) {
|
|
||||||
if data, err := ioutil.ReadAll(r); err == nil {
|
|
||||||
return &dag.Node{Data: data}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, errors.New("")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *testIpfsHandler) AddNodeToDAG(nd *dag.Node) (u.Key, error) {
|
|
||||||
if len(nd.Data) != 0 && string(nd.Data) != "something that causes an error in adding to DAG" {
|
|
||||||
return u.Key(nd.Data), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return "", errors.New("")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *testIpfsHandler) NewDagReader(nd *dag.Node) (io.Reader, error) {
|
|
||||||
if string(nd.Data) != "data that breaks dagreader" {
|
|
||||||
return bytes.NewReader(nd.Data), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, errors.New("")
|
|
||||||
}
|
|
@ -1,42 +0,0 @@
|
|||||||
package http
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
|
|
||||||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
|
|
||||||
core "github.com/jbenet/go-ipfs/core"
|
|
||||||
"github.com/jbenet/go-ipfs/importer"
|
|
||||||
chunk "github.com/jbenet/go-ipfs/importer/chunk"
|
|
||||||
dag "github.com/jbenet/go-ipfs/merkledag"
|
|
||||||
path "github.com/jbenet/go-ipfs/path"
|
|
||||||
uio "github.com/jbenet/go-ipfs/unixfs/io"
|
|
||||||
u "github.com/jbenet/go-ipfs/util"
|
|
||||||
)
|
|
||||||
|
|
||||||
type ipfs interface {
|
|
||||||
ResolvePath(string) (*dag.Node, error)
|
|
||||||
NewDagFromReader(io.Reader) (*dag.Node, error)
|
|
||||||
AddNodeToDAG(nd *dag.Node) (u.Key, error)
|
|
||||||
NewDagReader(nd *dag.Node) (io.Reader, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type ipfsHandler struct {
|
|
||||||
node *core.IpfsNode
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *ipfsHandler) ResolvePath(fpath string) (*dag.Node, error) {
|
|
||||||
return i.node.Resolver.ResolvePath(path.Path(fpath))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *ipfsHandler) NewDagFromReader(r io.Reader) (*dag.Node, error) {
|
|
||||||
return importer.BuildDagFromReader(
|
|
||||||
r, i.node.DAG, i.node.Pinning.GetManual(), chunk.DefaultSplitter)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *ipfsHandler) AddNodeToDAG(nd *dag.Node) (u.Key, error) {
|
|
||||||
return i.node.DAG.Add(nd)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *ipfsHandler) NewDagReader(nd *dag.Node) (io.Reader, error) {
|
|
||||||
return uio.NewDagReader(context.TODO(), nd, i.node.DAG)
|
|
||||||
}
|
|
Reference in New Issue
Block a user