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
ee11c896
Commit
ee11c896
authored
Dec 14, 2019
by
Mygod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Optimize Subnet.matches
parent
cd744cb4
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
14 additions
and
11 deletions
+14
-11
core/src/main/java/com/github/shadowsocks/acl/AclMatcher.kt
core/src/main/java/com/github/shadowsocks/acl/AclMatcher.kt
+6
-4
core/src/main/java/com/github/shadowsocks/net/LocalDnsServer.kt
...rc/main/java/com/github/shadowsocks/net/LocalDnsServer.kt
+2
-2
core/src/main/java/com/github/shadowsocks/net/Subnet.kt
core/src/main/java/com/github/shadowsocks/net/Subnet.kt
+6
-5
No files found.
core/src/main/java/com/github/shadowsocks/acl/AclMatcher.kt
View file @
ee11c896
...
...
@@ -25,14 +25,16 @@ import java.net.Inet4Address
import
java.net.Inet6Address
class
AclMatcher
(
acl
:
Acl
)
{
private
val
subnetsIpv4
=
acl
.
subnets
.
asIterable
().
filter
{
it
.
address
is
Inet4Address
}
private
val
subnetsIpv6
=
acl
.
subnets
.
asIterable
().
filter
{
it
.
address
is
Inet6Address
}
private
val
subnetsIpv4
=
acl
.
subnets
.
asIterable
().
filter
{
it
.
address
is
Inet4Address
}.
map
{
it
to
it
.
address
.
address
}
private
val
subnetsIpv6
=
acl
.
subnets
.
asIterable
().
filter
{
it
.
address
is
Inet6Address
}.
map
{
it
to
it
.
address
.
address
}
private
val
bypassDomains
=
acl
.
bypassHostnames
.
asIterable
().
map
{
it
.
toRegex
()
}
private
val
proxyDomains
=
acl
.
proxyHostnames
.
asIterable
().
map
{
it
.
toRegex
()
}
private
val
bypass
=
acl
.
bypass
fun
shouldBypass
(
ip
:
Inet4Address
)
=
bypass
xor
subnetsIpv4
.
any
{
it
.
matches
(
ip
)
}
fun
shouldBypass
(
ip
:
Inet6Address
)
=
bypass
xor
subnetsIpv6
.
any
{
it
.
matches
(
ip
)
}
fun
shouldBypass
Ipv4
(
ip
:
ByteArray
)
=
bypass
xor
subnetsIpv4
.
any
{
(
subnet
,
subnetIp
)
->
subnet
.
matches
(
subnetIp
,
ip
)
}
fun
shouldBypass
Ipv6
(
ip
:
ByteArray
)
=
bypass
xor
subnetsIpv6
.
any
{
(
subnet
,
subnetIp
)
->
subnet
.
matches
(
subnetIp
,
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/LocalDnsServer.kt
View file @
ee11c896
...
...
@@ -145,14 +145,14 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd
if
(
isIpv6
)
{
val
filtered
=
localResults
.
filterIsInstance
<
Inet6Address
>()
if
(
useLocal
)
return
@supervisorScope
cookDnsResponse
(
request
,
filtered
)
if
(
filtered
.
any
{
acl
.
shouldBypass
(
it
)
})
{
if
(
filtered
.
any
{
acl
.
shouldBypass
Ipv6
(
it
.
address
)
})
{
remote
.
cancel
()
cookDnsResponse
(
request
,
filtered
)
}
else
remote
.
await
()
}
else
{
val
filtered
=
localResults
.
filterIsInstance
<
Inet4Address
>()
if
(
useLocal
)
return
@supervisorScope
cookDnsResponse
(
request
,
filtered
)
if
(
filtered
.
any
{
acl
.
shouldBypass
(
it
)
})
{
if
(
filtered
.
any
{
acl
.
shouldBypass
Ipv4
(
it
.
address
)
})
{
remote
.
cancel
()
cookDnsResponse
(
request
,
filtered
)
}
else
remote
.
await
()
...
...
core/src/main/java/com/github/shadowsocks/net/Subnet.kt
View file @
ee11c896
...
...
@@ -45,11 +45,12 @@ class Subnet(val address: InetAddress, val prefixSize: Int) : Comparable<Subnet>
require
(
prefixSize
in
0
..
addressLength
)
{
"prefixSize $prefixSize not in 0..$addressLength"
}
}
fun
matches
(
other
:
InetAddress
):
Boolean
{
if
(
address
.
javaClass
!=
other
.
javaClass
)
return
false
// TODO optimize?
val
a
=
address
.
address
val
b
=
other
.
address
/**
* Optimized version of matches.
* a corresponds to this address and b is the address in question.
*/
fun
matches
(
a
:
ByteArray
,
b
:
ByteArray
):
Boolean
{
if
(
a
.
size
!=
b
.
size
)
return
false
var
i
=
0
while
(
i
*
8
<
prefixSize
&&
i
*
8
+
8
<=
prefixSize
)
{
if
(
a
[
i
]
!=
b
[
i
])
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