From fe8dd282f6c75c79d5bb9e80f78b7d17fce43a9e Mon Sep 17 00:00:00 2001 From: Julius Schwartzenberg Date: Fri, 22 Dec 2023 11:09:02 +0100 Subject: [PATCH] Resolve problems connectings coming in via SOCKS --- forward.c | 14 +++++++++++++- main.c | 3 ++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/forward.c b/forward.c index e43d148..2d78f21 100644 --- a/forward.c +++ b/forward.c @@ -509,6 +509,7 @@ int prepare_http_connect(int sd, struct auth_s *credentials, const char *thost) rr_data_t data2; int rc = 0; hlist_t tl; + char *pos; if (!sd || !thost || !strlen(thost)) return 0; @@ -519,6 +520,13 @@ int prepare_http_connect(int sd, struct auth_s *credentials, const char *thost) data1->req = 1; data1->method = strdup("CONNECT"); data1->url = strdup(thost); + data1->hostname = strdup(thost); + if ((pos = strchr(data1->hostname, ':')) != NULL) { // separate port + *pos = 0; + data1->port = atoi(pos + 1); + } + else + data1->port = 0; data1->http = strdup("HTTP/1.1"); data1->headers = hlist_mod(data1->headers, "Proxy-Connection", "keep-alive", 1); @@ -586,11 +594,15 @@ int forward_tunnel(void *thread_data) { assert(thread_data != NULL); int cd = ((struct thread_arg_s *)thread_data)->fd; char *thost = ((struct thread_arg_s *)thread_data)->target; + char *thost_noport = strdup(thost); + char *pos; char saddr[INET6_ADDRSTRLEN] = {0}; INET_NTOP(&((struct thread_arg_s *)thread_data)->addr, saddr, INET6_ADDRSTRLEN); tcreds = new_auth(); - sd = proxy_connect(tcreds, "/", thost); + if ((pos = strchr(thost_noport, ':')) != NULL) // separate port + *pos = 0; + sd = proxy_connect(tcreds, thost, thost_noport); if (sd < 0) goto bailout; diff --git a/main.c b/main.c index 521b201..109b257 100644 --- a/main.c +++ b/main.c @@ -611,11 +611,12 @@ void *socks5_thread(void *thread_data) { i = (sd >= 0); } else { snprintf(tport, MINIBUF_SIZE, "%d", ntohs(port)); + char *thost_noport = strdup(thost); strlcat(thost, ":", HOST_BUFSIZE); strlcat(thost, tport, HOST_BUFSIZE); tcreds = new_auth(); - sd = proxy_connect(tcreds, "/", thost); + sd = proxy_connect(tcreds, thost, thost_noport); if (sd == -2) { // remove previously added port to thost char* t = thost;