From 0acc79b430b451cbcf50c8eec69c9f31ec551d1a Mon Sep 17 00:00:00 2001 From: Max Lv Date: Sat, 14 Jan 2017 12:33:49 +0800 Subject: [PATCH] Fix UDP relay when plugin enabled --- src/jconf.c | 2 -- src/local.c | 22 ++++++++++++++++------ src/manager.c | 4 ++-- src/redir.c | 24 +++++++++++++++++------- src/server.c | 47 +++++++++++++++++++++++++++++++---------------- src/tunnel.c | 28 ++++++++++++++++++---------- src/utils.c | 2 +- 7 files changed, 85 insertions(+), 44 deletions(-) diff --git a/src/jconf.c b/src/jconf.c index e6323e6c..6b472afe 100644 --- a/src/jconf.c +++ b/src/jconf.c @@ -114,8 +114,6 @@ read_jconf(const char *file) char *buf; json_value *obj; - LOGI("test"); - FILE *f = fopen(file, "rb"); if (f == NULL) { FATAL("Invalid config path."); diff --git a/src/local.c b/src/local.c index ed018c30..ef98325f 100644 --- a/src/local.c +++ b/src/local.c @@ -1475,10 +1475,6 @@ main(int argc, char **argv) if (err) { FATAL("failed to start the plugin"); } - - remote_num = 1; - remote_addr[0].host = plugin_host; - remote_addr[0].port = plugin_port; } #ifdef __MINGW32__ @@ -1502,12 +1498,18 @@ main(int argc, char **argv) char *host = remote_addr[i].host; char *port = remote_addr[i].port == NULL ? remote_port : remote_addr[i].port; + if (plugin != NULL) { + host = plugin_host; + port = plugin_port; + } struct sockaddr_storage *storage = ss_malloc(sizeof(struct sockaddr_storage)); memset(storage, 0, sizeof(struct sockaddr_storage)); if (get_sockaddr(host, port, storage, 1, ipv6first) == -1) { FATAL("failed to resolve the provided hostname"); } listen_ctx.remote_addr[i] = (struct sockaddr *)storage; + + if (plugin != NULL) break; } listen_ctx.timeout = atoi(timeout); listen_ctx.iface = iface; @@ -1552,8 +1554,16 @@ main(int argc, char **argv) // Setup UDP if (mode != TCP_ONLY) { LOGI("udprelay enabled"); - init_udprelay(local_addr, local_port, listen_ctx.remote_addr[0], - get_sockaddr_len(listen_ctx.remote_addr[0]), mtu, m, auth, listen_ctx.timeout, iface); + char *host = remote_addr[0].host; + char *port = remote_addr[0].port == NULL ? remote_port : remote_addr[0].port; + struct sockaddr_storage *storage = ss_malloc(sizeof(struct sockaddr_storage)); + memset(storage, 0, sizeof(struct sockaddr_storage)); + if (get_sockaddr(host, port, storage, 1, ipv6first) == -1) { + FATAL("failed to resolve the provided hostname"); + } + struct sockaddr *addr = (struct sockaddr *)storage; + init_udprelay(local_addr, local_port, addr, + get_sockaddr_len(addr), mtu, m, auth, listen_ctx.timeout, iface); } #ifdef HAVE_LAUNCHD diff --git a/src/manager.c b/src/manager.c index 6093b566..4e92eaaf 100644 --- a/src/manager.c +++ b/src/manager.c @@ -183,7 +183,7 @@ construct_command_line(struct manager_ctx *manager, struct server *server) } if (manager->plugin_opts) { int len = strlen(cmd); - snprintf(cmd + len, BUF_SIZE - len, " --plugin_opts \"%s\"", manager->plugin_opts); + snprintf(cmd + len, BUF_SIZE - len, " --plugin-opts \"%s\"", manager->plugin_opts); } for (i = 0; i < manager->nameserver_num; i++) { int len = strlen(cmd); @@ -912,7 +912,7 @@ main(int argc, char **argv) { "executable", required_argument, 0, 0 }, { "mtu", required_argument, 0, 0 }, { "plugin", required_argument, 0, 0 }, - { "plugin_opts", required_argument, 0, 0 }, + { "plugin-opts", required_argument, 0, 0 }, { "help", no_argument, 0, 0 }, { 0, 0, 0, 0 } }; diff --git a/src/redir.c b/src/redir.c index 0543e49d..c484efef 100644 --- a/src/redir.c +++ b/src/redir.c @@ -808,7 +808,7 @@ main(int argc, char **argv) { "mtu", required_argument, 0, 0 }, { "mptcp", no_argument, 0, 0 }, { "plugin", required_argument, 0, 0 }, - { "plugin_opts", required_argument, 0, 0 }, + { "plugin-opts", required_argument, 0, 0 }, { "help", no_argument, 0, 0 }, { 0, 0, 0, 0 } }; @@ -1033,10 +1033,6 @@ main(int argc, char **argv) if (err) { FATAL("failed to start the plugin"); } - - remote_num = 1; - remote_addr[0].host = plugin_host; - remote_addr[0].port = plugin_port; } // ignore SIGPIPE @@ -1063,12 +1059,18 @@ main(int argc, char **argv) char *host = remote_addr[i].host; char *port = remote_addr[i].port == NULL ? remote_port : remote_addr[i].port; + if (plugin != NULL) { + host = plugin_host; + port = plugin_port; + } struct sockaddr_storage *storage = ss_malloc(sizeof(struct sockaddr_storage)); memset(storage, 0, sizeof(struct sockaddr_storage)); if (get_sockaddr(host, port, storage, 1, ipv6first) == -1) { FATAL("failed to resolve the provided hostname"); } listen_ctx.remote_addr[i] = (struct sockaddr *)storage; + + if (plugin != NULL) break; } listen_ctx.timeout = atoi(timeout); listen_ctx.method = m; @@ -1097,8 +1099,16 @@ main(int argc, char **argv) // Setup UDP if (mode != TCP_ONLY) { LOGI("UDP relay enabled"); - init_udprelay(local_addr, local_port, listen_ctx.remote_addr[0], - get_sockaddr_len(listen_ctx.remote_addr[0]), mtu, m, auth, listen_ctx.timeout, NULL); + char *host = remote_addr[0].host; + char *port = remote_addr[0].port == NULL ? remote_port : remote_addr[0].port; + struct sockaddr_storage *storage = ss_malloc(sizeof(struct sockaddr_storage)); + memset(storage, 0, sizeof(struct sockaddr_storage)); + if (get_sockaddr(host, port, storage, 1, ipv6first) == -1) { + FATAL("failed to resolve the provided hostname"); + } + struct sockaddr *addr = (struct sockaddr *)storage; + init_udprelay(local_addr, local_port, addr, + get_sockaddr_len(addr), mtu, m, auth, listen_ctx.timeout, NULL); } if (mode == UDP_ONLY) { diff --git a/src/server.c b/src/server.c index 944fec21..02a2f40d 100644 --- a/src/server.c +++ b/src/server.c @@ -1603,7 +1603,7 @@ main(int argc, char **argv) { "mtu", required_argument, 0, 0 }, { "help", no_argument, 0, 0 }, { "plugin", required_argument, 0, 0 }, - { "plugin_opts", required_argument, 0, 0 }, + { "plugin-opts", required_argument, 0, 0 }, #ifdef __linux__ { "mptcp", no_argument, 0, 0 }, #endif @@ -1902,9 +1902,6 @@ main(int argc, char **argv) len = strlen(server_str); } - server_host[0] = "127.0.0.1"; - server_num = 1; - int err = start_plugin(plugin, plugin_opts, server_str, plugin_port, server_host[0], server_port); if (err) { @@ -1916,10 +1913,14 @@ main(int argc, char **argv) listen_ctx_t listen_ctx_list[server_num]; // bind to each interface - for (int i = 0; i < server_num; i++) { - const char *host = server_host[i]; + if (mode != UDP_ONLY) { + for (int i = 0; i < server_num; i++) { + const char *host = server_host[i]; + + if (plugin != NULL) { + host = "127.0.0.1"; + } - if (mode != UDP_ONLY) { // Bind to port int listenfd; listenfd = create_and_bind(host, server_port, mptcp); @@ -1942,18 +1943,31 @@ main(int argc, char **argv) ev_io_init(&listen_ctx->io, accept_cb, listenfd, EV_READ); ev_io_start(loop, &listen_ctx->io); - } - // Setup UDP - if (mode != TCP_ONLY) { - init_udprelay(server_host[i], server_port, mtu, m, - auth, atoi(timeout), iface); + if (host && strcmp(host, ":") > 0) + LOGI("tcp server listening at [%s]:%s", host, server_port); + else + LOGI("tcp server listening at %s:%s", host ? host : "*", server_port); + + if (plugin != NULL) break; } + } - if (host && strcmp(host, ":") > 0) - LOGI("listening at [%s]:%s", host, server_port); - else - LOGI("listening at %s:%s", host ? host : "*", server_port); + if (mode != TCP_ONLY) { + for (int i = 0; i < server_num; i++) { + const char *host = server_host[i]; + const char *port = server_port; + if (plugin != NULL) { + port = plugin_port; + } + // Setup UDP + init_udprelay(host, port, mtu, m, + auth, atoi(timeout), iface); + if (host && strcmp(host, ":") > 0) + LOGI("udp server listening at [%s]:%s", host, port); + else + LOGI("udp server listening at %s:%s", host ? host : "*", port); + } } if (manager_address != NULL) { @@ -2008,6 +2022,7 @@ main(int argc, char **argv) ev_io_stop(loop, &listen_ctx->io); close(listen_ctx->fd); } + if (plugin != NULL) break; } if (mode != UDP_ONLY) { diff --git a/src/tunnel.c b/src/tunnel.c index 670b7d33..7fffee86 100644 --- a/src/tunnel.c +++ b/src/tunnel.c @@ -776,7 +776,7 @@ main(int argc, char **argv) { "mtu", required_argument, 0, 0 }, { "mptcp", no_argument, 0, 0 }, { "plugin", required_argument, 0, 0 }, - { "plugin_opts", required_argument, 0, 0 }, + { "plugin-opts", required_argument, 0, 0 }, { "help", no_argument, 0, 0 }, { 0, 0, 0, 0 } }; @@ -1033,10 +1033,6 @@ main(int argc, char **argv) if (err) { FATAL("failed to start the plugin"); } - - remote_num = 1; - remote_addr[0].host = plugin_host; - remote_addr[0].port = plugin_port; } #ifdef __MINGW32__ @@ -1067,14 +1063,19 @@ main(int argc, char **argv) memset(listen_ctx.remote_addr, 0, sizeof(struct sockaddr *) * remote_num); for (i = 0; i < remote_num; i++) { char *host = remote_addr[i].host; - char *port = remote_addr[i].port == NULL ? remote_port : - remote_addr[i].port; + char *port = remote_addr[i].port == NULL ? remote_port : remote_addr[i].port; + if (plugin != NULL) { + host = plugin_host; + port = plugin_port; + } struct sockaddr_storage *storage = ss_malloc(sizeof(struct sockaddr_storage)); memset(storage, 0, sizeof(struct sockaddr_storage)); if (get_sockaddr(host, port, storage, 1, ipv6first) == -1) { FATAL("failed to resolve the provided hostname"); } listen_ctx.remote_addr[i] = (struct sockaddr *)storage; + + if (plugin != NULL) break; } listen_ctx.timeout = atoi(timeout); listen_ctx.iface = iface; @@ -1104,9 +1105,16 @@ main(int argc, char **argv) // Setup UDP if (mode != TCP_ONLY) { LOGI("UDP relay enabled"); - init_udprelay(local_addr, local_port, listen_ctx.remote_addr[0], - get_sockaddr_len(listen_ctx.remote_addr[0]), - tunnel_addr, mtu, m, auth, listen_ctx.timeout, iface); + char *host = remote_addr[0].host; + char *port = remote_addr[0].port == NULL ? remote_port : remote_addr[0].port; + struct sockaddr_storage *storage = ss_malloc(sizeof(struct sockaddr_storage)); + memset(storage, 0, sizeof(struct sockaddr_storage)); + if (get_sockaddr(host, port, storage, 1, ipv6first) == -1) { + FATAL("failed to resolve the provided hostname"); + } + struct sockaddr *addr = (struct sockaddr *)storage; + init_udprelay(local_addr, local_port, addr, get_sockaddr_len(addr), + tunnel_addr, mtu, m, auth, listen_ctx.timeout, iface); } if (mode == UDP_ONLY) { diff --git a/src/utils.c b/src/utils.c index 9b3c9a48..12ef85be 100644 --- a/src/utils.c +++ b/src/utils.c @@ -361,7 +361,7 @@ usage() printf( " [--plugin ] Enable SIP003 plugin. (Experimental)\n"); printf( - " [--plugin_opts ] Set SIP003 plugin options. (Experimental)\n"); + " [--plugin-opts ] Set SIP003 plugin options. (Experimental)\n"); #endif printf("\n"); printf(