From 3d2b98016149ddf4c0c6912b16f56f2a56cb658b Mon Sep 17 00:00:00 2001 From: Max Lv Date: Fri, 24 Jul 2015 21:38:12 +0800 Subject: [PATCH] add android vpn mode option --- src/android.c | 6 +++--- src/local.c | 25 ++++++++++++++++++++----- src/tunnel.c | 23 +++++++++++++++++++---- src/udprelay.c | 13 ++++++++----- 4 files changed, 50 insertions(+), 17 deletions(-) diff --git a/src/android.c b/src/android.c index 793d6e6c..33a76eea 100644 --- a/src/android.c +++ b/src/android.c @@ -46,7 +46,7 @@ #include "utils.h" int protect_socket(int fd) { - int sock, recvfd; + int sock; struct sockaddr_un addr; if ( (sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { @@ -56,8 +56,8 @@ int protect_socket(int fd) { // Set timeout to 100us struct timeval tv; - tv.tv_sec = 0; /* 30 Secs Timeout */ - tv.tv_usec = 1000; // Not init'ing this can cause strange errors + tv.tv_sec = 1; /* 0 Secs Timeout */ + tv.tv_usec = 0; // Not init'ing this can cause strange errors setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)); setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval)); diff --git a/src/local.c b/src/local.c index 8f7c239b..c0388949 100644 --- a/src/local.c +++ b/src/local.c @@ -80,6 +80,9 @@ #endif int verbose = 0; +#ifdef ANDROID +int vpn = 0; +#endif static int acl = 0; static int mode = TCP_ONLY; @@ -255,11 +258,13 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) if (!remote->send_ctx->connected) { #ifdef ANDROID - if (protect_socket(remote->fd) == -1) { - ERROR("protect_socket"); - close_and_free_remote(EV_A_ remote); - close_and_free_server(EV_A_ server); - return; + if (vpn) { + if (protect_socket(remote->fd) == -1) { + ERROR("protect_socket"); + close_and_free_remote(EV_A_ remote); + close_and_free_server(EV_A_ server); + return; + } } #endif @@ -915,8 +920,13 @@ int main(int argc, char **argv) USE_TTY(); +#ifdef ANDROID + while ((c = getopt_long(argc, argv, "f:s:p:l:k:t:m:i:c:b:a:uvV", + long_options, &option_index)) != -1) { +#else while ((c = getopt_long(argc, argv, "f:s:p:l:k:t:m:i:c:b:a:uv", long_options, &option_index)) != -1) { +#endif switch (c) { case 0: if (option_index == 0) { @@ -969,6 +979,11 @@ int main(int argc, char **argv) case 'v': verbose = 1; break; +#ifdef ANDROID + case 'V': + vpn = 1; + break; +#endif } } diff --git a/src/tunnel.c b/src/tunnel.c index f975339c..1407319f 100644 --- a/src/tunnel.c +++ b/src/tunnel.c @@ -85,7 +85,11 @@ static void close_and_free_remote(EV_P_ struct remote *remote); static void free_server(struct server *server); static void close_and_free_server(EV_P_ struct server *server); +#ifdef ANDROID +int vpn = 0; +#endif int verbose = 0; + static int mode = TCP_ONLY; #ifndef __MINGW32__ @@ -603,10 +607,12 @@ static void accept_cb(EV_P_ ev_io *w, int revents) } #ifdef ANDROID - if (protect_socket(remotefd) == -1) { - ERROR("protect_socket"); - close(remotefd); - return; + if (vpn) { + if (protect_socket(remotefd) == -1) { + ERROR("protect_socket"); + close(remotefd); + return; + } } #endif @@ -662,7 +668,11 @@ int main(int argc, char **argv) USE_TTY(); +#ifdef ANDROID + while ((c = getopt(argc, argv, "f:s:p:l:k:t:m:i:c:b:L:a:uUvV")) != -1) { +#else while ((c = getopt(argc, argv, "f:s:p:l:k:t:m:i:c:b:L:a:uUv")) != -1) { +#endif switch (c) { case 's': if (remote_num < MAX_REMOTE_NUM) { @@ -713,6 +723,11 @@ int main(int argc, char **argv) case 'v': verbose = 1; break; +#ifdef ANDROID + case 'V': + vpn = 1; + break; +#endif } } diff --git a/src/udprelay.c b/src/udprelay.c index 6c772b98..a3e7443c 100644 --- a/src/udprelay.c +++ b/src/udprelay.c @@ -95,6 +95,7 @@ static void close_and_free_remote(EV_P_ struct remote_ctx *ctx); static struct remote_ctx * new_remote(int fd, struct server_ctx * server_ctx); extern int verbose; +extern int vpn; static int server_num = 0; static struct server_ctx *server_ctx_list[MAX_REMOTE_NUM] = { NULL }; @@ -1059,11 +1060,13 @@ static void server_recv_cb(EV_P_ ev_io *w, int revents) buf = ss_encrypt_all(BUF_SIZE, buf, &buf_len, server_ctx->method); -#if defined(ANDROID) - if (protect_socket(remote_ctx->fd) == -1) { - ERROR("protect_socket"); - close_and_free_remote(EV_A_ remote_ctx); - goto CLEAN_UP; +#ifdef ANDROID + if (vpn) { + if (protect_socket(remote_ctx->fd) == -1) { + ERROR("protect_socket"); + close_and_free_remote(EV_A_ remote_ctx); + goto CLEAN_UP; + } } #endif int s = sendto(remote_ctx->fd, buf, buf_len, 0, remote_addr, remote_addr_len);