mirror of
https://github.com/ipfs/kubo.git
synced 2025-07-01 19:24:14 +08:00
fix fuse mounting issues
this time, without loading the private key on every startup
This commit is contained in:
@ -11,6 +11,7 @@ import (
|
|||||||
core "github.com/jbenet/go-ipfs/core"
|
core "github.com/jbenet/go-ipfs/core"
|
||||||
corecmds "github.com/jbenet/go-ipfs/core/commands"
|
corecmds "github.com/jbenet/go-ipfs/core/commands"
|
||||||
coreunix "github.com/jbenet/go-ipfs/core/coreunix"
|
coreunix "github.com/jbenet/go-ipfs/core/coreunix"
|
||||||
|
ipns "github.com/jbenet/go-ipfs/fuse/ipns"
|
||||||
ci "github.com/jbenet/go-ipfs/p2p/crypto"
|
ci "github.com/jbenet/go-ipfs/p2p/crypto"
|
||||||
peer "github.com/jbenet/go-ipfs/p2p/peer"
|
peer "github.com/jbenet/go-ipfs/p2p/peer"
|
||||||
repo "github.com/jbenet/go-ipfs/repo"
|
repo "github.com/jbenet/go-ipfs/repo"
|
||||||
@ -110,6 +111,11 @@ func doInit(repoRoot string, force bool, nBitsForKeypair int) (interface{}, erro
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = initializeIpnsKeyspace(repoRoot)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,6 +144,29 @@ func addTheWelcomeFile(repoRoot string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func initializeIpnsKeyspace(repoRoot string) error {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
r := fsrepo.At(repoRoot)
|
||||||
|
if err := r.Open(); err != nil { // NB: repo is owned by the node
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
nd, err := core.NewIPFSNode(ctx, core.Offline(r))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer nd.Close()
|
||||||
|
|
||||||
|
err = nd.SetupOfflineRouting()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return ipns.InitializeKeyspace(nd, nd.PrivateKey)
|
||||||
|
}
|
||||||
|
|
||||||
func datastoreConfig() (*config.Datastore, error) {
|
func datastoreConfig() (*config.Datastore, error) {
|
||||||
dspath, err := config.DataStorePath("")
|
dspath, err := config.DataStorePath("")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
14
core/core.go
14
core/core.go
@ -34,9 +34,10 @@ import (
|
|||||||
config "github.com/jbenet/go-ipfs/repo/config"
|
config "github.com/jbenet/go-ipfs/repo/config"
|
||||||
routing "github.com/jbenet/go-ipfs/routing"
|
routing "github.com/jbenet/go-ipfs/routing"
|
||||||
dht "github.com/jbenet/go-ipfs/routing/dht"
|
dht "github.com/jbenet/go-ipfs/routing/dht"
|
||||||
|
offroute "github.com/jbenet/go-ipfs/routing/offline"
|
||||||
|
eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
|
||||||
util "github.com/jbenet/go-ipfs/util"
|
util "github.com/jbenet/go-ipfs/util"
|
||||||
debugerror "github.com/jbenet/go-ipfs/util/debugerror"
|
debugerror "github.com/jbenet/go-ipfs/util/debugerror"
|
||||||
eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
|
|
||||||
lgbl "github.com/jbenet/go-ipfs/util/eventlog/loggables"
|
lgbl "github.com/jbenet/go-ipfs/util/eventlog/loggables"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -350,6 +351,17 @@ func (n *IpfsNode) loadPrivateKey() error {
|
|||||||
|
|
||||||
n.PrivateKey = sk
|
n.PrivateKey = sk
|
||||||
n.Peerstore.AddPrivKey(n.Identity, n.PrivateKey)
|
n.Peerstore.AddPrivKey(n.Identity, n.PrivateKey)
|
||||||
|
n.Peerstore.AddPubKey(n.Identity, sk.GetPublic())
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *IpfsNode) SetupOfflineRouting() error {
|
||||||
|
err := n.loadPrivateKey()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
n.Routing = offroute.NewOfflineRouter(n.Repo.Datastore(), n.PrivateKey)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@ import (
|
|||||||
core "github.com/jbenet/go-ipfs/core"
|
core "github.com/jbenet/go-ipfs/core"
|
||||||
chunk "github.com/jbenet/go-ipfs/importer/chunk"
|
chunk "github.com/jbenet/go-ipfs/importer/chunk"
|
||||||
mdag "github.com/jbenet/go-ipfs/merkledag"
|
mdag "github.com/jbenet/go-ipfs/merkledag"
|
||||||
|
nsys "github.com/jbenet/go-ipfs/namesys"
|
||||||
ci "github.com/jbenet/go-ipfs/p2p/crypto"
|
ci "github.com/jbenet/go-ipfs/p2p/crypto"
|
||||||
ft "github.com/jbenet/go-ipfs/unixfs"
|
ft "github.com/jbenet/go-ipfs/unixfs"
|
||||||
uio "github.com/jbenet/go-ipfs/unixfs/io"
|
uio "github.com/jbenet/go-ipfs/unixfs/io"
|
||||||
@ -32,6 +33,22 @@ var (
|
|||||||
longRepublishTimeout = time.Millisecond * 500
|
longRepublishTimeout = time.Millisecond * 500
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func InitializeKeyspace(n *core.IpfsNode, key ci.PrivKey) error {
|
||||||
|
emptyDir := &mdag.Node{Data: ft.FolderPBData()}
|
||||||
|
k, err := n.DAG.Add(emptyDir)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
pub := nsys.NewRoutingPublisher(n.Routing)
|
||||||
|
err = pub.Publish(key, k.B58String())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// FileSystem is the readwrite IPNS Fuse Filesystem.
|
// FileSystem is the readwrite IPNS Fuse Filesystem.
|
||||||
type FileSystem struct {
|
type FileSystem struct {
|
||||||
Ipfs *core.IpfsNode
|
Ipfs *core.IpfsNode
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
ci "github.com/jbenet/go-ipfs/p2p/crypto"
|
||||||
host "github.com/jbenet/go-ipfs/p2p/host"
|
host "github.com/jbenet/go-ipfs/p2p/host"
|
||||||
peer "github.com/jbenet/go-ipfs/p2p/peer"
|
peer "github.com/jbenet/go-ipfs/p2p/peer"
|
||||||
protocol "github.com/jbenet/go-ipfs/p2p/protocol"
|
protocol "github.com/jbenet/go-ipfs/p2p/protocol"
|
||||||
@ -234,9 +235,23 @@ func (dht *IpfsDHT) getLocal(key u.Key) ([]byte, error) {
|
|||||||
return rec.GetValue(), nil
|
return rec.GetValue(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (dht *IpfsDHT) getOwnPrivateKey() (ci.PrivKey, error) {
|
||||||
|
sk := dht.peerstore.PrivKey(dht.self)
|
||||||
|
if sk == nil {
|
||||||
|
log.Errorf("%s dht cannot get own private key!", dht.self)
|
||||||
|
return nil, fmt.Errorf("cannot get private key to sign record!")
|
||||||
|
}
|
||||||
|
return sk, nil
|
||||||
|
}
|
||||||
|
|
||||||
// putLocal stores the key value pair in the datastore
|
// putLocal stores the key value pair in the datastore
|
||||||
func (dht *IpfsDHT) putLocal(key u.Key, value []byte) error {
|
func (dht *IpfsDHT) putLocal(key u.Key, value []byte) error {
|
||||||
rec, err := dht.makePutRecord(key, value)
|
sk, err := dht.getOwnPrivateKey()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rec, err := MakePutRecord(sk, key, value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -98,7 +98,13 @@ func TestGetFailures(t *testing.T) {
|
|||||||
{
|
{
|
||||||
typ := pb.Message_GET_VALUE
|
typ := pb.Message_GET_VALUE
|
||||||
str := "hello"
|
str := "hello"
|
||||||
rec, err := d.makePutRecord(u.Key(str), []byte("blah"))
|
|
||||||
|
sk, err := d.getOwnPrivateKey()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
rec, err := MakePutRecord(sk, u.Key(str), []byte("blah"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -43,20 +43,20 @@ func RecordBlobForSig(r *pb.Record) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// creates and signs a dht record for the given key/value pair
|
// creates and signs a dht record for the given key/value pair
|
||||||
func (dht *IpfsDHT) makePutRecord(key u.Key, value []byte) (*pb.Record, error) {
|
func MakePutRecord(sk ci.PrivKey, key u.Key, value []byte) (*pb.Record, error) {
|
||||||
record := new(pb.Record)
|
record := new(pb.Record)
|
||||||
|
|
||||||
record.Key = proto.String(string(key))
|
record.Key = proto.String(string(key))
|
||||||
record.Value = value
|
record.Value = value
|
||||||
record.Author = proto.String(string(dht.self))
|
|
||||||
blob := RecordBlobForSig(record)
|
|
||||||
|
|
||||||
sk := dht.peerstore.PrivKey(dht.self)
|
pkh, err := sk.GetPublic().Hash()
|
||||||
if sk == nil {
|
if err != nil {
|
||||||
log.Errorf("%s dht cannot get own private key!", dht.self)
|
return nil, err
|
||||||
return nil, fmt.Errorf("cannot get private key to sign record!")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
record.Author = proto.String(string(pkh))
|
||||||
|
blob := RecordBlobForSig(record)
|
||||||
|
|
||||||
sig, err := sk.Sign(blob)
|
sig, err := sk.Sign(blob)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -36,7 +36,12 @@ func (dht *IpfsDHT) PutValue(ctx context.Context, key u.Key, value []byte) error
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
rec, err := dht.makePutRecord(key, value)
|
sk, err := dht.getOwnPrivateKey()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
rec, err := MakePutRecord(sk, key, value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Creation of record failed!")
|
log.Error("Creation of record failed!")
|
||||||
return err
|
return err
|
||||||
@ -75,6 +80,8 @@ func (dht *IpfsDHT) GetValue(ctx context.Context, key u.Key) ([]byte, error) {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
log.Debug("have it locally")
|
log.Debug("have it locally")
|
||||||
return val, nil
|
return val, nil
|
||||||
|
} else {
|
||||||
|
log.Debug("failed to get value locally: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// get closest peers in the routing table
|
// get closest peers in the routing table
|
||||||
|
89
routing/offline/offline.go
Normal file
89
routing/offline/offline.go
Normal file
@ -0,0 +1,89 @@
|
|||||||
|
package offline
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
|
||||||
|
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
|
||||||
|
ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
|
||||||
|
ci "github.com/jbenet/go-ipfs/p2p/crypto"
|
||||||
|
"github.com/jbenet/go-ipfs/p2p/peer"
|
||||||
|
routing "github.com/jbenet/go-ipfs/routing"
|
||||||
|
dht "github.com/jbenet/go-ipfs/routing/dht"
|
||||||
|
pb "github.com/jbenet/go-ipfs/routing/dht/pb"
|
||||||
|
eventlog "github.com/jbenet/go-ipfs/thirdparty/eventlog"
|
||||||
|
u "github.com/jbenet/go-ipfs/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
var log = eventlog.Logger("offlinerouting")
|
||||||
|
|
||||||
|
var ErrOffline = errors.New("routing system in offline mode")
|
||||||
|
|
||||||
|
func NewOfflineRouter(dstore ds.Datastore, privkey ci.PrivKey) routing.IpfsRouting {
|
||||||
|
return &offlineRouting{
|
||||||
|
datastore: dstore,
|
||||||
|
sk: privkey,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type offlineRouting struct {
|
||||||
|
datastore ds.Datastore
|
||||||
|
sk ci.PrivKey
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *offlineRouting) PutValue(ctx context.Context, key u.Key, val []byte) error {
|
||||||
|
rec, err := dht.MakePutRecord(c.sk, key, val)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
data, err := proto.Marshal(rec)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.datastore.Put(key.DsKey(), data)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *offlineRouting) GetValue(ctx context.Context, key u.Key) ([]byte, error) {
|
||||||
|
v, err := c.datastore.Get(key.DsKey())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
byt, ok := v.([]byte)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("value stored in datastore not []byte")
|
||||||
|
}
|
||||||
|
rec := new(pb.Record)
|
||||||
|
err = proto.Unmarshal(byt, rec)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return rec.GetValue(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *offlineRouting) FindProviders(ctx context.Context, key u.Key) ([]peer.PeerInfo, error) {
|
||||||
|
return nil, ErrOffline
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *offlineRouting) FindPeer(ctx context.Context, pid peer.ID) (peer.PeerInfo, error) {
|
||||||
|
return peer.PeerInfo{}, ErrOffline
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *offlineRouting) FindProvidersAsync(ctx context.Context, k u.Key, max int) <-chan peer.PeerInfo {
|
||||||
|
out := make(chan peer.PeerInfo)
|
||||||
|
close(out)
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *offlineRouting) Provide(_ context.Context, key u.Key) error {
|
||||||
|
return ErrOffline
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *offlineRouting) Ping(ctx context.Context, p peer.ID) (time.Duration, error) {
|
||||||
|
return 0, ErrOffline
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ routing.IpfsRouting = &offlineRouting{}
|
Reference in New Issue
Block a user