i2pd/SOCKS.cpp

450 lines
13 KiB
C++
Raw Normal View History

2015-01-07 00:40:45 +03:00
#include <cstring>
#include <cassert>
2014-07-14 20:40:06 +04:00
#include "SOCKS.h"
#include "Identity.h"
#include "Destination.h"
2014-10-16 04:52:17 +04:00
#include "ClientContext.h"
#include "I2PEndian.h"
2014-07-14 20:40:06 +04:00
namespace i2p
{
namespace proxy
{
2015-01-03 23:57:15 +03:00
void SOCKSHandler::AsyncSockRead()
2014-07-14 20:40:06 +04:00
{
LogPrint(eLogDebug,"--- SOCKS async sock read");
2014-07-14 20:40:06 +04:00
if(m_sock) {
m_sock->async_receive(boost::asio::buffer(m_sock_buff, socks_buffer_size),
2015-01-03 23:57:15 +03:00
std::bind(&SOCKSHandler::HandleSockRecv, this,
std::placeholders::_1, std::placeholders::_2));
2014-07-14 20:40:06 +04:00
} else {
LogPrint(eLogError,"--- SOCKS no socket for read");
2014-07-14 20:40:06 +04:00
}
}
void SOCKSHandler::Done() {
if (m_parent) m_parent->RemoveHandler (shared_from_this ());
2014-07-14 20:40:06 +04:00
}
void SOCKSHandler::Terminate() {
if (dead.exchange(true)) return;
if (m_sock) {
LogPrint(eLogDebug,"--- SOCKS close sock");
m_sock->close();
delete m_sock;
m_sock = nullptr;
}
if (m_stream) {
LogPrint(eLogDebug,"--- SOCKS close stream");
m_stream.reset ();
}
Done();
2015-01-05 14:37:52 +03:00
}
boost::asio::const_buffers_1 SOCKSHandler::GenerateSOCKS4Response(SOCKSHandler::errTypes error, uint32_t ip, uint16_t port)
{
assert(error >= SOCKS4_OK);
m_response[0] = '\x00'; //Version
m_response[1] = error; //Response code
htobe16buf(m_response+2,port); //Port
htobe32buf(m_response+4,ip); //IP
return boost::asio::const_buffers_1(m_response,8);
2015-01-05 14:37:52 +03:00
}
boost::asio::const_buffers_1 SOCKSHandler::GenerateSOCKS5Response(SOCKSHandler::errTypes error, SOCKSHandler::addrTypes type,
const SOCKSHandler::address &addr, uint16_t port)
{
size_t size;
assert(error <= SOCKS5_ADDR_UNSUP);
m_response[0] = '\x05'; //Version
m_response[1] = error; //Response code
m_response[2] = '\x00'; //RSV
m_response[3] = type; //Address type
2015-01-05 14:37:52 +03:00
switch (type) {
case ADDR_IPV4:
size = 10;
htobe32buf(m_response+4,addr.ip);
2015-01-05 14:37:52 +03:00
break;
case ADDR_IPV6:
size = 22;
memcpy(m_response+4,addr.ipv6, 16);
2015-01-05 14:37:52 +03:00
break;
case ADDR_DNS:
size = 7+addr.dns.size;
m_response[4] = addr.dns.size;
memcpy(m_response+5,addr.dns.value, addr.dns.size);
2015-01-05 14:37:52 +03:00
break;
}
htobe16buf(m_response+size-2,port); //Port
return boost::asio::const_buffers_1(m_response,size);
2015-01-05 14:37:52 +03:00
}
bool SOCKSHandler::Socks5ChooseAuth()
2014-07-14 20:40:06 +04:00
{
m_response[0] = '\x05'; //Version
m_response[1] = m_authchosen; //Response code
boost::asio::const_buffers_1 response(m_response,2);
if (m_authchosen == AUTH_UNACCEPTABLE) {
LogPrint(eLogWarning,"--- SOCKS5 authentication negotiation failed");
boost::asio::async_write(*m_sock, response, std::bind(&SOCKSHandler::SentSocksFailed, this, std::placeholders::_1));
return false;
} else {
LogPrint(eLogDebug,"--- SOCKS5 choosing authentication method: ", m_authchosen);
boost::asio::async_write(*m_sock, response, std::bind(&SOCKSHandler::SentSocksResponse, this, std::placeholders::_1));
return true;
}
2015-01-03 23:57:15 +03:00
}
2015-01-05 14:37:52 +03:00
/* All hope is lost beyond this point */
void SOCKSHandler::SocksRequestFailed(SOCKSHandler::errTypes error)
2015-01-03 23:57:15 +03:00
{
2015-01-05 14:37:52 +03:00
boost::asio::const_buffers_1 response(nullptr,0);
assert(error != SOCKS4_OK && error != SOCKS5_OK);
2015-01-03 23:57:15 +03:00
switch (m_socksv) {
case SOCKS4:
2015-01-05 14:37:52 +03:00
LogPrint(eLogWarning,"--- SOCKS4 failed: ", error);
if (error < SOCKS4_OK) error = SOCKS4_FAIL; //Transparently map SOCKS5 errors
response = GenerateSOCKS4Response(error, m_4aip, m_port);
2015-01-03 23:57:15 +03:00
break;
case SOCKS5:
2015-01-05 14:37:52 +03:00
LogPrint(eLogWarning,"--- SOCKS5 failed: ", error);
response = GenerateSOCKS5Response(error, m_addrtype, m_address, m_port);
2015-01-03 23:57:15 +03:00
break;
}
2015-01-05 14:37:52 +03:00
boost::asio::async_write(*m_sock, response, std::bind(&SOCKSHandler::SentSocksFailed, this, std::placeholders::_1));
2015-01-03 23:57:15 +03:00
}
void SOCKSHandler::SocksRequestSuccess()
{
2015-01-05 14:37:52 +03:00
boost::asio::const_buffers_1 response(nullptr,0);
//TODO: this should depend on things like the command type and callbacks may change
2015-01-03 23:57:15 +03:00
switch (m_socksv) {
case SOCKS4:
2015-01-03 23:57:15 +03:00
LogPrint(eLogInfo,"--- SOCKS4 connection success");
2015-01-05 14:37:52 +03:00
response = GenerateSOCKS4Response(SOCKS4_OK, m_4aip, m_port);
2015-01-03 23:57:15 +03:00
break;
case SOCKS5:
2015-01-03 23:57:15 +03:00
LogPrint(eLogInfo,"--- SOCKS5 connection success");
2015-01-05 14:37:52 +03:00
auto s = i2p::client::context.GetAddressBook().ToAddress(m_parent->GetLocalDestination()->GetIdentHash());
address ad; ad.dns.FromString(s);
//HACK only 16 bits passed in port as SOCKS5 doesn't allow for more
response = GenerateSOCKS5Response(SOCKS5_OK, ADDR_DNS, ad, m_stream->GetRecvStreamID());
break;
}
boost::asio::async_write(*m_sock, response, std::bind(&SOCKSHandler::SentSocksDone, this, std::placeholders::_1));
}
2014-07-14 20:40:06 +04:00
void SOCKSHandler::EnterState(SOCKSHandler::state nstate, uint8_t parseleft) {
switch (nstate) {
case GET_PORT: parseleft = 2; break;
case GET_IPV4: m_addrtype = ADDR_IPV4; m_address.ip = 0; parseleft = 4; break;
case GET4_IDENT: m_4aip = m_address.ip; break;
case GET4A_HOST:
case GET5_HOST: m_addrtype = ADDR_DNS; m_address.dns.size = 0; break;
case GET5_IPV6: m_addrtype = ADDR_IPV6; parseleft = 16; break;
default:;
2015-01-03 23:57:15 +03:00
}
m_parseleft = parseleft;
m_state = nstate;
2015-01-03 23:57:15 +03:00
}
bool SOCKSHandler::ValidateSOCKSRequest() {
if ( m_cmd != CMD_CONNECT ) {
//TODO: we need to support binds and other shit!
LogPrint(eLogError,"--- SOCKS unsupported command: ", m_cmd);
2015-01-05 14:37:52 +03:00
SocksRequestFailed(SOCKS5_CMD_UNSUP);
return false;
}
//TODO: we may want to support other address types!
if ( m_addrtype != ADDR_DNS ) {
switch (m_socksv) {
case SOCKS5:
LogPrint(eLogError,"--- SOCKS5 unsupported address type: ", m_addrtype);
break;
case SOCKS4:
LogPrint(eLogError,"--- SOCKS4a rejected because it's actually SOCKS4");
break;
}
2015-01-05 14:37:52 +03:00
SocksRequestFailed(SOCKS5_ADDR_UNSUP);
return false;
}
//TODO: we may want to support other domains
2015-01-05 14:37:52 +03:00
if(m_addrtype == ADDR_DNS && m_address.dns.ToString().find(".i2p") == std::string::npos) {
LogPrint(eLogError,"--- SOCKS invalid hostname: ", m_address.dns.ToString());
SocksRequestFailed(SOCKS5_ADDR_UNSUP);
return false;
}
return true;
}
2015-01-05 14:37:52 +03:00
bool SOCKSHandler::HandleData(uint8_t *sock_buff, std::size_t len)
2015-01-03 23:57:15 +03:00
{
assert(len); // This should always be called with a least a byte left to parse
2015-01-03 23:57:15 +03:00
while (len > 0) {
switch (m_state) {
case GET_SOCKSV:
m_socksv = (SOCKSHandler::socksVersions) *sock_buff;
switch (*sock_buff) {
case SOCKS4:
EnterState(GET_COMMAND); //Initialize the parser at the right position
break;
case SOCKS5:
EnterState(GET5_AUTHNUM); //Initialize the parser at the right position
break;
default:
LogPrint(eLogError,"--- SOCKS rejected invalid version: ", ((int)*sock_buff));
Terminate();
return false;
}
break;
case GET5_AUTHNUM:
EnterState(GET5_AUTH, *sock_buff);
break;
case GET5_AUTH:
m_parseleft --;
if (*sock_buff == AUTH_NONE)
m_authchosen = AUTH_NONE;
if ( m_parseleft == 0 ) {
if (!Socks5ChooseAuth()) return false;
EnterState(GET5_REQUESTV);
}
break;
case GET_COMMAND:
switch (*sock_buff) {
case CMD_CONNECT:
case CMD_BIND:
break;
case CMD_UDP:
if (m_socksv == SOCKS5) break;
default:
LogPrint(eLogError,"--- SOCKS invalid command: ", ((int)*sock_buff));
2015-01-05 14:37:52 +03:00
SocksRequestFailed(SOCKS5_GEN_FAIL);
return false;
}
m_cmd = (SOCKSHandler::cmdTypes)*sock_buff;
switch (m_socksv) {
case SOCKS5: EnterState(GET5_GETRSV); break;
case SOCKS4: EnterState(GET_PORT); break;
}
break;
case GET_PORT:
m_port = (m_port << 8)|((uint16_t)*sock_buff);
2015-01-05 14:45:10 +03:00
m_parseleft--;
if (m_parseleft == 0) {
switch (m_socksv) {
case SOCKS5: EnterState(DONE); break;
case SOCKS4: EnterState(GET_IPV4); break;
}
}
break;
case GET_IPV4:
2015-01-05 14:37:52 +03:00
m_address.ip = (m_address.ip << 8)|((uint32_t)*sock_buff);
2015-01-05 14:45:10 +03:00
m_parseleft--;
if (m_parseleft == 0) {
switch (m_socksv) {
case SOCKS5: EnterState(GET_PORT); break;
case SOCKS4: EnterState(GET4_IDENT); m_4aip = m_address.ip; break;
}
}
break;
case GET4_IDENT:
if (!*sock_buff) {
2015-01-05 14:37:52 +03:00
if( m_4aip == 0 || m_4aip > 255 ) {
EnterState(DONE);
} else {
EnterState(GET4A_HOST);
}
}
break;
case GET4A_HOST:
if (!*sock_buff) {
EnterState(DONE);
break;
}
2015-01-05 14:37:52 +03:00
if (m_address.dns.size >= max_socks_hostname_size) {
LogPrint(eLogError,"--- SOCKS4a destination is too large");
2015-01-05 14:37:52 +03:00
SocksRequestFailed(SOCKS4_FAIL);
return false;
2015-01-03 23:57:15 +03:00
}
2015-01-05 14:37:52 +03:00
m_address.dns.push_back(*sock_buff);
2015-01-03 23:57:15 +03:00
break;
case GET5_REQUESTV:
if (*sock_buff != SOCKS5) {
LogPrint(eLogError,"--- SOCKS5 rejected unknown request version: ", ((int)*sock_buff));
2015-01-05 14:37:52 +03:00
SocksRequestFailed(SOCKS5_GEN_FAIL);
return false;
2015-01-03 23:57:15 +03:00
}
EnterState(GET_COMMAND);
2015-01-03 23:57:15 +03:00
break;
case GET5_GETRSV:
if ( *sock_buff != 0 ) {
LogPrint(eLogError,"--- SOCKS5 unknown reserved field: ", ((int)*sock_buff));
2015-01-05 14:37:52 +03:00
SocksRequestFailed(SOCKS5_GEN_FAIL);
return false;
2015-01-03 23:57:15 +03:00
}
EnterState(GET5_GETADDRTYPE);
2015-01-03 23:57:15 +03:00
break;
case GET5_GETADDRTYPE:
switch (*sock_buff) {
case ADDR_IPV4: EnterState(GET_IPV4); break;
case ADDR_IPV6: EnterState(GET5_IPV6); break;
case ADDR_DNS : EnterState(GET5_HOST_SIZE); break;
default:
LogPrint(eLogError,"--- SOCKS5 unknown address type: ", ((int)*sock_buff));
2015-01-05 14:37:52 +03:00
SocksRequestFailed(SOCKS5_GEN_FAIL);
return false;
}
break;
case GET5_IPV6:
2015-01-05 14:45:10 +03:00
m_address.ipv6[16-m_parseleft] = *sock_buff;
m_parseleft--;
if (m_parseleft == 0) EnterState(GET_PORT);
2015-01-03 23:57:15 +03:00
break;
case GET5_HOST_SIZE:
EnterState(GET5_HOST, *sock_buff);
2015-01-03 23:57:15 +03:00
break;
case GET5_HOST:
2015-01-05 14:37:52 +03:00
m_address.dns.push_back(*sock_buff);
2015-01-05 14:45:10 +03:00
m_parseleft--;
if (m_parseleft == 0) EnterState(GET_PORT);
2015-01-03 23:57:15 +03:00
break;
default:
LogPrint(eLogError,"--- SOCKS parse state?? ", m_state);
2015-01-03 23:57:15 +03:00
Terminate();
return false;
}
2015-01-03 23:57:15 +03:00
sock_buff++;
len--;
if (m_state == DONE) {
m_remaining_data_len = len;
m_remaining_data = sock_buff;
return ValidateSOCKSRequest();
}
2015-01-03 23:57:15 +03:00
}
return true;
2015-01-03 23:57:15 +03:00
}
void SOCKSHandler::HandleSockRecv(const boost::system::error_code & ecode, std::size_t len)
{
LogPrint(eLogDebug,"--- SOCKS sock recv: ", len);
if(ecode) {
LogPrint(eLogWarning," --- SOCKS sock recv got error: ", ecode);
Terminate();
return;
}
2014-07-14 20:40:06 +04:00
if (HandleData(m_sock_buff, len)) {
if (m_state == DONE) {
LogPrint(eLogInfo,"--- SOCKS requested ", m_address.dns.ToString(), ":" , m_port);
m_parent->GetLocalDestination ()->CreateStream (
std::bind (&SOCKSHandler::HandleStreamRequestComplete,
this, std::placeholders::_1), m_address.dns.ToString(), m_port);
} else {
AsyncSockRead();
}
}
2014-07-14 20:40:06 +04:00
}
2015-01-03 23:57:15 +03:00
void SOCKSHandler::SentSocksFailed(const boost::system::error_code & ecode)
2014-07-14 20:40:06 +04:00
{
if (!ecode) {
Terminate();
2015-01-03 23:57:15 +03:00
} else {
LogPrint (eLogError,"--- SOCKS Closing socket after sending failure because: ", ecode.message ());
2014-07-14 20:40:06 +04:00
Terminate();
}
}
void SOCKSHandler::SentSocksDone(const boost::system::error_code & ecode)
2014-07-14 20:40:06 +04:00
{
if (!ecode) {
if (dead.exchange(true)) return;
LogPrint (eLogInfo,"--- SOCKS New I2PTunnel connection");
auto connection = std::make_shared<i2p::client::I2PTunnelConnection>((i2p::client::I2PTunnel *)m_parent, m_sock, m_stream);
m_parent->AddConnection (connection);
connection->I2PConnect (m_remaining_data,m_remaining_data_len);
Done();
}
else
{
LogPrint (eLogError,"--- SOCKS Closing socket after completion reply because: ", ecode.message ());
Terminate();
}
}
void SOCKSHandler::SentSocksResponse(const boost::system::error_code & ecode)
{
if (ecode) {
2015-01-03 23:57:15 +03:00
LogPrint (eLogError,"--- SOCKS Closing socket after sending reply because: ", ecode.message ());
Terminate();
}
2014-07-14 20:40:06 +04:00
}
2015-01-03 23:57:15 +03:00
void SOCKSHandler::HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream)
2014-07-14 20:40:06 +04:00
{
2015-01-03 23:57:15 +03:00
if (stream) {
m_stream = stream;
2015-01-03 23:57:15 +03:00
SocksRequestSuccess();
} else {
LogPrint (eLogError,"--- SOCKS Issue when creating the stream, check the previous warnings for more info.");
2015-01-05 14:37:52 +03:00
SocksRequestFailed(SOCKS5_HOST_UNREACH);
2014-07-14 20:40:06 +04:00
}
}
2015-01-03 23:57:15 +03:00
void SOCKSServer::Start ()
2014-07-14 20:40:06 +04:00
{
m_Acceptor.listen ();
Accept ();
2014-07-14 20:40:06 +04:00
}
2015-01-03 23:57:15 +03:00
void SOCKSServer::Stop ()
2014-07-14 20:40:06 +04:00
{
m_Acceptor.close();
m_Timer.cancel ();
ClearConnections ();
ClearHandlers();
2014-07-14 20:40:06 +04:00
}
2015-01-03 23:57:15 +03:00
void SOCKSServer::Accept ()
2014-07-14 20:40:06 +04:00
{
auto newSocket = new boost::asio::ip::tcp::socket (GetService ());
2015-01-03 23:57:15 +03:00
m_Acceptor.async_accept (*newSocket, std::bind (&SOCKSServer::HandleAccept, this,
std::placeholders::_1, newSocket));
}
void SOCKSServer::AddHandler (std::shared_ptr<SOCKSHandler> handler) {
std::unique_lock<std::mutex> l(m_HandlersMutex);
m_Handlers.insert (handler);
}
void SOCKSServer::RemoveHandler (std::shared_ptr<SOCKSHandler> handler)
{
std::unique_lock<std::mutex> l(m_HandlersMutex);
m_Handlers.erase (handler);
}
void SOCKSServer::ClearHandlers ()
{
std::unique_lock<std::mutex> l(m_HandlersMutex);
m_Handlers.clear ();
}
2015-01-03 23:57:15 +03:00
void SOCKSServer::HandleAccept (const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket)
2014-07-14 20:40:06 +04:00
{
if (!ecode)
{
LogPrint(eLogDebug,"--- SOCKS accepted");
AddHandler(std::make_shared<SOCKSHandler> (this, socket));
2014-07-14 20:40:06 +04:00
Accept();
}
else
{
LogPrint (eLogError,"--- SOCKS Closing socket on accept because: ", ecode.message ());
delete socket;
}
2014-07-14 20:40:06 +04:00
}
2014-07-14 20:40:06 +04:00
}
}