From 0f7105afa8343654540aed8b4670a3045802b57e Mon Sep 17 00:00:00 2001 From: GreaterFire <32649575+GreaterFire@users.noreply.github.com> Date: Thu, 22 Nov 2018 18:48:36 -0800 Subject: [PATCH] Proxy Usable --- app/src/main/cpp/jni-helper.cpp | 18 +++++++++++++--- .../github/trojan_gfw/igniter/JNIHelper.java | 2 ++ .../trojan_gfw/igniter/TrojanService.java | 21 ++++++++++++------- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/app/src/main/cpp/jni-helper.cpp b/app/src/main/cpp/jni-helper.cpp index ad3d8988..26fbc861 100644 --- a/app/src/main/cpp/jni-helper.cpp +++ b/app/src/main/cpp/jni-helper.cpp @@ -8,20 +8,32 @@ using namespace std; using namespace Net2Tr; -void startTrojan(const string &filename) +static void startTrojan(const string &config) { Config c; - c.load(filename); + c.load(config); Service(c).run(); } -void startN2S(int tun_fd, const string &ip_addr, const string &netmask, const string &ip6_addr, uint16_t mtu, const string &socks5_addr, uint16_t socks5_port) +static void startN2S(int tun_fd, const string &ip_addr, const string &netmask, const string &ip6_addr, uint16_t mtu, const string &socks5_addr, uint16_t socks5_port) { N2T n2t(ip_addr, netmask, ip6_addr, mtu); N2S(tun_fd, n2t, socks5_addr, socks5_port).start(); } extern "C" { + JNIEXPORT void JNICALL Java_io_github_trojan_1gfw_igniter_JNIHelper_trojan(JNIEnv *env, jclass, jstring config) { + const char *s = env->GetStringUTFChars(config, 0); + startTrojan(s); + } + + JNIEXPORT void JNICALL Java_io_github_trojan_1gfw_igniter_JNIHelper_n2s(JNIEnv *env, jclass, jint tun_fd, jstring ip_addr, jstring netmask, jstring ip6_addr, jint mtu, jstring socks5_addr, jint socks5_port) { + const char *s1 = env->GetStringUTFChars(ip_addr, 0); + const char *s2 = env->GetStringUTFChars(netmask, 0); + const char *s3 = env->GetStringUTFChars(ip6_addr, 0); + const char *s4 = env->GetStringUTFChars(socks5_addr, 0); + startN2S(tun_fd, s1, s2, s3, uint16_t(mtu), s4, uint16_t(socks5_port)); + } } jint JNI_OnLoad(JavaVM* vm, void* reserved) diff --git a/app/src/main/java/io/github/trojan_gfw/igniter/JNIHelper.java b/app/src/main/java/io/github/trojan_gfw/igniter/JNIHelper.java index ac1c45a2..a94f158e 100644 --- a/app/src/main/java/io/github/trojan_gfw/igniter/JNIHelper.java +++ b/app/src/main/java/io/github/trojan_gfw/igniter/JNIHelper.java @@ -4,4 +4,6 @@ public class JNIHelper { static { System.loadLibrary("jni-helper"); } + public static native void trojan(String config); + public static native void n2s(int tun_fd, String ip_addr, String netmask, String ip6_addr, int mtu, String socks5_addr, int socks5_port); } diff --git a/app/src/main/java/io/github/trojan_gfw/igniter/TrojanService.java b/app/src/main/java/io/github/trojan_gfw/igniter/TrojanService.java index 004a3930..fa5d0961 100644 --- a/app/src/main/java/io/github/trojan_gfw/igniter/TrojanService.java +++ b/app/src/main/java/io/github/trojan_gfw/igniter/TrojanService.java @@ -3,11 +3,6 @@ import android.content.Intent; import android.net.VpnService; import android.os.ParcelFileDescriptor; -import android.util.Log; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.Scanner; public class TrojanService extends VpnService { @Override @@ -27,8 +22,20 @@ public int onStartCommand(Intent intent, int flags, int startId) { b.addDnsServer("1.1.1.1"); b.addDnsServer("8.8.8.8"); ParcelFileDescriptor pfd = b.establish(); - int fd = pfd.detachFd(); - String trojanConfigPath = getCacheDir() + "/config.json"; + final int fd = pfd.detachFd(); + final String trojanConfigPath = getCacheDir() + "/config.json"; + new Thread(new Runnable() { + @Override + public void run() { + JNIHelper.trojan(trojanConfigPath); + } + }).start(); + new Thread(new Runnable() { + @Override + public void run() { + JNIHelper.n2s(fd, "10.114.51.5", "255.255.255.254", "", 1500, "127.0.0.1", 1080); + } + }).start(); return START_STICKY; } }