Call tun2socks in main process

This commit is contained in:
Max Lv
2014-11-24 10:39:45 +08:00
parent 08d5d56e46
commit 567543d00e
11 changed files with 264 additions and 111 deletions
@@ -123,7 +123,7 @@ object Shadowsocks {
val FEATRUE_PREFS = Array(Key.route, Key.isGlobalProxy, Key.proxyedApps,
Key.isUdpDns, Key.isAutoConnect)
val EXECUTABLES = Array(Executable.PDNSD, Executable.REDSOCKS, Executable.IPTABLES)
val EXECUTABLES = Array(Executable.PDNSD, Executable.REDSOCKS, Executable.SS_TUNNEL, Executable.SS_LOCAL)
// Helper functions
def updateListPreference(pref: Preference, value: String) {
@@ -834,6 +834,14 @@ class Shadowsocks
crash_recovery()
copyAssets(System.getABI)
val ab = new ArrayBuffer[String]
for (executable <- Shadowsocks.EXECUTABLES) {
ab.append("chmod 755 " + Path.BASE + executable)
}
Console.runCommand(ab.toArray)
}
private def recovery() {
@@ -102,14 +102,17 @@ class ShadowsocksNatService extends Service with BaseService {
})
}
val conf = ConfigUtils
.SHADOWSOCKS.format(config.proxy, config.remotePort, config.localPort,
config.sitekey, config.encMethod, 10)
ConfigUtils.printToFile(new File(Path.BASE + "ss-local.json"))(p => {
p.println(conf)
})
val cmd = new ArrayBuffer[String]
cmd += ("ss-local"
cmd += (Path.BASE + "ss-local"
, "-b" , "127.0.0.1"
, "-s" , config.proxy
, "-p" , config.remotePort.toString
, "-l" , config.localPort.toString
, "-k" , config.sitekey
, "-m" , config.encMethod
, "-c" , Path.BASE + "ss-local.json"
, "-f" , Path.BASE + "ss-local.pid")
if (config.route != Route.ALL) {
@@ -118,49 +121,59 @@ class ShadowsocksNatService extends Service with BaseService {
}
if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" "))
Core.sslocal(cmd.toArray)
Console.runCommand(cmd.mkString(" "))
}
def startDnsDaemon() {
def startTunnel() {
if (config.isUdpDns) {
val conf = ConfigUtils
.SHADOWSOCKS.format(config.proxy, config.remotePort, 8153,
config.sitekey, config.encMethod, 10)
ConfigUtils.printToFile(new File(Path.BASE + "ss-tunnel.json"))(p => {
p.println(conf)
})
val cmd = new ArrayBuffer[String]
cmd += ("ss-tunnel" , "-u"
cmd += (Path.BASE + "ss-tunnel" , "-u"
, "-b" , "127.0.0.1"
, "-l" , "8153"
, "-L" , "8.8.8.8:53"
, "-s" , config.proxy
, "-p" , config.remotePort.toString
, "-k" , config.sitekey
, "-m" , config.encMethod
, "-c" , Path.BASE + "ss-tunnel.json"
, "-f" , Path.BASE + "ss-tunnel.pid")
if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" "))
Core.sstunnel(cmd.toArray)
} else {
val cmdBuf = new ArrayBuffer[String]
cmdBuf += ("ss-tunnel"
, "-b", "127.0.0.1"
, "-l", "8163"
, "-L", "8.8.8.8:53"
, "-s", config.proxy
, "-p", config.remotePort.toString
, "-k", config.sitekey
, "-m", config.encMethod
, "-f", Path.BASE + "ss-tunnel.pid")
if (BuildConfig.DEBUG) Log.d(TAG, cmdBuf.mkString(" "))
Core.sstunnel(cmdBuf.toArray)
if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" "))
Console.runCommand(cmd.mkString(" "))
} else {
val conf = ConfigUtils
.PDNSD_BYPASS.format("127.0.0.1", getString(R.string.exclude), 8163)
ConfigUtils.printToFile(new File(Path.BASE + "pdnsd.conf"))(p => {
p.println(conf)
.SHADOWSOCKS.format(config.proxy, config.remotePort, 8163,
config.sitekey, config.encMethod, 10)
ConfigUtils.printToFile(new File(Path.BASE + "ss-tunnel.json"))(p => {
p.println(conf)
})
val cmd = Path.BASE + "pdnsd -c " + Path.BASE + "pdnsd.conf"
if (BuildConfig.DEBUG) Log.d(TAG, cmd)
Core.pdnsd(cmd.split(" "))
val cmdBuf = new ArrayBuffer[String]
cmdBuf += (Path.BASE + "ss-tunnel"
, "-b" , "127.0.0.1"
, "-l" , "8163"
, "-L" , "8.8.8.8:53"
, "-c" , Path.BASE + "ss-tunnel.json"
, "-f" , Path.BASE + "ss-tunnel.pid")
if (BuildConfig.DEBUG) Log.d(TAG, cmdBuf.mkString(" "))
Console.runCommand(cmdBuf.mkString(" "))
}
}
def startDnsDaemon() {
val conf = ConfigUtils
.PDNSD_BYPASS.format("127.0.0.1", getString(R.string.exclude), 8163)
ConfigUtils.printToFile(new File(Path.BASE + "pdnsd.conf"))(p => {
p.println(conf)
})
val cmd = Path.BASE + "pdnsd -c " + Path.BASE + "pdnsd.conf"
if (BuildConfig.DEBUG) Log.d(TAG, cmd)
Console.runCommand(cmd)
}
def getVersionName: String = {
var version: String = null
try {
@@ -175,20 +188,21 @@ class ShadowsocksNatService extends Service with BaseService {
def startRedsocksDaemon() {
val conf = ConfigUtils.REDSOCKS.format(config.localPort)
val cmd = "redsocks -p %sredsocks.pid -c %sredsocks.conf"
val cmd = Path.BASE + "redsocks -p %sredsocks.pid -c %sredsocks.conf"
.format(Path.BASE, Path.BASE)
ConfigUtils.printToFile(new File(Path.BASE + "redsocks.conf"))(p => {
p.println(conf)
})
if (BuildConfig.DEBUG) Log.d(TAG, cmd)
Core.redsocks(cmd.split(" "))
if (BuildConfig.DEBUG) Log.d(TAG, cmd)
Console.runCommand(cmd);
}
/** Called when the activity is first created. */
def handleConnection: Boolean = {
startDnsDaemon()
startTunnel()
if (!config.isUdpDns) startDnsDaemon()
startRedsocksDaemon()
startShadowsocksDaemon()
setupIptables()
@@ -266,22 +280,16 @@ class ShadowsocksNatService extends Service with BaseService {
}
def killProcesses() {
for (task <- Array("ss-local", "ss-tunnel", "pdnsd", "redsocks")) {
try {
val pid = scala.io.Source.fromFile(Path.BASE + task + ".pid").mkString.trim.toInt
Process.killProcess(pid)
Log.d(TAG, "kill pid: " + pid)
} catch {
case e: Throwable => Log.e(TAG, "unable to kill " + task, e)
}
}
Console.runRootCommand(Utils.getIptables + " -t nat -F OUTPUT")
val ab = new ArrayBuffer[String]
ab.append("kill -9 `cat " + Path.BASE + "ss-tunnel.pid`")
ab.append("kill -9 `cat " + Path.BASE +"redsocks.pid`")
ab.append("killall -9 redsocks")
ab.append("kill -15 `cat " + Path.BASE + "pdnsd.pid`")
ab.append("killall -15 pdnsd")
Console.runRootCommand(ab.toArray)
ab.clear()
ab.append("kill -9 `cat " + Path.BASE + "ss-local.pid`")
Console.runCommand(ab.toArray)
}
def flushDNS() {
@@ -107,14 +107,17 @@ class ShadowsocksVpnService extends VpnService with BaseService {
})
}
val conf = ConfigUtils
.SHADOWSOCKS.format(config.proxy, config.remotePort, config.localPort,
config.sitekey, config.encMethod, 10)
ConfigUtils.printToFile(new File(Path.BASE + "ss-local.json"))(p => {
p.println(conf)
})
val cmd = new ArrayBuffer[String]
cmd +=("ss-local", "-u"
cmd +=(Path.BASE + "ss-local", "-u"
, "-b", "127.0.0.1"
, "-s", config.proxy
, "-p", config.remotePort.toString
, "-l", config.localPort.toString
, "-k", config.sitekey
, "-m", config.encMethod
, "-c", Path.BASE + "ss-local.json"
, "-f", Path.BASE + "ss-local.pid")
if (Utils.isLollipopOrAbove && config.route != Route.ALL) {
@@ -123,22 +126,26 @@ class ShadowsocksVpnService extends VpnService with BaseService {
}
if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" "))
Core.sslocal(cmd.toArray)
Console.runCommand(cmd.mkString(" "))
}
def startDnsTunnel() = {
val conf = ConfigUtils
.SHADOWSOCKS.format(config.proxy, config.remotePort, 8163,
config.sitekey, config.encMethod, 10)
ConfigUtils.printToFile(new File(Path.BASE + "ss-tunnel.json"))(p => {
p.println(conf)
})
val cmd = new ArrayBuffer[String]
cmd +=("ss-tunnel"
cmd +=(Path.BASE + "ss-tunnel"
, "-b", "127.0.0.1"
, "-l", "8163"
, "-L", "8.8.8.8:53"
, "-s", config.proxy
, "-p", config.remotePort.toString
, "-k", config.sitekey
, "-m", config.encMethod
, "-c", Path.BASE + "ss-tunnel.json"
, "-f", Path.BASE + "ss-tunnel.pid")
if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" "))
Core.sstunnel(cmd.toArray)
Console.runCommand(cmd.mkString(" "))
}
def startDnsDaemon() {
@@ -153,8 +160,9 @@ class ShadowsocksVpnService extends VpnService with BaseService {
p.println(conf)
})
val cmd = Path.BASE + "pdnsd -c " + Path.BASE + "pdnsd.conf"
if (BuildConfig.DEBUG) Log.d(TAG, cmd)
Core.pdnsd(cmd.split(" "))
Console.runCommand(cmd)
}
def getVersionName: String = {
@@ -314,7 +322,9 @@ class ShadowsocksVpnService extends VpnService with BaseService {
if (BuildConfig.DEBUG) Log.d(TAG, cmd)
Core.tun2socks(cmd.split(" "))
spawn {
Tun2Socks.start(cmd.split(" "))
}
}
/** Called when the activity is first created. */
@@ -352,7 +362,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
}
def killProcesses() {
for (task <- Array("ss-local", "ss-tunnel", "tun2socks", "pdnsd")) {
for (task <- Array("ss-local", "ss-tunnel", "pdnsd")) {
try {
val pid = scala.io.Source.fromFile(Path.BASE + task + ".pid").mkString.trim.toInt
Process.killProcess(pid)
@@ -361,6 +371,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
case e: Throwable => Log.e(TAG, "unable to kill " + task, e)
}
}
Tun2Socks.stop();
}
override def startRunner(c: Config) {
@@ -46,7 +46,7 @@ import scalaj.http.{HttpOptions, Http}
import com.github.shadowsocks.aidl.Config
object ConfigUtils {
val SHADOWSOCKS = "{\"server\": [%s], \"server_port\": %d, \"local_port\": %d, \"password\": %s, \"timeout\": %d}"
val SHADOWSOCKS = "{\"server\": \"%s\", \"server_port\": %d, \"local_port\": %d, \"password\": \"%s\", \"method\":\"%s\", \"timeout\": %d}"
val REDSOCKS = "base {" +
" log_debug = off;" +
" log_info = off;" +