Skip to content

Commit

Permalink
HevSocks5Tunnel: Add option to init config from string
Browse files Browse the repository at this point in the history
  • Loading branch information
pronebird authored and heiher committed Feb 20, 2024
1 parent 48e090a commit 21f2a46
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 8 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
*
Expand Down
28 changes: 27 additions & 1 deletion src/hev-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
4 changes: 3 additions & 1 deletion src/hev-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
35 changes: 29 additions & 6 deletions src/hev-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,15 @@ 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;
int log_level;
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 ();

Expand Down Expand Up @@ -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)
{
Expand Down
30 changes: 30 additions & 0 deletions src/hev-main.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
*
Expand Down

0 comments on commit 21f2a46

Please sign in to comment.