Skip to content

Commit

Permalink
use the base host for autonat
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Jan 9, 2022
1 parent 68afe0f commit be0e28c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 50 deletions.
24 changes: 14 additions & 10 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import (
"fmt"
"time"

"github.com/libp2p/go-libp2p/p2p/host/autonat"
"github.com/libp2p/go-libp2p/p2p/host/autorelay"
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
"github.com/libp2p/go-libp2p/p2p/host/blank"
routed "github.com/libp2p/go-libp2p/p2p/host/routed"
circuitv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client"
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"

"github.com/libp2p/go-libp2p-core/connmgr"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/host"
Expand All @@ -18,15 +27,6 @@ import (
"github.com/libp2p/go-libp2p-core/transport"
"github.com/libp2p/go-libp2p-peerstore/pstoremem"

"github.com/libp2p/go-libp2p/p2p/host/autonat"
"github.com/libp2p/go-libp2p/p2p/host/autorelay"
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
routed "github.com/libp2p/go-libp2p/p2p/host/routed"
circuitv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client"
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"

blankhost "github.com/libp2p/go-libp2p-blankhost"
discovery "github.com/libp2p/go-libp2p-discovery"
swarm "github.com/libp2p/go-libp2p-swarm"
tptu "github.com/libp2p/go-libp2p-transport-upgrader"
Expand Down Expand Up @@ -333,7 +333,11 @@ func (cfg *Config) NewNode() (host.Host, error) {
h.Close()
return nil, err
}
dialerHost := blankhost.NewBlankHost(dialer)
dialerHost, err := blank.NewHost(dialer)
if err != nil {
h.Close()
return nil, err
}
if err := autoNatCfg.addTransports(dialerHost); err != nil {
dialerHost.Close()
h.Close()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/libp2p/go-conn-security-multistream v0.3.0
github.com/libp2p/go-eventbus v0.2.1
github.com/libp2p/go-libp2p-asn-util v0.1.0
github.com/libp2p/go-libp2p-blankhost v0.3.0
github.com/libp2p/go-libp2p-blankhost v0.3.0 // indirect
github.com/libp2p/go-libp2p-circuit v0.4.1-0.20220104091935-28fb8d25f785
github.com/libp2p/go-libp2p-core v0.13.1-0.20220104083644-a3dd401efe36
github.com/libp2p/go-libp2p-discovery v0.6.0
Expand Down
24 changes: 11 additions & 13 deletions p2p/host/autonat/autonat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"time"

pb "github.com/libp2p/go-libp2p/p2p/host/autonat/pb"
"github.com/libp2p/go-libp2p/p2p/host/blank"

"github.com/libp2p/go-libp2p-core/event"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"

bhost "github.com/libp2p/go-libp2p-blankhost"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
"github.com/libp2p/go-msgio/protoio"
ma "github.com/multiformats/go-multiaddr"
Expand All @@ -21,7 +21,8 @@ import (

// these are mock service implementations for testing
func makeAutoNATServicePrivate(t *testing.T) host.Host {
h := bhost.NewBlankHost(swarmt.GenSwarm(t))
h, err := blank.NewHost(swarmt.GenSwarm(t))
require.NoError(t, err)
h.SetStreamHandler(AutoNATProto, sayPrivateStreamHandler(t))
return h
}
Expand All @@ -44,7 +45,8 @@ func sayPrivateStreamHandler(t *testing.T) network.StreamHandler {
}

func makeAutoNATServicePublic(t *testing.T) host.Host {
h := bhost.NewBlankHost(swarmt.GenSwarm(t))
h, err := blank.NewHost(swarmt.GenSwarm(t))
require.NoError(t, err)
h.SetStreamHandler(AutoNATProto, func(s network.Stream) {
defer s.Close()
r := protoio.NewDelimitedReader(s, network.MessageSizeMax)
Expand All @@ -63,7 +65,8 @@ func makeAutoNATServicePublic(t *testing.T) host.Host {
}

func makeAutoNAT(t *testing.T, ash host.Host) (host.Host, AutoNAT) {
h := bhost.NewBlankHost(swarmt.GenSwarm(t))
h, err := blank.NewHost(swarmt.GenSwarm(t))
require.NoError(t, err)
h.Peerstore().AddAddrs(ash.ID(), ash.Addrs(), time.Minute)
h.Peerstore().AddProtocols(ash.ID(), AutoNATProto)
a, _ := New(h, WithSchedule(100*time.Millisecond, time.Second), WithoutStartupDelay())
Expand Down Expand Up @@ -275,21 +278,16 @@ func TestAutoNATObservationRecording(t *testing.T) {
if an.Status() != network.ReachabilityPublic {
t.Fatalf("too-extreme private transition.")
}

}

func TestStaticNat(t *testing.T) {
_, cancel := context.WithCancel(context.Background())
defer cancel()

h := bhost.NewBlankHost(swarmt.GenSwarm(t))
h, err := blank.NewHost(swarmt.GenSwarm(t))
require.NoError(t, err)
defer h.Close()
s, _ := h.EventBus().Subscribe(&event.EvtLocalReachabilityChanged{})

s, _ := h.EventBus().Subscribe(&event.EvtLocalReachabilityChanged{})
nat, err := New(h, WithReachability(network.ReachabilityPrivate))
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
if nat.Status() != network.ReachabilityPrivate {
t.Fatalf("should be private")
}
Expand Down
32 changes: 16 additions & 16 deletions p2p/host/autonat/dialpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"net"
"testing"

blankhost "github.com/libp2p/go-libp2p-blankhost"
"github.com/libp2p/go-libp2p/p2p/host/blank"

"github.com/stretchr/testify/require"

"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/transport"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"
Expand Down Expand Up @@ -55,7 +58,9 @@ func TestSkipDial(t *testing.T) {
defer cancel()

s := swarmt.GenSwarm(t)
d := dialPolicy{host: blankhost.NewBlankHost(s)}
h, err := blank.NewHost(s)
require.NoError(t, err)
d := dialPolicy{host: h}
if d.skipDial(makeMA("/ip4/8.8.8.8")) != false {
t.Fatal("failed dialing a valid public addr")
}
Expand All @@ -69,10 +74,7 @@ func TestSkipDial(t *testing.T) {
}

s.AddTransport(&mockT{ctx, makeMA("/ip4/8.8.8.8")})
err := s.AddListenAddr(makeMA("/ip4/8.8.8.8"))
if err != nil {
t.Fatal(err)
}
require.NoError(t, s.AddListenAddr(makeMA("/ip4/8.8.8.8")))
if d.skipDial(makeMA("/ip4/8.8.8.8")) != true {
t.Fatal("failed dialing a valid host address")
}
Expand All @@ -83,7 +85,9 @@ func TestSkipPeer(t *testing.T) {
defer cancel()

s := swarmt.GenSwarm(t)
d := dialPolicy{host: blankhost.NewBlankHost(s)}
h, err := blank.NewHost(s)
require.NoError(t, err)
d := dialPolicy{host: h}
if d.skipPeer([]multiaddr.Multiaddr{makeMA("/ip4/8.8.8.8")}) != false {
t.Fatal("failed dialing a valid public addr")
}
Expand All @@ -95,10 +99,7 @@ func TestSkipPeer(t *testing.T) {
}

s.AddTransport(&mockT{ctx, makeMA("/ip4/8.8.8.8")})
err := s.AddListenAddr(makeMA("/ip4/8.8.8.8"))
if err != nil {
t.Fatal(err)
}
require.NoError(t, s.AddListenAddr(makeMA("/ip4/8.8.8.8")))

if d.skipPeer([]multiaddr.Multiaddr{makeMA("/ip4/8.8.8.8"), makeMA("/ip4/192.168.0.1")}) != true {
t.Fatal("succeeded dialing host address")
Expand All @@ -116,12 +117,11 @@ func TestSkipLocalPeer(t *testing.T) {
defer cancel()

s := swarmt.GenSwarm(t)
d := dialPolicy{host: blankhost.NewBlankHost(s)}
h, err := blank.NewHost(s)
require.NoError(t, err)
d := dialPolicy{host: h}
s.AddTransport(&mockT{ctx, makeMA("/ip4/192.168.0.1")})
err := s.AddListenAddr(makeMA("/ip4/192.168.0.1"))
if err != nil {
t.Fatal(err)
}
require.NoError(t, s.AddListenAddr(makeMA("/ip4/192.168.0.1")))

if d.skipPeer([]multiaddr.Multiaddr{makeMA("/ip4/8.8.8.8")}) != false {
t.Fatal("failed dialing a valid public addr")
Expand Down
23 changes: 13 additions & 10 deletions p2p/host/autonat/svc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@ import (
"testing"
"time"

"github.com/libp2p/go-libp2p/p2p/host/blank"

"github.com/libp2p/go-libp2p-core/event"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"

bhost "github.com/libp2p/go-libp2p-blankhost"
swarmt "github.com/libp2p/go-libp2p-swarm/testing"

ma "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require"
)

func makeAutoNATConfig(t *testing.T) *config {
h := bhost.NewBlankHost(swarmt.GenSwarm(t))
dh := bhost.NewBlankHost(swarmt.GenSwarm(t))
h, err := blank.NewHost(swarmt.GenSwarm(t))
require.NoError(t, err)
dh, err := blank.NewHost(swarmt.GenSwarm(t))
require.NoError(t, err)
c := config{host: h, dialer: dh.Network()}
_ = defaults(&c)
c.forceReachability = true
Expand All @@ -29,16 +32,14 @@ func makeAutoNATConfig(t *testing.T) *config {

func makeAutoNATService(t *testing.T, c *config) *autoNATService {
as, err := newAutoNATService(c)
if err != nil {
t.Fatal(err)
}
require.NoError(t, err)
as.Enable()

return as
}

func makeAutoNATClient(t *testing.T) (host.Host, Client) {
h := bhost.NewBlankHost(swarmt.GenSwarm(t))
h, err := blank.NewHost(swarmt.GenSwarm(t))
require.NoError(t, err)
cli := NewAutoNATClient(h, nil)
return h, cli
}
Expand Down Expand Up @@ -195,9 +196,11 @@ func TestAutoNATServiceRateLimitJitter(t *testing.T) {
}

func TestAutoNATServiceStartup(t *testing.T) {
h := bhost.NewBlankHost(swarmt.GenSwarm(t))
h, err := blank.NewHost(swarmt.GenSwarm(t))
require.NoError(t, err)
defer h.Close()
dh := bhost.NewBlankHost(swarmt.GenSwarm(t))
dh, err := blank.NewHost(swarmt.GenSwarm(t))
require.NoError(t, err)
defer dh.Close()
an, err := New(h, EnableService(dh.Network()))
an.(*AmbientAutoNAT).config.dialPolicy.allowSelfDials = true
Expand Down

0 comments on commit be0e28c

Please sign in to comment.