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
64b78846
Commit
64b78846
authored
Jan 27, 2019
by
Mygod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create class ConcurrentLocalSocketListener
parent
3997896a
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
64 additions
and
34 deletions
+64
-34
core/src/main/java/com/github/shadowsocks/bg/TrafficMonitor.kt
...src/main/java/com/github/shadowsocks/bg/TrafficMonitor.kt
+1
-1
core/src/main/java/com/github/shadowsocks/bg/VpnService.kt
core/src/main/java/com/github/shadowsocks/bg/VpnService.kt
+19
-32
core/src/main/java/com/github/shadowsocks/net/ConcurrentLocalSocketListener.kt
...m/github/shadowsocks/net/ConcurrentLocalSocketListener.kt
+42
-0
core/src/main/java/com/github/shadowsocks/net/LocalSocketListener.kt
...in/java/com/github/shadowsocks/net/LocalSocketListener.kt
+2
-1
No files found.
core/src/main/java/com/github/shadowsocks/bg/TrafficMonitor.kt
View file @
64b78846
...
...
@@ -31,7 +31,7 @@ import java.nio.ByteOrder
class
TrafficMonitor
(
statFile
:
File
)
:
AutoCloseable
{
private
val
thread
=
object
:
LocalSocketListener
(
"TrafficMonitor"
,
statFile
)
{
override
fun
accept
(
socket
:
LocalSocket
)
=
socket
.
use
{
override
fun
accept
Internal
(
socket
:
LocalSocket
)
{
val
buffer
=
ByteArray
(
16
)
if
(
socket
.
inputStream
.
read
(
buffer
)
!=
16
)
throw
IOException
(
"Unexpected traffic stat length"
)
val
stat
=
ByteBuffer
.
wrap
(
buffer
).
order
(
ByteOrder
.
LITTLE_ENDIAN
)
...
...
core/src/main/java/com/github/shadowsocks/bg/VpnService.kt
View file @
64b78846
...
...
@@ -34,14 +34,14 @@ import com.github.shadowsocks.Core
import
com.github.shadowsocks.VpnRequestActivity
import
com.github.shadowsocks.acl.Acl
import
com.github.shadowsocks.core.R
import
com.github.shadowsocks.net.ConcurrentLocalSocketListener
import
com.github.shadowsocks.net.DefaultNetworkListener
import
com.github.shadowsocks.net.LocalSocketListener
import
com.github.shadowsocks.net.Subnet
import
com.github.shadowsocks.preference.DataStore
import
com.github.shadowsocks.utils.Key
import
com.github.shadowsocks.utils.parseNumericAddress
import
com.github.shadowsocks.utils.printLog
import
kotlinx.coroutines.
*
import
kotlinx.coroutines.
delay
import
java.io.Closeable
import
java.io.File
import
java.io.FileDescriptor
...
...
@@ -66,39 +66,26 @@ class VpnService : BaseVpnService(), LocalDnsService.Interface {
override
fun
close
()
=
Os
.
close
(
fd
)
}
private
inner
class
ProtectWorker
:
LocalSocketListener
(
"ShadowsocksVpnThread"
,
File
(
Core
.
deviceStorage
.
noBackupFilesDir
,
"protect_path"
)),
CoroutineScope
{
private
val
job
=
SupervisorJob
()
override
val
coroutineContext
get
()
=
Dispatchers
.
IO
+
job
+
CoroutineExceptionHandler
{
_
,
t
->
printLog
(
t
)
}
override
fun
accept
(
socket
:
LocalSocket
)
{
launch
{
socket
.
use
{
socket
.
inputStream
.
read
()
val
fd
=
socket
.
ancillaryFileDescriptors
!!
.
single
()
!!
CloseableFd
(
fd
).
use
{
socket
.
outputStream
.
write
(
if
(
underlyingNetwork
.
let
{
network
->
if
(
network
!=
null
&&
Build
.
VERSION
.
SDK_INT
>=
23
)
try
{
network
.
bindSocket
(
fd
)
true
}
catch
(
e
:
IOException
)
{
// suppress ENONET (Machine is not on the network)
if
((
e
.
cause
as
?
ErrnoException
)
?.
errno
!=
64
)
printLog
(
e
)
false
}
else
protect
(
getInt
.
invoke
(
fd
)
as
Int
)
})
0
else
1
)
}
}
private
inner
class
ProtectWorker
:
ConcurrentLocalSocketListener
(
"ShadowsocksVpnThread"
,
File
(
Core
.
deviceStorage
.
noBackupFilesDir
,
"protect_path"
))
{
override
fun
acceptInternal
(
socket
:
LocalSocket
)
{
socket
.
inputStream
.
read
()
val
fd
=
socket
.
ancillaryFileDescriptors
!!
.
single
()
!!
CloseableFd
(
fd
).
use
{
socket
.
outputStream
.
write
(
if
(
underlyingNetwork
.
let
{
network
->
if
(
network
!=
null
&&
Build
.
VERSION
.
SDK_INT
>=
23
)
try
{
network
.
bindSocket
(
fd
)
true
}
catch
(
e
:
IOException
)
{
// suppress ENONET (Machine is not on the network)
if
((
e
.
cause
as
?
ErrnoException
)
?.
errno
!=
64
)
printLog
(
e
)
false
}
else
protect
(
getInt
.
invoke
(
fd
)
as
Int
)
})
0
else
1
)
}
}
suspend
fun
shutdown
()
{
job
.
cancel
()
close
()
job
.
join
()
}
}
inner
class
NullConnectionException
:
NullPointerException
()
{
override
fun
getLocalizedMessage
()
=
getString
(
R
.
string
.
reboot_required
)
}
...
...
core/src/main/java/com/github/shadowsocks/net/ConcurrentLocalSocketListener.kt
0 → 100644
View file @
64b78846
/*******************************************************************************
* *
* Copyright (C) 2019 by Max Lv <max.c.lv@gmail.com> *
* Copyright (C) 2019 by Mygod Studio <contact-shadowsocks-android@mygod.be> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
package
com.github.shadowsocks.net
import
android.net.LocalSocket
import
com.github.shadowsocks.utils.printLog
import
kotlinx.coroutines.*
import
java.io.File
abstract
class
ConcurrentLocalSocketListener
(
name
:
String
,
socketFile
:
File
)
:
LocalSocketListener
(
name
,
socketFile
),
CoroutineScope
{
private
val
job
=
SupervisorJob
()
override
val
coroutineContext
get
()
=
Dispatchers
.
IO
+
job
+
CoroutineExceptionHandler
{
_
,
t
->
printLog
(
t
)
}
override
fun
accept
(
socket
:
LocalSocket
)
{
launch
{
super
.
accept
(
socket
)
}
}
suspend
fun
shutdown
()
{
job
.
cancel
()
close
()
job
.
join
()
}
}
core/src/main/java/com/github/shadowsocks/net/LocalSocketListener.kt
View file @
64b78846
...
...
@@ -42,7 +42,8 @@ abstract class LocalSocketListener(name: String, socketFile: File) : Thread(name
/**
* Inherited class do not need to close input/output streams as they will be closed automatically.
*/
protected
abstract
fun
accept
(
socket
:
LocalSocket
)
protected
open
fun
accept
(
socket
:
LocalSocket
)
=
socket
.
use
{
acceptInternal
(
socket
)
}
protected
abstract
fun
acceptInternal
(
socket
:
LocalSocket
)
final
override
fun
run
()
=
localSocket
.
use
{
while
(
running
)
{
try
{
...
...
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