From 57b0a1b92c6d2ac7a5a21ca1c0bb14d71bd259ff Mon Sep 17 00:00:00 2001 From: radkesvat <134321679+radkesvat@users.noreply.github.com> Date: Tue, 23 Apr 2024 12:33:30 +0000 Subject: [PATCH] fix the bugs created after reformat --- tunnels/adapters/connector/tcp/tcp_connector.c | 8 ++++++-- tunnels/adapters/connector/udp/udp_connector.c | 2 +- tunnels/adapters/listener/tcp/tcp_listener.c | 1 - .../server/trojan/socks/trojan_socks_server.c | 18 ++++++------------ ww/utils/sockutils.h | 1 + 5 files changed, 14 insertions(+), 16 deletions(-) diff --git a/tunnels/adapters/connector/tcp/tcp_connector.c b/tunnels/adapters/connector/tcp/tcp_connector.c index 8d082c00..b2115c2e 100644 --- a/tunnels/adapters/connector/tcp/tcp_connector.c +++ b/tunnels/adapters/connector/tcp/tcp_connector.c @@ -61,7 +61,7 @@ static bool resumeWriteQueue(tcp_connector_con_state_t *cstate) unsigned int bytes = bufLen(cw->payload); int nwrite = hio_write(io, cw->payload); - cw->payload = NULL; + cw->payload = NULL; contextQueuePush(cstate->finished_queue, cw); if (nwrite >= 0 && nwrite < bytes) { @@ -163,6 +163,10 @@ static void onClose(hio_t *io) static void onOutBoundConnected(hio_t *upstream_io) { tcp_connector_con_state_t *cstate = hevent_userdata(upstream_io); + if (cstate == NULL) + { + return; + } #ifdef PROFILE struct timeval tv2; gettimeofday(&tv2, NULL); @@ -206,7 +210,7 @@ static void upStream(tunnel_t *self, context_t *c) { unsigned int bytes = bufLen(c->payload); int nwrite = hio_write(cstate->io, c->payload); - c->payload = NULL; + c->payload = NULL; if (nwrite >= 0 && nwrite < bytes) { if (c->src_io) diff --git a/tunnels/adapters/connector/udp/udp_connector.c b/tunnels/adapters/connector/udp/udp_connector.c index 97fd3889..2c491e2e 100644 --- a/tunnels/adapters/connector/udp/udp_connector.c +++ b/tunnels/adapters/connector/udp/udp_connector.c @@ -68,7 +68,7 @@ static void upStream(tunnel_t *self, context_t *c) size_t nwrite = hio_write(cstate->io, c->payload); c->payload = NULL; - assert(nwrite >= 0 && nwrite < bytes); + assert(nwrite <= 0 || nwrite == bytes); destroyContext(c); } else diff --git a/tunnels/adapters/listener/tcp/tcp_listener.c b/tunnels/adapters/listener/tcp/tcp_listener.c index e2147041..d22f0d71 100644 --- a/tunnels/adapters/listener/tcp/tcp_listener.c +++ b/tunnels/adapters/listener/tcp/tcp_listener.c @@ -63,7 +63,6 @@ static void cleanup(tcp_listener_con_state_t *cstate, bool write_queue) { reuseContextBuffer(cw); } - reuseContextBuffer(cw); destroyContext(cw); } diff --git a/tunnels/server/trojan/socks/trojan_socks_server.c b/tunnels/server/trojan/socks/trojan_socks_server.c index d34c03c4..b475aa08 100644 --- a/tunnels/server/trojan/socks/trojan_socks_server.c +++ b/tunnels/server/trojan/socks/trojan_socks_server.c @@ -2,9 +2,9 @@ #include "buffer_stream.h" #include "hsocket.h" #include "loggers/network_logger.h" +#include "utils/sockutils.h" #include "utils/stringutils.h" #include "utils/userutils.h" -#include "utils/sockutils.h" #define CRLF_LEN 2 @@ -312,18 +312,12 @@ static bool processUdp(tunnel_t *self, trojan_socks_server_con_state_t *cstate, dest_context->address_type = kSatDomainName; // size_t addr_len = (unsigned char)(rawBuf(c->payload)[0]); shiftr(c->payload, 1); - if (dest_context->domain == NULL) + if (! cstate->first_sent) // print once per connection { - dest_context->domain = malloc(260); - - if (! cstate->first_sent) // print once per connection - { - LOGD("TrojanSocksServer: udp domain %.*s", domain_len, rawBuf(c->payload)); - } - - memcpy(dest_context->domain, rawBuf(c->payload), domain_len); - dest_context->domain[domain_len] = 0; + LOGD("TrojanSocksServer: udp domain %.*s", domain_len, rawBuf(c->payload)); } + + setSocketContextDomain(dest_context, rawBuf(c->payload), domain_len); shiftr(c->payload, domain_len); break; @@ -534,7 +528,7 @@ static inline void upStream(tunnel_t *self, context_t *c) memset(CSTATE(c), 0, sizeof(trojan_socks_server_con_state_t)); trojan_socks_server_con_state_t *cstate = CSTATE(c); cstate->udp_buf = newBufferStream(getContextBufferPool(c)); - allocateDomainBuffer(&(c->line->dest_ctx),true); + allocateDomainBuffer(&(c->line->dest_ctx), true); destroyContext(c); } else if (c->fin) diff --git a/ww/utils/sockutils.h b/ww/utils/sockutils.h index d022ff06..4cc919f4 100644 --- a/ww/utils/sockutils.h +++ b/ww/utils/sockutils.h @@ -49,6 +49,7 @@ inline void allocateDomainBuffer(socket_context_t *scontext, bool freeable) { scontext->domain_constant = ! freeable; scontext->domain = malloc(256); + scontext->domain_len = 0; #ifdef DEBUG memset(scontext->domain, 0xEE, 256); #endif