forked from heiher/hev-socks5-tunnel
-
Notifications
You must be signed in to change notification settings - Fork 0
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
18 changed files
with
1,027 additions
and
1,076 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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
============================================================================ | ||
Name : hev-config-const.h | ||
Author : Heiher <[email protected]> | ||
Copyright : Copyright (c) 2019 - 2021 Everyone. | ||
Copyright : Copyright (c) 2019 - 2021 hev | ||
Description : Config Constants | ||
============================================================================ | ||
*/ | ||
|
@@ -14,4 +14,13 @@ | |
#define MINOR_VERSION (2) | ||
#define MICRO_VERSION (1) | ||
|
||
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__ */ |
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
============================================================================ | ||
Name : hev-config.c | ||
Author : Heiher <[email protected]> | ||
Copyright : Copyright (c) 2019 - 2020 Everyone. | ||
Copyright : Copyright (c) 2019 - 2021 hev | ||
Description : Config | ||
============================================================================ | ||
*/ | ||
|
@@ -11,8 +11,8 @@ | |
#include <arpa/inet.h> | ||
#include <yaml.h> | ||
|
||
#include "hev-logger.h" | ||
#include "hev-config.h" | ||
#include "hev-config-const.h" | ||
|
||
static char tun_name[64]; | ||
static unsigned int tun_mtu = 8192; | ||
|
@@ -25,30 +25,13 @@ static char tun_ipv6_address[64]; | |
static char tun_ipv6_gateway[64]; | ||
static unsigned int tun_ipv6_prefix; | ||
|
||
static struct sockaddr_in6 socks5_address; | ||
static char srv_address[256]; | ||
static char srv_port[8]; | ||
|
||
static char log_file[1024]; | ||
static char log_level[16]; | ||
static char pid_file[1024]; | ||
static int limit_nofile = -2; | ||
|
||
static int | ||
address_to_sockaddr (const char *address, unsigned short port, | ||
struct sockaddr_in6 *addr) | ||
{ | ||
__builtin_bzero (addr, sizeof (*addr)); | ||
|
||
addr->sin6_family = AF_INET6; | ||
addr->sin6_port = htons (port); | ||
if (inet_pton (AF_INET, address, &addr->sin6_addr.s6_addr[12]) == 1) { | ||
((uint16_t *)&addr->sin6_addr)[5] = 0xffff; | ||
} else { | ||
if (inet_pton (AF_INET6, address, &addr->sin6_addr) != 1) | ||
return -1; | ||
} | ||
|
||
return 0; | ||
} | ||
static int log_level = HEV_LOGGER_WARN; | ||
|
||
static int | ||
hev_config_parse_tunnel_ipv4 (yaml_document_t *doc, yaml_node_t *base) | ||
|
@@ -186,8 +169,8 @@ static int | |
hev_config_parse_socks5 (yaml_document_t *doc, yaml_node_t *base) | ||
{ | ||
yaml_node_pair_t *pair; | ||
int port = 0; | ||
const char *addr = NULL; | ||
const char *port = NULL; | ||
|
||
if (!base || YAML_MAPPING_NODE != base->type) | ||
return -1; | ||
|
@@ -211,7 +194,7 @@ hev_config_parse_socks5 (yaml_document_t *doc, yaml_node_t *base) | |
value = (const char *)node->data.scalar.value; | ||
|
||
if (0 == strcmp (key, "port")) | ||
port = strtoul (value, NULL, 10); | ||
port = value; | ||
else if (0 == strcmp (key, "address")) | ||
addr = value; | ||
} | ||
|
@@ -226,7 +209,23 @@ hev_config_parse_socks5 (yaml_document_t *doc, yaml_node_t *base) | |
return -1; | ||
} | ||
|
||
return address_to_sockaddr (addr, port, &socks5_address); | ||
strncpy (srv_address, addr, 256 - 1); | ||
strncpy (srv_port, port, 8 - 1); | ||
|
||
return 0; | ||
} | ||
|
||
static int | ||
hev_config_parse_log_level (const char *value) | ||
{ | ||
if (0 == strcmp (value, "debug")) | ||
return HEV_LOGGER_DEBUG; | ||
else if (0 == strcmp (value, "info")) | ||
return HEV_LOGGER_INFO; | ||
else if (0 == strcmp (value, "error")) | ||
return HEV_LOGGER_ERROR; | ||
|
||
return HEV_LOGGER_WARN; | ||
} | ||
|
||
static int | ||
|
@@ -260,7 +259,7 @@ hev_config_parse_misc (yaml_document_t *doc, yaml_node_t *base) | |
else if (0 == strcmp (key, "log-file")) | ||
strncpy (log_file, value, 1024 - 1); | ||
else if (0 == strcmp (key, "log-level")) | ||
strncpy (log_level, value, 16 - 1); | ||
log_level = hev_config_parse_log_level (value); | ||
else if (0 == strcmp (key, "limit-nofile")) | ||
limit_nofile = strtol (value, NULL, 10); | ||
} | ||
|
@@ -410,11 +409,18 @@ hev_config_get_tunnel_ipv6_prefix (void) | |
return tun_ipv6_prefix; | ||
} | ||
|
||
struct sockaddr * | ||
hev_config_get_socks5_address (socklen_t *addr_len) | ||
const char * | ||
hev_config_get_socks5_address (int *port) | ||
{ | ||
*addr_len = sizeof (socks5_address); | ||
return (struct sockaddr *)&socks5_address; | ||
*port = strtoul (srv_port, NULL, 10); | ||
|
||
return srv_address; | ||
} | ||
|
||
int | ||
hev_config_get_misc_limit_nofile (void) | ||
{ | ||
return limit_nofile; | ||
} | ||
|
||
const char * | ||
|
@@ -426,28 +432,17 @@ hev_config_get_misc_pid_file (void) | |
return pid_file; | ||
} | ||
|
||
int | ||
hev_config_get_misc_limit_nofile (void) | ||
{ | ||
return limit_nofile; | ||
} | ||
|
||
const char * | ||
hev_config_get_misc_log_file (void) | ||
{ | ||
if (!log_file[0]) | ||
return NULL; | ||
if (0 == strcmp (log_file, "null")) | ||
return NULL; | ||
return "stderr"; | ||
|
||
return log_file; | ||
} | ||
|
||
const char * | ||
int | ||
hev_config_get_misc_log_level (void) | ||
{ | ||
if (!log_level[0]) | ||
return "warn"; | ||
|
||
return log_level; | ||
} |
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
============================================================================ | ||
Name : hev-config.h | ||
Author : Heiher <[email protected]> | ||
Copyright : Copyright (c) 2019 - 2020 Everyone. | ||
Copyright : Copyright (c) 2019 - 2021 hev | ||
Description : Config | ||
============================================================================ | ||
*/ | ||
|
@@ -24,13 +24,11 @@ const char *hev_config_get_tunnel_ipv6_address (void); | |
const char *hev_config_get_tunnel_ipv6_gateway (void); | ||
unsigned int hev_config_get_tunnel_ipv6_prefix (void); | ||
|
||
struct sockaddr *hev_config_get_socks5_address (socklen_t *addr_len); | ||
|
||
const char *hev_config_get_misc_pid_file (void); | ||
const char *hev_config_get_socks5_address (int *port); | ||
|
||
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); | ||
const char *hev_config_get_misc_log_level (void); | ||
int hev_config_get_misc_log_level (void); | ||
|
||
#endif /* __HEV_CONFIG_H__ */ |
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
============================================================================ | ||
Name : hev-main.c | ||
Author : Heiher <[email protected]> | ||
Copyright : Copyright (c) 2019 - 2020 Everyone. | ||
Copyright : Copyright (c) 2019 - 2021 hev | ||
Description : Main | ||
============================================================================ | ||
*/ | ||
|
@@ -21,6 +21,7 @@ | |
#include "hev-config.h" | ||
#include "hev-config-const.h" | ||
#include "hev-logger.h" | ||
#include "hev-socks5-logger.h" | ||
#include "hev-socks5-tunnel.h" | ||
|
||
#include "hev-main.h" | ||
|
@@ -39,7 +40,7 @@ run_as_daemon (const char *pid_file) | |
|
||
fp = fopen (pid_file, "w+"); | ||
if (!fp) { | ||
LOG_E ("Open pid file %s failed!", pid_file); | ||
LOG_E ("open pid file %s", pid_file); | ||
return; | ||
} | ||
|
||
|
@@ -94,32 +95,47 @@ int | |
main (int argc, char *argv[]) | ||
{ | ||
const char *pid_file; | ||
int tun_fd = -1, limit_nofile; | ||
const char *log_file; | ||
int tun_fd = -1; | ||
int log_level; | ||
int nofile; | ||
int res; | ||
|
||
if (2 > argc) { | ||
if (argc < 2) { | ||
show_help (argv[0]); | ||
return -1; | ||
} | ||
|
||
if (2 < argc) | ||
if (argc > 2) | ||
tun_fd = strtol (argv[2], NULL, 10); | ||
|
||
if (0 > hev_config_init (argv[1])) | ||
res = hev_config_init (argv[1]); | ||
if (res < 0) | ||
return -2; | ||
|
||
if (0 > hev_logger_init ()) | ||
log_file = hev_config_get_misc_log_file (); | ||
log_level = hev_config_get_misc_log_level (); | ||
|
||
res = hev_logger_init (log_level, log_file); | ||
if (res < 0) | ||
return -3; | ||
|
||
limit_nofile = hev_config_get_misc_limit_nofile (); | ||
if (0 > set_limit_nofile (limit_nofile)) { | ||
LOG_E ("Set limit nofile failed!"); | ||
res = hev_socks5_logger_init (log_level, log_file); | ||
if (res < 0) | ||
return -4; | ||
|
||
nofile = hev_config_get_misc_limit_nofile (); | ||
res = set_limit_nofile (nofile); | ||
if (res < 0) { | ||
LOG_E ("set limit nofile"); | ||
return -5; | ||
} | ||
|
||
lwip_init (); | ||
|
||
if (0 > hev_socks5_tunnel_init (tun_fd)) | ||
return -5; | ||
res = hev_socks5_tunnel_init (tun_fd); | ||
if (res < 0) | ||
return -6; | ||
|
||
pid_file = hev_config_get_misc_pid_file (); | ||
if (pid_file) | ||
|
@@ -128,6 +144,7 @@ main (int argc, char *argv[]) | |
hev_socks5_tunnel_run (); | ||
|
||
hev_socks5_tunnel_fini (); | ||
hev_socks5_logger_fini (); | ||
hev_logger_fini (); | ||
hev_config_fini (); | ||
lwip_fini (); | ||
|
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 |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
============================================================================ | ||
Name : hev-main.h | ||
Author : Heiher <[email protected]> | ||
Copyright : Copyright (c) 2019 - 2020 Everyone. | ||
Copyright : Copyright (c) 2019 - 2021 hev | ||
Description : Main | ||
============================================================================ | ||
*/ | ||
|
Oops, something went wrong.