From 21f2a4650f6ddb32babd68b7b4c5ac7f732fc3b7 Mon Sep 17 00:00:00 2001 From: Andrej Mihajlov Date: Wed, 14 Feb 2024 05:27:39 +0100 Subject: [PATCH] HevSocks5Tunnel: Add option to init config from string --- README.md | 32 ++++++++++++++++++++++++++++++++ src/hev-config.c | 28 +++++++++++++++++++++++++++- src/hev-config.h | 4 +++- src/hev-main.c | 35 +++++++++++++++++++++++++++++------ src/hev-main.h | 30 ++++++++++++++++++++++++++++++ 5 files changed, 121 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 5f587be..c97ed40 100644 --- a/README.md +++ b/README.md @@ -180,12 +180,44 @@ sudo route change -inet6 default -interface utun99 * Start and run the socks5 tunnel, this function will blocks until the * hev_socks5_tunnel_quit is called or an error occurs. * + * Alias of hev_socks5_tunnel_main_from_file + * * Returns: returns zero on successful, otherwise returns -1. * * Since: 2.4.6 */ int hev_socks5_tunnel_main (const char *config_path, int tun_fd); +/** + * hev_socks5_tunnel_main_from_file: + * @config_path: config file path + * @tun_fd: tunnel file descriptor + * + * Start and run the socks5 tunnel, this function will blocks until the + * hev_socks5_tunnel_quit is called or an error occurs. + * + * Returns: returns zero on successful, otherwise returns -1. + * + * Since: 2.6.7 + */ +int hev_socks5_tunnel_main_from_file (const char *config_path, int tun_fd); + +/** + * hev_socks5_tunnel_main_from_str: + * @config_str: string config + * @config_len: the byte length of string config + * @tun_fd: tunnel file descriptor + * + * Start and run the socks5 tunnel, this function will blocks until the + * hev_socks5_tunnel_quit is called or an error occurs. + * + * Returns: returns zero on successful, otherwise returns -1. + * + * Since: 2.6.7 + */ +int hev_socks5_tunnel_main_from_str (const unsigned char *config_str, + unsigned int config_len, int tun_fd); + /** * hev_socks5_tunnel_quit: * diff --git a/src/hev-config.c b/src/hev-config.c index d0a4afc..c8d3ff1 100644 --- a/src/hev-config.c +++ b/src/hev-config.c @@ -340,7 +340,7 @@ hev_config_parse_doc (yaml_document_t *doc) } int -hev_config_init (const char *config_path) +hev_config_init_from_file (const char *config_path) { yaml_parser_t parser; yaml_document_t doc; @@ -373,6 +373,32 @@ hev_config_init (const char *config_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) { diff --git a/src/hev-config.h b/src/hev-config.h index 163f515..f38403a 100644 --- a/src/hev-config.h +++ b/src/hev-config.h @@ -23,7 +23,9 @@ struct _HevConfigServer char addr[256]; }; -int hev_config_init (const char *config_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); const char *hev_config_get_tunnel_name (void); diff --git a/src/hev-main.c b/src/hev-main.c index e551186..79d668f 100644 --- a/src/hev-main.c +++ b/src/hev-main.c @@ -40,8 +40,8 @@ sigint_handler (int signum) hev_socks5_tunnel_stop (); } -int -hev_socks5_tunnel_main (const char *config_path, int tun_fd) +static int +hev_socks5_tunnel_main_inner (int tun_fd) { const char *pid_file; const char *log_file; @@ -49,10 +49,6 @@ hev_socks5_tunnel_main (const char *config_path, int tun_fd) int nofile; int res; - res = hev_config_init (config_path); - if (res < 0) - return -1; - log_file = hev_config_get_misc_log_file (); log_level = hev_config_get_misc_log_level (); @@ -94,6 +90,33 @@ hev_socks5_tunnel_main (const char *config_path, int tun_fd) return 0; } +int +hev_socks5_tunnel_main_from_file (const char *config_path, int tun_fd) +{ + int res = hev_config_init_from_file (config_path); + if (res < 0) + return -1; + + return hev_socks5_tunnel_main_inner (tun_fd); +} + +int +hev_socks5_tunnel_main_from_str (const unsigned char *config_str, + unsigned int config_len, int tun_fd) +{ + int res = hev_config_init_from_str (config_str, config_len); + if (res < 0) + return -1; + + return hev_socks5_tunnel_main_inner (tun_fd); +} + +int +hev_socks5_tunnel_main (const char *config_path, int tun_fd) +{ + return hev_socks5_tunnel_main_from_file (config_path, tun_fd); +} + void hev_socks5_tunnel_quit (void) { diff --git a/src/hev-main.h b/src/hev-main.h index cb6b0e3..49896b9 100644 --- a/src/hev-main.h +++ b/src/hev-main.h @@ -30,6 +30,36 @@ extern "C" { */ int hev_socks5_tunnel_main (const char *config_path, int tun_fd); +/** + * hev_socks5_tunnel_main_from_file: + * @config_path: config file path + * @tun_fd: tunnel file descriptor + * + * Start and run the socks5 tunnel, this function will blocks until the + * hev_socks5_tunnel_quit is called or an error occurs. + * + * Returns: returns zero on successful, otherwise returns -1. + * + * Since: 2.6.7 + */ +int hev_socks5_tunnel_main_from_file (const char *config_path, int tun_fd); + +/** + * hev_socks5_tunnel_main_from_str: + * @config_str: string config + * @config_len: the byte length of string config + * @tun_fd: tunnel file descriptor + * + * Start and run the socks5 tunnel, this function will blocks until the + * hev_socks5_tunnel_quit is called or an error occurs. + * + * Returns: returns zero on successful, otherwise returns -1. + * + * Since: 2.6.7 + */ +int hev_socks5_tunnel_main_from_str (const unsigned char *config_str, + unsigned int config_len, int tun_fd); + /** * hev_socks5_tunnel_quit: *