Commit 92e72830 authored by Max Lv's avatar Max Lv

Merge branch 'master' into traffic-stat

parents 6fb8db77 e50a26f3
...@@ -43,7 +43,7 @@ public class System { ...@@ -43,7 +43,7 @@ public class System {
java.lang.System.loadLibrary("system"); java.lang.System.loadLibrary("system");
} }
public static native void exec(String cmd); public static native int exec(String cmd);
public static native String getABI(); public static native String getABI();
public static native int sendfd(int fd); public static native int sendfd(int fd);
public static native void jniclose(int fd); public static native void jniclose(int fd);
......
...@@ -214,7 +214,7 @@ include $(BUILD_STATIC_LIBRARY) ...@@ -214,7 +214,7 @@ include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS) include $(CLEAR_VARS)
REDSOCKS_SOURCES := base.c dnstc.c http-connect.c \ REDSOCKS_SOURCES := base.c http-connect.c \
log.c md5.c socks5.c \ log.c md5.c socks5.c \
base64.c http-auth.c http-relay.c main.c \ base64.c http-auth.c http-relay.c main.c \
parser.c redsocks.c socks4.c utils.c parser.c redsocks.c socks4.c utils.c
...@@ -223,7 +223,8 @@ LOCAL_STATIC_LIBRARIES := libevent ...@@ -223,7 +223,8 @@ LOCAL_STATIC_LIBRARIES := libevent
LOCAL_MODULE := redsocks LOCAL_MODULE := redsocks
LOCAL_SRC_FILES := $(addprefix redsocks/, $(REDSOCKS_SOURCES)) LOCAL_SRC_FILES := $(addprefix redsocks/, $(REDSOCKS_SOURCES))
LOCAL_CFLAGS := -O2 -std=gnu99 -I$(LOCAL_PATH)/redsocks \ LOCAL_CFLAGS := -O2 -std=gnu99 -DUSE_IPTABLES \
-I$(LOCAL_PATH)/redsocks \
-I$(LOCAL_PATH)/libevent/include \ -I$(LOCAL_PATH)/libevent/include \
-I$(LOCAL_PATH)/libevent -I$(LOCAL_PATH)/libevent
......
Subproject commit 65d039c7b302510188dc7b3d765a206c3980db51 Subproject commit 76348e933a3a15176fdb54616d04bc6fdd3c8e0b
...@@ -37,10 +37,27 @@ jstring Java_com_github_shadowsocks_system_getabi(JNIEnv *env, jobject thiz) { ...@@ -37,10 +37,27 @@ jstring Java_com_github_shadowsocks_system_getabi(JNIEnv *env, jobject thiz) {
return env->NewStringUTF(abi); return env->NewStringUTF(abi);
} }
void Java_com_github_shadowsocks_system_exec(JNIEnv *env, jobject thiz, jstring cmd) { jint Java_com_github_shadowsocks_system_exec(JNIEnv *env, jobject thiz, jstring cmd) {
const char *str = env->GetStringUTFChars(cmd, 0); const char *cmd_str = env->GetStringUTFChars(cmd, 0);
system(str);
env->ReleaseStringUTFChars(cmd, str); pid_t pid;
/* Fork off the parent process */
pid = fork();
if (pid < 0) {
env->ReleaseStringUTFChars(cmd, cmd_str);
return -1;
}
if (pid > 0) {
env->ReleaseStringUTFChars(cmd, cmd_str);
return pid;
}
execl("/system/bin/sh", "sh", "-c", cmd_str, NULL);
env->ReleaseStringUTFChars(cmd, cmd_str);
return 1;
} }
void Java_com_github_shadowsocks_system_jniclose(JNIEnv *env, jobject thiz, jint fd) { void Java_com_github_shadowsocks_system_jniclose(JNIEnv *env, jobject thiz, jint fd) {
...@@ -83,7 +100,7 @@ static JNINativeMethod method_table[] = { ...@@ -83,7 +100,7 @@ static JNINativeMethod method_table[] = {
(void*) Java_com_github_shadowsocks_system_jniclose }, (void*) Java_com_github_shadowsocks_system_jniclose },
{ "sendfd", "(I)I", { "sendfd", "(I)I",
(void*) Java_com_github_shadowsocks_system_sendfd }, (void*) Java_com_github_shadowsocks_system_sendfd },
{ "exec", "(Ljava/lang/String;)V", { "exec", "(Ljava/lang/String;)I",
(void*) Java_com_github_shadowsocks_system_exec }, (void*) Java_com_github_shadowsocks_system_exec },
{ "getABI", "()Ljava/lang/String;", { "getABI", "()Ljava/lang/String;",
(void*) Java_com_github_shadowsocks_system_getabi } (void*) Java_com_github_shadowsocks_system_getabi }
......
...@@ -75,6 +75,11 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -75,6 +75,11 @@ class ShadowsocksNatService extends Service with BaseService {
var apps: Array[ProxiedApp] = null var apps: Array[ProxiedApp] = null
val myUid = Process.myUid() val myUid = Process.myUid()
var sslocalPid: Int = 0;
var sstunnelPid: Int = 0;
var redsocksPid: Int = 0;
var pdnsdPid: Int = 0;
private val dnsAddressCache = new SparseArray[String] private val dnsAddressCache = new SparseArray[String]
def getNetId(network: Network): Int = { def getNetId(network: Network): Int = {
...@@ -184,8 +189,7 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -184,8 +189,7 @@ class ShadowsocksNatService extends Service with BaseService {
cmd += (Path.BASE + "ss-local" cmd += (Path.BASE + "ss-local"
, "-b" , "127.0.0.1" , "-b" , "127.0.0.1"
, "-t" , "600" , "-t" , "600"
, "-c" , Path.BASE + "ss-local-nat.conf" , "-c" , Path.BASE + "ss-local-nat.conf")
, "-f" , Path.BASE + "ss-local-nat.pid")
if (config.isAuth) cmd += "-A" if (config.isAuth) cmd += "-A"
...@@ -195,7 +199,7 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -195,7 +199,7 @@ class ShadowsocksNatService extends Service with BaseService {
} }
if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" ")) if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" "))
Console.runCommand(cmd.mkString(" ")) sslocalPid = System.exec(cmd.mkString(" "))
} }
def startTunnel() { def startTunnel() {
...@@ -212,8 +216,7 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -212,8 +216,7 @@ class ShadowsocksNatService extends Service with BaseService {
, "-t" , "10" , "-t" , "10"
, "-b" , "127.0.0.1" , "-b" , "127.0.0.1"
, "-L" , "8.8.8.8:53" , "-L" , "8.8.8.8:53"
, "-c" , Path.BASE + "ss-tunnel-nat.conf" , "-c" , Path.BASE + "ss-tunnel-nat.conf")
, "-f" , Path.BASE + "ss-tunnel-nat.pid")
cmd += ("-l" , "8153") cmd += ("-l" , "8153")
...@@ -221,7 +224,7 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -221,7 +224,7 @@ class ShadowsocksNatService extends Service with BaseService {
if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" ")) if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" "))
Console.runCommand(cmd.mkString(" ")) sstunnelPid = System.exec(cmd.mkString(" "))
} else { } else {
val conf = ConfigUtils val conf = ConfigUtils
...@@ -237,13 +240,13 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -237,13 +240,13 @@ class ShadowsocksNatService extends Service with BaseService {
, "-b" , "127.0.0.1" , "-b" , "127.0.0.1"
, "-l" , "8163" , "-l" , "8163"
, "-L" , "8.8.8.8:53" , "-L" , "8.8.8.8:53"
, "-c" , Path.BASE + "ss-tunnel-nat.conf" , "-c" , Path.BASE + "ss-tunnel-nat.conf")
, "-f" , Path.BASE + "ss-tunnel-nat.pid")
if (config.isAuth) cmdBuf += "-A" if (config.isAuth) cmdBuf += "-A"
if (BuildConfig.DEBUG) Log.d(TAG, cmdBuf.mkString(" ")) if (BuildConfig.DEBUG) Log.d(TAG, cmdBuf.mkString(" "))
Console.runCommand(cmdBuf.mkString(" "))
sstunnelPid = System.exec(cmdBuf.mkString(" "))
} }
} }
...@@ -253,10 +256,10 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -253,10 +256,10 @@ class ShadowsocksNatService extends Service with BaseService {
val reject = ConfigUtils.getRejectList(getContext) val reject = ConfigUtils.getRejectList(getContext)
val blackList = ConfigUtils.getBlackList(getContext) val blackList = ConfigUtils.getBlackList(getContext)
ConfigUtils.PDNSD_DIRECT.formatLocal(Locale.ENGLISH, "127.0.0.1", 8153, ConfigUtils.PDNSD_DIRECT.formatLocal(Locale.ENGLISH, "127.0.0.1", 8153,
Path.BASE + "pdnsd-nat.pid", reject, blackList, 8163, "") reject, blackList, 8163, "")
} else { } else {
ConfigUtils.PDNSD_LOCAL.formatLocal(Locale.ENGLISH, "127.0.0.1", 8153, ConfigUtils.PDNSD_LOCAL.formatLocal(Locale.ENGLISH, "127.0.0.1", 8153,
Path.BASE + "pdnsd-nat.pid", 8163, "") 8163, "")
} }
ConfigUtils.printToFile(new File(Path.BASE + "pdnsd-nat.conf"))(p => { ConfigUtils.printToFile(new File(Path.BASE + "pdnsd-nat.conf"))(p => {
...@@ -266,7 +269,7 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -266,7 +269,7 @@ class ShadowsocksNatService extends Service with BaseService {
if (BuildConfig.DEBUG) Log.d(TAG, cmd) if (BuildConfig.DEBUG) Log.d(TAG, cmd)
Console.runCommand(cmd) pdnsdPid = System.exec(cmd)
} }
...@@ -284,14 +287,14 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -284,14 +287,14 @@ class ShadowsocksNatService extends Service with BaseService {
def startRedsocksDaemon() { def startRedsocksDaemon() {
val conf = ConfigUtils.REDSOCKS.formatLocal(Locale.ENGLISH, config.localPort) val conf = ConfigUtils.REDSOCKS.formatLocal(Locale.ENGLISH, config.localPort)
val cmd = Path.BASE + "redsocks -p %sredsocks-nat.pid -c %sredsocks-nat.conf" val cmd = Path.BASE + "redsocks -c %sredsocks-nat.conf"
.formatLocal(Locale.ENGLISH, Path.BASE, Path.BASE) .formatLocal(Locale.ENGLISH, Path.BASE, Path.BASE)
ConfigUtils.printToFile(new File(Path.BASE + "redsocks-nat.conf"))(p => { ConfigUtils.printToFile(new File(Path.BASE + "redsocks-nat.conf"))(p => {
p.println(conf) p.println(conf)
}) })
if (BuildConfig.DEBUG) Log.d(TAG, cmd) if (BuildConfig.DEBUG) Log.d(TAG, cmd)
Console.runCommand(cmd) redsocksPid = System.exec(cmd)
} }
/** Called when the activity is first created. */ /** Called when the activity is first created. */
...@@ -348,27 +351,27 @@ class ShadowsocksNatService extends Service with BaseService { ...@@ -348,27 +351,27 @@ class ShadowsocksNatService extends Service with BaseService {
} }
def killProcesses() { def killProcesses() {
val cmd = new ArrayBuffer[String]() try {
Process.killProcess(sslocalPid)
for (task <- Array("ss-local", "ss-tunnel", "pdnsd", "redsocks")) { } catch {
cmd.append("chmod 666 %s%s-nat.pid".formatLocal(Locale.ENGLISH, Path.BASE, task)) case e: Throwable => Log.e(TAG, "unable to kill ss-local")
} }
Console.runRootCommand(cmd.toArray)
cmd.clear()
for (task <- Array("ss-local", "ss-tunnel", "pdnsd", "redsocks")) {
try { try {
val pid = scala.io.Source.fromFile(Path.BASE + task + "-nat.pid").mkString.trim.toInt Process.killProcess(sstunnelPid)
cmd.append("kill -9 %d".formatLocal(Locale.ENGLISH, pid))
Process.killProcess(pid)
} catch { } catch {
case e: Throwable => Log.e(TAG, "unable to kill " + task) case e: Throwable => Log.e(TAG, "unable to kill ss-tunnel")
} }
cmd.append("rm -f %s%s-nat.pid".formatLocal(Locale.ENGLISH, Path.BASE, task)) try {
cmd.append("rm -f %s%s-nat.conf".formatLocal(Locale.ENGLISH, Path.BASE, task)) Process.killProcess(redsocksPid)
} catch {
case e: Throwable => Log.e(TAG, "unable to kill redsocks")
}
try {
Process.killProcess(pdnsdPid)
} catch {
case e: Throwable => Log.e(TAG, "unable to kill pdnsd")
} }
Console.runRootCommand(cmd.toArray)
Console.runRootCommand(Utils.getIptables + " -t nat -F OUTPUT") Console.runRootCommand(Utils.getIptables + " -t nat -F OUTPUT")
} }
......
...@@ -71,6 +71,11 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -71,6 +71,11 @@ 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 sstunnelPid: Int = 0;
var tun2socksPid: Int = 0;
var pdnsdPid: Int = 0;
def isByass(net: SubnetUtils): Boolean = { def isByass(net: SubnetUtils): Boolean = {
val info = net.getInfo val info = net.getInfo
info.isInRange(config.proxy) info.isInRange(config.proxy)
...@@ -188,13 +193,25 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -188,13 +193,25 @@ class ShadowsocksVpnService extends VpnService with BaseService {
} }
def killProcesses() { def killProcesses() {
for (task <- Array("ss-local", "ss-tunnel", "pdnsd", "tun2socks")) {
try { try {
val pid = scala.io.Source.fromFile(Path.BASE + task + "-vpn.pid").mkString.trim.toInt Process.killProcess(sslocalPid)
Process.killProcess(pid)
} catch { } catch {
case e: Throwable => Log.e(TAG, "unable to kill " + task) case e: Throwable => Log.e(TAG, "unable to kill ss-local")
} }
try {
Process.killProcess(sstunnelPid)
} catch {
case e: Throwable => Log.e(TAG, "unable to kill ss-tunnel")
}
try {
Process.killProcess(tun2socksPid)
} catch {
case e: Throwable => Log.e(TAG, "unable to kill tun2socks")
}
try {
Process.killProcess(pdnsdPid)
} catch {
case e: Throwable => Log.e(TAG, "unable to kill pdnsd")
} }
} }
...@@ -316,8 +333,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -316,8 +333,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
cmd += (Path.BASE + "ss-local", "-V", "-u" cmd += (Path.BASE + "ss-local", "-V", "-u"
, "-b", "127.0.0.1" , "-b", "127.0.0.1"
, "-t", "600" , "-t", "600"
, "-c", Path.BASE + "ss-local-vpn.conf" , "-c", Path.BASE + "ss-local-vpn.conf")
, "-f", Path.BASE + "ss-local-vpn.pid")
if (config.isAuth) cmd += "-A" if (config.isAuth) cmd += "-A"
...@@ -327,7 +343,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -327,7 +343,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
} }
if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" ")) if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" "))
Console.runCommand(cmd.mkString(" ")) sslocalPid = System.exec(cmd.mkString(" "))
} }
def startDnsTunnel() = { def startDnsTunnel() = {
...@@ -345,13 +361,12 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -345,13 +361,12 @@ class ShadowsocksVpnService extends VpnService with BaseService {
, "-b", "127.0.0.1" , "-b", "127.0.0.1"
, "-l", "8163" , "-l", "8163"
, "-L", "8.8.8.8:53" , "-L", "8.8.8.8:53"
, "-c", Path.BASE + "ss-tunnel-vpn.conf" , "-c", Path.BASE + "ss-tunnel-vpn.conf")
, "-f", Path.BASE + "ss-tunnel-vpn.pid")
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(" "))
Console.runCommand(cmd.mkString(" ")) sstunnelPid = System.exec(cmd.mkString(" "))
} }
def startDnsDaemon() { def startDnsDaemon() {
...@@ -361,10 +376,10 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -361,10 +376,10 @@ class ShadowsocksVpnService extends VpnService with BaseService {
val reject = ConfigUtils.getRejectList(getContext) val reject = ConfigUtils.getRejectList(getContext)
val blackList = ConfigUtils.getBlackList(getContext) val blackList = ConfigUtils.getBlackList(getContext)
ConfigUtils.PDNSD_DIRECT.formatLocal(Locale.ENGLISH, "0.0.0.0", 8153, ConfigUtils.PDNSD_DIRECT.formatLocal(Locale.ENGLISH, "0.0.0.0", 8153,
Path.BASE + "pdnsd-vpn.pid", reject, blackList, 8163, ipv6) reject, blackList, 8163, ipv6)
} else { } else {
ConfigUtils.PDNSD_LOCAL.formatLocal(Locale.ENGLISH, "0.0.0.0", 8153, ConfigUtils.PDNSD_LOCAL.formatLocal(Locale.ENGLISH, "0.0.0.0", 8153,
Path.BASE + "pdnsd-vpn.pid", 8163, ipv6) 8163, ipv6)
} }
} }
ConfigUtils.printToFile(new File(Path.BASE + "pdnsd-vpn.conf"))(p => { ConfigUtils.printToFile(new File(Path.BASE + "pdnsd-vpn.conf"))(p => {
...@@ -373,7 +388,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -373,7 +388,7 @@ 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)
Console.runCommand(cmd) pdnsdPid = System.exec(cmd)
} }
override def getContext = getBaseContext override def getContext = getBaseContext
...@@ -446,8 +461,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -446,8 +461,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 ")
+ "--pid %stun2socks-vpn.pid")
.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)
...@@ -462,7 +476,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -462,7 +476,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
if (BuildConfig.DEBUG) Log.d(TAG, cmd) if (BuildConfig.DEBUG) Log.d(TAG, cmd)
Console.runCommand(cmd) tun2socksPid = System.exec(cmd)
fd fd
} }
......
...@@ -51,16 +51,16 @@ object ConfigUtils { ...@@ -51,16 +51,16 @@ object ConfigUtils {
" log_debug = off;" + " log_debug = off;" +
" log_info = off;" + " log_info = off;" +
" log = stderr;" + " log = stderr;" +
" daemon = on;" + " daemon = off;" +
" redirector = iptables;" + " redirector = iptables;" +
"}" + " }\n" +
"redsocks {" + "redsocks {" +
" local_ip = 127.0.0.1;" + " local_ip = 127.0.0.1;" +
" local_port = 8123;" + " local_port = 8123;" +
" ip = 127.0.0.1;" + " ip = 127.0.0.1;" +
" port = %d;" + " port = %d;" +
" type = socks5;" + " type = socks5;" +
"}" " }"
val PDNSD_LOCAL = val PDNSD_LOCAL =
""" """
...@@ -74,8 +74,7 @@ object ConfigUtils { ...@@ -74,8 +74,7 @@ object ConfigUtils {
| min_ttl = 15m; | min_ttl = 15m;
| max_ttl = 1w; | max_ttl = 1w;
| timeout = 10; | timeout = 10;
| daemon = on; | daemon = off;
| pid_file = %s;
|} |}
| |
|server { |server {
...@@ -109,8 +108,7 @@ object ConfigUtils { ...@@ -109,8 +108,7 @@ object ConfigUtils {
| min_ttl = 15m; | min_ttl = 15m;
| max_ttl = 1w; | max_ttl = 1w;
| timeout = 10; | timeout = 10;
| daemon = on; | daemon = off;
| pid_file = "%s";
|} |}
| |
|server { |server {
...@@ -153,8 +151,7 @@ object ConfigUtils { ...@@ -153,8 +151,7 @@ object ConfigUtils {
| min_ttl = 15m; | min_ttl = 15m;
| max_ttl = 1w; | max_ttl = 1w;
| timeout = 10; | timeout = 10;
| daemon = on; | daemon = off;
| pid_file = "%s";
|} |}
| |
|server { |server {
......
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