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
9e09e032
Commit
9e09e032
authored
Mar 07, 2016
by
Max Lv
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #594 from ayanamist/miui
Recover unexpected process exit on MIUI
parents
e4e1a421
c19eb4e9
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
175 additions
and
48 deletions
+175
-48
src/main/java/com/github/shadowsocks/GuardedProcess.scala
src/main/java/com/github/shadowsocks/GuardedProcess.scala
+147
-0
src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala
.../scala/com/github/shadowsocks/ShadowsocksNatService.scala
+5
-20
src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala
.../scala/com/github/shadowsocks/ShadowsocksVpnService.scala
+23
-28
No files found.
src/main/java/com/github/shadowsocks/GuardedProcess.scala
0 → 100644
View file @
9e09e032
/*
* Shadowsocks - A shadowsocks client for Android
* Copyright (C) 2015 <max.c.lv@gmail.com>
*
* 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/>.
*
*
* ___====-_ _-====___
* _--^^^#####// \\#####^^^--_
* _-^##########// ( ) \\##########^-_
* -############// |\^^/| \\############-
* _/############// (@::@) \\############\_
* /#############(( \\// ))#############\
* -###############\\ (oo) //###############-
* -#################\\ / VV \ //#################-
* -###################\\/ \//###################-
* _#/|##########/\######( /\ )######/\##########|\#_
* |/ |#/\#/\#/\/ \#/\##\ | | /##/\#/ \/\#/\#/\#| \|
* ` |/ V V ` V \#\| | | |/#/ V ' V V \| '
* ` ` ` ` / | | | | \ ' ' ' '
* ( | | | | )
* __\ | | | | /__
* (vvv(VVV)(VVV)vvv)
*
* HERE BE DRAGONS
*
*/
package
com.github.shadowsocks
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._
object
GuardedProcess
{
private
val
TAG
:
String
=
classOf
[
GuardedProcess
].
getSimpleName
@throws
(
classOf
[
IOException
])
private
def
startProcess
(
cmd
:
Seq
[
String
])
:
Process
=
{
new
ProcessBuilder
(
cmd
).
redirectErrorStream
(
true
).
start
}
}
class
GuardedProcess
extends
Process
{
private
var
guardThread
:
Thread
=
null
@volatile
private
var
isDestroyed
:
Boolean
=
false
@volatile
private
var
process
:
Process
=
null
@throws
(
classOf
[
InterruptedException
])
@throws
(
classOf
[
IOException
])
def
this
(
cmd
:
Seq
[
String
],
onRestartCallback
:
Runnable
=
null
)
{
this
()
initThread
(
cmd
,
onRestartCallback
)
}
def
initThread
(
cmd
:
Seq
[
String
],
onRestartCallback
:
Runnable
)
:
Unit
=
{
val
atomicIoException
:
AtomicReference
[
IOException
]
=
new
AtomicReference
[
IOException
](
null
)
val
countDownLatch
:
CountDownLatch
=
new
CountDownLatch
(
1
)
guardThread
=
new
Thread
(
new
Runnable
()
{
override
def
run
()
:
Unit
=
{
try
{
while
(!
isDestroyed
)
{
Log
.
i
(
GuardedProcess
.
TAG
,
"start process: "
+
cmd
)
val
startTime
:
Long
=
java
.
lang
.
System
.
currentTimeMillis
process
=
GuardedProcess
.
startProcess
(
cmd
)
if
(
onRestartCallback
!=
null
&&
countDownLatch
.
getCount
<=
0
)
{
onRestartCallback
.
run
()
}
countDownLatch
.
countDown
()
process
.
waitFor
if
(
java
.
lang
.
System
.
currentTimeMillis
-
startTime
<
1000
)
{
Log
.
w
(
GuardedProcess
.
TAG
,
"process exit too fast, stop guard: "
+
cmd
)
return
}
}
}
catch
{
case
ignored
:
InterruptedException
=>
Log
.
i
(
GuardedProcess
.
TAG
,
"thread interrupt, destroy process: "
+
cmd
)
process
.
destroy
()
case
e
:
IOException
=>
atomicIoException
.
compareAndSet
(
null
,
e
)
}
finally
{
countDownLatch
.
countDown
()
}
}
},
"GuardThread-"
+
cmd
)
guardThread
.
start
()
countDownLatch
.
await
()
val
ioException
:
IOException
=
atomicIoException
.
get
if
(
ioException
!=
null
)
{
throw
ioException
}
}
def
destroy
()
{
isDestroyed
=
true
guardThread
.
interrupt
()
process
.
destroy
()
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
}
@throws
(
classOf
[
InterruptedException
])
def
waitFor
:
Int
=
{
guardThread
.
join
()
0
}
}
\ No newline at end of file
src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala
View file @
9e09e032
...
...
@@ -196,10 +196,7 @@ class ShadowsocksNatService extends BaseService {
}
if
(
BuildConfig
.
DEBUG
)
Log
.
d
(
TAG
,
cmd
.
mkString
(
" "
))
sslocalProcess
=
new
ProcessBuilder
()
.
command
(
cmd
)
.
redirectErrorStream
(
true
)
.
start
()
sslocalProcess
=
new
GuardedProcess
(
cmd
)
}
def
startTunnel
()
{
...
...
@@ -225,10 +222,7 @@ class ShadowsocksNatService extends BaseService {
if
(
BuildConfig
.
DEBUG
)
Log
.
d
(
TAG
,
cmd
.
mkString
(
" "
))
sstunnelProcess
=
new
ProcessBuilder
()
.
command
(
cmd
)
.
redirectErrorStream
(
true
)
.
start
()
sstunnelProcess
=
new
GuardedProcess
(
cmd
)
}
else
{
val
conf
=
ConfigUtils
...
...
@@ -251,10 +245,7 @@ class ShadowsocksNatService extends BaseService {
if
(
BuildConfig
.
DEBUG
)
Log
.
d
(
TAG
,
cmdBuf
.
mkString
(
" "
))
sstunnelProcess
=
new
ProcessBuilder
()
.
command
(
cmdBuf
)
.
redirectErrorStream
(
true
)
.
start
()
sstunnelProcess
=
new
GuardedProcess
(
cmdBuf
)
}
}
...
...
@@ -277,10 +268,7 @@ class ShadowsocksNatService extends BaseService {
if
(
BuildConfig
.
DEBUG
)
Log
.
d
(
TAG
,
cmd
)
pdnsdProcess
=
new
ProcessBuilder
()
.
command
(
cmd
.
split
(
" "
).
toSeq
)
.
redirectErrorStream
(
true
)
.
start
()
pdnsdProcess
=
new
GuardedProcess
(
cmd
.
split
(
" "
).
toSeq
)
}
def
getVersionName
:
String
=
{
...
...
@@ -304,10 +292,7 @@ class ShadowsocksNatService extends BaseService {
})
if
(
BuildConfig
.
DEBUG
)
Log
.
d
(
TAG
,
cmd
)
redsocksProcess
=
new
ProcessBuilder
()
.
command
(
cmd
.
split
(
" "
).
toSeq
)
.
redirectErrorStream
(
true
)
.
start
()
redsocksProcess
=
new
GuardedProcess
(
cmd
.
split
(
" "
).
toSeq
)
}
/** Called when the activity is first created. */
...
...
src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala
View file @
9e09e032
...
...
@@ -256,18 +256,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
}
val
fd
=
startVpn
()
if
(
fd
!=
-
1
)
{
var
tries
=
1
while
(
tries
<
5
)
{
Thread
.
sleep
(
1000
*
tries
)
if
(
System
.
sendfd
(
fd
,
getApplicationInfo
.
dataDir
+
"/sock_path"
)
!=
-
1
)
{
return
true
}
tries
+=
1
}
}
false
sendFd
(
fd
)
}
def
startShadowsocksDaemon
()
{
...
...
@@ -307,10 +296,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
if
(
BuildConfig
.
DEBUG
)
Log
.
d
(
TAG
,
cmd
.
mkString
(
" "
))
sslocalProcess
=
new
ProcessBuilder
()
.
command
(
cmd
)
.
redirectErrorStream
(
true
)
.
start
()
sslocalProcess
=
new
GuardedProcess
(
cmd
)
}
def
startDnsTunnel
()
=
{
...
...
@@ -335,10 +321,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
if
(
BuildConfig
.
DEBUG
)
Log
.
d
(
TAG
,
cmd
.
mkString
(
" "
))
sstunnelProcess
=
new
ProcessBuilder
()
.
command
(
cmd
)
.
redirectErrorStream
(
true
)
.
start
()
sstunnelProcess
=
new
GuardedProcess
(
cmd
)
}
def
startDnsDaemon
()
{
...
...
@@ -361,10 +344,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
if
(
BuildConfig
.
DEBUG
)
Log
.
d
(
TAG
,
cmd
)
pdnsdProcess
=
new
ProcessBuilder
()
.
command
(
cmd
.
split
(
" "
).
toSeq
)
.
redirectErrorStream
(
true
)
.
start
()
pdnsdProcess
=
new
GuardedProcess
(
cmd
.
split
(
" "
).
toSeq
)
}
override
def
getContext
=
getBaseContext
...
...
@@ -454,14 +434,29 @@ class ShadowsocksVpnService extends VpnService with BaseService {
if
(
BuildConfig
.
DEBUG
)
Log
.
d
(
TAG
,
cmd
)
tun2socksProcess
=
new
ProcessBuilder
()
.
command
(
cmd
.
split
(
" "
).
toSeq
)
.
redirectErrorStream
(
true
)
.
start
()
tun2socksProcess
=
new
GuardedProcess
(
cmd
.
split
(
" "
).
toSeq
,
new
Runnable
{
override
def
run
()
:
Unit
=
{
sendFd
(
fd
)
}
})
fd
}
def
sendFd
(
fd
:
Int
)
:
Boolean
=
{
if
(
fd
!=
-
1
)
{
var
tries
=
1
while
(
tries
<
5
)
{
Thread
.
sleep
(
1000
*
tries
)
if
(
System
.
sendfd
(
fd
,
getApplicationInfo
.
dataDir
+
"/sock_path"
)
!=
-
1
)
{
return
true
}
tries
+=
1
}
}
false
}
override
def
getTag
=
TAG
override
def
getServiceMode
=
Mode
.
VPN
...
...
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