Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-p2p
Commits
6a14d8c2
Unverified
Commit
6a14d8c2
authored
Mar 01, 2021
by
Will
Committed by
GitHub
Mar 01, 2021
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1062 from libp2p/fix/autonatrace
fix autonat race
parents
bbde01b3
c2c59184
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
17 additions
and
5 deletions
+17
-5
config/config.go
config/config.go
+3
-1
p2p/host/basic/basic_host.go
p2p/host/basic/basic_host.go
+13
-3
p2p/host/basic/basic_host_test.go
p2p/host/basic/basic_host_test.go
+1
-1
No files found.
config/config.go
View file @
6a14d8c2
...
@@ -335,10 +335,12 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
...
@@ -335,10 +335,12 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
autonatOpts
=
append
(
autonatOpts
,
autonat
.
WithReachability
(
*
cfg
.
AutoNATConfig
.
ForceReachability
))
autonatOpts
=
append
(
autonatOpts
,
autonat
.
WithReachability
(
*
cfg
.
AutoNATConfig
.
ForceReachability
))
}
}
if
h
.
AutoNat
,
err
=
autonat
.
New
(
ctx
,
h
,
autonatOpts
...
);
err
!=
nil
{
autonat
,
err
:=
autonat
.
New
(
ctx
,
h
,
autonatOpts
...
)
if
err
!=
nil
{
h
.
Close
()
h
.
Close
()
return
nil
,
fmt
.
Errorf
(
"cannot enable autorelay; autonat failed to start: %v"
,
err
)
return
nil
,
fmt
.
Errorf
(
"cannot enable autorelay; autonat failed to start: %v"
,
err
)
}
}
h
.
SetAutoNat
(
autonat
)
// start the host background tasks
// start the host background tasks
h
.
Start
()
h
.
Start
()
...
...
p2p/host/basic/basic_host.go
View file @
6a14d8c2
...
@@ -112,7 +112,7 @@ type BasicHost struct {
...
@@ -112,7 +112,7 @@ type BasicHost struct {
signKey
crypto
.
PrivKey
signKey
crypto
.
PrivKey
caBook
peerstore
.
CertifiedAddrBook
caBook
peerstore
.
CertifiedAddrBook
A
utoNat
autonat
.
AutoNAT
a
utoNat
autonat
.
AutoNAT
}
}
var
_
host
.
Host
=
(
*
BasicHost
)(
nil
)
var
_
host
.
Host
=
(
*
BasicHost
)(
nil
)
...
@@ -811,6 +811,7 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
...
@@ -811,6 +811,7 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
h
.
addrMu
.
RLock
()
h
.
addrMu
.
RLock
()
filteredIfaceAddrs
:=
h
.
filteredInterfaceAddrs
filteredIfaceAddrs
:=
h
.
filteredInterfaceAddrs
allIfaceAddrs
:=
h
.
allInterfaceAddrs
allIfaceAddrs
:=
h
.
allInterfaceAddrs
autonat
:=
h
.
autoNat
h
.
addrMu
.
RUnlock
()
h
.
addrMu
.
RUnlock
()
// Iterate over all _unresolved_ listen addresses, resolving our primary
// Iterate over all _unresolved_ listen addresses, resolving our primary
...
@@ -831,8 +832,8 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
...
@@ -831,8 +832,8 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
// but not have an external network card,
// but not have an external network card,
// so net.InterfaceAddrs() not has the public ip
// so net.InterfaceAddrs() not has the public ip
// The host can indeed be dialed !!!
// The host can indeed be dialed !!!
if
h
.
A
uto
N
at
!=
nil
{
if
a
uto
n
at
!=
nil
{
publicAddr
,
_
:=
h
.
A
uto
N
at
.
PublicAddr
()
publicAddr
,
_
:=
a
uto
n
at
.
PublicAddr
()
if
publicAddr
!=
nil
{
if
publicAddr
!=
nil
{
finalAddrs
=
append
(
finalAddrs
,
publicAddr
)
finalAddrs
=
append
(
finalAddrs
,
publicAddr
)
}
}
...
@@ -992,6 +993,15 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
...
@@ -992,6 +993,15 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {
return
dedupAddrs
(
finalAddrs
)
return
dedupAddrs
(
finalAddrs
)
}
}
// SetAutoNat sets the autonat service for the host.
func
(
h
*
BasicHost
)
SetAutoNat
(
a
autonat
.
AutoNAT
)
{
h
.
addrMu
.
Lock
()
defer
h
.
addrMu
.
Unlock
()
if
h
.
autoNat
==
nil
{
h
.
autoNat
=
a
}
}
// Close shuts down the Host's services (network, etc).
// Close shuts down the Host's services (network, etc).
func
(
h
*
BasicHost
)
Close
()
error
{
func
(
h
*
BasicHost
)
Close
()
error
{
h
.
closeSync
.
Do
(
func
()
{
h
.
closeSync
.
Do
(
func
()
{
...
...
p2p/host/basic/basic_host_test.go
View file @
6a14d8c2
...
@@ -203,7 +203,7 @@ func TestHostAddrsFactory(t *testing.T) {
...
@@ -203,7 +203,7 @@ func TestHostAddrsFactory(t *testing.T) {
}
}
var
err
error
var
err
error
h
.
A
utoNat
,
err
=
autonat
.
New
(
ctx
,
h
,
autonat
.
WithReachability
(
network
.
ReachabilityPublic
))
h
.
a
utoNat
,
err
=
autonat
.
New
(
ctx
,
h
,
autonat
.
WithReachability
(
network
.
ReachabilityPublic
))
if
err
!=
nil
{
if
err
!=
nil
{
t
.
Fatalf
(
"should be able to attach autonat: %v"
,
err
)
t
.
Fatalf
(
"should be able to attach autonat: %v"
,
err
)
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment