Skip to content

Commit

Permalink
HevSocks5Tunnel: Use dynamic configs.
Browse files Browse the repository at this point in the history
  • Loading branch information
heiher committed May 25, 2021
1 parent 458df6e commit 7cccb92
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 12 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ socks5:
address: 127.0.0.1

#misc:
# task stack size (bytes)
# task-stack-size: 20480
# connect timeout (ms)
# connect-timeout: 5000
# read-write timeout (ms)
# read-write-timeout: 60000
# stdout, stderr or file-path
# log-file: stderr
# debug, info, warn or error
Expand Down
6 changes: 6 additions & 0 deletions conf/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ socks5:
address: 127.0.0.1

#misc:
# task stack size (bytes)
# task-stack-size: 20480
# connect timeout (ms)
# connect-timeout: 5000
# read-write timeout (ms)
# read-write-timeout: 60000
# stdout, stderr or file-path
# log-file: stderr
# debug, info, warn or error
Expand Down
5 changes: 0 additions & 5 deletions src/hev-config-const.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,4 @@ static const int TCP_BUF_SIZE = 8192;
static const int UDP_BUF_SIZE = 1500;
static const int UDP_POOL_SIZE = 512;

static const int IO_TIMEOUT = 60000;
static const int CONNECT_TIMEOUT = 3000;

static const int TASK_STACK_SIZE = 20480;

#endif /* __HEV_CONFIG_CONST_H__ */
29 changes: 28 additions & 1 deletion src/hev-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ static char srv_port[8];

static char log_file[1024];
static char pid_file[1024];
static int task_stack_size = 20480;
static int connect_timeout = 5000;
static int read_write_timeout = 60000;
static int limit_nofile = -2;
static int log_level = HEV_LOGGER_WARN;

Expand Down Expand Up @@ -254,7 +257,13 @@ hev_config_parse_misc (yaml_document_t *doc, yaml_node_t *base)
break;
value = (const char *)node->data.scalar.value;

if (0 == strcmp (key, "pid-file"))
if (0 == strcmp (key, "task-stack-size"))
task_stack_size = strtoul (value, NULL, 10);
else if (0 == strcmp (key, "connect-timeout"))
connect_timeout = strtoul (value, NULL, 10);
else if (0 == strcmp (key, "read-write-timeout"))
read_write_timeout = strtoul (value, NULL, 10);
else if (0 == strcmp (key, "pid-file"))
strncpy (pid_file, value, 1024 - 1);
else if (0 == strcmp (key, "log-file"))
strncpy (log_file, value, 1024 - 1);
Expand Down Expand Up @@ -417,6 +426,24 @@ hev_config_get_socks5_address (int *port)
return srv_address;
}

int
hev_config_get_misc_task_stack_size (void)
{
return task_stack_size;
}

int
hev_config_get_misc_connect_timeout (void)
{
return connect_timeout;
}

int
hev_config_get_misc_read_write_timeout (void)
{
return read_write_timeout;
}

int
hev_config_get_misc_limit_nofile (void)
{
Expand Down
3 changes: 3 additions & 0 deletions src/hev-config.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ unsigned int hev_config_get_tunnel_ipv6_prefix (void);

const char *hev_config_get_socks5_address (int *port);

int hev_config_get_misc_task_stack_size (void);
int hev_config_get_misc_connect_timeout (void);
int hev_config_get_misc_read_write_timeout (void);
int hev_config_get_misc_limit_nofile (void);
const char *hev_config_get_misc_pid_file (void);
const char *hev_config_get_misc_log_file (void);
Expand Down
9 changes: 6 additions & 3 deletions src/hev-socks5-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

#include "hev-logger.h"
#include "hev-config.h"
#include "hev-config-const.h"

#include "hev-socks5-session.h"

Expand Down Expand Up @@ -53,23 +52,27 @@ void
hev_socks5_session_run (HevSocks5Session *self)
{
HevSocks5SessionClass *klass;
int read_write_timeout;
int connect_timeout;
const char *addr;
int port;
int res;

LOG_D ("%p socks5 session run", self);

addr = hev_config_get_socks5_address (&port);
connect_timeout = hev_config_get_misc_connect_timeout ();
read_write_timeout = hev_config_get_misc_read_write_timeout ();

hev_socks5_set_timeout (HEV_SOCKS5 (self->client), CONNECT_TIMEOUT);
hev_socks5_set_timeout (HEV_SOCKS5 (self->client), connect_timeout);

res = hev_socks5_client_connect (self->client, addr, port);
if (res < 0) {
LOG_E ("%p socks5 session connect", self);
return;
}

hev_socks5_set_timeout (HEV_SOCKS5 (self->client), IO_TIMEOUT);
hev_socks5_set_timeout (HEV_SOCKS5 (self->client), read_write_timeout);

res = hev_socks5_client_handshake (self->client);
if (res < 0) {
Expand Down
9 changes: 6 additions & 3 deletions src/hev-socks5-tunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include "hev-config.h"
#include "hev-logger.h"
#include "hev-compiler.h"
#include "hev-config-const.h"
#include "hev-tunnel-linux.h"
#include "hev-socks5-session-tcp.h"
#include "hev-socks5-session-udp.h"
Expand Down Expand Up @@ -533,6 +532,7 @@ tcp_accept_handler (void *arg, struct tcp_pcb *pcb, err_t err)
{
HevSocks5SessionTCP *tcp;
HevSocks5Session *s;
int stack_size;
HevTask *task;

if (err != ERR_OK)
Expand All @@ -543,7 +543,8 @@ tcp_accept_handler (void *arg, struct tcp_pcb *pcb, err_t err)
return ERR_MEM;

s = HEV_SOCKS5_SESSION (tcp);
task = hev_task_new (TASK_STACK_SIZE);
stack_size = hev_config_get_misc_task_stack_size ();
task = hev_task_new (stack_size);
if (!task) {
hev_socks5_session_destroy (s);
return ERR_ABRT;
Expand All @@ -562,6 +563,7 @@ udp_recv_handler (void *arg, struct udp_pcb *pcb, struct pbuf *p,
{
HevSocks5SessionUDP *udp;
HevSocks5Session *s;
int stack_size;
HevTask *task;

udp = hev_socks5_session_udp_new (pcb, &mutex);
Expand All @@ -571,7 +573,8 @@ udp_recv_handler (void *arg, struct udp_pcb *pcb, struct pbuf *p,
}

s = HEV_SOCKS5_SESSION (udp);
task = hev_task_new (TASK_STACK_SIZE);
stack_size = hev_config_get_misc_task_stack_size ();
task = hev_task_new (stack_size);
if (!task) {
hev_socks5_session_destroy (s);
return;
Expand Down

0 comments on commit 7cccb92

Please sign in to comment.