FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
zstack/utils/src/main/java/org/zstack/utils/IpRangeSet.java at master · zstackio/zstack · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
zstackio
/
zstack
Public
Notifications
You must be signed in to change notification settings
Fork
399
Star
1.4k
Code
Issues
163
Pull requests
13
Actions
Projects
Wiki
Security and quality
4
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
zstack
/
utils
/
src
/
main
/
java
/
org
/
zstack
/
utils
/
IpRangeSet.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
122 lines (102 loc) · 4.51 KB
Breadcrumbs
zstack
/
utils
/
src
/
main
/
java
/
org
/
zstack
/
utils
/
IpRangeSet.java
Copy path
File metadata and controls
122 lines (102 loc) · 4.51 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package
org
.
zstack
.
utils
;
import
com
.
google
.
common
.
collect
.*;
import
com
.
google
.
common
.
collect
.
RangeSet
;
import
org
.
apache
.
commons
.
lang
.
StringUtils
;
import
org
.
apache
.
commons
.
net
.
util
.
SubnetUtils
;
import
org
.
zstack
.
utils
.
network
.
NetworkUtils
;
import
java
.
util
.
HashSet
;
import
java
.
util
.
Optional
;
import
java
.
util
.
Set
;
import
java
.
util
.
regex
.
Matcher
;
import
java
.
util
.
regex
.
Pattern
;
import
java
.
util
.
stream
.
Collectors
;
public
class
IpRangeSet
{
private
static
Pattern
rangePattern
=
Pattern
.
compile
(
"^([
\\
d.]+)(-[
\\
d.]+)?$"
);
private
static
Pattern
cidrPattern
=
Pattern
.
compile
(
"^([
\\
d.]+
\\
/.+)$"
);
private
RangeSet
<
Long
>
rangeSet
=
TreeRangeSet
.
create
();
private
static
String
IP_SET_SEPARATOR
=
","
;
private
static
String
IP_SET_INVERT_PREFIX
=
"^"
;
private
void
closed
(
String
origin
){
Matcher
rangeMatcher
=
rangePattern
.
matcher
(
origin
);
Matcher
cidrMatcher
=
cidrPattern
.
matcher
(
origin
);
if
(
rangeMatcher
.
matches
()) {
Long
start
=
NetworkUtils
.
ipv4StringToLong
(
rangeMatcher
.
group
(
1
));
Long
end
=
Optional
.
ofNullable
(
rangeMatcher
.
group
(
2
)).
map
(
it
->
NetworkUtils
.
ipv4StringToLong
(
it
.
replaceFirst
(
"-"
,
""
))).
orElse
(
start
);
rangeSet
.
add
(
Range
.
closed
(
start
,
end
));
}
else
if
(
cidrMatcher
.
matches
() &&
NetworkUtils
.
isCidr
(
cidrMatcher
.
group
(
1
))) {
String
cidr
=
cidrMatcher
.
group
(
1
);
SubnetUtils
utils
=
new
SubnetUtils
(
cidr
);
SubnetUtils
.
SubnetInfo
subnet
=
utils
.
getInfo
();
Long
start
=
NetworkUtils
.
ipv4StringToLong
(
subnet
.
getLowAddress
());
Long
end
=
NetworkUtils
.
ipv4StringToLong
(
subnet
.
getHighAddress
());
rangeSet
.
add
(
Range
.
closed
(
start
,
end
));
}
else
{
throw
new
IllegalArgumentException
(
String
.
format
(
"illegal word[%s] for ip range"
,
origin
));
}
}
private
void
remove
(
IpRangeSet
exclude
) {
rangeSet
.
removeAll
(
exclude
.
rangeSet
);
}
public
boolean
contains
(
Long
ip
) {
return
rangeSet
.
contains
(
ip
);
}
public
long
size
() {
long
size
=
0L
;
for
(
Range
<
Long
>
range
:
rangeSet
.
asRanges
()) {
range
=
ContiguousSet
.
create
(
range
,
DiscreteDomain
.
longs
()).
range
();
size
+= (
range
.
upperEndpoint
() -
range
.
lowerEndpoint
() +
1
);
}
return
size
;
}
public
static
IpRangeSet
generateIpRangeSet
(
String
word
) {
word
=
StringUtils
.
deleteWhitespace
(
word
);
String
[]
sets
=
word
.
split
(
IP_SET_SEPARATOR
);
IpRangeSet
contain
=
new
IpRangeSet
();
IpRangeSet
exclude
=
new
IpRangeSet
();
for
(
String
set
:
sets
) {
if
(
set
.
startsWith
(
IP_SET_INVERT_PREFIX
)) {
exclude
.
closed
(
set
.
substring
(
1
));
}
else
{
contain
.
closed
(
set
);
}
}
contain
.
remove
(
exclude
);
return
contain
;
}
public
static
RangeSet
<
Long
>
listAllRanges
(
String
word
) {
IpRangeSet
contain
=
generateIpRangeSet
(
word
);
long
size
=
contain
.
size
();
if
(
size
==
0
) {
throw
new
IllegalArgumentException
(
String
.
format
(
"Invalid empty ipset [%s]"
,
word
));
}
return
contain
.
rangeSet
;
}
public
static
Set
<
String
>
listAllIps
(
String
word
,
long
limit
) {
IpRangeSet
contain
=
generateIpRangeSet
(
word
);
long
size
=
contain
.
size
();
if
(
size
==
0
) {
throw
new
IllegalArgumentException
(
String
.
format
(
"Invalid empty ipset [%s]"
,
word
));
}
if
(
size
>
limit
) {
throw
new
IllegalArgumentException
(
String
.
format
(
"ip range length[%d] is too large, must less than %d"
,
size
,
limit
));
}
Set
<
String
>
results
=
new
HashSet
<>();
for
(
Range
<
Long
>
range
:
contain
.
rangeSet
.
asRanges
()) {
range
=
ContiguousSet
.
create
(
range
,
DiscreteDomain
.
longs
()).
range
();
for
(
long
i
=
range
.
lowerEndpoint
();
i
<=
range
.
upperEndpoint
() ;
i
++) {
results
.
add
(
NetworkUtils
.
longToIpv4String
(
i
));
}
}
return
results
;
}
@
Override
public
String
toString
(){
return
String
.
join
(
IP_SET_SEPARATOR
,
rangeSet
.
asRanges
().
stream
().
map
(
range
-> {
range
=
ContiguousSet
.
create
(
range
,
DiscreteDomain
.
longs
()).
range
();
long
start
=
range
.
lowerEndpoint
();
long
end
=
range
.
upperEndpoint
();
return
start
==
end
?
NetworkUtils
.
longToIpv4String
(
start
) :
String
.
format
(
"%s-%s"
,
NetworkUtils
.
longToIpv4String
(
start
),
NetworkUtils
.
longToIpv4String
(
end
));
}).
collect
(
Collectors
.
toList
()));
}
}
Back
|
FazBrowse Home
|
New Git URL