Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
gostnops
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
gostnops
Commits
cc62a113
Unverified
Commit
cc62a113
authored
Aug 10, 2020
by
Xin.Zh
Committed by
GitHub
Aug 10, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #30 from watermelo/feature/netutils
Add: add net ip match function
parents
8b727f60
ffb690e7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
179 additions
and
0 deletions
+179
-0
net.go
net/net.go
+137
-0
net_test.go
net/net_test.go
+42
-0
No files found.
net/net.go
View file @
cc62a113
...
@@ -18,7 +18,9 @@
...
@@ -18,7 +18,9 @@
package
gxnet
package
gxnet
import
(
import
(
"log"
"net"
"net"
"strconv"
"strings"
"strings"
)
)
...
@@ -30,6 +32,13 @@ var (
...
@@ -30,6 +32,13 @@ var (
privateBlocks
[]
*
net
.
IPNet
privateBlocks
[]
*
net
.
IPNet
)
)
const
(
// Ipv4SplitCharacter use for slipt Ipv4
Ipv4SplitCharacter
=
"."
// Ipv6SplitCharacter use for slipt Ipv6
Ipv6SplitCharacter
=
":"
)
func
init
()
{
func
init
()
{
for
_
,
b
:=
range
[]
string
{
"10.0.0.0/8"
,
"172.16.0.0/12"
,
"192.168.0.0/16"
}
{
for
_
,
b
:=
range
[]
string
{
"10.0.0.0/8"
,
"172.16.0.0/12"
,
"192.168.0.0/16"
}
{
if
_
,
block
,
err
:=
net
.
ParseCIDR
(
b
);
err
==
nil
{
if
_
,
block
,
err
:=
net
.
ParseCIDR
(
b
);
err
==
nil
{
...
@@ -175,3 +184,131 @@ func ListenOnUDPRandomPort(ip string) (*net.UDPConn, error) {
...
@@ -175,3 +184,131 @@ func ListenOnUDPRandomPort(ip string) (*net.UDPConn, error) {
return
net
.
ListenUDP
(
"udp4"
,
&
localAddr
)
return
net
.
ListenUDP
(
"udp4"
,
&
localAddr
)
}
}
// MatchIP is used to determine whether @pattern and @host:@port match, It's supports subnet/range
func
MatchIP
(
pattern
,
host
,
port
string
)
bool
{
// if the pattern is subnet format, it will not be allowed to config port param in pattern.
if
strings
.
Contains
(
pattern
,
"/"
)
{
_
,
subnet
,
_
:=
net
.
ParseCIDR
(
pattern
)
return
subnet
!=
nil
&&
subnet
.
Contains
(
net
.
ParseIP
(
host
))
}
return
matchIPRange
(
pattern
,
host
,
port
)
}
func
matchIPRange
(
pattern
,
host
,
port
string
)
bool
{
if
pattern
==
""
||
host
==
""
{
log
.
Print
(
"Illegal Argument pattern or hostName. Pattern:"
+
pattern
+
", Host:"
+
host
)
return
false
}
pattern
=
strings
.
TrimSpace
(
pattern
)
if
"*.*.*.*"
==
pattern
||
"*"
==
pattern
{
return
true
}
isIpv4
:=
true
ip4
:=
net
.
ParseIP
(
host
)
.
To4
()
if
ip4
==
nil
{
isIpv4
=
false
}
hostAndPort
:=
getPatternHostAndPort
(
pattern
,
isIpv4
)
if
hostAndPort
[
1
]
!=
""
&&
hostAndPort
[
1
]
!=
port
{
return
false
}
pattern
=
hostAndPort
[
0
]
splitCharacter
:=
Ipv4SplitCharacter
if
!
isIpv4
{
splitCharacter
=
Ipv6SplitCharacter
}
mask
:=
strings
.
Split
(
pattern
,
splitCharacter
)
// check format of pattern
if
err
:=
checkHostPattern
(
pattern
,
mask
,
isIpv4
);
err
!=
nil
{
log
.
Printf
(
"gost/net check host pattern error: %s"
,
err
.
Error
())
return
false
}
if
pattern
==
host
{
return
true
}
// short name condition
if
!
ipPatternContains
(
pattern
)
{
return
pattern
==
host
}
ipAddress
:=
strings
.
Split
(
host
,
splitCharacter
)
for
i
:=
0
;
i
<
len
(
mask
);
i
++
{
if
"*"
==
mask
[
i
]
||
mask
[
i
]
==
ipAddress
[
i
]
{
continue
}
else
if
strings
.
Contains
(
mask
[
i
],
"-"
)
{
rangeNumStrs
:=
strings
.
Split
(
mask
[
i
],
"-"
)
if
len
(
rangeNumStrs
)
!=
2
{
log
.
Print
(
"There is wrong format of ip Address: "
+
mask
[
i
])
return
false
}
min
:=
getNumOfIPSegment
(
rangeNumStrs
[
0
],
isIpv4
)
max
:=
getNumOfIPSegment
(
rangeNumStrs
[
1
],
isIpv4
)
ip
:=
getNumOfIPSegment
(
ipAddress
[
i
],
isIpv4
)
if
ip
<
min
||
ip
>
max
{
return
false
}
}
else
if
"0"
==
ipAddress
[
i
]
&&
"0"
==
mask
[
i
]
||
"00"
==
mask
[
i
]
||
"000"
==
mask
[
i
]
||
"0000"
==
mask
[
i
]
{
continue
}
else
if
mask
[
i
]
!=
ipAddress
[
i
]
{
return
false
}
}
return
true
}
func
ipPatternContains
(
pattern
string
)
bool
{
return
strings
.
Contains
(
pattern
,
"*"
)
||
strings
.
Contains
(
pattern
,
"-"
)
}
func
checkHostPattern
(
pattern
string
,
mask
[]
string
,
isIpv4
bool
)
error
{
if
!
isIpv4
{
if
len
(
mask
)
!=
8
&&
ipPatternContains
(
pattern
)
{
return
perrors
.
New
(
"If you config ip expression that contains '*' or '-', please fill qualified ip pattern like 234e:0:4567:0:0:0:3d:*. "
)
}
if
len
(
mask
)
!=
8
&&
!
strings
.
Contains
(
pattern
,
"::"
)
{
return
perrors
.
New
(
"The host is ipv6, but the pattern is not ipv6 pattern : "
+
pattern
)
}
}
else
{
if
len
(
mask
)
!=
4
{
return
perrors
.
New
(
"The host is ipv4, but the pattern is not ipv4 pattern : "
+
pattern
)
}
}
return
nil
}
func
getPatternHostAndPort
(
pattern
string
,
isIpv4
bool
)
[]
string
{
result
:=
make
([]
string
,
2
)
if
strings
.
HasPrefix
(
pattern
,
"["
)
&&
strings
.
Contains
(
pattern
,
"]:"
)
{
end
:=
strings
.
Index
(
pattern
,
"]:"
)
result
[
0
]
=
pattern
[
1
:
end
]
result
[
1
]
=
pattern
[
end
+
2
:
]
}
else
if
strings
.
HasPrefix
(
pattern
,
"["
)
&&
strings
.
HasSuffix
(
pattern
,
"]"
)
{
result
[
0
]
=
pattern
[
1
:
len
(
pattern
)
-
1
]
result
[
1
]
=
""
}
else
if
isIpv4
&&
strings
.
Contains
(
pattern
,
":"
)
{
end
:=
strings
.
Index
(
pattern
,
":"
)
result
[
0
]
=
pattern
[
:
end
]
result
[
1
]
=
pattern
[
end
+
1
:
]
}
else
{
result
[
0
]
=
pattern
}
return
result
}
func
getNumOfIPSegment
(
ipSegment
string
,
isIpv4
bool
)
int
{
if
isIpv4
{
ipSeg
,
_
:=
strconv
.
Atoi
(
ipSegment
)
return
ipSeg
}
ipSeg
,
_
:=
strconv
.
ParseInt
(
ipSegment
,
0
,
16
)
return
int
(
ipSeg
)
}
net/net_test.go
View file @
cc62a113
...
@@ -87,3 +87,45 @@ func TestListenOnUDPRandomPort(t *testing.T) {
...
@@ -87,3 +87,45 @@ func TestListenOnUDPRandomPort(t *testing.T) {
l
.
Close
()
l
.
Close
()
}
}
}
}
func
TestMatchIpIpv4Equal
(
t
*
testing
.
T
)
{
assert
.
True
(
t
,
MatchIP
(
"192.168.0.1:8080"
,
"192.168.0.1"
,
"8080"
))
assert
.
False
(
t
,
MatchIP
(
"192.168.0.1:8081"
,
"192.168.0.1"
,
"8080"
))
assert
.
True
(
t
,
MatchIP
(
"*"
,
"192.168.0.1"
,
"8080"
))
assert
.
True
(
t
,
MatchIP
(
"*"
,
"192.168.0.1"
,
""
))
assert
.
True
(
t
,
MatchIP
(
"*.*.*.*"
,
"192.168.0.1"
,
"8080"
))
assert
.
False
(
t
,
MatchIP
(
"*"
,
""
,
""
))
}
func
TestMatchIpIpv4Subnet
(
t
*
testing
.
T
)
{
assert
.
True
(
t
,
MatchIP
(
"206.0.68.0/23"
,
"206.0.68.123"
,
"8080"
))
assert
.
False
(
t
,
MatchIP
(
"206.0.68.0/23"
,
"207.0.69.123"
,
"8080"
))
}
func
TestMatchIpIpv4Range
(
t
*
testing
.
T
)
{
assert
.
True
(
t
,
MatchIP
(
"206.*.68.0"
,
"206.0.68.0"
,
"8080"
))
assert
.
False
(
t
,
MatchIP
(
"206.*.68.0"
,
"206.0.69.0"
,
"8080"
))
assert
.
True
(
t
,
MatchIP
(
"206.0.68-69.0"
,
"206.0.68.0"
,
"8080"
))
assert
.
False
(
t
,
MatchIP
(
"206.0.68-69.0"
,
"206.0.70.0"
,
"8080"
))
}
func
TestMatchIpIpv6Equal
(
t
*
testing
.
T
)
{
assert
.
True
(
t
,
MatchIP
(
"[1fff:0:a88:85a3::ac1f]:8080"
,
"1fff:0:a88:85a3::ac1f"
,
"8080"
))
assert
.
False
(
t
,
MatchIP
(
"[1fff:0:a88:85a3::ac1f]:8081"
,
"1fff:0:a88:85a3::ac1f"
,
"8080"
))
assert
.
True
(
t
,
MatchIP
(
"*"
,
"1fff:0:a88:85a3::ac1f"
,
"8080"
))
assert
.
True
(
t
,
MatchIP
(
"*"
,
"1fff:0:a88:85a3::ac1f"
,
""
))
assert
.
True
(
t
,
MatchIP
(
"*.*.*.*"
,
"1fff:0:a88:85a3::ac1f"
,
"8080"
))
assert
.
False
(
t
,
MatchIP
(
"*"
,
""
,
""
))
}
func
TestMatchIpIpv6Subnet
(
t
*
testing
.
T
)
{
assert
.
True
(
t
,
MatchIP
(
"1fff:0:a88:85a3::ac1f/64"
,
"1fff:0000:0a88:85a3:0000:0000:0000:0000"
,
"8080"
))
assert
.
False
(
t
,
MatchIP
(
"1fff:0:a88:85a3::ac1f/64"
,
"2fff:0000:0a88:85a3:0000:0000:0000:0000"
,
"8080"
))
}
func
TestMatchIpIpv6Range
(
t
*
testing
.
T
)
{
assert
.
True
(
t
,
MatchIP
(
"234e:0:4567:0:0:0:3d:*"
,
"234e:0:4567:0:0:0:3d:4"
,
"8080"
))
assert
.
False
(
t
,
MatchIP
(
"234e:0:4567:0:0:0:3d:*"
,
"234e:0:4567:0:0:0:2d:4"
,
"8080"
))
assert
.
True
(
t
,
MatchIP
(
"234e:0:4567:0:0:0:3d:1-2"
,
"234e:0:4567:0:0:0:3d:1"
,
"8080"
))
assert
.
False
(
t
,
MatchIP
(
"234e:0:4567:0:0:0:3d:1-2"
,
"234e:0:4567:0:0:0:3d:3"
,
"8080"
))
}
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