Skip to content

Commit

Permalink
Android: Build as a shared library.
Browse files Browse the repository at this point in the history
  • Loading branch information
heiher committed Jun 9, 2024
1 parent f49ea48 commit 2f12f5b
Show file tree
Hide file tree
Showing 11 changed files with 354 additions and 49 deletions.
6 changes: 3 additions & 3 deletions Android.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2021 The Android Open Source Project
# Copyright (C) 2024 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,11 +32,11 @@ LOCAL_SRC_FILES := $(patsubst $(SRCDIR)/%,src/%,$(SRCFILES))
LOCAL_C_INCLUDES := \
$(LOCAL_PATH)/src/misc \
$(LOCAL_PATH)/src/core/include \
$(LOCAL_PATH)/third-part/yaml/src \
$(LOCAL_PATH)/third-part/yaml/include \
$(LOCAL_PATH)/third-part/hev-task-system/include
LOCAL_CFLAGS += $(VERSION_CFLAGS)
ifeq ($(TARGET_ARCH_ABI),armeabi-v7a)
LOCAL_CFLAGS += -mfpu=neon
endif
LOCAL_STATIC_LIBRARIES := yaml hev-task-system
include $(BUILD_EXECUTABLE)
include $(BUILD_SHARED_LIBRARY)
2 changes: 1 addition & 1 deletion Application.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (C) 2020 The Android Open Source Project
# Copyright (C) 2024 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand Down
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,47 @@ jerry pass 1a
iptables -A OUTPUT -p tcp --syn -m mark --mark 0x1a -m connlimit --connlimit-above 2 -j REJECT
```

## API

```c
/**
* hev_socks5_server_main_from_file:
* @config_path: config file path
*
* Start and run the socks5 server, this function will blocks until the
* hev_socks5_server_quit is called or an error occurs.
*
* Returns: returns zero on successful, otherwise returns -1.
*
* Since: 2.6.7
*/
int hev_socks5_server_main_from_file (const char *config_path);

/**
* hev_socks5_server_main_from_str:
* @config_str: string config
* @config_len: the byte length of string config
*
* Start and run the socks5 server, this function will blocks until the
* hev_socks5_server_quit is called or an error occurs.
*
* Returns: returns zero on successful, otherwise returns -1.
*
* Since: 2.6.7
*/
int hev_socks5_server_main_from_str (const unsigned char *config_str,
unsigned int config_len);

/**
* hev_socks5_server_quit:
*
* Stop the socks5 server.
*
* Since: 2.6.7
*/
void hev_socks5_server_quit (void);
```
## Contributors
* **Ammar Faizi** - https://github.com/ammarfaizi2
Expand Down
32 changes: 29 additions & 3 deletions src/hev-config.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
============================================================================
Name : hev-config.c
Author : Heiher <[email protected]>
Copyright : Copyright (c) 2017 - 2023 hev
Author : hev <[email protected]>
Copyright : Copyright (c) 2017 - 2024 hev
Description : Config
============================================================================
*/
Expand Down Expand Up @@ -276,7 +276,7 @@ hev_config_parse_doc (yaml_document_t *doc)
}

int
hev_config_init (const char *path)
hev_config_init_from_file (const char *path)
{
yaml_parser_t parser;
yaml_document_t doc;
Expand Down Expand Up @@ -309,6 +309,32 @@ hev_config_init (const char *path)
return res;
}

int
hev_config_init_from_str (const unsigned char *config_str,
unsigned int config_len)
{
yaml_parser_t parser;
yaml_document_t doc;
int res = -1;

if (!yaml_parser_initialize (&parser))
goto exit;

yaml_parser_set_input_string (&parser, config_str, config_len);
if (!yaml_parser_load (&parser, &doc)) {
fprintf (stderr, "Failed to parse config.");
goto exit_free_parser;
}

res = hev_config_parse_doc (&doc);
yaml_document_delete (&doc);

exit_free_parser:
yaml_parser_delete (&parser);
exit:
return res;
}

void
hev_config_fini (void)
{
Expand Down
8 changes: 5 additions & 3 deletions src/hev-config.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
/*
============================================================================
Name : hev-config.h
Author : Heiher <[email protected]>
Copyright : Copyright (c) 2017 - 2022 hev
Author : hev <[email protected]>
Copyright : Copyright (c) 2017 - 2024 hev
Description : Config
============================================================================
*/

#ifndef __HEV_CONFIG_H__
#define __HEV_CONFIG_H__

int hev_config_init (const char *path);
int hev_config_init_from_file (const char *config_path);
int hev_config_init_from_str (const unsigned char *config_str,
unsigned int config_len);
void hev_config_fini (void);

unsigned int hev_config_get_workers (void);
Expand Down
130 changes: 130 additions & 0 deletions src/hev-jni.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
============================================================================
Name : hev-jni.c
Author : hev <[email protected]>
Copyright : Copyright (c) 2019 - 2024 hev
Description : Jave Native Interface
============================================================================
*/

#ifdef ANDROID

#include <jni.h>
#include <pthread.h>

#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>

#include "hev-main.h"

#include "hev-jni.h"

/* clang-format off */
#ifndef PKGNAME
#define PKGNAME hev/socks5
#endif
/* clang-format on */

#define STR(s) STR_ARG (s)
#define STR_ARG(c) #c
#define N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))

typedef struct _ThreadData ThreadData;

struct _ThreadData
{
char *path;
};

static JavaVM *java_vm;
static pthread_t work_thread;
static pthread_mutex_t mutex;
static pthread_key_t current_jni_env;

static void native_start_service (JNIEnv *env, jobject thiz,
jstring config_path);
static void native_stop_service (JNIEnv *env, jobject thiz);

static JNINativeMethod native_methods[] = {
{ "Socks5StartService", "(Ljava/lang/String;)V",
(void *)native_start_service },
{ "Socks5StopService", "()V", (void *)native_stop_service },
};

static void
detach_current_thread (void *env)
{
(*java_vm)->DetachCurrentThread (java_vm);
}

jint
JNI_OnLoad (JavaVM *vm, void *reserved)
{
JNIEnv *env = NULL;
jclass klass;

java_vm = vm;
if (JNI_OK != (*vm)->GetEnv (vm, (void **)&env, JNI_VERSION_1_4)) {
return 0;
}

klass = (*env)->FindClass (env, STR (PKGNAME) "/Socks5Service");
(*env)->RegisterNatives (env, klass, native_methods,
N_ELEMENTS (native_methods));
(*env)->DeleteLocalRef (env, klass);

pthread_key_create (&current_jni_env, detach_current_thread);
pthread_mutex_init (&mutex, NULL);

return JNI_VERSION_1_4;
}

static void *
thread_handler (void *data)
{
ThreadData *tdata = data;

hev_socks5_server_main_from_file (tdata->path);

free (tdata->path);
free (tdata);

return NULL;
}

static void
native_start_service (JNIEnv *env, jobject thiz, jstring config_path)
{
const jbyte *bytes;
ThreadData *tdata;

pthread_mutex_lock (&mutex);
if (work_thread)
return;

tdata = malloc (sizeof (ThreadData));

bytes = (const jbyte *)(*env)->GetStringUTFChars (env, config_path, NULL);
tdata->path = strdup ((const char *)bytes);
(*env)->ReleaseStringUTFChars (env, config_path, (const char *)bytes);

pthread_create (&work_thread, NULL, thread_handler, tdata);
pthread_mutex_unlock (&mutex);
}

static void
native_stop_service (JNIEnv *env, jobject thiz)
{
pthread_mutex_lock (&mutex);
if (!work_thread)
return;

hev_socks5_server_quit ();
pthread_join (work_thread, NULL);
work_thread = 0;
pthread_mutex_unlock (&mutex);
}

#endif /* ANDROID */
13 changes: 13 additions & 0 deletions src/hev-jni.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
============================================================================
Name : hev-jni.h
Author : hev <[email protected]>
Copyright : Copyright (c) 2019 - 2024 hev
Description : Java Native Interface
============================================================================
*/

#ifndef __HEV_JNI_H__
#define __HEV_JNI_H__

#endif /* __HEV_JNI_H__ */
Loading

0 comments on commit 2f12f5b

Please sign in to comment.