Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
adam.huang
go-libp2p
Commits
f6c38c9d
Commit
f6c38c9d
authored
Oct 18, 2018
by
vyzo
Browse files
filter unspecific relay address
parent
c6d1eebf
Changes
1
Show whitespace changes
Inline
Side-by-side
p2p/host/relay/autorelay.go
View file @
f6c38c9d
...
...
@@ -10,6 +10,7 @@ import (
basic
"github.com/libp2p/go-libp2p/p2p/host/basic"
autonat
"github.com/libp2p/go-libp2p-autonat"
_
"github.com/libp2p/go-libp2p-circuit"
discovery
"github.com/libp2p/go-libp2p-discovery"
host
"github.com/libp2p/go-libp2p-host"
inet
"github.com/libp2p/go-libp2p-net"
...
...
@@ -23,8 +24,18 @@ var (
DesiredRelays
=
3
BootDelay
=
60
*
time
.
Second
unspecificRelay
ma
.
Multiaddr
)
func
init
()
{
var
err
error
unspecificRelay
,
err
=
ma
.
NewMultiaddr
(
"/p2p-circuit"
)
if
err
!=
nil
{
panic
(
err
)
}
}
// AutoRelayHost is a Host that uses relays for connectivity when a NAT is detected.
type
AutoRelayHost
struct
{
*
basic
.
BasicHost
...
...
@@ -61,7 +72,7 @@ func (h *AutoRelayHost) hostAddrs(addrs []ma.Multiaddr) []ma.Multiaddr {
if
h
.
addrs
!=
nil
&&
h
.
autonat
.
Status
()
==
autonat
.
NATStatusPrivate
{
return
h
.
addrs
}
else
{
return
h
.
addrsF
(
addrs
)
return
filterUnspecificRelay
(
h
.
addrsF
(
addrs
)
)
}
}
...
...
@@ -164,7 +175,7 @@ func (h *AutoRelayHost) doUpdateAddrs() {
h
.
mx
.
Lock
()
defer
h
.
mx
.
Unlock
()
addrs
:=
h
.
addrsF
(
h
.
AllAddrs
())
addrs
:=
filterUnspecificRelay
(
h
.
addrsF
(
h
.
AllAddrs
())
)
raddrs
:=
make
([]
ma
.
Multiaddr
,
0
,
len
(
addrs
)
+
len
(
h
.
relays
))
// remove our public addresses from the list and replace them by just the public IP
...
...
@@ -216,6 +227,17 @@ func (h *AutoRelayHost) doUpdateAddrs() {
h
.
addrs
=
raddrs
}
func
filterUnspecificRelay
(
addrs
[]
ma
.
Multiaddr
)
[]
ma
.
Multiaddr
{
res
:=
make
([]
ma
.
Multiaddr
,
0
,
len
(
addrs
))
for
_
,
addr
:=
range
addrs
{
if
addr
.
Equal
(
unspecificRelay
)
{
continue
}
res
=
append
(
res
,
addr
)
}
return
res
}
func
shuffleRelays
(
pis
[]
pstore
.
PeerInfo
)
{
for
i
:=
range
pis
{
j
:=
rand
.
Intn
(
i
+
1
)
...
...
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