Commit f2292fb8 authored by Max Lv's avatar Max Lv

use ProcessBuilder instead of fork/exec

parent fe81182d
...@@ -42,6 +42,7 @@ package com.github.shadowsocks ...@@ -42,6 +42,7 @@ package com.github.shadowsocks
import java.io.File import java.io.File
import java.net.{Inet6Address, InetAddress} import java.net.{Inet6Address, InetAddress}
import java.util.Locale import java.util.Locale
import java.lang.{Process, ProcessBuilder}
import android.app._ import android.app._
import android.content._ import android.content._
...@@ -56,6 +57,7 @@ import com.github.shadowsocks.aidl.Config ...@@ -56,6 +57,7 @@ import com.github.shadowsocks.aidl.Config
import com.github.shadowsocks.utils._ import com.github.shadowsocks.utils._
import scala.collection._ import scala.collection._
import scala.collection.JavaConversions._
import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer
import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future import scala.concurrent.Future
...@@ -72,12 +74,12 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -72,12 +74,12 @@ class ShadowsocksNatService extends Service with BaseService {
var closeReceiver: BroadcastReceiver = null var closeReceiver: BroadcastReceiver = null
var connReceiver: BroadcastReceiver = null var connReceiver: BroadcastReceiver = null
var apps: Array[ProxiedApp] = null var apps: Array[ProxiedApp] = null
val myUid = Process.myUid() val myUid = android.os.Process.myUid()
var sslocalPid: Int = 0 var sslocalProcess: Process = null
var sstunnelPid: Int = 0 var sstunnelProcess: Process = null
var redsocksPid: Int = 0 var redsocksProcess: Process = null
var pdnsdPid: Int = 0 var pdnsdProcess: Process = null
private val dnsAddressCache = new SparseArray[String] private val dnsAddressCache = new SparseArray[String]
...@@ -198,7 +200,10 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -198,7 +200,10 @@ class ShadowsocksNatService extends Service with BaseService {
} }
if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" ")) if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" "))
sslocalPid = System.exec(cmd.mkString(" ")) sslocalProcess = new ProcessBuilder()
.command(cmd)
.redirectErrorStream(false)
.start()
} }
def startTunnel() { def startTunnel() {
...@@ -223,7 +228,10 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -223,7 +228,10 @@ class ShadowsocksNatService extends Service with BaseService {
if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" ")) if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" "))
sstunnelPid = System.exec(cmd.mkString(" ")) sstunnelProcess = new ProcessBuilder()
.command(cmd)
.redirectErrorStream(false)
.start()
} else { } else {
val conf = ConfigUtils val conf = ConfigUtils
...@@ -245,7 +253,10 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -245,7 +253,10 @@ class ShadowsocksNatService extends Service with BaseService {
if (BuildConfig.DEBUG) Log.d(TAG, cmdBuf.mkString(" ")) if (BuildConfig.DEBUG) Log.d(TAG, cmdBuf.mkString(" "))
sstunnelPid = System.exec(cmdBuf.mkString(" ")) sstunnelProcess = new ProcessBuilder()
.command(cmdBuf)
.redirectErrorStream(false)
.start()
} }
} }
...@@ -268,8 +279,10 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -268,8 +279,10 @@ class ShadowsocksNatService extends Service with BaseService {
if (BuildConfig.DEBUG) Log.d(TAG, cmd) if (BuildConfig.DEBUG) Log.d(TAG, cmd)
pdnsdPid = System.exec(cmd) pdnsdProcess = new ProcessBuilder()
.command(cmd.split(" ").toSeq)
.redirectErrorStream(false)
.start()
} }
def getVersionName: String = { def getVersionName: String = {
...@@ -293,7 +306,10 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -293,7 +306,10 @@ class ShadowsocksNatService extends Service with BaseService {
}) })
if (BuildConfig.DEBUG) Log.d(TAG, cmd) if (BuildConfig.DEBUG) Log.d(TAG, cmd)
redsocksPid = System.exec(cmd) redsocksProcess = new ProcessBuilder()
.command(cmd.split(" ").toSeq)
.redirectErrorStream(false)
.start()
} }
/** Called when the activity is first created. */ /** Called when the activity is first created. */
...@@ -350,25 +366,21 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -350,25 +366,21 @@ class ShadowsocksNatService extends Service with BaseService {
} }
def killProcesses() { def killProcesses() {
try { if (sslocalProcess != null) {
Process.killProcess(sslocalPid) sslocalProcess.destroy()
} catch { sslocalProcess = null
case e: Throwable => Log.e(TAG, "unable to kill ss-local")
} }
try { if (sstunnelProcess != null) {
Process.killProcess(sstunnelPid) sstunnelProcess.destroy()
} catch { sstunnelProcess = null
case e: Throwable => Log.e(TAG, "unable to kill ss-tunnel")
} }
try { if (redsocksProcess != null) {
Process.killProcess(redsocksPid) redsocksProcess.destroy()
} catch { redsocksProcess = null
case e: Throwable => Log.e(TAG, "unable to kill redsocks")
} }
try { if (pdnsdProcess != null) {
Process.killProcess(pdnsdPid) pdnsdProcess.destroy()
} catch { pdnsdProcess = null
case e: Throwable => Log.e(TAG, "unable to kill pdnsd")
} }
Console.runRootCommand(Utils.getIptables + " -t nat -F OUTPUT") Console.runRootCommand(Utils.getIptables + " -t nat -F OUTPUT")
......
...@@ -41,6 +41,7 @@ package com.github.shadowsocks ...@@ -41,6 +41,7 @@ package com.github.shadowsocks
import java.io.File import java.io.File
import java.util.Locale import java.util.Locale
import java.lang.{Process, ProcessBuilder}
import android.app._ import android.app._
import android.content._ import android.content._
...@@ -55,6 +56,7 @@ import com.github.shadowsocks.aidl.Config ...@@ -55,6 +56,7 @@ import com.github.shadowsocks.aidl.Config
import com.github.shadowsocks.utils._ import com.github.shadowsocks.utils._
import org.apache.commons.net.util.SubnetUtils import org.apache.commons.net.util.SubnetUtils
import scala.collection.JavaConversions._
import scala.collection.mutable import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer import scala.collection.mutable.ArrayBuffer
import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.ExecutionContext.Implicits.global
...@@ -70,10 +72,10 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -70,10 +72,10 @@ class ShadowsocksVpnService extends VpnService with BaseService {
var vpnThread: ShadowsocksVpnThread = null var vpnThread: ShadowsocksVpnThread = null
var closeReceiver: BroadcastReceiver = null var closeReceiver: BroadcastReceiver = null
var sslocalPid: Int = 0 var sslocalProcess: Process = null
var sstunnelPid: Int = 0 var sstunnelProcess: Process = null
var tun2socksPid: Int = 0 var pdnsdProcess: Process = null
var pdnsdPid: Int = 0 var tun2socksProcess: Process = null
def isByass(net: SubnetUtils): Boolean = { def isByass(net: SubnetUtils): Boolean = {
val info = net.getInfo val info = net.getInfo
...@@ -194,25 +196,21 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -194,25 +196,21 @@ class ShadowsocksVpnService extends VpnService with BaseService {
} }
def killProcesses() { def killProcesses() {
try { if (sslocalProcess != null) {
Process.killProcess(sslocalPid) sslocalProcess.destroy()
} catch { sslocalProcess = null
case e: Throwable => Log.e(TAG, "unable to kill ss-local")
} }
try { if (sstunnelProcess != null) {
Process.killProcess(sstunnelPid) sstunnelProcess.destroy()
} catch { sstunnelProcess = null
case e: Throwable => Log.e(TAG, "unable to kill ss-tunnel")
} }
try { if (tun2socksProcess != null) {
Process.killProcess(tun2socksPid) tun2socksProcess.destroy()
} catch { tun2socksProcess = null
case e: Throwable => Log.e(TAG, "unable to kill tun2socks")
} }
try { if (pdnsdProcess != null) {
Process.killProcess(pdnsdPid) pdnsdProcess.destroy()
} catch { pdnsdProcess = null
case e: Throwable => Log.e(TAG, "unable to kill pdnsd")
} }
} }
...@@ -344,7 +342,11 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -344,7 +342,11 @@ class ShadowsocksVpnService extends VpnService with BaseService {
} }
if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" ")) if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" "))
sslocalPid = System.exec(cmd.mkString(" "))
sslocalProcess = new ProcessBuilder()
.command(cmd)
.redirectErrorStream(false)
.start()
} }
def startDnsTunnel() = { def startDnsTunnel() = {
...@@ -367,7 +369,11 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -367,7 +369,11 @@ class ShadowsocksVpnService extends VpnService with BaseService {
if (config.isAuth) cmd += "-A" if (config.isAuth) cmd += "-A"
if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" ")) if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" "))
sstunnelPid = System.exec(cmd.mkString(" "))
sstunnelProcess = new ProcessBuilder()
.command(cmd)
.redirectErrorStream(false)
.start()
} }
def startDnsDaemon() { def startDnsDaemon() {
...@@ -389,7 +395,11 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -389,7 +395,11 @@ class ShadowsocksVpnService extends VpnService with BaseService {
val cmd = Path.BASE + "pdnsd -c " + Path.BASE + "pdnsd-vpn.conf" val cmd = Path.BASE + "pdnsd -c " + Path.BASE + "pdnsd-vpn.conf"
if (BuildConfig.DEBUG) Log.d(TAG, cmd) if (BuildConfig.DEBUG) Log.d(TAG, cmd)
pdnsdPid = System.exec(cmd)
pdnsdProcess = new ProcessBuilder()
.command(cmd.split(" ").toSeq)
.redirectErrorStream(false)
.start()
} }
override def getContext = getBaseContext override def getContext = getBaseContext
...@@ -465,7 +475,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -465,7 +475,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
+ "--socks-server-addr 127.0.0.1:%d " + "--socks-server-addr 127.0.0.1:%d "
+ "--tunfd %d " + "--tunfd %d "
+ "--tunmtu %d " + "--tunmtu %d "
+ "--loglevel 3 ") + "--loglevel 3")
.formatLocal(Locale.ENGLISH, .formatLocal(Locale.ENGLISH,
PRIVATE_VLAN.formatLocal(Locale.ENGLISH, "2"), PRIVATE_VLAN.formatLocal(Locale.ENGLISH, "2"),
config.localPort, fd, VPN_MTU, Path.BASE) config.localPort, fd, VPN_MTU, Path.BASE)
...@@ -480,7 +490,10 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -480,7 +490,10 @@ class ShadowsocksVpnService extends VpnService with BaseService {
if (BuildConfig.DEBUG) Log.d(TAG, cmd) if (BuildConfig.DEBUG) Log.d(TAG, cmd)
tun2socksPid = System.exec(cmd) tun2socksProcess = new ProcessBuilder()
.command(cmd.split(" ").toSeq)
.redirectErrorStream(false)
.start()
fd fd
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment