Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
com.ccwangluo.accelerator
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sheteng
com.ccwangluo.accelerator
Commits
476a6257
Commit
476a6257
authored
Dec 14, 2019
by
Mygod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Speed up subnet matching using binary search
parent
1421bb70
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
9 deletions
+31
-9
core/src/main/java/com/github/shadowsocks/acl/AclMatcher.kt
core/src/main/java/com/github/shadowsocks/acl/AclMatcher.kt
+11
-4
core/src/main/java/com/github/shadowsocks/net/Subnet.kt
core/src/main/java/com/github/shadowsocks/net/Subnet.kt
+20
-5
No files found.
core/src/main/java/com/github/shadowsocks/acl/AclMatcher.kt
View file @
476a6257
...
...
@@ -40,15 +40,22 @@ class AclMatcher {
},
{
if
(
it
.
startsWith
(
"(?:^|\\.)googleapis"
))
proxyDomains
.
add
(
it
.
toRegex
())
})
subnetsIpv4
=
subnets
.
filter
{
it
.
address
is
Inet4Address
}.
map
{
it
.
toImmutable
()
}
subnetsIpv6
=
subnets
.
filter
{
it
.
address
is
Inet6Address
}.
map
{
it
.
toImmutable
()
}
subnetsIpv4
=
subnets
.
asSequence
().
filter
{
it
.
address
is
Inet4Address
}.
map
{
it
.
toImmutable
()
}
.
sortedWith
(
Subnet
.
Immutable
).
toList
()
subnetsIpv6
=
subnets
.
asSequence
().
filter
{
it
.
address
is
Inet6Address
}.
map
{
it
.
toImmutable
()
}
.
sortedWith
(
Subnet
.
Immutable
).
toList
()
this
.
bypass
=
bypass
}
Log
.
d
(
"AclMatcher"
,
"ACL initialized in $time ns"
)
}
fun
shouldBypassIpv4
(
ip
:
ByteArray
)
=
bypass
xor
subnetsIpv4
.
any
{
it
.
matches
(
ip
)
}
fun
shouldBypassIpv6
(
ip
:
ByteArray
)
=
bypass
xor
subnetsIpv6
.
any
{
it
.
matches
(
ip
)
}
private
fun
quickMatches
(
subnets
:
List
<
Subnet
.
Immutable
>,
ip
:
ByteArray
):
Boolean
{
val
i
=
subnets
.
binarySearch
(
Subnet
.
Immutable
(
ip
),
Subnet
.
Immutable
)
return
i
>=
0
||
i
<
-
1
&&
subnets
[-
i
-
2
].
matches
(
ip
)
}
fun
shouldBypassIpv4
(
ip
:
ByteArray
)
=
bypass
xor
quickMatches
(
subnetsIpv4
,
ip
)
fun
shouldBypassIpv6
(
ip
:
ByteArray
)
=
bypass
xor
quickMatches
(
subnetsIpv6
,
ip
)
fun
shouldBypass
(
host
:
String
):
Boolean
?
{
if
(
bypassDomains
.
any
{
it
.
matches
(
host
)
})
return
true
if
(
proxyDomains
.
any
{
it
.
matches
(
host
)
})
return
false
...
...
core/src/main/java/com/github/shadowsocks/net/Subnet.kt
View file @
476a6257
...
...
@@ -45,7 +45,17 @@ class Subnet(val address: InetAddress, val prefixSize: Int) : Comparable<Subnet>
require
(
prefixSize
in
0
..
addressLength
)
{
"prefixSize $prefixSize not in 0..$addressLength"
}
}
class
Immutable
(
private
val
a
:
ByteArray
,
private
val
prefixSize
:
Int
)
{
class
Immutable
(
private
val
a
:
ByteArray
,
private
val
prefixSize
:
Int
=
0
)
{
companion
object
:
Comparator
<
Immutable
>
{
override
fun
compare
(
a
:
Immutable
,
b
:
Immutable
):
Int
{
check
(
a
.
a
.
size
==
b
.
a
.
size
)
for
(
i
in
a
.
a
.
indices
)
{
val
result
=
a
.
a
[
i
].
compareTo
(
b
.
a
[
i
])
if
(
result
!=
0
)
return
result
}
return
0
}
}
fun
matches
(
b
:
ByteArray
):
Boolean
{
if
(
a
.
size
!=
b
.
size
)
return
false
var
i
=
0
...
...
@@ -53,12 +63,17 @@ class Subnet(val address: InetAddress, val prefixSize: Int) : Comparable<Subnet>
if
(
a
[
i
]
!=
b
[
i
])
return
false
++
i
}
if
(
i
*
8
==
prefixSize
)
return
true
val
mask
=
256
-
(
1
shl
i
*
8
+
8
-
prefixSize
)
return
a
[
i
].
toInt
()
and
mask
==
b
[
i
].
toInt
()
and
mask
return
i
*
8
==
prefixSize
||
a
[
i
].
toInt
()
==
b
[
i
].
toInt
()
and
256
-
(
1
shl
i
*
8
+
8
-
prefixSize
)
}
}
fun
toImmutable
()
=
Immutable
(
address
.
address
,
prefixSize
)
fun
toImmutable
()
=
Immutable
(
address
.
address
.
also
{
var
i
=
prefixSize
/
8
if
(
prefixSize
%
8
>
0
)
{
it
[
i
]
=
(
it
[
i
].
toInt
()
and
256
-
(
1
shl
i
*
8
+
8
-
prefixSize
)).
toByte
()
++
i
}
while
(
i
<
it
.
size
)
it
[
i
++]
=
0
},
prefixSize
)
override
fun
toString
():
String
=
if
(
prefixSize
==
addressLength
)
address
.
hostAddress
else
address
.
hostAddress
+
'/'
+
prefixSize
...
...
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