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
3bb74625
Commit
3bb74625
authored
Mar 11, 2016
by
Mygod
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refine #594, again
parent
8fa2e60c
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
69 deletions
+41
-69
src/main/scala/com/github/shadowsocks/GuardedProcess.scala
src/main/scala/com/github/shadowsocks/GuardedProcess.scala
+39
-61
src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala
.../scala/com/github/shadowsocks/ShadowsocksVpnService.scala
+2
-8
No files found.
src/main/scala/com/github/shadowsocks/GuardedProcess.scala
View file @
3bb74625
...
...
@@ -39,67 +39,59 @@
package
com.github.shadowsocks
import
java.io.
{
IOException
,
InputStream
,
OutputStream
}
import
java.lang.System.currentTimeMillis
import
java.util.concurrent.Semaphore
import
android.util.Log
import
java.io.IOException
import
java.io.InputStream
import
java.io.OutputStream
import
java.util.concurrent.CountDownLatch
import
java.util.concurrent.atomic.AtomicReference
import
collection.JavaConversions._
import
scala.
collection.JavaConversions._
/**
* @author ayanamist@gmail.com
*/
class
GuardedProcess
(
cmd
:
Seq
[
String
],
onRestartCallback
:
Runnable
=
null
)
extends
Process
{
class
GuardedProcess
(
cmd
:
Seq
[
String
])
extends
Process
{
private
val
TAG
=
classOf
[
GuardedProcess
].
getSimpleName
@volatile
private
var
guardThread
:
Thread
=
null
@volatile
private
var
isDestroyed
:
Boolean
=
false
@volatile
private
var
process
:
Process
=
null
def
start
()
:
GuardedProcess
=
{
@volatile
private
var
guardThread
:
Thread
=
_
@volatile
private
var
isDestroyed
:
Boolean
=
_
@volatile
private
var
process
:
Process
=
_
val
atomicIoException
=
new
AtomicReference
[
IOException
](
null
)
val
countDownLatch
=
new
CountDownLatch
(
1
)
def
start
(
onRestartCallback
:
()
=>
Unit
=
null
)
:
GuardedProcess
=
{
val
semaphore
=
new
Semaphore
(
1
)
semaphore
.
acquire
var
ioException
:
IOException
=
null
guardThread
=
new
Thread
(
new
Runnable
()
{
override
def
run
()
{
guardThread
=
new
Thread
(()
=>
{
try
{
var
callback
:
()
=>
Unit
=
null
while
(!
isDestroyed
)
{
Log
.
i
(
TAG
,
"start process: "
+
cmd
)
val
startTime
=
java
.
lang
.
System
.
currentTimeMillis
val
startTime
=
currentTimeMillis
process
=
new
ProcessBuilder
(
cmd
).
redirectErrorStream
(
true
).
start
if
(
onRestartCallback
!=
null
&&
countDownLatch
.
getCount
<=
0
)
{
onRestartCallback
.
run
()
}
countDownLatch
.
countDown
()
if
(
callback
==
null
)
callback
=
onRestartCallback
else
callback
()
semaphore
.
release
process
.
waitFor
if
(
java
.
lang
.
System
.
currentTimeMillis
-
startTime
<
1000
)
{
if
(
currentTimeMillis
-
startTime
<
1000
)
{
Log
.
w
(
TAG
,
"process exit too fast, stop guard: "
+
cmd
)
return
isDestroyed
=
true
}
}
}
catch
{
case
ignored
:
InterruptedException
=>
Log
.
i
(
TAG
,
"thread interrupt, destroy process: "
+
cmd
)
process
.
destroy
()
case
e
:
IOException
=>
atomicIoException
.
compareAndSet
(
null
,
e
)
}
finally
{
countDownLatch
.
countDown
()
}
}
case
e
:
IOException
=>
ioException
=
e
}
finally
semaphore
.
release
},
"GuardThread-"
+
cmd
)
guardThread
.
start
()
countDownLatch
.
await
()
semaphore
.
acquire
val
ioException
:
IOException
=
atomicIoException
.
get
if
(
ioException
!=
null
)
{
throw
ioException
}
...
...
@@ -111,32 +103,18 @@ class GuardedProcess (cmd: Seq[String], onRestartCallback: Runnable = null) exte
isDestroyed
=
true
guardThread
.
interrupt
()
process
.
destroy
()
try
{
guardThread
.
join
()
}
catch
{
try
guardThread
.
join
()
catch
{
case
ignored
:
InterruptedException
=>
}
}
def
exitValue
:
Int
=
{
throw
new
UnsupportedOperationException
}
def
getErrorStream
:
InputStream
=
{
throw
new
UnsupportedOperationException
}
def
getInputStream
:
InputStream
=
{
throw
new
UnsupportedOperationException
}
def
getOutputStream
:
OutputStream
=
{
throw
new
UnsupportedOperationException
}
def
exitValue
:
Int
=
throw
new
UnsupportedOperationException
def
getErrorStream
:
InputStream
=
throw
new
UnsupportedOperationException
def
getInputStream
:
InputStream
=
throw
new
UnsupportedOperationException
def
getOutputStream
:
OutputStream
=
throw
new
UnsupportedOperationException
@throws
(
classOf
[
InterruptedException
])
def
waitFor
:
Int
=
{
def
waitFor
=
{
guardThread
.
join
()
0
}
...
...
src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala
View file @
3bb74625
...
...
@@ -44,18 +44,16 @@ import java.lang.Process
import
java.util.Locale
import
android.content._
import
android.content.pm.PackageManager.NameNotFoundException
import
android.content.pm.
{
PackageInfo
,
PackageManager
}
import
android.net.VpnService
import
android.os._
import
android.util.Log
import
android.widget.Toast
import
android.content.pm.PackageManager.NameNotFoundException
import
com.github.shadowsocks.aidl.Config
import
com.github.shadowsocks.utils._
import
org.apache.commons.net.util.SubnetUtils
import
scala.collection.JavaConversions._
import
scala.collection.mutable.ArrayBuffer
class
ShadowsocksVpnService
extends
VpnService
with
BaseService
{
...
...
@@ -434,11 +432,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
if
(
BuildConfig
.
DEBUG
)
Log
.
d
(
TAG
,
cmd
)
tun2socksProcess
=
new
GuardedProcess
(
cmd
.
split
(
" "
).
toSeq
,
new
Runnable
{
override
def
run
()
:
Unit
=
{
sendFd
(
fd
)
}
}).
start
()
tun2socksProcess
=
new
GuardedProcess
(
cmd
.
split
(
" "
).
toSeq
).
start
(()
=>
sendFd
(
fd
))
fd
}
...
...
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