diff --git a/.idea/misc.xml b/.idea/misc.xml index 0ad17cb..8978d23 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/app/src/main/cpp/native-lib.c b/app/src/main/cpp/native-lib.c index f33ddd6..74b7ca1 100644 --- a/app/src/main/cpp/native-lib.c +++ b/app/src/main/cpp/native-lib.c @@ -1,14 +1,14 @@ #include -#include #include #include -#include #include "byedpi/error.h" #include "byedpi/proxy.h" #include "utils.h" +static int g_proxy_fd = -1; + JNIEXPORT jint JNI_OnLoad( __attribute__((unused)) JavaVM *vm, __attribute__((unused)) void *reserved) { @@ -22,6 +22,11 @@ Java_io_github_dovecoteescapee_byedpi_core_ByeDpiProxy_jniCreateSocket( __attribute__((unused)) jobject thiz, jobjectArray args) { + if (g_proxy_fd != -1) { + LOG(LOG_S, "proxy already running, fd: %d", g_proxy_fd); + return -1; + } + int argc = (*env)->GetArrayLength(env, args); char *argv[argc]; for (int i = 0; i < argc; i++) { @@ -33,10 +38,6 @@ Java_io_github_dovecoteescapee_byedpi_core_ByeDpiProxy_jniCreateSocket( int res = parse_args(argc, argv); - for (int i = 0; i < argc; i++) { - free(argv[i]); - } - if (res < 0) { uniperror("parse_args"); return -1; @@ -49,6 +50,7 @@ Java_io_github_dovecoteescapee_byedpi_core_ByeDpiProxy_jniCreateSocket( return -1; } + g_proxy_fd = fd; LOG(LOG_S, "listen_socket, fd: %d", fd); return fd; } @@ -56,12 +58,11 @@ Java_io_github_dovecoteescapee_byedpi_core_ByeDpiProxy_jniCreateSocket( JNIEXPORT jint JNICALL Java_io_github_dovecoteescapee_byedpi_core_ByeDpiProxy_jniStartProxy( __attribute__((unused)) JNIEnv *env, - __attribute__((unused)) jobject thiz, - jint fd) { + __attribute__((unused)) jobject thiz) { - LOG(LOG_S, "start_proxy, fd: %d", fd); + LOG(LOG_S, "start_proxy, fd: %d", g_proxy_fd); - if (start_event_loop(fd) < 0) { + if (start_event_loop(g_proxy_fd) < 0) { uniperror("event_loop"); return get_e(); } @@ -72,13 +73,18 @@ Java_io_github_dovecoteescapee_byedpi_core_ByeDpiProxy_jniStartProxy( JNIEXPORT jint JNICALL Java_io_github_dovecoteescapee_byedpi_core_ByeDpiProxy_jniStopProxy( __attribute__((unused)) JNIEnv *env, - __attribute__((unused)) jobject thiz, - jint fd) { + __attribute__((unused)) jobject thiz) { - LOG(LOG_S, "stop_proxy, fd: %d", fd); + LOG(LOG_S, "stop_proxy, fd: %d", g_proxy_fd); + + if (g_proxy_fd < 0) { + LOG(LOG_S, "proxy is not running, fd: %d", g_proxy_fd); + return 0; + } - int res = shutdown(fd, SHUT_RDWR); reset_params(); + int res = shutdown(g_proxy_fd, SHUT_RDWR); + g_proxy_fd = -1; if (res < 0) { uniperror("shutdown"); diff --git a/app/src/main/java/io/github/dovecoteescapee/byedpi/activities/MainActivity.kt b/app/src/main/java/io/github/dovecoteescapee/byedpi/activities/MainActivity.kt index d9a96ad..bb0358b 100644 --- a/app/src/main/java/io/github/dovecoteescapee/byedpi/activities/MainActivity.kt +++ b/app/src/main/java/io/github/dovecoteescapee/byedpi/activities/MainActivity.kt @@ -140,11 +140,17 @@ class MainActivity : AppCompatActivity() { } binding.statusButton.setOnClickListener { + binding.statusButton.isEnabled = false + val (status, _) = appStatus when (status) { AppStatus.Halted -> start() AppStatus.Running -> stop() } + + binding.statusButton.postDelayed({ + binding.statusButton.isEnabled = true + }, 500) } val lang = getPreferences().getString("language", "system") @@ -255,7 +261,6 @@ class MainActivity : AppCompatActivity() { binding.statusButton.setText(R.string.proxy_start) } } - binding.statusButton.isEnabled = true } AppStatus.Running -> { @@ -270,7 +275,6 @@ class MainActivity : AppCompatActivity() { binding.statusButton.setText(R.string.proxy_stop) } } - binding.statusButton.isEnabled = true } } } diff --git a/app/src/main/java/io/github/dovecoteescapee/byedpi/activities/TestActivity.kt b/app/src/main/java/io/github/dovecoteescapee/byedpi/activities/TestActivity.kt index 067a88a..9cbd5c2 100644 --- a/app/src/main/java/io/github/dovecoteescapee/byedpi/activities/TestActivity.kt +++ b/app/src/main/java/io/github/dovecoteescapee/byedpi/activities/TestActivity.kt @@ -238,7 +238,6 @@ class TestActivity : AppCompatActivity() { appendTextToResults("$cmd\n") } - delay(delaySec * 1000L) val totalRequests = sites.size * requestsCount val checkResults = checkSitesAsync(sites, requestsCount, fullLog) val successfulCount = checkResults.sumOf { it.second } @@ -250,6 +249,7 @@ class TestActivity : AppCompatActivity() { if (isProxyRunning()) stopProxyService() waitForProxyStatus(ServiceStatus.Disconnected) + delay(delaySec * 1000L) } successfulCmds.sortByDescending { it.second } diff --git a/app/src/main/java/io/github/dovecoteescapee/byedpi/core/ByeDpiProxy.kt b/app/src/main/java/io/github/dovecoteescapee/byedpi/core/ByeDpiProxy.kt index 83326aa..e03d715 100644 --- a/app/src/main/java/io/github/dovecoteescapee/byedpi/core/ByeDpiProxy.kt +++ b/app/src/main/java/io/github/dovecoteescapee/byedpi/core/ByeDpiProxy.kt @@ -11,46 +11,37 @@ class ByeDpiProxy { } private val mutex = Mutex() - private var fd = -1 suspend fun startProxy(preferences: ByeDpiProxyPreferences): Int { val result = createSocket(preferences) if (result < 0) { - fd = -1 return -1 } - return jniStartProxy(result) + return jniStartProxy() } suspend fun stopProxy(): Int { mutex.withLock { - if (fd < 0) { - throw IllegalStateException("Proxy is not running") - } + val result = jniStopProxy() - val result = jniStopProxy(fd) + if (result < 0) { + return -1 + } - fd = -1 return result } } private suspend fun createSocket(preferences: ByeDpiProxyPreferences): Int = mutex.withLock { - if (fd >= 0) { - throw IllegalStateException("Proxy is already running") - } - val result = createSocketFromPreferences(preferences) if (result < 0) { - fd = -1 return -1 } - fd = result return result } @@ -61,6 +52,6 @@ class ByeDpiProxy { } private external fun jniCreateSocket(args: Array): Int - private external fun jniStartProxy(fd: Int): Int - private external fun jniStopProxy(fd: Int): Int + private external fun jniStartProxy(): Int + private external fun jniStopProxy(): Int } \ No newline at end of file