Skip to content

Commit

Permalink
config disableSocks4 , mini class create
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyoko-Jeremie committed Sep 2, 2023
1 parent 9223f8e commit 9f09734
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
1 change: 1 addition & 0 deletions example-config/FullConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"disableConnectTest": false,
"disableConnectionTracker": false,
"traditionTcpRelay": false,
"disableSocks4": true,
"multiListen": [
{
"host": "127.0.0.1",
Expand Down
5 changes: 5 additions & 0 deletions src/ConfigLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ void ConfigLoader::print() {
ss << "config.disableConnectionTracker:" << config.disableConnectionTracker << "\n";
ss << "config.traditionTcpRelay:" << config.traditionTcpRelay << "\n";

ss << "config.disableSocks4:" << config.disableSocks4 << "\n";

ss << "config.serverChangeTime:" << config.serverChangeTime.count() << "\n";

ss << "config.connectTimeout:" << config.connectTimeout.count() << "\n";
Expand Down Expand Up @@ -211,6 +213,9 @@ void ConfigLoader::parse_json(const boost::property_tree::ptree &tree) {
c.traditionTcpRelay = true;
#endif // FORCE_traditionTcpRelay

auto disableSocks4 = tree.get("disableSocks4", true);
c.disableSocks4 = disableSocks4;

auto serverChangeTime = tree.get("serverChangeTime", static_cast<long long>(60 * 1000));
c.serverChangeTime = ConfigTimeDuration{serverChangeTime};

Expand Down
2 changes: 2 additions & 0 deletions src/ConfigLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ struct Config {
bool disableConnectionTracker;
bool traditionTcpRelay;

bool disableSocks4;

EmbedWebServerConfig embedWebServerConfig;

ConfigTimeDuration connectTimeout;
Expand Down
54 changes: 48 additions & 6 deletions src/ProxyHandshakeAuth.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,24 @@ void ProxyHandshakeAuth::do_read_client_first_3_byte() {
connectType = ConnectType::socks5;
// do socks5 handshake with client (downside)
upsideConnectType = UpsideConnectType::socks5;
BOOST_ASSERT(!util_Socks5ServerImpl_);
util_Socks5ServerImpl_ = std::make_shared<decltype(util_Socks5ServerImpl_)::element_type>(
shared_from_this()
);
BOOST_ASSERT(util_Socks5ServerImpl_);
util_Socks5ServerImpl_->do_analysis_client_first_socks5_header();
} else if (d[0] == 0x04 &&
(d[1] == 0x02 || d[1] == 0x01)) {
if (configLoader->config.disableSocks4) {
BOOST_LOG_S5B_ID(relayId, warning)
<< "ProxyHandshakeAuth::do_read_client_first_3_byte()"
<< " seems like a socks4/socks4a income, but socks4 is disabled. denny!";
fail(error,
std::string{"ProxyHandshakeAuth::do_read_client_first_3_byte()"} +
std::string{" seems like a socks4/socks4a income, but socks4 is disabled. denny!"}
);
return;
}
auto data = downstream_buf_.data();
size_t len = data.size();
auto dd = reinterpret_cast<const unsigned char *>(data.data());
Expand All @@ -69,7 +84,7 @@ void ProxyHandshakeAuth::do_read_client_first_3_byte() {
BOOST_LOG_S5B_ID(relayId, error)
<< "ProxyHandshakeAuth::do_read_client_first_3_byte()"
<< " (!pLenOk || !pEndWithNull)";
BOOST_LOG_S5B_ID(relayId, error)
BOOST_LOG_S5B_ID(relayId, trace)
<< "ProxyHandshakeAuth::do_read_client_first_3_byte()"
<< " pLenOk:" << pLenOk
<< " pEndWithNull:" << pEndWithNull
Expand All @@ -88,6 +103,11 @@ void ProxyHandshakeAuth::do_read_client_first_3_byte() {
// do socks5 handshake with server (upside)
upsideConnectType = UpsideConnectType::socks5;
// do socks4 handshake with client (downside)
BOOST_ASSERT(!util_Socks4ServerImpl_);
util_Socks4ServerImpl_ = std::make_shared<decltype(util_Socks4ServerImpl_)::element_type>(
shared_from_this()
);
BOOST_ASSERT(util_Socks4ServerImpl_);
util_Socks4ServerImpl_->do_analysis_client_first_socks4_header();
} else if ((d[0] == 'C' || d[0] == 'c') &&
(d[1] == 'O' || d[1] == 'o') &&
Expand All @@ -98,6 +118,11 @@ void ProxyHandshakeAuth::do_read_client_first_3_byte() {
upsideConnectType = UpsideConnectType::socks5;
// do http handshake with client (downside)
connectType = ConnectType::httpConnect;
BOOST_ASSERT(!util_HttpServerImpl_);
util_HttpServerImpl_ = std::make_shared<decltype(util_HttpServerImpl_)::element_type>(
shared_from_this()
);
BOOST_ASSERT(util_HttpServerImpl_);
util_HttpServerImpl_->do_analysis_client_first_http_header();
} else {
// is other protocol
Expand Down Expand Up @@ -150,6 +175,11 @@ void ProxyHandshakeAuth::do_read_client_first_3_byte() {
upsideConnectType = UpsideConnectType::socks5;
// do http handshake with client (downside)
//connectType = ConnectType::httpConnect;
BOOST_ASSERT(!util_HttpServerImpl_);
util_HttpServerImpl_ = std::make_shared<decltype(util_HttpServerImpl_)::element_type>(
shared_from_this()
);
BOOST_ASSERT(util_HttpServerImpl_);
util_HttpServerImpl_->do_analysis_client_first_http_header();
}

Expand All @@ -169,11 +199,11 @@ void ProxyHandshakeAuth::do_read_client_first_3_byte() {
}

void ProxyHandshakeAuth::start() {
util_HttpClientImpl_ = std::make_shared<decltype(util_HttpClientImpl_)::element_type>(shared_from_this());
util_HttpServerImpl_ = std::make_shared<decltype(util_HttpServerImpl_)::element_type>(shared_from_this());
util_Socks5ClientImpl_ = std::make_shared<decltype(util_Socks5ClientImpl_)::element_type>(shared_from_this());
util_Socks5ServerImpl_ = std::make_shared<decltype(util_Socks5ServerImpl_)::element_type>(shared_from_this());
util_Socks4ServerImpl_ = std::make_shared<decltype(util_Socks4ServerImpl_)::element_type>(shared_from_this());
// util_HttpClientImpl_ = std::make_shared<decltype(util_HttpClientImpl_)::element_type>(shared_from_this());
// util_HttpServerImpl_ = std::make_shared<decltype(util_HttpServerImpl_)::element_type>(shared_from_this());
// util_Socks5ClientImpl_ = std::make_shared<decltype(util_Socks5ClientImpl_)::element_type>(shared_from_this());
// util_Socks5ServerImpl_ = std::make_shared<decltype(util_Socks5ServerImpl_)::element_type>(shared_from_this());
// util_Socks4ServerImpl_ = std::make_shared<decltype(util_Socks4ServerImpl_)::element_type>(shared_from_this());

auto ptr = tcpRelaySession;
if (ptr) {
Expand All @@ -195,13 +225,16 @@ void ProxyHandshakeAuth::do_whenUpReady() {
// do send last ok package to client in downside
switch (connectType) {
case ConnectType::socks5:
BOOST_ASSERT(util_Socks5ServerImpl_);
util_Socks5ServerImpl_->to_send_last_ok_package();
break;
case ConnectType::socks4:
BOOST_ASSERT(util_Socks4ServerImpl_);
util_Socks4ServerImpl_->to_send_last_ok_package();
break;
case ConnectType::httpConnect:
case ConnectType::httpOther:
BOOST_ASSERT(util_HttpServerImpl_);
util_HttpServerImpl_->to_send_last_ok_package();
break;
case ConnectType::unknown:
Expand All @@ -226,13 +259,16 @@ void ProxyHandshakeAuth::do_whenUpReadyError() {
// do send last error package to client in downside
switch (connectType) {
case ConnectType::socks5:
BOOST_ASSERT(util_Socks5ServerImpl_);
util_Socks5ServerImpl_->to_send_last_error_package();
break;
case ConnectType::socks4:
BOOST_ASSERT(util_Socks4ServerImpl_);
util_Socks4ServerImpl_->to_send_last_error_package();
break;
case ConnectType::httpConnect:
case ConnectType::httpOther:
BOOST_ASSERT(util_HttpServerImpl_);
util_HttpServerImpl_->to_send_last_error_package();
break;
case ConnectType::unknown:
Expand Down Expand Up @@ -261,6 +297,9 @@ void ProxyHandshakeAuth::do_whenDownReady() {
readyDown = true;
// start upside handshake
BOOST_LOG_S5B_ID(relayId, trace) << "ProxyHandshakeAuth::do_whenDownReady() start upside handshake";
BOOST_ASSERT(!util_Socks5ClientImpl_);
util_Socks5ClientImpl_ = std::make_shared<decltype(util_Socks5ClientImpl_)::element_type>(shared_from_this());
BOOST_ASSERT(util_Socks5ClientImpl_);
util_Socks5ClientImpl_->start();
// util_HttpClientImpl_->start();
}
Expand Down Expand Up @@ -338,6 +377,7 @@ bool ProxyHandshakeAuth::upside_support_udp_mode() {
bool ProxyHandshakeAuth::upside_in_udp_mode() {
switch (upsideConnectType) {
case UpsideConnectType::socks5:
BOOST_ASSERT(util_Socks5ClientImpl_);
return util_Socks5ClientImpl_->udpEnabled;
break;
case UpsideConnectType::socks4:
Expand All @@ -356,9 +396,11 @@ bool ProxyHandshakeAuth::upside_in_udp_mode() {
bool ProxyHandshakeAuth::downside_in_udp_mode() {
switch (connectType) {
case ConnectType::socks5:
BOOST_ASSERT(util_Socks5ServerImpl_);
return util_Socks5ServerImpl_->udpEnabled;
break;
case ConnectType::socks4:
BOOST_ASSERT(util_Socks4ServerImpl_);
return util_Socks4ServerImpl_->udpEnabled;
break;
case ConnectType::httpConnect:
Expand Down
2 changes: 1 addition & 1 deletion src/ProxyHandshakeAuth.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class ProxyHandshakeAuth : public std::enable_shared_from_this<ProxyHandshakeAut
uint16_t port{0};

private:
std::shared_ptr<HttpClientImpl> util_HttpClientImpl_;
// std::shared_ptr<HttpClientImpl> util_HttpClientImpl_;
std::shared_ptr<HttpServerImpl> util_HttpServerImpl_;
std::shared_ptr<Socks5ClientImpl> util_Socks5ClientImpl_;
std::shared_ptr<Socks5ServerImpl> util_Socks5ServerImpl_;
Expand Down

0 comments on commit 9f09734

Please sign in to comment.