Skip to content

Commit

Permalink
add -G/--graceful option, v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
zfl9 committed Oct 27, 2019
1 parent 3918af1 commit c65ed1e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ usage: ipt2socks <options...>. the existing options are as follows:
-c, --cache-size <size> max size of udp lrucache, default: 256
-f, --buffer-size <size> buffer size of tcp socket, default: 8192
-u, --run-user <user> run the ipt2socks with the specified user
-G, --graceful gracefully close the tcp connection pair
-R, --redirect use redirect instead of tproxy (for tcp)
-T, --tcp-only listen tcp only, aka: disable udp proxy
-U, --udp-only listen udp only, aka: disable tcp proxy
Expand All @@ -84,6 +85,7 @@ usage: ipt2socks <options...>. the existing options are as follows:
- -c 选项设置 ipt2socks 的 UDP 缓存最大大小。
- -f 选项设置 ipt2socks 的 TCP 接收缓冲区大小。
- -u 选项设置 ipt2socks 的用户ID,`run_as_user`
- -G 选项指示 ipt2socks 优雅关闭 TCP 代理连接对。
- -R 选项指示 ipt2socks 使用 REDIRECT 而非 TPROXY。
- -T 选项指示 ipt2socks 仅启用 TCP 透明代理监听端口。
- -U 选项指示 ipt2socks 仅启用 UDP 透明代理监听端口。
Expand Down
17 changes: 12 additions & 5 deletions ipt2socks.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ enum {
OPTION_IPV4 = 0x01 << 2, /* enable ipv4 */
OPTION_IPV6 = 0x01 << 3, /* enable ipv6 */
OPTION_DNAT = 0x01 << 4, /* use REDIRECT instead of TPROXY (for tcp) */
OPTION_HFCLS = 0x01 << 5, /* gracefully close the tcp connection pair */
OPTION_DEFAULT = OPTION_TCP | OPTION_UDP | OPTION_IPV4 | OPTION_IPV6, /* default behavior */
};

Expand All @@ -42,7 +43,7 @@ enum {
#define BIND_PORT_DEFAULT 60080

/* ipt2socks version string */
#define IPT2SOCKS_VERSION "ipt2socks v1.0.0 <https://github.com/zfl9/ipt2socks>"
#define IPT2SOCKS_VERSION "ipt2socks v1.0.1 <https://github.com/zfl9/ipt2socks>"

/* tcp stream context typedef */
typedef struct {
Expand Down Expand Up @@ -145,6 +146,7 @@ static void print_command_help(void) {
" -c, --cache-size <size> max size of udp lrucache, default: 256\n"
" -f, --buffer-size <size> buffer size of tcp socket, default: 8192\n"
" -u, --run-user <user> run the ipt2socks with the specified user\n"
" -G, --graceful gracefully close the tcp connection pair\n"
" -R, --redirect use redirect instead of tproxy (for tcp)\n"
" -T, --tcp-only listen tcp only, aka: disable udp proxy\n"
" -U, --udp-only listen udp only, aka: disable tcp proxy\n"
Expand All @@ -158,7 +160,7 @@ static void print_command_help(void) {

/* parsing command line arguments */
static void parse_command_args(int argc, char* argv[]) {
const char *optstr = ":s:p:b:B:l:j:n:o:c:f:u:RTU46vVh";
const char *optstr = ":s:p:b:B:l:j:n:o:c:f:u:GRTU46vVh";
const struct option options[] = {
{"server-addr", required_argument, NULL, 's'},
{"server-port", required_argument, NULL, 'p'},
Expand All @@ -171,6 +173,7 @@ static void parse_command_args(int argc, char* argv[]) {
{"cache-size", required_argument, NULL, 'c'},
{"buffer-size", required_argument, NULL, 'f'},
{"run-user", required_argument, NULL, 'u'},
{"graceful", no_argument, NULL, 'G'},
{"redirect", no_argument, NULL, 'R'},
{"tcp-only", no_argument, NULL, 'T'},
{"udp-only", no_argument, NULL, 'U'},
Expand Down Expand Up @@ -277,6 +280,9 @@ static void parse_command_args(int argc, char* argv[]) {
case 'u':
run_as_user(optarg, argv);
break;
case 'G':
g_options |= OPTION_HFCLS;
break;
case 'R':
g_options |= OPTION_DNAT;
strcpy(g_bind_ipstr4, IP4STR_WILDCARD);
Expand Down Expand Up @@ -370,6 +376,7 @@ int main(int argc, char* argv[]) {
if (g_options & OPTION_TCP) LOGINF("[main] enable tcp transparent proxy");
if (g_options & OPTION_UDP) LOGINF("[main] enable udp transparent proxy");
if (g_options & OPTION_DNAT) LOGINF("[main] use redirect instead of tproxy");
if (g_options & OPTION_HFCLS) LOGINF("[main] gracefully close tcp connection");
IF_VERBOSE LOGINF("[main] verbose mode (affect performance)");

for (int i = 0; i < g_nthreads - 1; ++i) {
Expand Down Expand Up @@ -482,7 +489,7 @@ static void tcp_socket_listen_cb(uv_stream_t *listener, int status) {

int sockfd = -1;
uv_fileno((void *)client_stream, &sockfd);
set_keepalive(sockfd); /* enable SO_KEEPALIVE */
if (g_options & OPTION_HFCLS) set_keepalive(sockfd);
skaddr6_t skaddr; char ipstr[IP6STRLEN]; portno_t portno;

IF_VERBOSE {
Expand Down Expand Up @@ -530,7 +537,7 @@ static void tcp_socket_listen_cb(uv_stream_t *listener, int status) {
}

uv_fileno((void *)socks5_stream, &sockfd);
set_keepalive(sockfd); /* enable SO_KEEPALIVE */
if (g_options & OPTION_HFCLS) set_keepalive(sockfd);

tcpcontext_t *context = malloc(sizeof(tcpcontext_t));
context->client_stream = client_stream;
Expand All @@ -539,7 +546,7 @@ static void tcp_socket_listen_cb(uv_stream_t *listener, int status) {
context->socks5_buffer = malloc(g_tcpbufsiz);
context->client_wrtreq = malloc(sizeof(uv_write_t));
context->socks5_wrtreq = malloc(sizeof(uv_write_t));
context->is_half_close = false;
context->is_half_close = g_options & OPTION_HFCLS ? false : true;
client_stream->data = context;
socks5_stream->data = context;

Expand Down

0 comments on commit c65ed1e

Please sign in to comment.