diff --git a/.gitignore b/.gitignore index bae30917e5..2d6544306e 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,8 @@ src/main/libs src/main/assets/armeabi src/main/assets/armeabi-v7a src/main/assets/x86 +src/main/assets/api-16/ src/main/jni +jni/Application.mk obj libs diff --git a/.travis.yml b/.travis.yml index 86e1d9dee7..a429239f03 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: scala scala: -- 2.10.3 +- 2.10.4 before_install: - sudo apt-get update - sudo apt-get install libc6-dev-i386 @@ -17,10 +17,9 @@ before_install: - tar xf android-ndk-r9d-linux-${ARCH}.tar.bz2 - export ANDROID_NDK=`pwd`/android-ndk-r9d - export ANDROID_HOME=`pwd`/android-sdk-linux -- export PATH=${PATH}:${ANDROID_NDK}:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools +- export PATH=${PATH}:${ANDROID_NDK_HOME}:${ANDROID_HOME}/tools:${ANDROID_HOME}/platform-tools - echo "y" | android update sdk --filter tools,platform-tools,build-tools-20.0.0,android-19,extra-google-m2repository --no-ui --no-https -a - echo "y" | android update sdk --filter extra-android-m2repository --no-ui --no-https -a -- ln -s $ANDROID_HOME/build-tools/20.0.0/zipalign $ANDROID_HOME/tools/ install: - gem install --version 0.8.9 faraday - gem install travis-artifacts diff --git a/build-ndk.sh b/build-ndk.sh index c127ad7fc0..79427e1d13 100755 --- a/build-ndk.sh +++ b/build-ndk.sh @@ -1,10 +1,16 @@ #!/bin/bash + +# Target 9 +cp jni/Application.mk.9 jni/Application.mk ndk-build clean ndk-build +rm -rf src/main/assets/armeabi +rm -rf src/main/assets/armeabi-v7a +rm -rf src/main/assets/x86 mkdir -p src/main/assets/armeabi mkdir -p src/main/assets/armeabi-v7a mkdir -p src/main/assets/x86 -for app in ip6tables iptables pdnsd redsocks ss-local ss-tunnel tun2socks +for app in pdnsd redsocks do mv libs/armeabi/$app src/main/assets/armeabi/ mv libs/armeabi-v7a/$app src/main/assets/armeabi-v7a/ @@ -12,3 +18,21 @@ do done rm -rf src/main/jni mv libs src/main/jni + +# Target 16 +cp jni/Application.mk.16 jni/Application.mk +ndk-build clean +ndk-build +rm -rf src/main/assets/api-16/armeabi +rm -rf src/main/assets/api-16/armeabi-v7a +rm -rf src/main/assets/api-16/x86 +mkdir -p src/main/assets/api-16/armeabi +mkdir -p src/main/assets/api-16/armeabi-v7a +mkdir -p src/main/assets/api-16/x86 +for app in pdnsd redsocks +do + mv libs/armeabi/$app src/main/assets/api-16/armeabi/ + mv libs/armeabi-v7a/$app src/main/assets/api-16/armeabi-v7a/ + mv libs/x86/$app src/main/assets/api-16/x86/ +done + diff --git a/jni/Android.mk b/jni/Android.mk index 2d4feca04b..654be81a21 100755 --- a/jni/Android.mk +++ b/jni/Android.mk @@ -143,16 +143,30 @@ LOCAL_CFLAGS := -Wall -O2 -I$(LOCAL_PATH)/pdnsd include $(BUILD_EXECUTABLE) ######################################################## -## shadowsocks local +## pdnsd library +######################################################## + +include $(CLEAR_VARS) + +PDNSD_SOURCES := $(wildcard $(LOCAL_PATH)/pdnsd/src/*.c) main-jni.cpp + +LOCAL_MODULE := pdnsd-jni +LOCAL_SRC_FILES := $(PDNSD_SOURCES:$(LOCAL_PATH)/%=%) +LOCAL_CFLAGS := -Wall -O2 -DPDNSD_JNI -I$(LOCAL_PATH)/pdnsd + +include $(BUILD_SHARED_LIBRARY) + +######################################################## +## shadowsocks local library ######################################################## include $(CLEAR_VARS) SHADOWSOCKS_SOURCES := local.c cache.c udprelay.c encrypt.c utils.c json.c jconf.c acl.c -LOCAL_MODULE := ss-local -LOCAL_SRC_FILES := $(addprefix shadowsocks/src/, $(SHADOWSOCKS_SOURCES)) -LOCAL_CFLAGS := -Wall -O2 -fno-strict-aliasing -DUDPRELAY_LOCAL \ +LOCAL_MODULE := ss-local-jni +LOCAL_SRC_FILES := $(addprefix shadowsocks/src/, $(SHADOWSOCKS_SOURCES)) main-jni.cpp +LOCAL_CFLAGS := -Wall -O2 -fno-strict-aliasing -DUDPRELAY_LOCAL -DSSLOCAL_JNI \ -DUSE_CRYPTO_OPENSSL -DANDROID -DHAVE_CONFIG_H \ -I$(LOCAL_PATH)/libev/ \ -I$(LOCAL_PATH)/openssl/include \ @@ -163,20 +177,41 @@ LOCAL_STATIC_LIBRARIES := libev libcrypto libipset libcork LOCAL_LDLIBS := -llog -include $(BUILD_EXECUTABLE) +include $(BUILD_SHARED_LIBRARY) ######################################################## ## shadowsocks tunnel ######################################################## +# include $(CLEAR_VARS) +# +# SHADOWSOCKS_SOURCES := tunnel.c cache.c udprelay.c encrypt.c utils.c json.c jconf.c +# +# LOCAL_MODULE := ss-tunnel +# LOCAL_SRC_FILES := $(addprefix shadowsocks/src/, $(SHADOWSOCKS_SOURCES)) +# LOCAL_CFLAGS := -Wall -O2 -fno-strict-aliasing -DUDPRELAY_LOCAL -DUDPRELAY_TUNNEL \ +# -DUSE_CRYPTO_OPENSSL -DANDROID -DHAVE_CONFIG_H \ +# -I$(LOCAL_PATH)/libev/ \ +# -I$(LOCAL_PATH)/openssl/include +# +# LOCAL_STATIC_LIBRARIES := libev libcrypto +# +# LOCAL_LDLIBS := -llog +# +# include $(BUILD_EXECUTABLE) + +######################################################## +## shadowsocks tunnel library +######################################################## + include $(CLEAR_VARS) SHADOWSOCKS_SOURCES := tunnel.c cache.c udprelay.c encrypt.c utils.c json.c jconf.c -LOCAL_MODULE := ss-tunnel -LOCAL_SRC_FILES := $(addprefix shadowsocks/src/, $(SHADOWSOCKS_SOURCES)) +LOCAL_MODULE := ss-tunnel-jni +LOCAL_SRC_FILES := $(addprefix shadowsocks/src/, $(SHADOWSOCKS_SOURCES)) main-jni.cpp LOCAL_CFLAGS := -Wall -O2 -fno-strict-aliasing -DUDPRELAY_LOCAL -DUDPRELAY_TUNNEL \ - -DUSE_CRYPTO_OPENSSL -DANDROID -DHAVE_CONFIG_H \ + -DUSE_CRYPTO_OPENSSL -DANDROID -DHAVE_CONFIG_H -DSSTUNNEL_JNI \ -I$(LOCAL_PATH)/libev/ \ -I$(LOCAL_PATH)/openssl/include @@ -184,7 +219,7 @@ LOCAL_STATIC_LIBRARIES := libev libcrypto LOCAL_LDLIBS := -llog -include $(BUILD_EXECUTABLE) +include $(BUILD_SHARED_LIBRARY) ######################################################## ## system @@ -204,7 +239,7 @@ LOCAL_STATIC_LIBRARIES := cpufeatures include $(BUILD_SHARED_LIBRARY) ######################################################## -## tun2socks +## tun2socks-library ######################################################## include $(CLEAR_VARS) @@ -214,6 +249,7 @@ LOCAL_CFLAGS += -DBADVPN_THREADWORK_USE_PTHREAD -DBADVPN_LINUX -DBADVPN_BREACTOR LOCAL_CFLAGS += -DBADVPN_USE_SELFPIPE -DBADVPN_USE_EPOLL LOCAL_CFLAGS += -DBADVPN_LITTLE_ENDIAN -DBADVPN_THREAD_SAFE LOCAL_CFLAGS += -DNDEBUG -DANDROID +LOCAL_CFLAGS += -DTUN2SOCKS_JNI LOCAL_C_INCLUDES:= \ $(LOCAL_PATH)/badvpn/lwip/src/include/ipv4 \ @@ -283,13 +319,13 @@ TUN2SOCKS_SOURCES := \ tun2socks/SocksUdpGwClient.c \ udpgw_client/UdpGwClient.c -LOCAL_MODULE := tun2socks +LOCAL_MODULE := tun2socks-jni LOCAL_LDLIBS := -ldl -llog -LOCAL_SRC_FILES := $(addprefix badvpn/, $(TUN2SOCKS_SOURCES)) +LOCAL_SRC_FILES := $(addprefix badvpn/, $(TUN2SOCKS_SOURCES)) main-jni.cpp -include $(BUILD_EXECUTABLE) +include $(BUILD_SHARED_LIBRARY) # OpenSSL openssl_subdirs := $(addprefix $(LOCAL_PATH)/openssl/,$(addsuffix /Android.mk, \ @@ -299,13 +335,13 @@ openssl_subdirs := $(addprefix $(LOCAL_PATH)/openssl/,$(addsuffix /Android.mk, \ include $(openssl_subdirs) # Iptables -LOCAL_PATH := $(ROOT_PATH) -iptables_subdirs := $(addprefix $(LOCAL_PATH)/iptables/,$(addsuffix /Android.mk, \ - iptables \ - extensions \ - libiptc \ - )) -include $(iptables_subdirs) +# LOCAL_PATH := $(ROOT_PATH) +# iptables_subdirs := $(addprefix $(LOCAL_PATH)/iptables/,$(addsuffix /Android.mk, \ +# iptables \ +# extensions \ +# libiptc \ +# )) +# include $(iptables_subdirs) # Import cpufeatures $(call import-module,android/cpufeatures) diff --git a/jni/Application.mk.16 b/jni/Application.mk.16 new file mode 100644 index 0000000000..7384409424 --- /dev/null +++ b/jni/Application.mk.16 @@ -0,0 +1,4 @@ +APP_ABI := armeabi armeabi-v7a x86 +APP_PLATFORM := android-16 +APP_STL := stlport_static +NDK_TOOLCHAIN_VERSION := 4.8 diff --git a/jni/Application.mk b/jni/Application.mk.9 similarity index 100% rename from jni/Application.mk rename to jni/Application.mk.9 diff --git a/jni/main-jni.cpp b/jni/main-jni.cpp new file mode 100644 index 0000000000..fa0298ac90 --- /dev/null +++ b/jni/main-jni.cpp @@ -0,0 +1,84 @@ +#include +#include +#include + +typedef unsigned short char16_t; + +class String8 { +public: + String8() { + mString = 0; + } + + ~String8() { + if (mString) { + free(mString); + } + } + + void set(const char16_t* o, size_t numChars) { + if (mString) { + free(mString); + } + mString = (char*) malloc(numChars + 1); + if (!mString) { + return; + } + for (size_t i = 0; i < numChars; i++) { + mString[i] = (char) o[i]; + } + mString[numChars] = '\0'; + } + + const char* string() { + return mString; + } +private: + char* mString; +}; + +extern "C" { + +int main(int argc, char* args[]); + +#if defined(PDNSD_JNI) +jint Java_com_github_shadowsocks_Core_pdnsd(JNIEnv *env, jobject thiz, jobjectArray argv) { +#elif defined(SSLOCAL_JNI) +jint Java_com_github_shadowsocks_Core_sslocal(JNIEnv *env, jobject thiz, jobjectArray argv) { +#elif defined(SSTUNNEL_JNI) +jint Java_com_github_shadowsocks_Core_sstunnel(JNIEnv *env, jobject thiz, jobjectArray argv) { +#elif defined(TUN2SOCKS_JNI) +jint Java_com_github_shadowsocks_Core_tun2socks(JNIEnv *env, jobject thiz, jobjectArray argv) { +#else +#error "invalid header" +#endif + + int argc = argv ? env->GetArrayLength(argv) : 0; + char **args = NULL; + String8 tmp_8; + + if (argc > 0) { + args = (char **)malloc((argc+1)*sizeof(char *)); + for (int i = 0; i < argc; ++i) { + jstring arg = reinterpret_cast(env->GetObjectArrayElement(argv, i)); + const jchar *str = env->GetStringCritical(arg, 0); + tmp_8.set(str, env->GetStringLength(arg)); + env->ReleaseStringCritical(arg, str); + args[i] = strdup(tmp_8.string()); + } + args[argc] = NULL; + + pid_t pid; + pid = fork(); + if (pid == 0) { + int ret = main(argc, args); + return ret; + } + + if (args != NULL) free(args); + } + + return 0; +} + +} diff --git a/project/build.properties b/project/build.properties index 9b860e23c5..be6c454fba 100644 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=0.12.3 +sbt.version=0.13.5 diff --git a/project/plugins.sbt b/project/plugins.sbt index b552dddb4a..c69b7e51a9 100644 --- a/project/plugins.sbt +++ b/project/plugins.sbt @@ -1,5 +1,5 @@ resolvers += Resolver.url("scalasbt releases", new URL("http://scalasbt.artifactoryonline.com/scalasbt/sbt-plugin-snapshots"))(Resolver.ivyStylePatterns) -addSbtPlugin("com.hanhuy.sbt" % "android-sdk-plugin" % "1.2.16") +addSbtPlugin("com.hanhuy.sbt" % "android-sdk-plugin" % "1.2.20") addSbtPlugin("com.github.mpeltonen" % "sbt-idea" % "1.6.0") diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index 31a5db16c0..2a85dcaef4 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -1,7 +1,7 @@ + android:versionCode="68" + android:versionName="2.2.0"> diff --git a/src/main/java/com/github/shadowsocks/Core.java b/src/main/java/com/github/shadowsocks/Core.java new file mode 100755 index 0000000000..792638cdbf --- /dev/null +++ b/src/main/java/com/github/shadowsocks/Core.java @@ -0,0 +1,53 @@ +/* Shadowsocks - A shadowsocks client for Android + * Copyright (C) 2012 + * + * 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 . + * + * + * ___====-_ _-====___ + * _--^^^#####// \\#####^^^--_ + * _-^##########// ( ) \\##########^-_ + * -############// |\^^/| \\############- + * _/############// (@::@) \\############\_ + * /#############(( \\// ))#############\ + * -###############\\ (oo) //###############- + * -#################\\ / VV \ //#################- + * -###################\\/ \//###################- + * _#/|##########/\######( /\ )######/\##########|\#_ + * |/ |#/\#/\#/\/ \#/\##\ | | /##/\#/ \/\#/\#/\#| \| + * ` |/ V V ` V \#\| | | |/#/ V ' V V \| ' + * ` ` ` ` / | | | | \ ' ' ' ' + * ( | | | | ) + * __\ | | | | /__ + * (vvv(VVV)(VVV)vvv) + * + * HERE BE DRAGONS + * + */ + +package com.github.shadowsocks; + +public class Core { + static { + java.lang.System.loadLibrary("ss-local-jni"); + java.lang.System.loadLibrary("ss-tunnel-jni"); + java.lang.System.loadLibrary("tun2socks-jni"); + java.lang.System.loadLibrary("pdnsd-jni"); + } + + public static native void sslocal(String[] cmd); + public static native void sstunnel(String[] cmd); + public static native void tun2socks(String[] cmd); + public static native void pdnsd(String[] cmd); +} diff --git a/src/main/scala/com/github/shadowsocks/BaseService.scala b/src/main/scala/com/github/shadowsocks/BaseService.scala index e955d2e170..19da9814f3 100644 --- a/src/main/scala/com/github/shadowsocks/BaseService.scala +++ b/src/main/scala/com/github/shadowsocks/BaseService.scala @@ -49,8 +49,8 @@ import android.content.Context trait BaseService { - var state = State.INIT - var callbackCount = 0 + @volatile var state = State.INIT + @volatile var callbackCount = 0 final val callbacks = new RemoteCallbackList[IShadowsocksServiceCallback] @@ -105,15 +105,17 @@ trait BaseService { handler.post(new Runnable { override def run() { if (state != s) { - val n = callbacks.beginBroadcast() - for (i <- 0 to n - 1) { - try { - callbacks.getBroadcastItem(i).stateChanged(s, msg) - } catch { - case _: Exception => // Ignore + if (callbackCount > 0) { + val n = callbacks.beginBroadcast() + for (i <- 0 to n - 1) { + try { + callbacks.getBroadcastItem(i).stateChanged(s, msg) + } catch { + case _: Exception => // Ignore + } } + callbacks.finishBroadcast() } - callbacks.finishBroadcast() state = s } } diff --git a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala index d087bf4e33..b0db7bf53d 100644 --- a/src/main/scala/com/github/shadowsocks/Shadowsocks.scala +++ b/src/main/scala/com/github/shadowsocks/Shadowsocks.scala @@ -38,41 +38,37 @@ */ package com.github.shadowsocks +import java.util +import java.io.{OutputStream, InputStream, ByteArrayInputStream, ByteArrayOutputStream, IOException, FileOutputStream} + +import android.app.backup.BackupManager import android.app.{Activity, AlertDialog, ProgressDialog} import android.content._ +import android.content.pm.{PackageInfo, PackageManager} import android.content.res.AssetManager -import android.graphics.{Color, Bitmap, Typeface} +import android.graphics.{Bitmap, Color, Typeface} +import android.net.{Uri, VpnService} import android.os._ import android.preference._ -import android.util.{Log, DisplayMetrics} +import android.util.{DisplayMetrics, Log} import android.view._ +import android.webkit.{WebView, WebViewClient} import android.widget._ -import com.google.analytics.tracking.android.{MapBuilder, EasyTracker} -import de.keyboardsurfer.android.widget.crouton.{Crouton, Style, Configuration} -import java.util.Hashtable import com.actionbarsherlock.app.SherlockPreferenceActivity -import org.jraf.android.backport.switchwidget.Switch -import android.content.pm.{PackageInfo, PackageManager} -import android.net.{Uri, VpnService} -import android.webkit.{WebViewClient, WebView} -import android.app.backup.BackupManager -import scala.concurrent.ops._ +import com.github.shadowsocks.aidl.{IShadowsocksService, IShadowsocksServiceCallback} +import com.github.shadowsocks.database._ +import com.github.shadowsocks.preferences.{PasswordEditTextPreference, ProfileEditTextPreference, SummaryEditTextPreference} +import com.github.shadowsocks.utils._ +import com.google.analytics.tracking.android.{EasyTracker, MapBuilder} import com.google.android.gms.ads.{AdRequest, AdSize, AdView} +import com.google.zxing.integration.android.IntentIntegrator +import com.nostra13.universalimageloader.core.download.BaseImageDownloader +import de.keyboardsurfer.android.widget.crouton.{Configuration, Crouton, Style} import net.simonvt.menudrawer.MenuDrawer +import org.jraf.android.backport.switchwidget.Switch -import com.github.shadowsocks.database._ import scala.collection.mutable.{ArrayBuffer, ListBuffer} -import com.github.shadowsocks.database.Profile -import com.nostra13.universalimageloader.core.download.BaseImageDownloader -import com.github.shadowsocks.preferences.{ProfileEditTextPreference, PasswordEditTextPreference, SummaryEditTextPreference} -import com.github.shadowsocks.utils._ -import com.google.zxing.integration.android.IntentIntegrator -import com.github.shadowsocks.aidl.{IShadowsocksServiceCallback, IShadowsocksService} -import java.io._ -import scala.Some -import com.github.shadowsocks.database.Item -import com.github.shadowsocks.database.Category -import com.github.shadowsocks.utils.Console +import scala.concurrent.ops._ class ProfileIconDownloader(context: Context, connectTimeout: Int, readTimeout: Int) extends BaseImageDownloader(context, connectTimeout, readTimeout) { @@ -113,7 +109,7 @@ object Typefaces { } private final val TAG = "Typefaces" - private final val cache = new Hashtable[String, Typeface] + private final val cache = new util.Hashtable[String, Typeface] } object Shadowsocks { @@ -128,8 +124,7 @@ object Shadowsocks { val FEATRUE_PREFS = Array(Key.isGFWList, Key.isGlobalProxy, Key.proxyedApps, Key.isTrafficStat, Key.isUdpDns, Key.isAutoConnect) - val EXECUTABLES = Array(Executable.IPTABLES, Executable.PDNSD, Executable.REDSOCKS, - Executable.SS_LOCAL, Executable.SS_TUNNEL, Executable.TUN2SOCKS) + val EXECUTABLES = Array(Executable.PDNSD, Executable.REDSOCKS) // Helper functions def updateListPreference(pref: Preference, value: String) { @@ -332,10 +327,6 @@ class Shadowsocks ab.append("kill -9 `cat /data/data/com.github.shadowsocks/ss-local.pid`") ab.append("kill -9 `cat /data/data/com.github.shadowsocks/ss-tunnel.pid`") ab.append("kill -9 `cat /data/data/com.github.shadowsocks/tun2socks.pid`") - ab.append("killall -9 pdnsd") - ab.append("killall -9 ss-local") - ab.append("killall -9 ss-tunnel") - ab.append("killall -9 tun2socks") ab.append("rm /data/data/com.github.shadowsocks/pdnsd.conf") ab.append("rm /data/data/com.github.shadowsocks/pdnsd.cache") @@ -343,7 +334,6 @@ class Shadowsocks ab.clear() ab.append("kill -9 `cat /data/data/com.github.shadowsocks/redsocks.pid`") - ab.append("killall -9 redsocks") ab.append("rm /data/data/com.github.shadowsocks/redsocks.conf") ab.append(Utils.getIptables + " -t nat -F OUTPUT") @@ -427,7 +417,7 @@ class Shadowsocks if (settings.getString(Key.proxy, "") == "198.199.101.152") { val layoutView = { if (Build.VERSION.SDK_INT > 10) { - drawer.getContentContainer.asInstanceOf[ViewGroup].getChildAt(0) + drawer.getContentContainer.getChildAt(0) } else { getLayoutView(drawer.getContentContainer.getParent) } @@ -528,9 +518,9 @@ class Shadowsocks } catch { case ignored: RemoteException => // Nothing } - unbindService(connection) bgService = null } + unbindService(connection) } override def onRestoreInstanceState(inState: Bundle) { @@ -803,15 +793,39 @@ class Shadowsocks new BackupManager(this).dataChanged() } + def copyToSystem() { + val ab = new ArrayBuffer[String] + ab.append("mount -o rw,remount -t yaffs2 /dev/block/mtdblock3 /system") + for (executable <- Shadowsocks.EXECUTABLES) { + ab.append("cp %s%s /system/bin/".format(Path.BASE, executable)) + ab.append("chmod 755 /system/bin/" + executable) + ab.append("chown root:shell /system/bin/" + executable) + } + ab.append("mount -o ro,remount -t yaffs2 /dev/block/mtdblock3 /system") + Console.runRootCommand(ab.toArray) + + } + def reset() { + crash_recovery() - copyAssets(System.getABI) + + if (Build.VERSION.SDK_INT >= 16) { + copyAssets("api-16/" + System.getABI) + } else { + copyAssets(System.getABI) + } + + if (Build.VERSION.SDK_INT >= 20) { + copyToSystem() + } val ab = new ArrayBuffer[String] for (executable <- Shadowsocks.EXECUTABLES) { ab.append("chmod 755 " + Path.BASE + executable) } Console.runCommand(ab.toArray) + } private def recovery() { diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala index 39137b7524..b6ddbc5423 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksNatService.scala @@ -118,19 +118,23 @@ class ShadowsocksNatService extends Service with BaseService { }) } - val args = (Path.BASE + - "ss-local -b 127.0.0.1 -s '%s' -p '%d' -l '%d' -k ''%s' -m '%s' -f " + + val raw_args = ("ss-local -b 127.0.0.1 -s %s -p %d -l %d -k %s -m %s -f " + Path.BASE + "ss-local.pid") .format(config.proxy, config.remotePort, config.localPort, config.sitekey, config.encMethod) + val args = if (Build.VERSION.SDK_INT >= 20) { + raw_args + } else { + Path.BASE + raw_args + } val cmd = if (config.isGFWList && isACLEnabled) args + " --acl " + Path.BASE + "chn.acl" else args if (BuildConfig.DEBUG) Log.d(TAG, cmd) - Console.runCommand(cmd) + + Core.sslocal(cmd.split(" ")) } def startDnsDaemon() { - val cmd = if (config.isUdpDns) { - (Path.BASE + - "ss-tunnel -b 127.0.0.1 -s '%s' -p '%d' -l '%d' -k '%s' -m '%s' -L 8.8.8.8:53 -u -f " + + val args = if (config.isUdpDns) { + ("ss-tunnel -b 127.0.0.1 -s %s -p %d -l %d -k %s -m %s -L 8.8.8.8:53 -u -f " + Path.BASE + "ss-tunnel.pid") .format(config.proxy, config.remotePort, 8153, config.sitekey, config.encMethod) } else { @@ -143,10 +147,22 @@ class ShadowsocksNatService extends Service with BaseService { ConfigUtils.printToFile(new File(Path.BASE + "pdnsd.conf"))(p => { p.println(conf) }) - Path.BASE + "pdnsd -c " + Path.BASE + "pdnsd.conf" + "pdnsd -c " + Path.BASE + "pdnsd.conf" } + + val cmd = if (Build.VERSION.SDK_INT >= 20) { + "/system/bin/" + args + } else { + Path.BASE + args + } + if (BuildConfig.DEBUG) Log.d(TAG, cmd) - Console.runRootCommand(cmd) + + if (config.isUdpDns) { + Core.sstunnel(cmd.split(" ")) + } else { + Console.runRootCommand(cmd) + } } def getVersionName: String = { @@ -163,21 +179,26 @@ class ShadowsocksNatService extends Service with BaseService { def startRedsocksDaemon() { val conf = ConfigUtils.REDSOCKS.format(config.localPort) - val cmd = "%sredsocks -p %sredsocks.pid -c %sredsocks.conf" - .format(Path.BASE, Path.BASE, Path.BASE) + val args = "redsocks -p %sredsocks.pid -c %sredsocks.conf" + .format(Path.BASE, Path.BASE) ConfigUtils.printToFile(new File(Path.BASE + "redsocks.conf"))(p => { p.println(conf) }) + val cmd = if (Build.VERSION.SDK_INT >= 20) { + "/system/bin/" + args + } else { + Path.BASE + args + } Console.runRootCommand(cmd) } /** Called when the activity is first created. */ def handleConnection: Boolean = { - startShadowsocksDaemon() startDnsDaemon() startRedsocksDaemon() - setupIptables + startShadowsocksDaemon() + setupIptables() flushDNS() true @@ -288,36 +309,38 @@ class ShadowsocksNatService extends Service with BaseService { } } + override def onStartCommand(intent: Intent, flags: Int, startId: Int): Int = { + Service.START_STICKY + } + + override def onTaskRemoved(intent: Intent) { + stopRunner() + } + def killProcesses() { 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 -9 `cat " + Path.BASE + "ss-tunnel.pid`") - ab.append("killall -9 ss-tunnel") ab.append("kill -15 `cat " + Path.BASE + "pdnsd.pid`") ab.append("killall -15 pdnsd") Console.runRootCommand(ab.toArray) - ab.clear() + ab.clear() ab.append("kill -9 `cat " + Path.BASE + "ss-local.pid`") - ab.append("killall -9 ss-local") Console.runCommand(ab.toArray) } - override def onStartCommand(intent: Intent, flags: Int, startId: Int): Int = { - Service.START_STICKY - } - def flushDNS() { Console.runRootCommand(Array("ndc resolver flushdefaultif", "ndc resolver flushif wlan0")) } - def setupIptables: Boolean = { + def setupIptables() = { val init_sb = new ArrayBuffer[String] val http_sb = new ArrayBuffer[String] @@ -384,7 +407,6 @@ class ShadowsocksNatService extends Service with BaseService { } Console.runRootCommand(init_sb.toArray) Console.runRootCommand(http_sb.toArray) - true } /** @@ -435,9 +457,7 @@ class ShadowsocksNatService extends Service with BaseService { filter.addAction(Action.CLOSE) receiver = new BroadcastReceiver() { def onReceive(p1: Context, p2: Intent) { - spawn { - stopRunner() - } + stopRunner() } } registerReceiver(receiver, filter) @@ -562,7 +582,6 @@ class ShadowsocksNatService extends Service with BaseService { override def stopBackgroundService() { stopRunner() - stopSelf() } override def getTag = TAG diff --git a/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala b/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala index 9619559241..87105c0e0c 100644 --- a/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala +++ b/src/main/scala/com/github/shadowsocks/ShadowsocksVpnService.scala @@ -52,12 +52,12 @@ import org.apache.http.conn.util.InetAddressUtils import android.os.Message import scala.concurrent.ops._ import org.apache.commons.net.util.SubnetUtils -import java.net.InetAddress import com.github.shadowsocks.utils._ import scala.Some import com.github.shadowsocks.aidl.{IShadowsocksService, Config} import scala.collection.mutable.ArrayBuffer import java.io.File +import java.net.InetAddress class ShadowsocksVpnService extends VpnService with BaseService { @@ -82,11 +82,11 @@ class ShadowsocksVpnService extends VpnService with BaseService { def startShadowsocksDaemon() { val cmd: String = (Path.BASE + - "ss-local -b 127.0.0.1 -s '%s' -p '%d' -l '%d' -k '%s' -m '%s' -u -f " + + "ss-local -b 127.0.0.1 -s %s -p %d -l %d -k %s -m %s -u -f " + Path.BASE + "ss-local.pid") .format(config.proxy, config.remotePort, config.localPort, config.sitekey, config.encMethod) if (BuildConfig.DEBUG) Log.d(TAG, cmd) - System.exec(cmd) + Core.sslocal(cmd.split(" ")) } def startDnsDaemon() { @@ -101,7 +101,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { }) val cmd = Path.BASE + "pdnsd -c " + Path.BASE + "pdnsd.conf" if (BuildConfig.DEBUG) Log.d(TAG, cmd) - System.exec(cmd) + Core.pdnsd(cmd.split(" ")) } def getVersionName: String = { @@ -118,8 +118,13 @@ class ShadowsocksVpnService extends VpnService with BaseService { def startVpn() { + val openIntent: Intent = new Intent(this, classOf[Shadowsocks]) + openIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) + val configIntent = PendingIntent.getActivity(this, 0, openIntent, 0) + val builder = new Builder() builder + .setConfigureIntent(configIntent) .setSession(config.profileName) .setMtu(VPN_MTU) .addAddress(PRIVATE_VLAN.format("1"), 24) @@ -202,10 +207,9 @@ class ShadowsocksVpnService extends VpnService with BaseService { else cmd += " --dnsgw %s:8153".format(PRIVATE_VLAN.format("1")) - if (BuildConfig.DEBUG) - Log.d(TAG, cmd) + if (BuildConfig.DEBUG) Log.d(TAG, cmd) - System.exec(cmd) + Core.tun2socks(cmd.split(" ")) } /** Called when the activity is first created. */ @@ -249,7 +253,14 @@ class ShadowsocksVpnService extends VpnService with BaseService { notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) .asInstanceOf[NotificationManager] + } + + override def onStartCommand(intent: Intent, flags: Int, startId: Int): Int = { + Service.START_STICKY + } + override def onTaskRemoved(intent: Intent) { + stopRunner() } override def onRevoke() { @@ -260,19 +271,12 @@ class ShadowsocksVpnService extends VpnService with BaseService { val ab = new ArrayBuffer[String] ab.append("kill -9 `cat " + Path.BASE + "ss-local.pid`") - ab.append("killall -9 ss-local") ab.append("kill -9 `cat " + Path.BASE + "tun2socks.pid`") - ab.append("killall -9 tun2socks") ab.append("kill -15 `cat " + Path.BASE + "pdnsd.pid`") - ab.append("killall -15 pdnsd") Console.runCommand(ab.toArray) } - override def onStartCommand(intent: Intent, flags: Int, startId: Int): Int = { - Service.START_STICKY - } - override def startRunner(c: Config) { config = c @@ -298,9 +302,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { filter.addAction(Intent.ACTION_SHUTDOWN) receiver = new BroadcastReceiver { def onReceive(p1: Context, p2: Intent) { - spawn { - stopRunner() - } + stopRunner() } } registerReceiver(receiver, filter) @@ -387,7 +389,6 @@ class ShadowsocksVpnService extends VpnService with BaseService { override def stopBackgroundService() { stopRunner() - stopSelf() } override def getTag = TAG