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
2945cfc2
Commit
2945cfc2
authored
Oct 16, 2018
by
vyzo
Committed by
Steven Allen
Oct 18, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add IsPrivateAddr
parent
49e7bdea
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
23 additions
and
0 deletions
+23
-0
private.go
private.go
+15
-0
private_test.go
private_test.go
+8
-0
No files found.
private.go
View file @
2945cfc2
...
...
@@ -61,6 +61,21 @@ func IsPublicAddr(a ma.Multiaddr) bool {
return
false
}
// IsPrivateAddr returns true if the IP part of the mutiadr is in a private network
func
IsPrivateAddr
(
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
{
...
...
private_test.go
View file @
2945cfc2
...
...
@@ -16,6 +16,10 @@ func TestIsPublicAddr(t *testing.T) {
t
.
Fatal
(
"192.168.1.1 is not a public address!"
)
}
if
!
IsPrivateAddr
(
a
)
{
t
.
Fatal
(
"192.168.1.1 is a private address!"
)
}
a
,
err
=
ma
.
NewMultiaddr
(
"/ip4/1.1.1.1/tcp/80"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
...
...
@@ -24,4 +28,8 @@ func TestIsPublicAddr(t *testing.T) {
if
!
IsPublicAddr
(
a
)
{
t
.
Fatal
(
"1.1.1.1 is a public address!"
)
}
if
IsPrivateAddr
(
a
)
{
t
.
Fatal
(
"1.1.1.1 is not a private address!"
)
}
}
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