Skip to content

Commit

Permalink
convert daemon run mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
KagayamaKaede committed Nov 15, 2015
1 parent 6b612fd commit 4321a4e
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 55 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Current can't use.......................

language: android

jdk: oraclejdk8
Expand All @@ -19,3 +21,5 @@ script:
- echo "ndk.dir=${ANDROID_NDK_HOME}" >> local.properties
- ./gradlew assemble
- ./gradlew check

# Current can't use.
8 changes: 8 additions & 0 deletions PluginDevSpec.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

####开发步骤

#####准备:

1. 挑选您熟悉的语言:C,C++,Java,Kotlin。

2. 如果使用C/C++则需要利用JNI间接调用。

3. 如果使用Java或Kotlin则可以直接上手了。

#####协议插件:

1. 协议插件应写在com.proxy.shadowsocksr.impl.plugin.proto包下,并继承AbsProtocol类。根据协议类型,类名以Verify或Auth开头,以Protocol结尾。如:AuthSimpleProtocol。
Expand Down
4 changes: 2 additions & 2 deletions Readme.cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

##计划

* 完成 java 版本的 Shadowsocks.
* 完成 Kotlin 版本的 Shadowsocks.
* 完善材料设计UI.
* 提升性能,降低内存和电池使用率。
* 修复bug。
*java 版本的 ShadowsocksR 插件.
*Kotlin 版本的 ShadowsocksR 插件.

##开发

Expand Down
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ Compatible Shadowsocks protocol and support some ShadowsocksR function.

##Plans

* Full java implementation of Shadowsocks stable function.
* Full Kotlin implementation of Shadowsocks stable function.
* Improve the material design UI.
* Improve performance, reduce memory and battery usage.
* Fix bug.
* Write java implementation of ShadowsocksR plugin.
* Write Kotlin implementation of ShadowsocksR plugin.

##Develop

Expand Down
Binary file modified app/libs/armeabi-v7a/libJni.so
Binary file not shown.
Binary file modified app/libs/x86/libJni.so
Binary file not shown.
4 changes: 2 additions & 2 deletions app/src/main/java/com/proxy/shadowsocksr/Jni.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public final class Jni
System.loadLibrary("Jni");
}

//public static native void exec(String cmd);
public static native int exec(String cmd);
public static native String getABI();
public static native int sendFd(int fd);
//public static native void jniClose(int fd);
}
}
63 changes: 26 additions & 37 deletions app/src/main/java/com/proxy/shadowsocksr/SSRVPNService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.os.RemoteException;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;

import com.proxy.shadowsocksr.impl.SSRLocal;
import com.proxy.shadowsocksr.impl.SSRTunnel;
Expand All @@ -33,7 +34,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public final class SSRVPNService extends VpnService implements OnNeedProtectTCPListener,
OnNeedProtectUDPListener
Expand All @@ -43,6 +43,9 @@ public final class SSRVPNService extends VpnService implements OnNeedProtectTCPL
private final String PRIVATE_VLAN6 = "fdfe:dcba:9875::%s";
private ParcelFileDescriptor conn;

private int pdnsdPID = 0;
private int tun2socksPID = 0;

private ConnectProfile connProfile;

private volatile boolean isVPNConnected = false;
Expand Down Expand Up @@ -397,21 +400,20 @@ private void startDnsDaemon()
String reject = getResources().getString(R.string.reject);
String blklst = getResources().getString(R.string.black_list);

pdnsd = String.format(ConfFileUtil.PdNSdDirect, "0.0.0.0", 8153,
Consts.baseDir + "pdnsd-vpn.pid", reject, blklst, 8163,
pdnsd = String.format(ConfFileUtil.PdNSdDirect, "0.0.0.0", 8153, reject, blklst, 8163,
connProfile.getIpv6Route() ? "" : "reject = ::/0;");
}
else
{
pdnsd = String.format(ConfFileUtil.PdNSdLocal, "0.0.0.0", 8153,
Consts.baseDir + "pdnsd-vpn.pid", 8163,
pdnsd = String.format(ConfFileUtil.PdNSdLocal, "0.0.0.0", 8153, 8163,
connProfile.getIpv6Route() ? "" : "reject = ::/0;");
}

ConfFileUtil.Companion.writeToFile(pdnsd, new File(Consts.baseDir + "pdnsd-vpn.conf"));

String cmd = Consts.baseDir + "pdnsd -c " + Consts.baseDir + "pdnsd-vpn.conf";
new ShellUtil().runCmd(cmd);

pdnsdPID = Jni.exec(cmd);
}

private int startVpn()
Expand Down Expand Up @@ -486,10 +488,9 @@ private int startVpn()
+ " --socks-server-addr 127.0.0.1:%d"
+ " --tunfd %d"
+ " --tunmtu %d"
+ " --loglevel 0"
+ " --pid %stun2socks-vpn.pid",
+ " --loglevel 0",
String.format(PRIVATE_VLAN, "2"),
connProfile.getLocalPort(), fd, VPN_MTU, Consts.baseDir);
connProfile.getLocalPort(), fd, VPN_MTU);

if (connProfile.getIpv6Route())
{
Expand All @@ -505,7 +506,7 @@ private int startVpn()
cmd += String.format(" --dnsgw %s:8153", String.format(PRIVATE_VLAN, "1"));
}

new ShellUtil().runCmd(cmd);
tun2socksPID = Jni.exec(cmd);

return fd;
}
Expand All @@ -529,36 +530,24 @@ private void killProcesses()
udprs.stopUDPRelayServer();
udprs = null;
}

final String[] tasks = new String[]{"pdnsd", "tun2socks"};
List<String> cmds = new ArrayList<>();
String[] cmdarr;

for (String task : tasks)
//
try
{
cmds.add(String.format("chmod 666 %s%s-vpn.pid", Consts.baseDir, task));
android.os.Process.killProcess(pdnsdPID);
}
cmdarr = new String[cmds.size()];
cmds.toArray(cmdarr);
new ShellUtil().runCmd(cmdarr);
cmds.clear();

for (String t : tasks)
catch (Exception e)
{
try
{
File pidf = new File(Consts.baseDir + t + "-vpn.pid");
int pid = new Scanner(pidf).useDelimiter("\\Z").nextInt();
android.os.Process.killProcess(pid);
}
catch (Exception ignored)
{
}
cmds.add(String.format("rm -f %s%s-vpn.conf", Consts.baseDir, t));
cmds.add(String.format("rm -f %s%s-vpn.pid", Consts.baseDir, t));
Log.e("EXC", "PDNSD KILL FAILED: " + e.getMessage());
}
try
{
android.os.Process.killProcess(tun2socksPID);
}
cmdarr = new String[cmds.size()];
cmds.toArray(cmdarr);
new ShellUtil().runCmd(cmdarr);
catch (Exception e)
{
Log.e("EXC", "TUN2SOCKS KILL FAILED: " + e.getMessage());
}
//
new ShellUtil().runCmd(String.format("rm -f %spdnsd-vpn.conf", Consts.baseDir));
}
}
6 changes: 2 additions & 4 deletions app/src/main/java/com/proxy/shadowsocksr/util/ConfFileUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ class ConfFileUtil
min_ttl = 15m;
max_ttl = 1w;
timeout = 10;
daemon = on;
pid_file = %s;
daemon = off;
}
server {
Expand Down Expand Up @@ -51,8 +50,7 @@ rr {
min_ttl = 15m;
max_ttl = 1w;
timeout = 10;
daemon = on;
pid_file = %s;
daemon = off;
}
server {
Expand Down
33 changes: 25 additions & 8 deletions app/src/main/jni/Jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,29 @@

#include "jni.h"

//void Java_com_proxy_shadowsocksr_Jni_exec
// (JNIEnv *env, jobject thiz, jstring cmd) {
// const char *str = env->GetStringUTFChars(cmd, 0);
// system(str);
// env->ReleaseStringUTFChars(cmd, str);
//}
jint Java_com_proxy_shadowsocksr_Jni_exec
(JNIEnv *env, jobject thiz, jstring cmd) {
const char *cmd_str = env->GetStringUTFChars(cmd, 0);

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;
}

jstring Java_com_proxy_shadowsocksr_Jni_getABI
(JNIEnv *env, jobject thiz) {
Expand Down Expand Up @@ -74,8 +91,8 @@ static const char *classPathName = "com/proxy/shadowsocksr/Jni";
static JNINativeMethod method_table[] = {
{"sendFd", "(I)I",
(void *) Java_com_proxy_shadowsocksr_Jni_sendFd},
// {"exec", "(Ljava/lang/String;)V",
// (void *) Java_com_proxy_shadowsocksr_Jni_exec},
{"exec", "(Ljava/lang/String;)I",
(void *) Java_com_proxy_shadowsocksr_Jni_exec},
{"getABI", "()Ljava/lang/String;",
(void *) Java_com_proxy_shadowsocksr_Jni_getABI}
};
Expand Down

0 comments on commit 4321a4e

Please sign in to comment.