Browse Source

Fix UDP relay when plugin enabled

pull/1085/head
Max Lv 7 years ago
parent
commit
0acc79b430
7 changed files with 85 additions and 44 deletions
  1. 2
      src/jconf.c
  2. 22
      src/local.c
  3. 4
      src/manager.c
  4. 24
      src/redir.c
  5. 47
      src/server.c
  6. 28
      src/tunnel.c
  7. 2
      src/utils.c

2
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.");

22
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

4
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 }
};

24
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) {

47
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) {

28
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) {

2
src/utils.c

@ -361,7 +361,7 @@ usage()
printf(
" [--plugin <name>] Enable SIP003 plugin. (Experimental)\n");
printf(
" [--plugin_opts <options>] Set SIP003 plugin options. (Experimental)\n");
" [--plugin-opts <options>] Set SIP003 plugin options. (Experimental)\n");
#endif
printf("\n");
printf(

Loading…
Cancel
Save