Skip to content

Commit

Permalink
Revert "build: remove the SOCKS_WITH_TS build condition (apache#9776)"
Browse files Browse the repository at this point in the history
This reverts commit da5ea31.
  • Loading branch information
bneradt committed Nov 26, 2024
1 parent 63752c1 commit ae206db
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
6 changes: 6 additions & 0 deletions include/iocore/net/Socks.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@

#pragma once

/*When this is being compiled with TS, we enable more features the use
non modularized stuff. namely:
ip_ranges and multiple socks server support.
*/
#define SOCKS_WITH_TS

#define SOCKS_DEFAULT_VERSION 0 // defined the configuration variable
#define SOCKS4_VERSION 4
#define SOCKS5_VERSION 5
Expand Down
18 changes: 17 additions & 1 deletion src/iocore/net/P_Socks.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
#include "iocore/net/Socks.h"
#include "tsutil/ts_errata.h"

#ifdef SOCKS_WITH_TS
#include "proxy/ParentSelection.h"
#endif

enum {
// types of events for Socks auth handlers
Expand All @@ -55,9 +57,21 @@ struct socks_conf_struct {
int accept_port = 0;
unsigned short http_port = 1080;

#ifdef SOCKS_WITH_TS
swoc::IPRangeSet ip_addrs;
#endif

socks_conf_struct() {}
#ifndef SOCKS_WITH_TS
IpEndpoint server_addr;
#endif

socks_conf_struct()

{
#if !defined(SOCKS_WITH_TS)
memset(&server_addr, 0, sizeof(server_addr));
#endif
}
};

void start_SocksProxy(int port);
Expand Down Expand Up @@ -108,10 +122,12 @@ struct SocksEntry : public Continuation {
SocksAuthHandler auth_handler = nullptr;
unsigned char socks_cmd = NORMAL_SOCKS;

#ifdef SOCKS_WITH_TS
// socks server selection:
ParentConfigParams *server_params = nullptr;
HttpRequestData req_data; // We dont use any http specific fields.
ParentResult server_result;
#endif

int startEvent(int event, void *data);
int mainEvent(int event, void *data);
Expand Down
23 changes: 21 additions & 2 deletions src/iocore/net/Socks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ SocksEntry::init(Ptr<ProxyMutex> &m, SocksNetVC *vc, unsigned char socks_support

ats_ip_copy(&target_addr, vc->get_remote_addr());

#ifdef SOCKS_WITH_TS
req_data.hdr = nullptr;
req_data.hostname_str = nullptr;
req_data.api_info = nullptr;
Expand All @@ -83,6 +84,7 @@ SocksEntry::init(Ptr<ProxyMutex> &m, SocksNetVC *vc, unsigned char socks_support
ats_ip_copy(&req_data.src_ip, &target_addr);

server_params = SocksServerConfig::acquire();
#endif

nattempts = 0;
findServer();
Expand All @@ -98,6 +100,7 @@ SocksEntry::findServer()
unsigned int fail_threshold = server_params->policy.FailThreshold;
unsigned int retry_time = server_params->policy.ParentRetryTime;

#ifdef SOCKS_WITH_TS
if (nattempts == 1) {
ink_assert(server_result.result == PARENT_UNDEFINED);
server_params->findParent(&req_data, &server_result, fail_threshold, retry_time);
Expand Down Expand Up @@ -135,6 +138,12 @@ SocksEntry::findServer()
case PARENT_FAIL:
memset(&server_addr, 0, sizeof(server_addr));
}
#else
if (nattempts > netProcessor.socks_conf_stuff->connection_attempts)
memset(&server_addr, 0, sizeof(server_addr));
else
ats_ip_copy(&server_addr, &g_socks_conf_stuff->server_addr);
#endif // SOCKS_WITH_TS

char buff[INET6_ADDRSTRLEN];
Dbg(dbg_ctl_SocksParent, "findServer result: %s:%d", ats_ip_ntop(&server_addr.sa, buff, sizeof(buff)),
Expand All @@ -153,9 +162,11 @@ SocksEntry::free()
timeout->cancel(this);
}

#ifdef SOCKS_WITH_TS
if (!lerrno && netVConnection && server_result.retry) {
server_params->markParentUp(&server_result);
}
#endif

if ((action_.cancelled || lerrno) && netVConnection) {
netVConnection->do_io_close();
Expand All @@ -176,7 +187,9 @@ SocksEntry::free()
action_.continuation->handleEvent(NET_EVENT_OPEN, netVConnection);
}
}
#ifdef SOCKS_WITH_TS
SocksServerConfig::release(server_params);
#endif

free_MIOBuffer(buf);
action_ = nullptr;
Expand Down Expand Up @@ -452,8 +465,10 @@ SocksEntry::mainEvent(int event, void *data)
void
loadSocksConfiguration(socks_conf_struct *socks_conf_stuff)
{
ats_scoped_str config_pathname;
swoc::Errata errata;
ats_scoped_str config_pathname;
#ifdef SOCKS_WITH_TS
swoc::Errata errata;
#endif
std::error_code ec;
std::string config_text;

Expand Down Expand Up @@ -488,7 +503,9 @@ loadSocksConfiguration(socks_conf_struct *socks_conf_stuff)
"accept_port = %d http_port = %d",
socks_conf_stuff->accept_enabled, socks_conf_stuff->accept_port, socks_conf_stuff->http_port);

#ifdef SOCKS_WITH_TS
SocksServerConfig::startup();
#endif

config_pathname = RecConfigReadConfigPath("proxy.config.socks.socks_config_file");
Dbg(dbg_ctl_Socks, "Socks Config File: %s", (const char *)config_pathname);
Expand All @@ -505,13 +522,15 @@ loadSocksConfiguration(socks_conf_struct *socks_conf_stuff)
goto error;
}

#ifdef SOCKS_WITH_TS
errata = loadSocksIPAddrs(config_text, socks_conf_stuff);

if (!errata.is_ok()) {
swoc::bwprint(config_text, "SOCK Config: Error\n{}", errata);
Error("%s", config_text.c_str());
goto error;
}
#endif

if (loadSocksAuthInfo(config_text, socks_conf_stuff) != 0) {
Error("SOCKS Config: Error while reading Socks auth info");
Expand Down
19 changes: 10 additions & 9 deletions src/iocore/net/UnixNetProcessor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,16 @@ UnixNetProcessor::connect_re(Continuation *cont, sockaddr const *target, NetVCOp
vc->options = opt;

vc->set_context(NET_VCONNECTION_OUT);

const bool using_socks = (socks_conf_stuff->socks_needed && opt.socks_support != NO_SOCKS &&
(opt.socks_version != SOCKS_DEFAULT_VERSION ||
/* This implies we are tunnelling.
* we need to connect using socks server even
* if this ip is in no_socks list.
*/
!socks_conf_stuff->ip_addrs.contains(swoc::IPAddr(target))));

bool using_socks = (socks_conf_stuff->socks_needed && opt.socks_support != NO_SOCKS
#ifdef SOCKS_WITH_TS
&& (opt.socks_version != SOCKS_DEFAULT_VERSION ||
/* This implies we are tunnelling.
* we need to connect using socks server even
* if this ip is in no_socks list.
*/
!socks_conf_stuff->ip_addrs.contains(swoc::IPAddr(target)))
#endif
);
SocksEntry *socksEntry = nullptr;

vc->id = net_next_connection_number();
Expand Down

0 comments on commit ae206db

Please sign in to comment.