Skip to content

Commit

Permalink
android ephemeral port only if configured
Browse files Browse the repository at this point in the history
  • Loading branch information
ghazel committed Feb 25, 2019
1 parent 6901ed0 commit 494b3c4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 9 deletions.
47 changes: 46 additions & 1 deletion android.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
int pfd[2];
static JavaVM *g_jvm;
static jobject bugsnagClient;
port_t http_port;
port_t socks_port;
bool use_ephemeral_port;

void* stdio_thread(void *useradata)
{
Expand Down Expand Up @@ -199,18 +202,60 @@ void bugsnag_leave_breadcrumb_log(const char *buf)
bugsnag_add_breadcrumb(crumb);
}

JNIEXPORT void JNICALL Java_com_clostra_newnode_NewNode_useEphemeralPort(JNIEnv* env, jobject thiz)
{
use_ephemeral_port = true;
}

JNIEXPORT void JNICALL Java_com_clostra_newnode_NewNode_setCacheDir(JNIEnv* env, jobject thiz, jstring cacheDir)
{
const char* cCacheDir = (*env)->GetStringUTFChars(env, cacheDir, NULL);
chdir(cCacheDir);

bugsnag_client_setup(env);

newnode_init();
if (!use_ephemeral_port) {
http_port = 8006;
socks_port = 8007;
}
newnode_init(&http_port, &socks_port);

(*env)->ReleaseStringUTFChars(env, cacheDir, cCacheDir);
}

JNIEXPORT void JNICALL Java_com_clostra_newnode_NewNode_registerProxy(JNIEnv* env, jobject thiz)
{
if (!http_port || !socks_port) {
return;
}

IMPORT(java/lang, System);
CATCH(return);
jmethodID mSetProp = (*env)->GetStaticMethodID(env, cSystem, "setProperty", "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
CATCH(return);

char port[6];
snprintf(port, sizeof(port), "%u", socks_port);
(*env)->CallStaticObjectMethod(env, cSystem, mSetProp, JSTR("socksProxyHost"), JSTR("127.0.0.1"));
(*env)->CallStaticObjectMethod(env, cSystem, mSetProp, JSTR("socksProxyPort"), JSTR(port));

snprintf(port, sizeof(port), "%u", http_port);
(*env)->CallStaticObjectMethod(env, cSystem, mSetProp, JSTR("proxyHost"), JSTR("127.0.0.1"));
(*env)->CallStaticObjectMethod(env, cSystem, mSetProp, JSTR("proxyPort"), JSTR(port));
}

JNIEXPORT void JNICALL Java_com_clostra_newnode_NewNode_unregisterProxy(JNIEnv* env, jobject thiz)
{
IMPORT(java/lang, System);
CATCH(return);
jmethodID mClearProp = (*env)->GetStaticMethodID(env, cSystem, "clearProperty", "(Ljava/lang/String;)Ljava/lang/String;");
CATCH(return);
(*env)->CallStaticObjectMethod(env, cSystem, mClearProp, JSTR("socksProxyHost"));
(*env)->CallStaticObjectMethod(env, cSystem, mClearProp, JSTR("socksProxyPort"));
(*env)->CallStaticObjectMethod(env, cSystem, mClearProp, JSTR("proxyHost"));
(*env)->CallStaticObjectMethod(env, cSystem, mClearProp, JSTR("proxyPort"));
}

// XXX: compat
JNIEXPORT void JNICALL Java_com_clostra_dcdn_Dcdn_setCacheDir(JNIEnv* env, jobject thiz, jstring cacheDir)
{
Expand Down
14 changes: 6 additions & 8 deletions android/src/main/java/com/clostra/newnode/NewNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ private static void update() throws Exception {
public static void init() {
if (!started) {
try {
useEphemeralPort();
setCacheDir(app().getCacheDir().getAbsolutePath());
Log.e("newnode", "version " + VERSION + " started");
started = true;
Expand All @@ -138,10 +139,7 @@ public static void init() {
}

if (started) {
System.setProperty("socksProxyHost", "127.0.0.1");
System.setProperty("socksProxyPort", "8007");
System.setProperty("proxyHost", "127.0.0.1");
System.setProperty("proxyPort", "8006");
registerProxy();
}

if (updateThread == null) {
Expand All @@ -163,10 +161,7 @@ public static void init() {
}

public static void shutdown() {
System.clearProperty("socksProxyHost");
System.clearProperty("socksProxyPort");
System.clearProperty("proxyHost");
System.clearProperty("proxyPort");
unregisterProxy();

if (updateThread != null) {
updateThread.interrupt();
Expand All @@ -186,5 +181,8 @@ public void update(Observable observable, Object arg) {
}

public static native void setCacheDir(String cacheDir);
public static native void useEphemeralPort();
public static native void registerProxy();
public static native void unregisterProxy();
public static native void updateBugsnagDetails(int notifyType);
}

0 comments on commit 494b3c4

Please sign in to comment.