-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
354 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
============================================================================ | ||
*/ | ||
|
@@ -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; | ||
|
@@ -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) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 (¤t_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 */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ */ |
Oops, something went wrong.