Skip to content

Commit

Permalink
HevSocks5Tunnel: Add tunnel post-up and pre-down script.
Browse files Browse the repository at this point in the history
  • Loading branch information
heiher committed Jan 1, 2024
1 parent 79c31de commit 91a133d
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ tunnel:
ipv4: 198.18.0.1
# IPv6 address
ipv6: 'fc00::1'
# Post up script
# post-up-script: up.sh
# Pre down script
# pre-down-script: down.sh

socks5:
# Socks5 server port
Expand Down
4 changes: 4 additions & 0 deletions conf/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ tunnel:
ipv4: 198.18.0.1
# IPv6 address
ipv6: 'fc00::1'
# Post up script
# post-up-script: up.sh
# Pre down script
# pre-down-script: down.sh

socks5:
# Socks5 server port
Expand Down
25 changes: 25 additions & 0 deletions src/hev-config.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ static int multi_queue;
static char tun_ipv4_address[16];
static char tun_ipv6_address[64];

static char tun_post_up_script[1024];
static char tun_pre_down_script[1024];

static HevConfigServer srv;

static char log_file[1024];
Expand Down Expand Up @@ -135,6 +138,10 @@ hev_config_parse_tunnel (yaml_document_t *doc, yaml_node_t *base)
strncpy (tun_ipv4_address, value, 16 - 1);
else if (0 == strcmp (key, "ipv6"))
strncpy (tun_ipv6_address, value, 64 - 1);
else if (0 == strcmp (key, "post-up-script"))
strncpy (tun_post_up_script, value, 64 - 1);
else if (0 == strcmp (key, "pre-down-script"))
strncpy (tun_pre_down_script, value, 64 - 1);
} else {
if (0 == strcmp (key, "ipv4"))
hev_config_parse_tunnel_ipv4 (doc, node);
Expand Down Expand Up @@ -404,6 +411,24 @@ hev_config_get_tunnel_ipv6_address (void)
return tun_ipv6_address;
}

const char *
hev_config_get_tunnel_post_up_script (void)
{
if (!tun_post_up_script[0])
return NULL;

return tun_post_up_script;
}

const char *
hev_config_get_tunnel_pre_down_script (void)
{
if (!tun_pre_down_script[0])
return NULL;

return tun_pre_down_script;
}

HevConfigServer *
hev_config_get_socks5_server (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 @@ -32,6 +32,9 @@ int hev_config_get_tunnel_multi_queue (void);
const char *hev_config_get_tunnel_ipv4_address (void);
const char *hev_config_get_tunnel_ipv6_address (void);

const char *hev_config_get_tunnel_post_up_script (void);
const char *hev_config_get_tunnel_pre_down_script (void);

HevConfigServer *hev_config_get_socks5_server (void);

int hev_config_get_misc_task_stack_size (void);
Expand Down
22 changes: 17 additions & 5 deletions src/hev-socks5-tunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <hev-task-system.h>
#include <hev-memory-allocator.h>

#include "hev-exec.h"
#include "hev-list.h"
#include "hev-compiler.h"
#include "hev-config.h"
Expand Down Expand Up @@ -297,7 +298,7 @@ lwip_timer_task_entry (void *data)
static int
tunnel_init (int extern_tun_fd)
{
const char *name, *ipv4, *ipv6;
const char *script_path, *name, *ipv4, *ipv6;
int multi_queue, res;
unsigned int mtu;

Expand Down Expand Up @@ -345,17 +346,28 @@ tunnel_init (int extern_tun_fd)
return -1;
}

script_path = hev_config_get_tunnel_post_up_script ();
if (script_path)
hev_exec_run (script_path, hev_tunnel_get_name (), 0);

tun_fd_local = 1;
return 0;
}

static void
tunnel_fini (void)
{
if (tun_fd_local) {
hev_tunnel_close (tun_fd);
tun_fd = -1;
}
const char *script_path;

if (!tun_fd_local)
return;

script_path = hev_config_get_tunnel_pre_down_script ();
if (script_path)
hev_exec_run (script_path, hev_tunnel_get_name (), 1);

hev_tunnel_close (tun_fd);
tun_fd = -1;
}

static int
Expand Down
6 changes: 6 additions & 0 deletions src/hev-tunnel-freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ hev_tunnel_set_state (int state)
return res;
}

const char *
hev_tunnel_get_name (void)
{
return tun_name;
}

int
hev_tunnel_set_ipv4 (const char *addr, unsigned int prefix)
{
Expand Down
6 changes: 6 additions & 0 deletions src/hev-tunnel-linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ hev_tunnel_set_state (int state)
return res;
}

const char *
hev_tunnel_get_name (void)
{
return tun_name;
}

int
hev_tunnel_set_ipv4 (const char *addr, unsigned int prefix)
{
Expand Down
6 changes: 6 additions & 0 deletions src/hev-tunnel-macos.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@ hev_tunnel_set_state (int state)
return res;
}

const char *
hev_tunnel_get_name (void)
{
return tun_name;
}

int
hev_tunnel_set_ipv4 (const char *addr, unsigned int prefix)
{
Expand Down
1 change: 1 addition & 0 deletions src/hev-tunnel.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void hev_tunnel_close (int fd);

int hev_tunnel_set_mtu (int mtu);
int hev_tunnel_set_state (int state);
const char *hev_tunnel_get_name (void);

int hev_tunnel_set_ipv4 (const char *addr, unsigned int prefix);
int hev_tunnel_set_ipv6 (const char *addr, unsigned int prefix);
Expand Down
46 changes: 46 additions & 0 deletions src/misc/hev-exec.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
============================================================================
Name : hev-exec.c
Author : hev <r@hev.cc>
Copyright : Copyright (c) 2023 hev
Description : Exec
============================================================================
*/

#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/wait.h>

#include "hev-logger.h"

#include "hev-exec.h"

static void
signal_handler (int signum)
{
waitpid (-1, NULL, WNOHANG);
}

void
hev_exec_run (const char *script_path, const char *tun_name, int wait)
{
pid_t pid;

signal (SIGCHLD, signal_handler);

pid = fork ();
if (pid < 0) {
LOG_E ("exec fork");
return;
} else if (pid != 0) {
if (wait)
waitpid (pid, NULL, 0);
return;
}

execl (script_path, script_path, tun_name, NULL);

LOG_E ("exec %s %s", script_path, tun_name);
exit (-1);
}
15 changes: 15 additions & 0 deletions src/misc/hev-exec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
============================================================================
Name : hev-exec.h
Author : hev <r@hev.cc>
Copyright : Copyright (c) 2023 hev
Description : Exec
============================================================================
*/

#ifndef __HEV_EXEC_H__
#define __HEV_EXEC_H__

void hev_exec_run (const char *script_path, const char *tun_name, int wait);

#endif /* __HEV_EXEC_H__ */

0 comments on commit 91a133d

Please sign in to comment.