mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-03-22 00:59:08 +01:00
[socks] try fix SOCKS5 upstream connection
Signed-off-by: R4SAS <r4sas@i2pmail.org>
This commit is contained in:
parent
18cb3912e5
commit
cb54a50dc5
1 changed files with 96 additions and 59 deletions
|
@ -30,6 +30,8 @@ namespace proxy
|
||||||
static const size_t SOCKS_FORWARDER_BUFFER_SIZE = 8192;
|
static const size_t SOCKS_FORWARDER_BUFFER_SIZE = 8192;
|
||||||
|
|
||||||
static const size_t SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE = 8;
|
static const size_t SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE = 8;
|
||||||
|
static const size_t SOCKS_UPSTREAM_SOCKS5_INIT_REPLY_SIZE = 2;
|
||||||
|
static const size_t SOCKS_UPSTREAM_SOCKS5_CONNECT_REPLY_MIN_SIZE = 10;
|
||||||
|
|
||||||
struct SOCKSDnsAddress
|
struct SOCKSDnsAddress
|
||||||
{
|
{
|
||||||
|
@ -127,7 +129,7 @@ namespace proxy
|
||||||
boost::asio::const_buffers_1 GenerateSOCKS5SelectAuth(authMethods method);
|
boost::asio::const_buffers_1 GenerateSOCKS5SelectAuth(authMethods method);
|
||||||
boost::asio::const_buffers_1 GenerateSOCKS4Response(errTypes error, uint32_t ip, uint16_t port);
|
boost::asio::const_buffers_1 GenerateSOCKS4Response(errTypes error, uint32_t ip, uint16_t port);
|
||||||
boost::asio::const_buffers_1 GenerateSOCKS5Response(errTypes error, addrTypes type, const address &addr, uint16_t port);
|
boost::asio::const_buffers_1 GenerateSOCKS5Response(errTypes error, addrTypes type, const address &addr, uint16_t port);
|
||||||
boost::asio::const_buffers_1 GenerateUpstreamRequest();
|
boost::asio::const_buffers_1 GenerateUpstreamRequest(int version, bool initial);
|
||||||
bool Socks5ChooseAuth();
|
bool Socks5ChooseAuth();
|
||||||
void SocksRequestFailed(errTypes error);
|
void SocksRequestFailed(errTypes error);
|
||||||
void SocksRequestSuccess();
|
void SocksRequestSuccess();
|
||||||
|
@ -139,7 +141,7 @@ namespace proxy
|
||||||
|
|
||||||
void SocksUpstreamSuccess();
|
void SocksUpstreamSuccess();
|
||||||
void AsyncUpstreamSockRead();
|
void AsyncUpstreamSockRead();
|
||||||
void SendUpstreamRequest();
|
void SendUpstreamRequest(int version, bool initial);
|
||||||
void HandleUpstreamData(uint8_t * buff, std::size_t len);
|
void HandleUpstreamData(uint8_t * buff, std::size_t len);
|
||||||
void HandleUpstreamSockSend(const boost::system::error_code & ecode, std::size_t bytes_transfered);
|
void HandleUpstreamSockSend(const boost::system::error_code & ecode, std::size_t bytes_transfered);
|
||||||
void HandleUpstreamSockRecv(const boost::system::error_code & ecode, std::size_t bytes_transfered);
|
void HandleUpstreamSockRecv(const boost::system::error_code & ecode, std::size_t bytes_transfered);
|
||||||
|
@ -274,12 +276,27 @@ namespace proxy
|
||||||
return boost::asio::const_buffers_1(m_response, size);
|
return boost::asio::const_buffers_1(m_response, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
boost::asio::const_buffers_1 SOCKSHandler::GenerateUpstreamRequest()
|
boost::asio::const_buffers_1 SOCKSHandler::GenerateUpstreamRequest(int version, bool initial)
|
||||||
{
|
{
|
||||||
size_t upstreamRequestSize = 0;
|
size_t upstreamRequestSize = 0;
|
||||||
// TODO: negotiate with upstream
|
// TODO: negotiate with upstream
|
||||||
// SOCKS 4a
|
if (version == 5) // SOCKS5
|
||||||
m_upstream_request[0] = '\x04'; //version
|
{
|
||||||
|
if (initial)
|
||||||
|
{
|
||||||
|
m_upstream_request[0] = '\x05'; // VER
|
||||||
|
m_upstream_request[1] = 1; // NAUTH
|
||||||
|
m_upstream_request[2] = 0; // AUTH
|
||||||
|
upstreamRequestSize += 3;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return GenerateSOCKS5Response(SOCKS5_GEN_FAIL, m_addrtype, m_address, m_port); // dirty hack, SOCKS5_GEN_FAIL == 1, that's a "establish a TCP/IP stream connection" command
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (version == 4) // SOCKS4a
|
||||||
|
{
|
||||||
|
m_upstream_request[0] = '\x04'; // VER
|
||||||
m_upstream_request[1] = m_cmd;
|
m_upstream_request[1] = m_cmd;
|
||||||
htobe16buf(m_upstream_request + 2, m_port);
|
htobe16buf(m_upstream_request + 2, m_port);
|
||||||
m_upstream_request[4] = 0;
|
m_upstream_request[4] = 0;
|
||||||
|
@ -300,7 +317,8 @@ namespace proxy
|
||||||
// null terminate
|
// null terminate
|
||||||
m_upstream_request[++upstreamRequestSize] = 0;
|
m_upstream_request[++upstreamRequestSize] = 0;
|
||||||
} else {
|
} else {
|
||||||
LogPrint(eLogError, "SOCKS: BUG!!! m_addr.dns.sizs > max_socks_hostname - ( upstreamRequestSize + 1 ) )");
|
LogPrint(eLogError, "SOCKS: BUG!!! m_addr.dns.size > max_socks_hostname - ( upstreamRequestSize + 1 ) )");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return boost::asio::const_buffers_1(m_upstream_request, upstreamRequestSize);
|
return boost::asio::const_buffers_1(m_upstream_request, upstreamRequestSize);
|
||||||
}
|
}
|
||||||
|
@ -417,6 +435,7 @@ namespace proxy
|
||||||
{
|
{
|
||||||
case GET_SOCKSV:
|
case GET_SOCKSV:
|
||||||
m_socksv = (SOCKSHandler::socksVersions) *sock_buff;
|
m_socksv = (SOCKSHandler::socksVersions) *sock_buff;
|
||||||
|
LogPrint(eLogInfo, "SOCKS: received version: ", ((int)*sock_buff));
|
||||||
switch (*sock_buff)
|
switch (*sock_buff)
|
||||||
{
|
{
|
||||||
case SOCKS4:
|
case SOCKS4:
|
||||||
|
@ -715,7 +734,6 @@ namespace proxy
|
||||||
GetOwner()->AddHandler(forwarder);
|
GetOwner()->AddHandler(forwarder);
|
||||||
forwarder->Start();
|
forwarder->Start();
|
||||||
Terminate();
|
Terminate();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SOCKSHandler::HandleUpstreamData(uint8_t * dataptr, std::size_t len)
|
void SOCKSHandler::HandleUpstreamData(uint8_t * dataptr, std::size_t len)
|
||||||
|
@ -723,7 +741,26 @@ namespace proxy
|
||||||
if (m_state == UPSTREAM_HANDSHAKE) {
|
if (m_state == UPSTREAM_HANDSHAKE) {
|
||||||
m_upstream_response_len += len;
|
m_upstream_response_len += len;
|
||||||
// handle handshake data
|
// handle handshake data
|
||||||
if (m_upstream_response_len < SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE) {
|
if (m_upstream_response_len == SOCKS_UPSTREAM_SOCKS5_INIT_REPLY_SIZE) {
|
||||||
|
if (m_upstream_response[0] == '\x05' && m_upstream_response[1] != AUTH_UNACCEPTABLE) {
|
||||||
|
LogPrint(eLogInfo, "SOCKS: upstream proxy: success greeting");
|
||||||
|
SendUpstreamRequest(m_socksv, false);
|
||||||
|
} else {
|
||||||
|
LogPrint(eLogError, "SOCKS: upstream proxy failure: no acceptable methods were offered");
|
||||||
|
SocksRequestFailed(SOCKS5_GEN_FAIL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (m_upstream_response[0] == '\x05' && m_upstream_response_len >= SOCKS_UPSTREAM_SOCKS5_CONNECT_REPLY_MIN_SIZE)
|
||||||
|
{
|
||||||
|
uint8_t resp = m_upstream_response[1];
|
||||||
|
if (resp == SOCKS5_OK) {
|
||||||
|
SocksUpstreamSuccess();
|
||||||
|
} else {
|
||||||
|
LogPrint(eLogError, "SOCKS: upstream proxy failure: ", (int) resp);
|
||||||
|
SocksRequestFailed(SOCKS5_GEN_FAIL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (m_upstream_response_len < SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE) {
|
||||||
// too small, continue reading
|
// too small, continue reading
|
||||||
AsyncUpstreamSockRead();
|
AsyncUpstreamSockRead();
|
||||||
} else if (len == SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE) {
|
} else if (len == SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE) {
|
||||||
|
@ -748,12 +785,12 @@ namespace proxy
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SOCKSHandler::SendUpstreamRequest()
|
void SOCKSHandler::SendUpstreamRequest(int version, bool initial)
|
||||||
{
|
{
|
||||||
LogPrint(eLogInfo, "SOCKS: Negotiating with upstream proxy");
|
LogPrint(eLogInfo, "SOCKS: Negotiating with upstream proxy");
|
||||||
EnterState(UPSTREAM_HANDSHAKE);
|
EnterState(UPSTREAM_HANDSHAKE);
|
||||||
if (m_upstreamSock) {
|
if (m_upstreamSock) {
|
||||||
boost::asio::write(*m_upstreamSock, GenerateUpstreamRequest());
|
boost::asio::write(*m_upstreamSock, GenerateUpstreamRequest(version, initial));
|
||||||
AsyncUpstreamSockRead();
|
AsyncUpstreamSockRead();
|
||||||
} else {
|
} else {
|
||||||
LogPrint(eLogError, "SOCKS: No upstream socket to send handshake to");
|
LogPrint(eLogError, "SOCKS: No upstream socket to send handshake to");
|
||||||
|
@ -768,7 +805,7 @@ namespace proxy
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
LogPrint(eLogInfo, "SOCKS: Connected to upstream proxy");
|
LogPrint(eLogInfo, "SOCKS: Connected to upstream proxy");
|
||||||
SendUpstreamRequest();
|
SendUpstreamRequest(m_socksv, true); // try SOCKS5 first
|
||||||
}
|
}
|
||||||
|
|
||||||
void SOCKSHandler::HandleUpstreamResolved(const boost::system::error_code & ecode, boost::asio::ip::tcp::resolver::iterator itr)
|
void SOCKSHandler::HandleUpstreamResolved(const boost::system::error_code & ecode, boost::asio::ip::tcp::resolver::iterator itr)
|
||||||
|
|
Loading…
Add table
Reference in a new issue