mirror of
https://github.com/ipfs/kubo.git
synced 2025-06-26 07:28:20 +08:00
fix panic caused by accessing config after repo closed
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
This commit is contained in:
@ -204,7 +204,13 @@ func daemonFunc(req cmds.Request, res cmds.Response) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if routingOption == routingOptionSupernodeKwd {
|
if routingOption == routingOptionSupernodeKwd {
|
||||||
servers, err := repo.Config().SupernodeRouting.ServerIPFSAddrs()
|
rcfg, err := repo.Config()
|
||||||
|
if err != nil {
|
||||||
|
res.SetError(err, cmds.ErrNormal)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
servers, err := rcfg.SupernodeRouting.ServerIPFSAddrs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
res.SetError(err, cmds.ErrNormal)
|
res.SetError(err, cmds.ErrNormal)
|
||||||
repo.Close() // because ownership hasn't been transferred to the node
|
repo.Close() // because ownership hasn't been transferred to the node
|
||||||
|
@ -192,7 +192,7 @@ func cmdCtx(node *core.IpfsNode, repoPath string) commands.Context {
|
|||||||
Online: true,
|
Online: true,
|
||||||
ConfigRoot: repoPath,
|
ConfigRoot: repoPath,
|
||||||
LoadConfig: func(path string) (*config.Config, error) {
|
LoadConfig: func(path string) (*config.Config, error) {
|
||||||
return node.Repo.Config(), nil
|
return node.Repo.Config()
|
||||||
},
|
},
|
||||||
ConstructNode: func() (*core.IpfsNode, error) {
|
ConstructNode: func() (*core.IpfsNode, error) {
|
||||||
return node, nil
|
return node, nil
|
||||||
|
@ -135,7 +135,11 @@ func setupNode(ctx context.Context, n *IpfsNode, cfg *BuildCfg) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Online {
|
if cfg.Online {
|
||||||
do := setupDiscoveryOption(n.Repo.Config().Discovery)
|
rcfg, err := n.Repo.Config()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
do := setupDiscoveryOption(rcfg.Discovery)
|
||||||
if err := n.startOnlineServices(ctx, cfg.Routing, cfg.Host, do); err != nil {
|
if err := n.startOnlineServices(ctx, cfg.Routing, cfg.Host, do); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,11 @@ in the bootstrap list).
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer r.Close()
|
defer r.Close()
|
||||||
cfg := r.Config()
|
cfg, err := r.Config()
|
||||||
|
if err != nil {
|
||||||
|
res.SetError(err, cmds.ErrNormal)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
deflt, _, err := req.Option("default").Bool()
|
deflt, _, err := req.Option("default").Bool()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -148,7 +152,11 @@ var bootstrapRemoveCmd = &cmds.Command{
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer r.Close()
|
defer r.Close()
|
||||||
cfg := r.Config()
|
cfg, err := r.Config()
|
||||||
|
if err != nil {
|
||||||
|
res.SetError(err, cmds.ErrNormal)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
all, _, err := req.Option("all").Bool()
|
all, _, err := req.Option("all").Bool()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -197,7 +205,11 @@ var bootstrapListCmd = &cmds.Command{
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer r.Close()
|
defer r.Close()
|
||||||
cfg := r.Config()
|
cfg, err := r.Config()
|
||||||
|
if err != nil {
|
||||||
|
res.SetError(err, cmds.ErrNormal)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
peers, err := cfg.BootstrapPeers()
|
peers, err := cfg.BootstrapPeers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
30
core/core.go
30
core/core.go
@ -136,7 +136,10 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
|
|||||||
n.Reporter = metrics.NewBandwidthCounter()
|
n.Reporter = metrics.NewBandwidthCounter()
|
||||||
|
|
||||||
// get undialable addrs from config
|
// get undialable addrs from config
|
||||||
cfg := n.Repo.Config()
|
cfg, err := n.Repo.Config()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
var addrfilter []*net.IPNet
|
var addrfilter []*net.IPNet
|
||||||
for _, s := range cfg.Swarm.AddrFilters {
|
for _, s := range cfg.Swarm.AddrFilters {
|
||||||
f, err := mamask.NewMask(s)
|
f, err := mamask.NewMask(s)
|
||||||
@ -156,7 +159,7 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Ok, now we're ready to listen.
|
// Ok, now we're ready to listen.
|
||||||
if err := startListening(ctx, n.PeerHost, n.Repo.Config()); err != nil {
|
if err := startListening(ctx, n.PeerHost, cfg); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -325,7 +328,7 @@ func (n *IpfsNode) Bootstrap(cfg BootstrapConfig) error {
|
|||||||
cfg.BootstrapPeers = func() []peer.PeerInfo {
|
cfg.BootstrapPeers = func() []peer.PeerInfo {
|
||||||
ps, err := n.loadBootstrapPeers()
|
ps, err := n.loadBootstrapPeers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warningf("failed to parse bootstrap peers from config: %s", n.Repo.Config().Bootstrap)
|
log.Warning("failed to parse bootstrap peers from config")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return ps
|
return ps
|
||||||
@ -342,7 +345,12 @@ func (n *IpfsNode) loadID() error {
|
|||||||
return errors.New("identity already loaded")
|
return errors.New("identity already loaded")
|
||||||
}
|
}
|
||||||
|
|
||||||
cid := n.Repo.Config().Identity.PeerID
|
cfg, err := n.Repo.Config()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
cid := cfg.Identity.PeerID
|
||||||
if cid == "" {
|
if cid == "" {
|
||||||
return errors.New("Identity was not set in config (was ipfs init run?)")
|
return errors.New("Identity was not set in config (was ipfs init run?)")
|
||||||
}
|
}
|
||||||
@ -363,7 +371,12 @@ func (n *IpfsNode) LoadPrivateKey() error {
|
|||||||
return errors.New("private key already loaded")
|
return errors.New("private key already loaded")
|
||||||
}
|
}
|
||||||
|
|
||||||
sk, err := loadPrivateKey(&n.Repo.Config().Identity, n.Identity)
|
cfg, err := n.Repo.Config()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
sk, err := loadPrivateKey(&cfg.Identity, n.Identity)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -375,7 +388,12 @@ func (n *IpfsNode) LoadPrivateKey() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (n *IpfsNode) loadBootstrapPeers() ([]peer.PeerInfo, error) {
|
func (n *IpfsNode) loadBootstrapPeers() ([]peer.PeerInfo, error) {
|
||||||
parsed, err := n.Repo.Config().BootstrapPeers()
|
cfg, err := n.Repo.Config()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
parsed, err := cfg.BootstrapPeers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -107,8 +107,12 @@ func commandsOption(cctx commands.Context, command *commands.Command) ServeOptio
|
|||||||
AllowedMethods: []string{"GET", "POST", "PUT"},
|
AllowedMethods: []string{"GET", "POST", "PUT"},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
rcfg, err := n.Repo.Config()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
addHeadersFromConfig(cfg, n.Repo.Config())
|
addHeadersFromConfig(cfg, rcfg)
|
||||||
addCORSFromEnv(cfg)
|
addCORSFromEnv(cfg)
|
||||||
addCORSDefaults(cfg)
|
addCORSDefaults(cfg)
|
||||||
patchCORSVars(cfg, l.Addr())
|
patchCORSVars(cfg, l.Addr())
|
||||||
|
@ -30,7 +30,12 @@ func NewGateway(conf GatewayConfig) *Gateway {
|
|||||||
func (g *Gateway) ServeOption() ServeOption {
|
func (g *Gateway) ServeOption() ServeOption {
|
||||||
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
|
return func(n *core.IpfsNode, _ net.Listener, mux *http.ServeMux) (*http.ServeMux, error) {
|
||||||
// pass user's HTTP headers
|
// pass user's HTTP headers
|
||||||
g.Config.Headers = n.Repo.Config().Gateway.HTTPHeaders
|
cfg, err := n.Repo.Config()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
g.Config.Headers = cfg.Gateway.HTTPHeaders
|
||||||
|
|
||||||
gateway, err := newGatewayHandler(n, g.Config)
|
gateway, err := newGatewayHandler(n, g.Config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -11,7 +11,11 @@ import (
|
|||||||
|
|
||||||
// Mount mounts ipns at a given location, and returns a mount.Mount instance.
|
// Mount mounts ipns at a given location, and returns a mount.Mount instance.
|
||||||
func Mount(ipfs *core.IpfsNode, ipnsmp, ipfsmp string) (mount.Mount, error) {
|
func Mount(ipfs *core.IpfsNode, ipnsmp, ipfsmp string) (mount.Mount, error) {
|
||||||
cfg := ipfs.Repo.Config()
|
cfg, err := ipfs.Repo.Config()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
allow_other := cfg.Mounts.FuseAllowOther
|
allow_other := cfg.Mounts.FuseAllowOther
|
||||||
|
|
||||||
if ipfs.IpnsFs == nil {
|
if ipfs.IpnsFs == nil {
|
||||||
|
@ -10,7 +10,10 @@ import (
|
|||||||
|
|
||||||
// Mount mounts ipfs at a given location, and returns a mount.Mount instance.
|
// Mount mounts ipfs at a given location, and returns a mount.Mount instance.
|
||||||
func Mount(ipfs *core.IpfsNode, mountpoint string) (mount.Mount, error) {
|
func Mount(ipfs *core.IpfsNode, mountpoint string) (mount.Mount, error) {
|
||||||
cfg := ipfs.Repo.Config()
|
cfg, err := ipfs.Repo.Config()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
allow_other := cfg.Mounts.FuseAllowOther
|
allow_other := cfg.Mounts.FuseAllowOther
|
||||||
fsys := NewFileSystem(ipfs)
|
fsys := NewFileSystem(ipfs)
|
||||||
return mount.NewMount(ipfs.Process(), fsys, mountpoint, allow_other)
|
return mount.NewMount(ipfs.Process(), fsys, mountpoint, allow_other)
|
||||||
|
@ -445,11 +445,8 @@ func (r *FSRepo) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Config returns the FSRepo's config. This method must not be called if the
|
|
||||||
// repo is not open.
|
|
||||||
//
|
|
||||||
// Result when not Open is undefined. The method may panic if it pleases.
|
// Result when not Open is undefined. The method may panic if it pleases.
|
||||||
func (r *FSRepo) Config() *config.Config {
|
func (r *FSRepo) Config() (*config.Config, error) {
|
||||||
|
|
||||||
// It is not necessary to hold the package lock since the repo is in an
|
// It is not necessary to hold the package lock since the repo is in an
|
||||||
// opened state. The package lock is _not_ meant to ensure that the repo is
|
// opened state. The package lock is _not_ meant to ensure that the repo is
|
||||||
@ -460,9 +457,9 @@ func (r *FSRepo) Config() *config.Config {
|
|||||||
defer packageLock.Unlock()
|
defer packageLock.Unlock()
|
||||||
|
|
||||||
if r.closed {
|
if r.closed {
|
||||||
panic("repo is closed")
|
return nil, errors.New("cannot access config, repo not open")
|
||||||
}
|
}
|
||||||
return r.config
|
return r.config, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// setConfigUnsynced is for private use.
|
// setConfigUnsynced is for private use.
|
||||||
|
@ -15,8 +15,8 @@ type Mock struct {
|
|||||||
D ds.ThreadSafeDatastore
|
D ds.ThreadSafeDatastore
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Mock) Config() *config.Config {
|
func (m *Mock) Config() (*config.Config, error) {
|
||||||
return &m.C // FIXME threadsafety
|
return &m.C, nil // FIXME threadsafety
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Mock) SetConfig(updated *config.Config) error {
|
func (m *Mock) SetConfig(updated *config.Config) error {
|
||||||
|
@ -14,7 +14,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Repo interface {
|
type Repo interface {
|
||||||
Config() *config.Config
|
Config() (*config.Config, error)
|
||||||
SetConfig(*config.Config) error
|
SetConfig(*config.Config) error
|
||||||
|
|
||||||
SetConfigKey(key string, value interface{}) error
|
SetConfigKey(key string, value interface{}) error
|
||||||
|
@ -70,7 +70,11 @@ func run() error {
|
|||||||
if err != nil { // owned by node
|
if err != nil { // owned by node
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
cfg := repo.Config()
|
cfg, err := repo.Config()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
cfg.Bootstrap = servers
|
cfg.Bootstrap = servers
|
||||||
if err := repo.SetConfig(cfg); err != nil {
|
if err := repo.SetConfig(cfg); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -236,7 +240,7 @@ func cmdCtx(node *core.IpfsNode, repoPath string) commands.Context {
|
|||||||
Online: true,
|
Online: true,
|
||||||
ConfigRoot: repoPath,
|
ConfigRoot: repoPath,
|
||||||
LoadConfig: func(path string) (*config.Config, error) {
|
LoadConfig: func(path string) (*config.Config, error) {
|
||||||
return node.Repo.Config(), nil
|
return node.Repo.Config()
|
||||||
},
|
},
|
||||||
ConstructNode: func() (*core.IpfsNode, error) {
|
ConstructNode: func() (*core.IpfsNode, error) {
|
||||||
return node, nil
|
return node, nil
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
package util
|
|
||||||
|
|
||||||
// FIXME: we need the go-random/random utility for our sharness test wich depends on go-humanize
|
|
||||||
// Godep will drop it if we dont use it in ipfs. There should be a better way to do this.
|
|
||||||
import _ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/dustin/go-humanize"
|
|
||||||
|
|
||||||
// similar to the above, only used in the tests makefile
|
|
||||||
import _ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/whyrusleeping/iptb"
|
|
||||||
|
|
||||||
import _ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/chriscool/go-sleep"
|
|
||||||
|
|
||||||
// imported by chegga/pb on windows, this is here so running godeps on non-windows doesnt
|
|
||||||
// drop it from our vendoring
|
|
||||||
import _ "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/olekukonko/ts"
|
|
Reference in New Issue
Block a user