generation of dialer host when needed
This commit is contained in:
parent
1921bc20ae
commit
8037ce2dca
|
@ -13,6 +13,7 @@ import (
|
|||
"github.com/libp2p/go-libp2p-core/peerstore"
|
||||
"github.com/libp2p/go-libp2p-core/pnet"
|
||||
"github.com/libp2p/go-libp2p-core/routing"
|
||||
"github.com/libp2p/go-libp2p-peerstore/pstoremem"
|
||||
|
||||
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
|
||||
relay "github.com/libp2p/go-libp2p/p2p/host/relay"
|
||||
|
@ -77,14 +78,11 @@ type Config struct {
|
|||
Routing RoutingC
|
||||
|
||||
EnableAutoRelay bool
|
||||
AutoNATOpts []autonat.Option
|
||||
EnableAutoNAT bool
|
||||
StaticRelays []peer.AddrInfo
|
||||
}
|
||||
|
||||
// NewNode constructs a new libp2p Host from the Config.
|
||||
//
|
||||
// This function consumes the config. Do not reuse it (really!).
|
||||
func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
|
||||
func (cfg *Config) newHost(ctx context.Context, store peerstore.Peerstore) (*bhost.BasicHost, error) {
|
||||
// Check this early. Prevents us from even *starting* without verifying this.
|
||||
if pnet.ForcePrivateNetwork && len(cfg.PSK) == 0 {
|
||||
log.Error("tried to create a libp2p node with no Private" +
|
||||
|
@ -105,19 +103,15 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
if cfg.Peerstore == nil {
|
||||
return nil, fmt.Errorf("no peerstore specified")
|
||||
}
|
||||
|
||||
if err := cfg.Peerstore.AddPrivKey(pid, cfg.PeerKey); err != nil {
|
||||
if err := store.AddPrivKey(pid, cfg.PeerKey); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := cfg.Peerstore.AddPubKey(pid, cfg.PeerKey.GetPublic()); err != nil {
|
||||
if err := store.AddPubKey(pid, cfg.PeerKey.GetPublic()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO: Make the swarm implementation configurable.
|
||||
swrm := swarm.NewSwarm(ctx, pid, cfg.Peerstore, cfg.Reporter)
|
||||
swrm := swarm.NewSwarm(ctx, pid, store, cfg.Reporter)
|
||||
if cfg.Filters != nil {
|
||||
swrm.Filters = cfg.Filters
|
||||
}
|
||||
|
@ -185,6 +179,21 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
|
|||
return nil, err
|
||||
}
|
||||
}
|
||||
return h, nil
|
||||
}
|
||||
|
||||
// NewNode constructs a new libp2p Host from the Config.
|
||||
//
|
||||
// This function consumes the config. Do not reuse it (really!).
|
||||
func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
|
||||
if cfg.Peerstore == nil {
|
||||
return nil, fmt.Errorf("no peerstore specified")
|
||||
}
|
||||
|
||||
h, err := cfg.newHost(ctx, cfg.Peerstore)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO: This method succeeds if listening on one address succeeds. We
|
||||
// should probably fail if listening on *any* addr fails.
|
||||
|
@ -203,20 +212,13 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
|
|||
}
|
||||
}
|
||||
|
||||
if cfg.EnableAutoRelay || cfg.AutoNATOpts != nil {
|
||||
if _, err = autonat.New(ctx, h, cfg.AutoNATOpts...); err != nil {
|
||||
h.Close()
|
||||
return nil, fmt.Errorf("cannot enable autorelay; autonat failed to start: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
hop := false
|
||||
if cfg.EnableAutoRelay {
|
||||
if !cfg.Relay {
|
||||
h.Close()
|
||||
return nil, fmt.Errorf("cannot enable autorelay; relay is not enabled")
|
||||
}
|
||||
|
||||
hop := false
|
||||
for _, opt := range cfg.RelayOpts {
|
||||
if opt == circuit.OptHop {
|
||||
hop = true
|
||||
|
@ -225,7 +227,7 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
|
|||
}
|
||||
|
||||
if !hop && len(cfg.StaticRelays) > 0 {
|
||||
_ = relay.NewAutoRelay(swrm.Context(), h, nil, router, cfg.StaticRelays)
|
||||
_ = relay.NewAutoRelay(ctx, h, nil, router, cfg.StaticRelays)
|
||||
} else {
|
||||
if router == nil {
|
||||
h.Close()
|
||||
|
@ -244,11 +246,30 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
|
|||
// advertise ourselves
|
||||
relay.Advertise(ctx, discovery)
|
||||
} else {
|
||||
_ = relay.NewAutoRelay(swrm.Context(), h, discovery, router, cfg.StaticRelays)
|
||||
_ = relay.NewAutoRelay(ctx, h, discovery, router, cfg.StaticRelays)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.EnableAutoRelay || cfg.EnableAutoNAT {
|
||||
var autonatOpts []autonat.Option
|
||||
if hop {
|
||||
dialerStore := pstoremem.NewPeerstore()
|
||||
dialerHost, err := cfg.newHost(ctx, dialerStore)
|
||||
if err != nil {
|
||||
h.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
autonatOpts = append(autonatOpts, autonat.EnableService(dialerHost.Network(), false))
|
||||
}
|
||||
|
||||
if _, err = autonat.New(ctx, h, autonatOpts...); err != nil {
|
||||
h.Close()
|
||||
return nil, fmt.Errorf("cannot enable autorelay; autonat failed to start: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// start the host background tasks
|
||||
h.Start()
|
||||
|
||||
|
|
2
go.mod
2
go.mod
|
@ -10,7 +10,7 @@ require (
|
|||
github.com/jbenet/goprocess v0.1.3
|
||||
github.com/libp2p/go-conn-security-multistream v0.1.0
|
||||
github.com/libp2p/go-eventbus v0.1.0
|
||||
github.com/libp2p/go-libp2p-autonat v0.1.1
|
||||
github.com/libp2p/go-libp2p-autonat v0.1.2-0.20200316221154-f1cd6ba0fd67
|
||||
github.com/libp2p/go-libp2p-blankhost v0.1.4
|
||||
github.com/libp2p/go-libp2p-circuit v0.1.4
|
||||
github.com/libp2p/go-libp2p-core v0.5.0
|
||||
|
|
8
go.sum
8
go.sum
|
@ -148,8 +148,11 @@ github.com/libp2p/go-flow-metrics v0.0.1 h1:0gxuFd2GuK7IIP5pKljLwps6TvcuYgvG7Atq
|
|||
github.com/libp2p/go-flow-metrics v0.0.1/go.mod h1:Iv1GH0sG8DtYN3SVJ2eG221wMiNpZxBdp967ls1g+k8=
|
||||
github.com/libp2p/go-flow-metrics v0.0.3 h1:8tAs/hSdNvUiLgtlSy3mxwxWP4I9y/jlkPFT7epKdeM=
|
||||
github.com/libp2p/go-flow-metrics v0.0.3/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs=
|
||||
github.com/libp2p/go-libp2p v0.6.0/go.mod h1:mfKWI7Soz3ABX+XEBR61lGbg+ewyMtJHVt043oWeqwg=
|
||||
github.com/libp2p/go-libp2p-autonat v0.1.1 h1:WLBZcIRsjZlWdAZj9CiBSvU2wQXoUOiS1Zk1tM7DTJI=
|
||||
github.com/libp2p/go-libp2p-autonat v0.1.1/go.mod h1:OXqkeGOY2xJVWKAGV2inNF5aKN/djNA3fdpCWloIudE=
|
||||
github.com/libp2p/go-libp2p-autonat v0.1.2-0.20200316221154-f1cd6ba0fd67 h1:3UM/mimkSSSQiosUHiauy7w/7JwAxMgEY4StoeMHR4A=
|
||||
github.com/libp2p/go-libp2p-autonat v0.1.2-0.20200316221154-f1cd6ba0fd67/go.mod h1:yoF5B6yi+Wjxci55xdLlJo7gfyHwG80df4B9hiO2A88=
|
||||
github.com/libp2p/go-libp2p-blankhost v0.1.1/go.mod h1:pf2fvdLJPsC1FsVrNP3DUUvMzUts2dsLLBEpo1vW1ro=
|
||||
github.com/libp2p/go-libp2p-blankhost v0.1.4 h1:I96SWjR4rK9irDHcHq3XHN6hawCRTPUADzkJacgZLvk=
|
||||
github.com/libp2p/go-libp2p-blankhost v0.1.4/go.mod h1:oJF0saYsAXQCSfDq254GMNmLNz6ZTHTOvtF4ZydUvwU=
|
||||
|
@ -262,6 +265,7 @@ github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNx
|
|||
github.com/mattn/go-isatty v0.0.5 h1:tHXDdz1cpzGaovsTB+TVB8q90WEokoVmfMqoVcrLUgw=
|
||||
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
|
||||
github.com/miekg/dns v1.1.12/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
|
||||
github.com/miekg/dns v1.1.28 h1:gQhy5bsJa8zTlVI8lywCTZp1lguor+xevFoYlzeCTQY=
|
||||
github.com/miekg/dns v1.1.28/go.mod h1:KNUDUusw/aVsxyTYZM1oqvCicbwhgbNgztCETuNZ7xM=
|
||||
github.com/minio/blake2b-simd v0.0.0-20160723061019-3f5f724cb5b1 h1:lYpkrQH5ajf0OXOcUbGjvZxxijuBwbbmlSxLiuofa+g=
|
||||
|
@ -424,6 +428,8 @@ golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443 h1:IcSOAf4PyMp3U3XbIEj1/x
|
|||
golang.org/x/crypto v0.0.0-20190618222545-ea8f1a30c443/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 h1:ObdrDkeb4kJdCP557AjRjq69pTHfNouLtWZG7j9rPN8=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d h1:1ZiEyfaQIg3Qh0EoqpwAakHVhecoE5wlSg5GjnafJGw=
|
||||
golang.org/x/crypto v0.0.0-20200221231518-2aa609cf4a9d/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
||||
|
@ -465,6 +471,8 @@ golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e h1:N7DeIrjYszNmSW409R3frPPwg
|
|||
golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5 h1:LfCXLvNmTYH9kEmVgqbnsWfruoXZIrh4YBgqVHtDvw0=
|
||||
golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
|
||||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
|
||||
|
|
|
@ -278,6 +278,15 @@ func DefaultStaticRelays() Option {
|
|||
}
|
||||
}
|
||||
|
||||
// EnableAutoNAT configures libp2p to monitor its NAT status and track when
|
||||
// the local host is externally reachable.
|
||||
func EnableAutoNAT() Option {
|
||||
return func(cfg *Config) error {
|
||||
cfg.EnableAutoNAT = true
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// FilterAddresses configures libp2p to never dial nor accept connections from
|
||||
// the given addresses. FilterAddresses should be used for cases where the
|
||||
// addresses you want to deny are known ahead of time.
|
||||
|
|
Loading…
Reference in New Issue