From 29b0a3345eec210fda2cffe788eff6227b007407 Mon Sep 17 00:00:00 2001 From: nfalse Date: Thu, 17 Aug 2023 10:00:02 +0800 Subject: [PATCH] Fix the issue where the program exits when the operating system does not support TCP_FASTOPEN_CONNECT. If the operating system does not support TCP_FASTOPEN_CONNECT or in a container environment, the service should still function normally, albeit with lower performance. --- src/local.c | 2 +- src/redir.c | 2 +- src/server.c | 2 +- src/tunnel.c | 2 +- src/utils.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/local.c b/src/local.c index fa1ca7b3..5eb3c683 100644 --- a/src/local.c +++ b/src/local.c @@ -714,7 +714,7 @@ server_stream(EV_P_ ev_io *w, buffer_t *buf) int optval = 1; if (setsockopt(remote->fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, (void *)&optval, sizeof(optval)) < 0) - FATAL("failed to set TCP_FASTOPEN_CONNECT"); + LOGW("failed to set TCP_FASTOPEN_CONNECT"); s = connect(remote->fd, (struct sockaddr *)&(remote->addr), remote->addr_len); #else FATAL("fast open is not enabled in this build"); diff --git a/src/redir.c b/src/redir.c index d36fe3fe..1a709f04 100644 --- a/src/redir.c +++ b/src/redir.c @@ -551,7 +551,7 @@ remote_send_cb(EV_P_ ev_io *w, int revents) int optval = 1; if (setsockopt(remote->fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, (void *)&optval, sizeof(optval)) < 0) - FATAL("failed to set TCP_FASTOPEN_CONNECT"); + LOGW("failed to set TCP_FASTOPEN_CONNECT"); s = connect(remote->fd, remote->addr, get_sockaddr_len(remote->addr)); if (s == 0) s = send(remote->fd, remote->buf->data, remote->buf->len, 0); diff --git a/src/server.c b/src/server.c index 73b65996..ec75470f 100644 --- a/src/server.c +++ b/src/server.c @@ -789,7 +789,7 @@ connect_to_remote(EV_P_ struct addrinfo *res, int optval = 1; if (setsockopt(sockfd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, (void *)&optval, sizeof(optval)) < 0) - FATAL("failed to set TCP_FASTOPEN_CONNECT"); + LOGW("failed to set TCP_FASTOPEN_CONNECT"); s = connect(sockfd, res->ai_addr, res->ai_addrlen); #elif defined(CONNECT_DATA_IDEMPOTENT) struct sockaddr_in sa; diff --git a/src/tunnel.c b/src/tunnel.c index 99ed4128..5e166395 100644 --- a/src/tunnel.c +++ b/src/tunnel.c @@ -534,7 +534,7 @@ remote_send_cb(EV_P_ ev_io *w, int revents) int optval = 1; if (setsockopt(remote->fd, IPPROTO_TCP, TCP_FASTOPEN_CONNECT, (void *)&optval, sizeof(optval)) < 0) - FATAL("failed to set TCP_FASTOPEN_CONNECT"); + LOGW("failed to set TCP_FASTOPEN_CONNECT"); s = connect(remote->fd, remote->addr, get_sockaddr_len(remote->addr)); if (s == 0) s = send(remote->fd, remote->buf->data, remote->buf->len, 0); diff --git a/src/utils.h b/src/utils.h index 1df24c16..44e52211 100644 --- a/src/utils.h +++ b/src/utils.h @@ -40,6 +40,9 @@ #define LOGI(...) \ ((void)__android_log_print(ANDROID_LOG_DEBUG, "shadowsocks", \ __VA_ARGS__)) +#define LOGW(...) \ + ((void)__android_log_print(ANDROID_LOG_WARN, "shadowsocks", \ + __VA_ARGS__)) #define LOGE(...) \ ((void)__android_log_print(ANDROID_LOG_ERROR, "shadowsocks", \ __VA_ARGS__)) @@ -74,6 +77,17 @@ extern FILE *logfile; fflush(logfile); } \ } \ while (0) +#define LOGW(format, ...) \ + do { \ + if (logfile != NULL) { \ + time_t now = time(NULL); \ + char timestr[20]; \ + strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \ + fprintf(logfile, " %s WARNNING: " format "\n", timestr, \ + ## __VA_ARGS__); \ + fflush(logfile); } \ + } \ + while (0) #define LOGE(format, ...) \ do { \ if (logfile != NULL) { \ @@ -107,6 +121,19 @@ extern FILE *logfile; } \ while (0) +#define LOGW(format, ...) \ + do { \ + time_t now = time(NULL); \ + char timestr[20]; \ + strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \ + ss_color_error(); \ + fprintf(stderr, " %s WARNNING: ", timestr); \ + ss_color_reset(); \ + fprintf(stderr, format "\n", ## __VA_ARGS__); \ + fflush(stderr); \ + } \ + while (0) + #define LOGE(format, ...) \ do { \ time_t now = time(NULL); \ @@ -165,6 +192,26 @@ extern int use_syslog; } \ while (0) +#define LOGW(format, ...) \ + do { \ + if (use_syslog) { \ + syslog(LOG_ERR, format, ## __VA_ARGS__); \ + } else { \ + time_t now = time(NULL); \ + char timestr[20]; \ + strftime(timestr, 20, TIME_FORMAT, localtime(&now)); \ + if (use_tty) { \ + fprintf(stderr, "\e[01;35m %s WARNNING: \e[0m" format "\n", timestr, \ + ## __VA_ARGS__); \ + fflush(stderr); \ + } else { \ + fprintf(stderr, " %s WARNNING: " format "\n", timestr, \ + ## __VA_ARGS__); \ + fflush(stderr); \ + } \ + } } \ + while (0) + #define LOGE(format, ...) \ do { \ if (use_syslog) { \