From 67fab0392fc7344be20c0ef3eab304edf1261333 Mon Sep 17 00:00:00 2001 From: Mygod Date: Sat, 7 Jan 2017 19:59:44 +0800 Subject: [PATCH] Let Android system extract native libraries --- build.sh | 12 +------ kcptun/make.bash | 4 +-- src/main/AndroidManifest.xml | 1 - .../java/com/github/shadowsocks/System.java | 1 - src/main/jni/Android.mk | 14 ++++---- src/main/jni/build-shared-executable.mk | 31 +++++++++++++++++ src/main/jni/system.cpp | 23 +------------ .../com/github/shadowsocks/MainActivity.scala | 1 + .../shadowsocks/ShadowsocksApplication.scala | 34 +++++++------------ .../shadowsocks/ShadowsocksNatService.scala | 12 +++---- .../shadowsocks/ShadowsocksVpnService.scala | 12 +++---- .../KcpCliPreferenceDialogFragment.scala | 2 +- .../github/shadowsocks/utils/Constants.scala | 2 ++ 13 files changed, 71 insertions(+), 78 deletions(-) create mode 100644 src/main/jni/build-shared-executable.mk diff --git a/build.sh b/build.sh index 47b5b5fb..e8ab8df4 100755 --- a/build.sh +++ b/build.sh @@ -7,19 +7,9 @@ function try () { # Build native binaries pushd src/main try $ANDROID_NDK_HOME/ndk-build -j8 - +# Clean up old binaries (no longer used) rm -rf assets/armeabi-v7a rm -rf assets/x86 -mkdir -p assets/armeabi-v7a -mkdir -p assets/x86 - -#copy executables -for app in pdnsd redsocks ss-local ss-tunnel tun2socks -do - echo $app - try mv libs/armeabi-v7a/$app assets/armeabi-v7a/ - try mv libs/x86/$app assets/x86/ -done popd # Build kcptun diff --git a/kcptun/make.bash b/kcptun/make.bash index 4240dbb3..2eb1454d 100755 --- a/kcptun/make.bash +++ b/kcptun/make.bash @@ -58,12 +58,12 @@ go get echo "Cross compile kcptun for arm" try env CGO_ENABLED=1 CC=$ANDROID_ARM_CC GOOS=android GOARCH=arm GOARM=7 go build -ldflags="-s -w" try $ANDROID_ARM_STRIP client -try mv client $DIR/../src/main/assets/armeabi-v7a/kcptun +try mv client $DIR/../src/main/libs/armeabi-v7a/libkcptun.so echo "Cross compile kcptun for x86" try env CGO_ENABLED=1 CC=$ANDROID_X86_CC GOOS=android GOARCH=386 go build -ldflags="-s -w" try $ANDROID_X86_STRIP client -try mv client $DIR/../src/main/assets/x86/kcptun +try mv client $DIR/../src/main/libs/x86/libkcptun.so popd echo "Successfully build kcptun" diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index ca53db84..5594a06b 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -19,7 +19,6 @@ NewStringUTF(abi); -} - jint Java_com_github_shadowsocks_system_exec(JNIEnv *env, jobject thiz, jstring cmd) { const char *cmd_str = env->GetStringUTFChars(cmd, 0); @@ -103,9 +84,7 @@ static JNINativeMethod method_table[] = { { "sendfd", "(ILjava/lang/String;)I", (void*) Java_com_github_shadowsocks_system_sendfd }, { "exec", "(Ljava/lang/String;)I", - (void*) Java_com_github_shadowsocks_system_exec }, - { "getABI", "()Ljava/lang/String;", - (void*) Java_com_github_shadowsocks_system_getabi } + (void*) Java_com_github_shadowsocks_system_exec } }; diff --git a/src/main/scala/com/github/shadowsocks/MainActivity.scala b/src/main/scala/com/github/shadowsocks/MainActivity.scala index 08e77d95..7bab1a25 100644 --- a/src/main/scala/com/github/shadowsocks/MainActivity.scala +++ b/src/main/scala/com/github/shadowsocks/MainActivity.scala @@ -377,6 +377,7 @@ class MainActivity extends Activity with ServiceBoundContext with Drawer.OnDrawe override def handleMessage(msg: Message): Unit = if (dialog.isShowing && !isDestroyed) dialog.dismiss() } Utils.ThrowableFuture { + app.crashRecovery() app.copyAssets() handler.sendEmptyMessage(0) } diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala index f136b50e..68ac4dac 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksApplication.scala @@ -50,8 +50,6 @@ object ShadowsocksApplication { var app: ShadowsocksApplication = _ private final val TAG = "ShadowsocksApplication" - private val EXECUTABLES = Array(Executable.PDNSD, Executable.REDSOCKS, Executable.SS_TUNNEL, Executable.SS_LOCAL, - Executable.TUN2SOCKS, Executable.KCPTUN) // The ones in Locale doesn't have script included private final lazy val SIMPLIFIED_CHINESE = @@ -180,25 +178,11 @@ class ShadowsocksApplication extends Application { if (holder != null) holder.refresh() } - private def copyAssets(path: String) { - val assetManager = getAssets - var files: Array[String] = null - try files = assetManager.list(path) catch { - case e: IOException => - Log.e(TAG, e.getMessage) - app.track(e) - } - if (files != null) for (file <- files) - autoClose(assetManager.open(if (path.nonEmpty) path + '/' + file else file))(in => - autoClose(new FileOutputStream(getApplicationInfo.dataDir + '/' + file))(out => - IOUtils.copy(in, out))) - } - def crashRecovery() { val cmd = new ArrayBuffer[String]() - for (task <- Array("ss-local", "ss-tunnel", "pdnsd", "redsocks", "tun2socks", "kcptun")) { - cmd.append("killall %s".formatLocal(Locale.ENGLISH, task)) + for (task <- Executable.EXECUTABLES) { + cmd.append("killall lib%s.so".formatLocal(Locale.ENGLISH, task)) cmd.append("rm -f %1$s/%2$s-nat.conf %1$s/%2$s-vpn.conf" .formatLocal(Locale.ENGLISH, getApplicationInfo.dataDir, task)) } @@ -212,10 +196,16 @@ class ShadowsocksApplication extends Application { } def copyAssets() { - crashRecovery() // ensure executables are killed before writing to them - copyAssets(System.getABI) - copyAssets("acl") - Shell.SH.run(EXECUTABLES.map("chmod 755 " + getApplicationInfo.dataDir + '/' + _)) + val assetManager = getAssets + var files: Array[String] = null + try files = assetManager.list("acl") catch { + case e: IOException => + Log.e(TAG, e.getMessage) + app.track(e) + } + if (files != null) for (file <- files) autoClose(assetManager.open("acl/" + file))(in => + autoClose(new FileOutputStream(getApplicationInfo.dataDir + '/' + file))(out => + IOUtils.copy(in, out))) editor.putInt(Key.currentVersionCode, BuildConfig.VERSION_CODE).apply() } diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala index 7424a21f..58ad4139 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala @@ -66,7 +66,7 @@ class ShadowsocksNatService extends BaseService { p.println(conf) }) - val cmd = ArrayBuffer[String](getApplicationInfo.dataDir + "/ss-local" + val cmd = ArrayBuffer[String](getApplicationInfo.nativeLibraryDir + "/libss-local.so" , "-b" , "127.0.0.1" , "-t" , "600" , "-P", getApplicationInfo.dataDir @@ -94,7 +94,7 @@ class ShadowsocksNatService extends BaseService { profile.host } - val cmd = ArrayBuffer[String](getApplicationInfo.dataDir + "/kcptun" + val cmd = ArrayBuffer[String](getApplicationInfo.nativeLibraryDir + "/libkcptun.so" , "-r", host + ":" + profile.kcpPort , "-l", "127.0.0.1:" + (profile.localPort + 90)) try cmd ++= Utils.translateCommandline(profile.kcpcli) catch { @@ -114,7 +114,7 @@ class ShadowsocksNatService extends BaseService { Utils.printToFile(new File(getApplicationInfo.dataDir + "/ss-tunnel-nat.conf"))(p => { p.println(conf) }) - val cmd = ArrayBuffer[String](getApplicationInfo.dataDir + "/ss-tunnel" + val cmd = ArrayBuffer[String](getApplicationInfo.nativeLibraryDir + "/libss-tunnel.so" , "-u" , "-t" , "10" , "-b" , "127.0.0.1" @@ -142,7 +142,7 @@ class ShadowsocksNatService extends BaseService { Utils.printToFile(new File(getApplicationInfo.dataDir + "/ss-tunnel-nat.conf"))(p => { p.println(conf) }) - val cmdBuf = ArrayBuffer[String](getApplicationInfo.dataDir + "/ss-tunnel" + val cmdBuf = ArrayBuffer[String](getApplicationInfo.nativeLibraryDir + "/libss-tunnel.so" , "-t" , "10" , "-b" , "127.0.0.1" , "-l" , (profile.localPort + 63).toString @@ -179,7 +179,7 @@ class ShadowsocksNatService extends BaseService { Utils.printToFile(new File(getApplicationInfo.dataDir + "/pdnsd-nat.conf"))(p => { p.println(conf) }) - val cmd = Array(getApplicationInfo.dataDir + "/pdnsd", "-c", getApplicationInfo.dataDir + "/pdnsd-nat.conf") + val cmd = Array(getApplicationInfo.nativeLibraryDir + "/libpdnsd.so", "-c", getApplicationInfo.dataDir + "/pdnsd-nat.conf") if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" ")) @@ -188,7 +188,7 @@ class ShadowsocksNatService extends BaseService { def startRedsocksDaemon() { val conf = ConfigUtils.REDSOCKS.formatLocal(Locale.ENGLISH, profile.localPort) - val cmd = Array(getApplicationInfo.dataDir + "/redsocks", "-c", getApplicationInfo.dataDir + "/redsocks-nat.conf") + val cmd = Array(getApplicationInfo.nativeLibraryDir + "/libredsocks.so", "-c", getApplicationInfo.dataDir + "/redsocks-nat.conf") Utils.printToFile(new File(getApplicationInfo.dataDir + "/redsocks-nat.conf"))(p => { p.println(conf) }) diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala index cf57613a..9218aee5 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala @@ -182,7 +182,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { profile.host } - val cmd = ArrayBuffer(getApplicationInfo.dataDir + "/kcptun" + val cmd = ArrayBuffer(getApplicationInfo.nativeLibraryDir + "/libkcptun.so" , "-r", host + ":" + profile.kcpPort , "-l", "127.0.0.1:" + (profile.localPort + 90) , "--path", protectPath) @@ -204,7 +204,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { p.println(conf) }) - val cmd = ArrayBuffer[String](getApplicationInfo.dataDir + "/ss-local", "-V", "-U" + val cmd = ArrayBuffer[String](getApplicationInfo.nativeLibraryDir + "/libss-local.so", "-V", "-U" , "-b", "127.0.0.1" , "-t", "600" , "-P", getApplicationInfo.dataDir @@ -231,7 +231,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { p.println(conf) }) - val cmd = ArrayBuffer[String](getApplicationInfo.dataDir + "/ss-local", "-V" + val cmd = ArrayBuffer[String](getApplicationInfo.nativeLibraryDir + "/libss-local.so", "-V" , "-b", "127.0.0.1" , "-t", "600" , "-P", getApplicationInfo.dataDir @@ -266,7 +266,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { Utils.printToFile(new File(getApplicationInfo.dataDir + "/ss-tunnel-vpn.conf"))(p => { p.println(conf) }) - val cmd = ArrayBuffer[String](getApplicationInfo.dataDir + "/ss-tunnel" + val cmd = ArrayBuffer[String](getApplicationInfo.nativeLibraryDir + "/libss-tunnel.so" , "-V" , "-t", "10" , "-b", "127.0.0.1" @@ -300,7 +300,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { Utils.printToFile(new File(getApplicationInfo.dataDir + "/pdnsd-vpn.conf"))(p => { p.println(conf) }) - val cmd = Array(getApplicationInfo.dataDir + "/pdnsd", "-c", getApplicationInfo.dataDir + "/pdnsd-vpn.conf") + val cmd = Array(getApplicationInfo.nativeLibraryDir + "/libpdnsd.so", "-c", getApplicationInfo.dataDir + "/pdnsd-vpn.conf") if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" ")) @@ -358,7 +358,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { val fd = conn.getFd - var cmd = ArrayBuffer[String](getApplicationInfo.dataDir + "/tun2socks", + var cmd = ArrayBuffer[String](getApplicationInfo.nativeLibraryDir + "/libtun2socks.so", "--netif-ipaddr", PRIVATE_VLAN.formatLocal(Locale.ENGLISH, "2"), "--netif-netmask", "255.255.255.0", "--socks-server-addr", "127.0.0.1:" + profile.localPort, diff --git a/src/main/scala/com/github/shadowsocks/preferences/KcpCliPreferenceDialogFragment.scala b/src/main/scala/com/github/shadowsocks/preferences/KcpCliPreferenceDialogFragment.scala index 1bbd8351..05319d9c 100644 --- a/src/main/scala/com/github/shadowsocks/preferences/KcpCliPreferenceDialogFragment.scala +++ b/src/main/scala/com/github/shadowsocks/preferences/KcpCliPreferenceDialogFragment.scala @@ -35,7 +35,7 @@ class KcpCliPreferenceDialogFragment extends EditTextPreferenceDialogFragment { super.onPrepareDialogBuilder(builder) builder.setNeutralButton("?", ((_, _) => new AlertDialog.Builder(builder.getContext) .setTitle("?") - .setMessage(Shell.SH.run(builder.getContext.getApplicationInfo.dataDir + "/kcptun --help") + .setMessage(Shell.SH.run(builder.getContext.getApplicationInfo.nativeLibraryDir + "/libkcptun.so --help") .asScala .dropWhile(line => line != "GLOBAL OPTIONS:") .drop(1) diff --git a/src/main/scala/com/github/shadowsocks/utils/Constants.scala b/src/main/scala/com/github/shadowsocks/utils/Constants.scala index beb961e4..ea504bc0 100644 --- a/src/main/scala/com/github/shadowsocks/utils/Constants.scala +++ b/src/main/scala/com/github/shadowsocks/utils/Constants.scala @@ -27,6 +27,8 @@ object Executable { val SS_TUNNEL = "ss-tunnel" val TUN2SOCKS = "tun2socks" val KCPTUN = "kcptun" + + val EXECUTABLES = Array(SS_LOCAL, SS_TUNNEL, PDNSD, REDSOCKS, TUN2SOCKS, KCPTUN) } object ConfigUtils {