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
1421bb70
Commit
1421bb70
authored
Dec 14, 2019
by
Mygod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make acl init async
parent
8b4b2409
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
34 additions
and
20 deletions
+34
-20
core/src/main/java/com/github/shadowsocks/acl/Acl.kt
core/src/main/java/com/github/shadowsocks/acl/Acl.kt
+10
-3
core/src/main/java/com/github/shadowsocks/acl/AclMatcher.kt
core/src/main/java/com/github/shadowsocks/acl/AclMatcher.kt
+18
-13
core/src/main/java/com/github/shadowsocks/bg/LocalDnsService.kt
...rc/main/java/com/github/shadowsocks/bg/LocalDnsService.kt
+3
-2
core/src/main/java/com/github/shadowsocks/net/LocalDnsServer.kt
...rc/main/java/com/github/shadowsocks/net/LocalDnsServer.kt
+3
-2
No files found.
core/src/main/java/com/github/shadowsocks/acl/Acl.kt
View file @
1421bb70
...
...
@@ -28,11 +28,15 @@ import com.github.shadowsocks.Core
import
com.github.shadowsocks.net.Subnet
import
com.github.shadowsocks.preference.DataStore
import
com.github.shadowsocks.utils.asIterable
import
kotlinx.coroutines.Job
import
kotlinx.coroutines.ensureActive
import
kotlinx.coroutines.runBlocking
import
java.io.File
import
java.io.IOException
import
java.io.Reader
import
java.net.URL
import
java.net.URLConnection
import
kotlin.coroutines.coroutineContext
class
Acl
{
companion
object
{
...
...
@@ -65,7 +69,7 @@ class Acl {
value
.
proxyHostnames
.
size
()
==
0
&&
value
.
urls
.
size
()
==
0
)
null
else
value
.
toString
())
fun
save
(
id
:
String
,
acl
:
Acl
)
=
getFile
(
id
).
writeText
(
acl
.
toString
())
fun
<
T
>
parse
(
reader
:
Reader
,
bypassHostnames
:
(
String
)
->
T
,
proxyHostnames
:
(
String
)
->
T
,
suspend
fun
<
T
>
parse
(
reader
:
Reader
,
bypassHostnames
:
(
String
)
->
T
,
proxyHostnames
:
(
String
)
->
T
,
urls
:
((
URL
)
->
T
)?
=
null
,
defaultBypass
:
Boolean
=
false
):
Pair
<
Boolean
,
List
<
Subnet
>>
{
var
bypass
=
defaultBypass
val
bypassSubnets
=
mutableListOf
<
Subnet
>()
...
...
@@ -74,6 +78,7 @@ class Acl {
var
subnets
:
MutableList
<
Subnet
>?
=
if
(
defaultBypass
)
proxySubnets
else
bypassSubnets
reader
.
useLines
{
for
(
line
in
it
)
{
coroutineContext
[
Job
]
!!
.
ensureActive
()
val
input
=
(
if
(
urls
==
null
)
line
else
{
val
blocks
=
line
.
split
(
'#'
,
limit
=
2
)
val
url
=
networkAclParser
.
matchEntire
(
blocks
.
getOrElse
(
1
)
{
""
})
?.
groupValues
?.
getOrNull
(
1
)
...
...
@@ -150,7 +155,9 @@ class Acl {
proxyHostnames
.
clear
()
subnets
.
clear
()
urls
.
clear
()
val
(
bypass
,
subnets
)
=
parse
(
reader
,
bypassHostnames
::
add
,
proxyHostnames
::
add
,
urls
::
add
,
defaultBypass
)
val
(
bypass
,
subnets
)
=
runBlocking
{
parse
(
reader
,
bypassHostnames
::
add
,
proxyHostnames
::
add
,
urls
::
add
,
defaultBypass
)
}
this
.
bypass
=
bypass
for
(
item
in
subnets
)
this
.
subnets
.
add
(
item
)
return
this
...
...
core/src/main/java/com/github/shadowsocks/acl/AclMatcher.kt
View file @
1421bb70
...
...
@@ -20,20 +20,23 @@
package
com.github.shadowsocks.acl
import
android.util.Log
import
com.github.shadowsocks.net.Subnet
import
java.net.Inet4Address
import
java.net.Inet6Address
import
kotlin.system.measureNanoTime
class
AclMatcher
(
id
:
String
)
{
private
va
l
subnetsIpv4
:
List
<
Subnet
.
Immutable
>
private
va
l
subnetsIpv6
:
List
<
Subnet
.
Immutable
>
class
AclMatcher
{
private
va
r
subnetsIpv4
=
emptyList
<
Subnet
.
Immutable
>()
private
va
r
subnetsIpv6
=
emptyList
<
Subnet
.
Immutable
>()
private
val
bypassDomains
=
mutableListOf
<
Regex
>()
private
val
proxyDomains
=
mutableListOf
<
Regex
>()
private
va
l
bypass
:
Boolean
private
va
r
bypass
=
false
init
{
suspend
fun
init
(
id
:
String
)
{
val
time
=
measureNanoTime
{
val
(
bypass
,
subnets
)
=
Acl
.
parse
(
Acl
.
getFile
(
id
).
bufferedReader
(),
{
//
bypassDomains.add(it.toRegex())
//
bypassDomains.add(it.toRegex())
},
{
if
(
it
.
startsWith
(
"(?:^|\\.)googleapis"
))
proxyDomains
.
add
(
it
.
toRegex
())
})
...
...
@@ -41,6 +44,8 @@ class AclMatcher(id: String) {
subnetsIpv6
=
subnets
.
filter
{
it
.
address
is
Inet6Address
}.
map
{
it
.
toImmutable
()
}
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
)
}
...
...
core/src/main/java/com/github/shadowsocks/bg/LocalDnsService.kt
View file @
1421bb70
...
...
@@ -48,8 +48,9 @@ object LocalDnsService {
Socks5Endpoint
(
dns
.
host
,
if
(
dns
.
port
<
0
)
53
else
dns
.
port
),
DataStore
.
proxyAddress
,
hosts
,
!
profile
.
udpdns
,
if
(
profile
.
route
==
Acl
.
ALL
)
null
else
AclMatcher
(
profile
.
route
)).
also
{
!
profile
.
udpdns
)
{
if
(
profile
.
route
==
Acl
.
ALL
)
null
else
AclMatcher
().
apply
{
init
(
profile
.
route
)
}
}.
also
{
servers
[
this
]
=
it
}.
start
(
InetSocketAddress
(
DataStore
.
listenAddress
,
DataStore
.
portLocalDns
))
}
...
...
core/src/main/java/com/github/shadowsocks/net/LocalDnsServer.kt
View file @
1421bb70
...
...
@@ -51,7 +51,7 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd
* Forward UDP queries to TCP.
*/
private
val
tcp
:
Boolean
=
false
,
private
val
acl
:
AclMatcher
?
=
null
)
:
CoroutineScope
{
private
val
acl
Spawn
:
suspend
()
->
AclMatcher
?
=
{
null
}
)
:
CoroutineScope
{
companion
object
{
private
const
val
TAG
=
"LocalDnsServer"
private
const
val
TIMEOUT
=
10_000L
...
...
@@ -84,6 +84,7 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd
override
val
coroutineContext
=
SupervisorJob
()
+
CoroutineExceptionHandler
{
_
,
t
->
if
(
t
is
IOException
)
Crashlytics
.
log
(
Log
.
WARN
,
TAG
,
t
.
message
)
else
printLog
(
t
)
}
private
val
acl
=
async
{
aclSpawn
()
}
suspend
fun
start
(
listen
:
SocketAddress
)
=
DatagramChannel
.
open
().
run
{
configureBlocking
(
false
)
...
...
@@ -128,7 +129,7 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd
remote
.
cancel
()
return
@supervisorScope
cookDnsResponse
(
request
,
hostsResults
)
}
if
(
acl
==
null
)
return
@supervisorScope
remote
.
await
()
val
acl
=
acl
.
await
()
?:
return
@supervisorScope
remote
.
await
()
val
useLocal
=
when
(
acl
.
shouldBypass
(
host
))
{
true
->
true
.
also
{
remote
.
cancel
()
}
false
->
return
@supervisorScope
remote
.
await
()
...
...
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