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
mf
go-multiaddr
Commits
984c8ac5
Commit
984c8ac5
authored
Oct 16, 2018
by
vyzo
Committed by
Steven Allen
Oct 18, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
private networks and IsPublicAddr
parent
2503d999
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
73 additions
and
0 deletions
+73
-0
private.go
private.go
+73
-0
No files found.
private.go
0 → 100644
View file @
984c8ac5
package
manet
import
(
"net"
ma
"github.com/multiformats/go-multiaddr"
)
// Private4 and Private6 are well-known private networks
// These are exported to allow overriding for testing
var
Private4
,
Private6
[]
*
net
.
IPNet
var
privateCIDR4
=
[]
string
{
// localhost
"127.0.0.0/8"
,
// private networks
"10.0.0.0/8"
,
"100.64.0.0/10"
,
"172.16.0.0/12"
,
"192.168.0.0/16"
,
// link local
"169.254.0.0/16"
,
}
var
privateCIDR6
=
[]
string
{
// localhost
"::1/128"
,
// ULA reserved
"fc00::/7"
,
// link local
"fe80::/10"
,
}
func
init
()
{
Private4
=
parsePrivateCIDR
(
privateCIDR4
)
Private6
=
parsePrivateCIDR
(
privateCIDR6
)
}
func
parsePrivateCIDR
(
cidrs
[]
string
)
[]
*
net
.
IPNet
{
ipnets
:=
make
([]
*
net
.
IPNet
,
len
(
cidrs
))
for
i
,
cidr
:=
range
cidrs
{
_
,
ipnet
,
err
:=
net
.
ParseCIDR
(
cidr
)
if
err
!=
nil
{
panic
(
err
)
}
ipnets
[
i
]
=
ipnet
}
return
ipnets
}
// IsPublicAddr retruns true if the IP part of the multiaddr is not in a private network
func
IsPublicAddr
(
a
ma
.
Multiaddr
)
bool
{
ip
,
err
:=
a
.
ValueForProtocol
(
ma
.
P_IP4
)
if
err
==
nil
{
return
!
inAddrRange
(
ip
,
Private4
)
}
ip
,
err
=
a
.
ValueForProtocol
(
ma
.
P_IP6
)
if
err
==
nil
{
return
!
inAddrRange
(
ip
,
Private6
)
}
return
false
}
func
inAddrRange
(
s
string
,
ipnets
[]
*
net
.
IPNet
)
bool
{
ip
:=
net
.
ParseIP
(
s
)
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