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-kad-dht
Commits
278c3ea7
Unverified
Commit
278c3ea7
authored
Apr 20, 2020
by
Will Scott
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
stricter definition of public for DHT
parent
7c7c46d3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
5 deletions
+50
-5
dht_filters.go
dht_filters.go
+50
-5
No files found.
dht_filters.go
View file @
278c3ea7
...
...
@@ -23,6 +23,41 @@ type QueryFilterFunc func(dht *IpfsDHT, ai peer.AddrInfo) bool
// the local route table.
type
RouteTableFilterFunc
func
(
dht
*
IpfsDHT
,
conns
[]
network
.
Conn
)
bool
var
publicCIDR6
=
"2000::/3"
var
public6
*
net
.
IPNet
func
init
()
{
_
,
public6
,
_
=
net
.
ParseCIDR
(
publicCIDR6
)
}
// isPublicAddr follows the logic of manet.IsPublicAddr, except it uses
// a stricter definition of "public" for ipv6: namely "is it in 2000::/3"?
func
isPublicAddr
(
a
ma
.
Multiaddr
)
bool
{
ip
,
err
:=
manet
.
ToIP
(
a
)
if
err
!=
nil
{
return
false
}
if
ip
.
To4
()
!=
nil
{
return
!
inAddrRange
(
ip
,
manet
.
Private4
)
&&
!
inAddrRange
(
ip
,
manet
.
Unroutable4
)
}
return
public6
.
Contains
(
ip
)
}
// isPrivateAddr follows the logic of manet.IsPrivateAddr, except that
// it uses a stricter definition of "public" for ipv6
func
isPrivateAddr
(
a
ma
.
Multiaddr
)
bool
{
ip
,
err
:=
manet
.
ToIP
(
a
)
if
err
!=
nil
{
return
false
}
if
ip
.
To4
()
!=
nil
{
return
inAddrRange
(
ip
,
manet
.
Private4
)
}
return
!
public6
.
Contains
(
ip
)
&&
!
inAddrRange
(
ip
,
manet
.
Unroutable6
)
}
// PublicQueryFilter returns true if the peer is suspected of being publicly accessible
func
PublicQueryFilter
(
_
*
IpfsDHT
,
ai
peer
.
AddrInfo
)
bool
{
if
len
(
ai
.
Addrs
)
==
0
{
...
...
@@ -31,7 +66,7 @@ func PublicQueryFilter(_ *IpfsDHT, ai peer.AddrInfo) bool {
var
hasPublicAddr
bool
for
_
,
a
:=
range
ai
.
Addrs
{
if
!
isRelayAddr
(
a
)
&&
manet
.
I
sPublicAddr
(
a
)
{
if
!
isRelayAddr
(
a
)
&&
i
sPublicAddr
(
a
)
{
hasPublicAddr
=
true
}
}
...
...
@@ -51,7 +86,7 @@ func PublicRoutingTableFilter(dht *IpfsDHT, conns []network.Conn) bool {
id
:=
conns
[
0
]
.
RemotePeer
()
known
:=
dht
.
peerstore
.
PeerInfo
(
id
)
for
_
,
a
:=
range
known
.
Addrs
{
if
!
isRelayAddr
(
a
)
&&
manet
.
I
sPublicAddr
(
a
)
{
if
!
isRelayAddr
(
a
)
&&
i
sPublicAddr
(
a
)
{
return
true
}
}
...
...
@@ -106,7 +141,7 @@ func PrivateRoutingTableFilter(dht *IpfsDHT, conns []network.Conn) bool {
router
:=
getCachedRouter
()
myAdvertisedIPs
:=
make
([]
net
.
IP
,
0
)
for
_
,
a
:=
range
dht
.
Host
()
.
Addrs
()
{
if
manet
.
I
sPublicAddr
(
a
)
&&
!
isRelayAddr
(
a
)
{
if
i
sPublicAddr
(
a
)
&&
!
isRelayAddr
(
a
)
{
ip
,
err
:=
manet
.
ToIP
(
a
)
if
err
!=
nil
{
continue
...
...
@@ -117,11 +152,11 @@ func PrivateRoutingTableFilter(dht *IpfsDHT, conns []network.Conn) bool {
for
_
,
c
:=
range
conns
{
ra
:=
c
.
RemoteMultiaddr
()
if
manet
.
I
sPrivateAddr
(
ra
)
&&
!
isRelayAddr
(
ra
)
{
if
i
sPrivateAddr
(
ra
)
&&
!
isRelayAddr
(
ra
)
{
return
true
}
if
manet
.
I
sPublicAddr
(
ra
)
{
if
i
sPublicAddr
(
ra
)
{
ip
,
err
:=
manet
.
ToIP
(
ra
)
if
err
!=
nil
{
continue
...
...
@@ -173,3 +208,13 @@ func isRelayAddr(a ma.Multiaddr) bool {
})
return
found
}
func
inAddrRange
(
ip
net
.
IP
,
ipnets
[]
*
net
.
IPNet
)
bool
{
for
_
,
ipnet
:=
range
ipnets
{
if
ipnet
.
Contains
(
ip
)
{
return
true
}
}
return
false
}
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