Skip to content

Commit

Permalink
Stop mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
GreaterFire committed Dec 7, 2018
1 parent 88812b3 commit c976563
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 45 deletions.
57 changes: 50 additions & 7 deletions app/src/main/cpp/jni-helper.cpp
Original file line number Diff line number Diff line change
@@ -1,38 +1,81 @@
#include "jni.h"
#include <cstdint>
#include <string>
#include <thread>
#include <config.h>
#include <service.h>
#include <n2t/n2t.h>
#include <n2t/n2s.h>
using namespace std;
using namespace Net2Tr;

static thread *trojanThread = nullptr;
static thread *n2sThread = nullptr;
static Config *trojanConfig = nullptr;
static Service *trojanService = nullptr;
static N2T *n2tService = nullptr;
static N2S *n2sService = nullptr;

static void startTrojan(const string &config)
{
Config c;
c.load(config);
Service(c).run();
trojanConfig = new Config();
trojanConfig->load(config);
trojanService = new Service(*trojanConfig);
trojanService->run();
}

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();
n2tService = new N2T(ip_addr, netmask, ip6_addr, mtu);
n2sService = new N2S(tun_fd, *n2tService, socks5_addr, socks5_port);
n2sService->start();
}

extern "C" {
JNIEXPORT void JNICALL Java_io_github_trojan_1gfw_igniter_JNIHelper_trojan(JNIEnv *env, jclass, jstring config) {
if (trojanThread != nullptr)
return;
const char *s = env->GetStringUTFChars(config, 0);
startTrojan(s);
string a(s);
env->ReleaseStringUTFChars(config, s);
trojanThread = new thread(startTrojan, a);
}

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) {
if (n2sThread != nullptr)
return;
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));
string a(s1);
string b(s2);
string c(s3);
string d(s4);
env->ReleaseStringUTFChars(socks5_addr, s4);
env->ReleaseStringUTFChars(ip6_addr, s3);
env->ReleaseStringUTFChars(netmask, s2);
env->ReleaseStringUTFChars(ip_addr, s1);
n2sThread = new thread(startN2S, tun_fd, a, b, c, uint16_t(mtu), d, uint16_t(socks5_port));
}

JNIEXPORT void JNICALL Java_io_github_trojan_1gfw_igniter_JNIHelper_stop(JNIEnv *env, jclass) {
if (n2sThread != nullptr) {
n2sService->stop();
n2sThread->join();
delete n2sService;
delete n2tService;
delete n2sThread;
n2sThread = nullptr;
}
if (trojanThread != nullptr) {
trojanService->stop();
trojanThread->join();
delete trojanService;
delete trojanConfig;
delete trojanThread;
trojanThread = nullptr;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/main/cpp/libn2t
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ public class JNIHelper {
}
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);
public static native void stop();
}
43 changes: 24 additions & 19 deletions app/src/main/java/io/github/trojan_gfw/igniter/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

public class MainActivity extends AppCompatActivity {

EditText remoteAddrText;
EditText remotePortText;
EditText passwordText;
Button startStopButton;
private EditText remoteAddrText;
private EditText remotePortText;
private EditText passwordText;
private Button startStopButton;

private static String getConfig(String remoteAddr, short remotePort, String password) {
try {
Expand Down Expand Up @@ -52,22 +52,27 @@ protected void onCreate(Bundle savedInstanceState) {
startStopButton = findViewById(R.id.startStopButton);
startStopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String config = getConfig(remoteAddrText.getText().toString(),
Short.parseShort(remotePortText.getText().toString()),
passwordText.getText().toString());
File file = new File(getCacheDir(), "config.json");
try {
FileOutputStream os = new FileOutputStream(file);
os.write(config.getBytes());
os.close();
} catch (Exception e) {
e.printStackTrace();
}
Intent i = VpnService.prepare(getApplicationContext());
if (i != null) {
startActivityForResult(i, 0);
TrojanService serviceInstance = TrojanService.getInstance();
if (serviceInstance == null) {
String config = getConfig(remoteAddrText.getText().toString(),
Short.parseShort(remotePortText.getText().toString()),
passwordText.getText().toString());
File file = new File(getCacheDir(), "config.json");
try {
FileOutputStream os = new FileOutputStream(file);
os.write(config.getBytes());
os.close();
} catch (Exception e) {
e.printStackTrace();
}
Intent i = VpnService.prepare(getApplicationContext());
if (i != null) {
startActivityForResult(i, 0);
} else {
onActivityResult(0, Activity.RESULT_OK, null);
}
} else {
onActivityResult(0, Activity.RESULT_OK, null);
serviceInstance.stop();
}
}
});
Expand Down
47 changes: 32 additions & 15 deletions app/src/main/java/io/github/trojan_gfw/igniter/TrojanService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,25 @@
import android.os.ParcelFileDescriptor;

public class TrojanService extends VpnService {
private static TrojanService instance;
private ParcelFileDescriptor pfd;

@Override
public void onCreate() {
super.onCreate();
instance = this;
}

@Override
public void onDestroy() {
super.onDestroy();
instance = null;
}

public static TrojanService getInstance() {
return instance;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
VpnService.Builder b = new VpnService.Builder();
Expand All @@ -21,21 +40,19 @@ public int onStartCommand(Intent intent, int flags, int startId) {
b.addDnsServer("8.8.4.4");
b.addDnsServer("1.1.1.1");
b.addDnsServer("8.8.8.8");
ParcelFileDescriptor pfd = b.establish();
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();
pfd = b.establish();
JNIHelper.trojan(getCacheDir() + "/config.json");
JNIHelper.n2s(pfd.getFd(), "10.114.51.5", "255.255.255.254", "", 1500, "127.0.0.1", 1080);
return START_STICKY;
}

public void stop() {
try {
JNIHelper.stop();
pfd.close();
} catch (Exception e) {
e.printStackTrace();
}
stopSelf();
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
android:id="@+id/startStopButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/start" />
android:text="@string/startStop" />
</LinearLayout>
</ScrollView>

Expand Down
3 changes: 1 addition & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
<string name="remote_port">Remote Port</string>
<string name="default_port">443</string>
<string name="password">Password</string>
<string name="start">Start</string>
<string name="stop">Stop</string>
<string name="startStop">Start/Stop</string>
</resources>

0 comments on commit c976563

Please sign in to comment.