Skip to content

Commit

Permalink
edit native-lib and byedpiproxy
Browse files Browse the repository at this point in the history
  • Loading branch information
romanvht committed Feb 9, 2025
1 parent 242f9c7 commit 4a17b31
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 34 deletions.
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 20 additions & 14 deletions app/src/main/cpp/native-lib.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include <string.h>
#include <netdb.h>

#include <jni.h>
#include <android/log.h>
#include <malloc.h>

#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) {
Expand All @@ -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++) {
Expand All @@ -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;
Expand All @@ -49,19 +50,19 @@ Java_io_github_dovecoteescapee_byedpi_core_ByeDpiProxy_jniCreateSocket(
return -1;
}

g_proxy_fd = fd;
LOG(LOG_S, "listen_socket, fd: %d", fd);
return fd;
}

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();
}
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -255,7 +261,6 @@ class MainActivity : AppCompatActivity() {
binding.statusButton.setText(R.string.proxy_start)
}
}
binding.statusButton.isEnabled = true
}

AppStatus.Running -> {
Expand All @@ -270,7 +275,6 @@ class MainActivity : AppCompatActivity() {
binding.statusButton.setText(R.string.proxy_stop)
}
}
binding.statusButton.isEnabled = true
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -250,6 +249,7 @@ class TestActivity : AppCompatActivity() {

if (isProxyRunning()) stopProxyService()
waitForProxyStatus(ServiceStatus.Disconnected)
delay(delaySec * 1000L)
}

successfulCmds.sortByDescending { it.second }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -61,6 +52,6 @@ class ByeDpiProxy {
}

private external fun jniCreateSocket(args: Array<String>): 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
}

0 comments on commit 4a17b31

Please sign in to comment.