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-swarm
Commits
3feb6126
Unverified
Commit
3feb6126
authored
Feb 08, 2021
by
Steven Allen
Committed by
GitHub
Feb 08, 2021
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #237 from libp2p/prevent-self-dial
prevent dialing addresses that we're listening on
parents
95888d69
88c64b72
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
1 deletion
+32
-1
swarm_dial.go
swarm_dial.go
+1
-1
swarm_test.go
swarm_test.go
+31
-0
No files found.
swarm_dial.go
View file @
3feb6126
...
...
@@ -440,7 +440,7 @@ func (s *Swarm) filterKnownUndialables(p peer.ID, addrs []ma.Multiaddr) []ma.Mul
for
_
,
addr
:=
range
lisAddrs
{
protos
:=
addr
.
Protocols
()
// we're only sure about filtering out /ip4 and /ip6 addresses, so far
if
len
(
protos
)
==
2
&&
(
protos
[
0
]
.
Code
==
ma
.
P_IP4
||
protos
[
0
]
.
Code
==
ma
.
P_IP6
)
{
if
protos
[
0
]
.
Code
==
ma
.
P_IP4
||
protos
[
0
]
.
Code
==
ma
.
P_IP6
{
ourAddrs
=
append
(
ourAddrs
,
addr
)
}
}
...
...
swarm_test.go
View file @
3feb6126
...
...
@@ -3,8 +3,10 @@ package swarm_test
import
(
"bytes"
"context"
"errors"
"fmt"
"io"
"strings"
"sync"
"testing"
"time"
...
...
@@ -20,6 +22,7 @@ import (
logging
"github.com/ipfs/go-log"
ma
"github.com/multiformats/go-multiaddr"
manet
"github.com/multiformats/go-multiaddr/net"
"github.com/stretchr/testify/require"
)
...
...
@@ -457,3 +460,31 @@ func TestCloseWithOpenStreams(t *testing.T) {
t
.
Fatal
(
err
)
}
}
func
TestPreventDialListenAddr
(
t
*
testing
.
T
)
{
s
:=
GenSwarm
(
t
,
context
.
Background
(),
OptDialOnly
)
if
err
:=
s
.
Listen
(
ma
.
StringCast
(
"/ip4/0.0.0.0/udp/0/quic"
));
err
!=
nil
{
t
.
Fatal
(
err
)
}
addrs
,
err
:=
s
.
InterfaceListenAddresses
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
var
addr
ma
.
Multiaddr
for
_
,
a
:=
range
addrs
{
_
,
s
,
err
:=
manet
.
DialArgs
(
a
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
strings
.
Split
(
s
,
":"
)[
0
]
==
"127.0.0.1"
{
addr
=
a
break
}
}
remote
:=
peer
.
ID
(
"foobar"
)
s
.
Peerstore
()
.
AddAddr
(
remote
,
addr
,
time
.
Hour
)
_
,
err
=
s
.
DialPeer
(
context
.
Background
(),
remote
)
if
!
errors
.
Is
(
err
,
ErrNoGoodAddresses
)
{
t
.
Fatal
(
"expected dial to fail: %w"
,
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