mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 08:14:15 +00:00
commit
729cc4330e
218
SOCKS.cpp
218
SOCKS.cpp
@ -5,6 +5,7 @@
|
|||||||
#include "ClientContext.h"
|
#include "ClientContext.h"
|
||||||
#include "I2PEndian.h"
|
#include "I2PEndian.h"
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
namespace i2p
|
namespace i2p
|
||||||
{
|
{
|
||||||
@ -15,17 +16,13 @@ namespace proxy
|
|||||||
|
|
||||||
void SOCKS4AHandler::AsyncSockRead()
|
void SOCKS4AHandler::AsyncSockRead()
|
||||||
{
|
{
|
||||||
LogPrint("--- socks4a async sock read");
|
LogPrint(eLogDebug,"--- SOCKS async sock read");
|
||||||
if(m_sock) {
|
if(m_sock) {
|
||||||
if (m_state == INITIAL) {
|
m_sock->async_receive(boost::asio::buffer(m_sock_buff, socks_buffer_size),
|
||||||
m_sock->async_receive(boost::asio::buffer(m_sock_buff, socks_buffer_size),
|
std::bind(&SOCKS4AHandler::HandleSockRecv, this,
|
||||||
std::bind(&SOCKS4AHandler::HandleSockRecv, this,
|
std::placeholders::_1, std::placeholders::_2));
|
||||||
std::placeholders::_1, std::placeholders::_2));
|
|
||||||
} else {
|
|
||||||
LogPrint("--- socks4a state?? ", m_state);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
LogPrint("--- socks4a no socket for read");
|
LogPrint(eLogError,"--- SOCKS no socket for read");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -37,7 +34,7 @@ namespace proxy
|
|||||||
|
|
||||||
void SOCKS4AHandler::SocksFailed()
|
void SOCKS4AHandler::SocksFailed()
|
||||||
{
|
{
|
||||||
LogPrint("--- socks4a failed");
|
LogPrint(eLogWarning,"--- SOCKS failed");
|
||||||
//TODO: send the right response
|
//TODO: send the right response
|
||||||
boost::asio::async_write(*m_sock, boost::asio::buffer("\x00\x5b 12345"),
|
boost::asio::async_write(*m_sock, boost::asio::buffer("\x00\x5b 12345"),
|
||||||
std::bind(&SOCKS4AHandler::SentSocksFailed, this,
|
std::bind(&SOCKS4AHandler::SentSocksFailed, this,
|
||||||
@ -47,7 +44,7 @@ namespace proxy
|
|||||||
void SOCKS4AHandler::CloseSock()
|
void SOCKS4AHandler::CloseSock()
|
||||||
{
|
{
|
||||||
if (m_sock) {
|
if (m_sock) {
|
||||||
LogPrint("--- socks4a close sock");
|
LogPrint(eLogDebug,"--- SOCKS close sock");
|
||||||
m_sock->close();
|
m_sock->close();
|
||||||
delete m_sock;
|
delete m_sock;
|
||||||
m_sock = nullptr;
|
m_sock = nullptr;
|
||||||
@ -57,7 +54,7 @@ namespace proxy
|
|||||||
void SOCKS4AHandler::CloseStream()
|
void SOCKS4AHandler::CloseStream()
|
||||||
{
|
{
|
||||||
if (m_stream) {
|
if (m_stream) {
|
||||||
LogPrint("--- socks4a close stream");
|
LogPrint(eLogDebug,"--- SOCKS close stream");
|
||||||
m_stream.reset ();
|
m_stream.reset ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -66,78 +63,147 @@ namespace proxy
|
|||||||
const size_t socks_ident_size = 1024;
|
const size_t socks_ident_size = 1024;
|
||||||
const size_t destb32_len = 52;
|
const size_t destb32_len = 52;
|
||||||
|
|
||||||
|
std::size_t SOCKS4AHandler::HandleData(uint8_t *sock_buff, std::size_t len)
|
||||||
|
{
|
||||||
|
assert(len); // This should always be called with a least a byte left to parse
|
||||||
|
switch (m_state) {
|
||||||
|
case GET_VERSION:
|
||||||
|
return HandleVersion(sock_buff);
|
||||||
|
case SOCKS4A:
|
||||||
|
return HandleSOCKS4A(sock_buff,len);
|
||||||
|
default:
|
||||||
|
LogPrint(eLogError,"--- SOCKS state?? ", m_state);
|
||||||
|
Terminate();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t SOCKS4AHandler::HandleVersion(uint8_t *sock_buff)
|
||||||
|
{
|
||||||
|
switch (*sock_buff) {
|
||||||
|
case 4:
|
||||||
|
m_state = SOCKS4A; // Switch to the 4a handler
|
||||||
|
m_pstate = GET4A_COMMAND; //Initialize the parser at the right position
|
||||||
|
return 1;
|
||||||
|
default:
|
||||||
|
LogPrint(eLogError,"--- SOCKS rejected invalid version", ((int)*sock_buff));
|
||||||
|
Terminate();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::size_t SOCKS4AHandler::HandleSOCKS4A(uint8_t *sock_buff, std::size_t len)
|
||||||
|
{
|
||||||
|
std::size_t rv = 0;
|
||||||
|
while (len > 0) {
|
||||||
|
rv++;
|
||||||
|
switch (m_pstate)
|
||||||
|
{
|
||||||
|
case GET4A_COMMAND:
|
||||||
|
if ( *sock_buff != 1 ) {
|
||||||
|
//TODO: we need to support binds and other shit!
|
||||||
|
LogPrint(eLogError,"--- SOCKS4a unsupported command", ((int)*sock_buff));
|
||||||
|
SocksFailed();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
m_pstate = GET4A_PORT1;
|
||||||
|
break;
|
||||||
|
case GET4A_PORT1:
|
||||||
|
m_port = ((uint16_t)*sock_buff) << 8;
|
||||||
|
m_pstate = GET4A_PORT2;
|
||||||
|
break;
|
||||||
|
case GET4A_PORT2:
|
||||||
|
m_port |= ((uint16_t)*sock_buff);
|
||||||
|
m_pstate = GET4A_IP1;
|
||||||
|
break;
|
||||||
|
case GET4A_IP1:
|
||||||
|
m_ip = ((uint32_t)*sock_buff) << 24;
|
||||||
|
m_pstate = GET4A_IP2;
|
||||||
|
break;
|
||||||
|
case GET4A_IP2:
|
||||||
|
m_ip |= ((uint32_t)*sock_buff) << 16;
|
||||||
|
m_pstate = GET4A_IP3;
|
||||||
|
break;
|
||||||
|
case GET4A_IP3:
|
||||||
|
m_ip |= ((uint32_t)*sock_buff) << 8;
|
||||||
|
m_pstate = GET4A_IP4;
|
||||||
|
break;
|
||||||
|
case GET4A_IP4:
|
||||||
|
m_ip |= ((uint32_t)*sock_buff);
|
||||||
|
m_pstate = GET4A_IDENT;
|
||||||
|
if( m_ip == 0 || m_ip > 255 ) {
|
||||||
|
LogPrint(eLogError,"--- SOCKS4a rejected because it's actually SOCKS4");
|
||||||
|
SocksFailed();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case GET4A_IDENT:
|
||||||
|
if (!*sock_buff)
|
||||||
|
m_pstate = GET4A_HOST;
|
||||||
|
break;
|
||||||
|
case GET4A_HOST:
|
||||||
|
if (!*sock_buff) {
|
||||||
|
m_pstate = DONE;
|
||||||
|
m_state = READY;
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
if (m_destination.size() > HOST_NAME_MAX) {
|
||||||
|
LogPrint(eLogError,"--- SOCKS4a destination is too large ");
|
||||||
|
SocksFailed();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
m_destination.push_back(*sock_buff);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
LogPrint(eLogError,"--- SOCKS4a parse state?? ", m_pstate);
|
||||||
|
Terminate();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
sock_buff++;
|
||||||
|
len--;
|
||||||
|
}
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
void SOCKS4AHandler::HandleSockRecv(const boost::system::error_code & ecode, std::size_t len)
|
void SOCKS4AHandler::HandleSockRecv(const boost::system::error_code & ecode, std::size_t len)
|
||||||
{
|
{
|
||||||
LogPrint("--- socks4a sock recv: ", len);
|
LogPrint(eLogDebug,"--- SOCKS sock recv: ", len);
|
||||||
//TODO: we may not have received all the data :(
|
|
||||||
if(ecode) {
|
if(ecode) {
|
||||||
LogPrint(" --- sock recv got error: ", ecode);
|
LogPrint(eLogWarning," --- SOCKS sock recv got error: ", ecode);
|
||||||
Terminate();
|
Terminate();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
char hostbuff[socks_hostname_size];
|
std::size_t pos = 0;
|
||||||
char identbuff[socks_ident_size];
|
while (pos != len && m_state != READY) {
|
||||||
std::memset(hostbuff, 0, sizeof(hostbuff));
|
assert(pos < len); //We are overflowing the buffer otherwise
|
||||||
std::memset(identbuff, 0, sizeof(hostbuff));
|
std::size_t rv = HandleData(m_sock_buff + pos, len - pos);
|
||||||
std::string dest;
|
if (!rv) return; //Something went wrong die misserably
|
||||||
|
pos += rv;
|
||||||
uint16_t port = 0;
|
|
||||||
uint32_t address = 0;
|
|
||||||
uint16_t idx1 = 0;
|
|
||||||
uint16_t idx2 = 0;
|
|
||||||
|
|
||||||
LogPrint(eLogDebug,"--- socks4a state initial ", len);
|
|
||||||
|
|
||||||
// check valid request
|
|
||||||
if( m_sock_buff[0] != 4 || m_sock_buff[1] != 1 || m_sock_buff[len-1] ) {
|
|
||||||
LogPrint(eLogError,"--- socks4a rejected invalid");
|
|
||||||
SocksFailed();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// get port
|
if (m_state == READY) {
|
||||||
port = bufbe16toh(m_sock_buff + 2);
|
LogPrint(eLogInfo,"--- SOCKS requested ", m_destination, ":" , m_port);
|
||||||
|
if (pos != len) {
|
||||||
// get address
|
LogPrint(eLogError,"--- SOCKS rejected because be can't handle extra data");
|
||||||
address = bufbe32toh(m_sock_buff + 4);
|
SocksFailed();
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
if(m_destination.find(".i2p") == std::string::npos) {
|
||||||
|
LogPrint(eLogError,"--- SOCKS invalid hostname: ", m_destination);
|
||||||
|
SocksFailed();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// check valid request
|
m_parent->GetLocalDestination ()->CreateStream (
|
||||||
if( address == 0 || address > 255 ) {
|
std::bind (&SOCKS4AHandler::HandleStreamRequestComplete,
|
||||||
LogPrint(eLogError,"--- socks4a rejected because it's actually socks4");
|
this, std::placeholders::_1), m_destination, m_port);
|
||||||
SocksFailed();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// read ident
|
|
||||||
do {
|
|
||||||
LogPrint(eLogDebug,"--- socks4a ", (int) m_sock_buff[9+idx1]);
|
|
||||||
identbuff[idx1] = m_sock_buff[8+idx1];
|
|
||||||
} while( identbuff[idx1++] && idx1 < socks_ident_size );
|
|
||||||
|
|
||||||
LogPrint(eLogInfo,"--- socks4a ident ", identbuff);
|
|
||||||
// read hostname
|
|
||||||
do {
|
|
||||||
hostbuff[idx2] = m_sock_buff[8+idx1+idx2];
|
|
||||||
} while( hostbuff[idx2++] && idx2 < socks_hostname_size );
|
|
||||||
|
|
||||||
LogPrint(eLogInfo,"--- socks4a requested ", hostbuff, ":" , port);
|
|
||||||
|
|
||||||
dest = std::string(hostbuff);
|
|
||||||
if(dest.find(".i2p") == std::string::npos) {
|
|
||||||
LogPrint("--- socks4a invalid hostname: ", dest);
|
|
||||||
SocksFailed();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_parent->GetLocalDestination ()->CreateStream (
|
|
||||||
std::bind (&SOCKS4AHandler::HandleStreamRequestComplete,
|
|
||||||
this, std::placeholders::_1), dest);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SOCKS4AHandler::ConnectionSuccess()
|
void SOCKS4AHandler::ConnectionSuccess()
|
||||||
{
|
{
|
||||||
LogPrint(eLogInfo,"--- socks4a connection success");
|
LogPrint(eLogInfo,"--- SOCKS connection success");
|
||||||
//TODO: send the right response
|
//TODO: send the right response
|
||||||
boost::asio::async_write(*m_sock, boost::asio::buffer("\x00\x5a 12345"),
|
boost::asio::async_write(*m_sock, boost::asio::buffer("\x00\x5a 12345"),
|
||||||
std::bind(&SOCKS4AHandler::SentConnectionSuccess, this,
|
std::bind(&SOCKS4AHandler::SentConnectionSuccess, this,
|
||||||
@ -151,7 +217,7 @@ namespace proxy
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LogPrint (eLogError,"--- socks4a Closing socket after sending failure because: ", ecode.message ());
|
LogPrint (eLogError,"--- SOCKS Closing socket after sending failure because: ", ecode.message ());
|
||||||
Terminate();
|
Terminate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -159,14 +225,14 @@ namespace proxy
|
|||||||
void SOCKS4AHandler::SentConnectionSuccess(const boost::system::error_code & ecode)
|
void SOCKS4AHandler::SentConnectionSuccess(const boost::system::error_code & ecode)
|
||||||
{
|
{
|
||||||
if (!ecode) {
|
if (!ecode) {
|
||||||
LogPrint (eLogInfo,"--- socks4a New I2PTunnel connection");
|
LogPrint (eLogInfo,"--- SOCKS New I2PTunnel connection");
|
||||||
auto connection = std::make_shared<i2p::client::I2PTunnelConnection>((i2p::client::I2PTunnel *)m_parent, m_sock, m_stream);
|
auto connection = std::make_shared<i2p::client::I2PTunnelConnection>((i2p::client::I2PTunnel *)m_parent, m_sock, m_stream);
|
||||||
m_parent->AddConnection (connection);
|
m_parent->AddConnection (connection);
|
||||||
connection->I2PConnect ();
|
connection->I2PConnect ();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LogPrint (eLogError,"--- socks4a Closing socket after sending success because: ", ecode.message ());
|
LogPrint (eLogError,"--- SOCKS Closing socket after sending success because: ", ecode.message ());
|
||||||
Terminate();
|
Terminate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -180,7 +246,7 @@ namespace proxy
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LogPrint (eLogError,"--- socks4a Issue when creating the stream, check the previous warnings for more info.");
|
LogPrint (eLogError,"--- SOCKS Issue when creating the stream, check the previous warnings for more info.");
|
||||||
SocksFailed();
|
SocksFailed();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -210,13 +276,13 @@ namespace proxy
|
|||||||
{
|
{
|
||||||
if (!ecode)
|
if (!ecode)
|
||||||
{
|
{
|
||||||
LogPrint(eLogDebug,"--- socks4a accepted");
|
LogPrint(eLogDebug,"--- SOCKS accepted");
|
||||||
new SOCKS4AHandler(this, socket);
|
new SOCKS4AHandler(this, socket);
|
||||||
Accept();
|
Accept();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LogPrint (eLogError,"--- socks4a Closing socket on accept because: ", ecode.message ());
|
LogPrint (eLogError,"--- SOCKS Closing socket on accept because: ", ecode.message ());
|
||||||
delete socket;
|
delete socket;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
70
SOCKS.h
70
SOCKS.h
@ -1,6 +1,7 @@
|
|||||||
#ifndef SOCKS4A_H__
|
#ifndef SOCKS4A_H__
|
||||||
#define SOCKS4A_H__
|
#define SOCKS4A_H__
|
||||||
|
|
||||||
|
#include <climits>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
#include "Identity.h"
|
#include "Identity.h"
|
||||||
@ -18,39 +19,58 @@ namespace proxy
|
|||||||
class SOCKS4AHandler {
|
class SOCKS4AHandler {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
enum state {
|
enum state {
|
||||||
INITIAL,
|
GET_VERSION,
|
||||||
OKAY,
|
SOCKS4A,
|
||||||
END
|
READY
|
||||||
};
|
};
|
||||||
|
enum parseState {
|
||||||
|
GET4A_COMMAND,
|
||||||
|
GET4A_PORT1,
|
||||||
|
GET4A_PORT2,
|
||||||
|
GET4A_IP1,
|
||||||
|
GET4A_IP2,
|
||||||
|
GET4A_IP3,
|
||||||
|
GET4A_IP4,
|
||||||
|
GET4A_IDENT,
|
||||||
|
GET4A_HOST,
|
||||||
|
DONE
|
||||||
|
};
|
||||||
|
|
||||||
void GotClientRequest(boost::system::error_code & ecode, std::string & host, uint16_t port);
|
void GotClientRequest(boost::system::error_code & ecode, std::string & host, uint16_t port);
|
||||||
void HandleSockRecv(const boost::system::error_code & ecode, std::size_t bytes_transfered);
|
std::size_t HandleData(uint8_t *sock_buff, std::size_t len);
|
||||||
void Terminate();
|
std::size_t HandleVersion(uint8_t *sock_buff);
|
||||||
void CloseSock();
|
std::size_t HandleSOCKS4A(uint8_t *sock_buff, std::size_t len);
|
||||||
void CloseStream();
|
void HandleSockRecv(const boost::system::error_code & ecode, std::size_t bytes_transfered);
|
||||||
void AsyncSockRead();
|
void Terminate();
|
||||||
void SocksFailed();
|
void CloseSock();
|
||||||
void SentSocksFailed(const boost::system::error_code & ecode);
|
void CloseStream();
|
||||||
void SentConnectionSuccess(const boost::system::error_code & ecode);
|
void AsyncSockRead();
|
||||||
void ConnectionSuccess();
|
void SocksFailed();
|
||||||
void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream);
|
void SentSocksFailed(const boost::system::error_code & ecode);
|
||||||
|
void SentConnectionSuccess(const boost::system::error_code & ecode);
|
||||||
|
void ConnectionSuccess();
|
||||||
|
void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream);
|
||||||
|
|
||||||
|
uint8_t m_sock_buff[socks_buffer_size];
|
||||||
|
|
||||||
uint8_t m_sock_buff[socks_buffer_size];
|
SOCKS4AServer * m_parent;
|
||||||
|
boost::asio::ip::tcp::socket * m_sock;
|
||||||
SOCKS4AServer * m_parent;
|
std::shared_ptr<i2p::stream::Stream> m_stream;
|
||||||
boost::asio::ip::tcp::socket * m_sock;
|
state m_state;
|
||||||
std::shared_ptr<i2p::stream::Stream> m_stream;
|
parseState m_pstate;
|
||||||
state m_state;
|
uint8_t m_command;
|
||||||
|
uint16_t m_port;
|
||||||
|
uint32_t m_ip;
|
||||||
|
std::string m_destination;
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SOCKS4AHandler(SOCKS4AServer * parent, boost::asio::ip::tcp::socket * sock) :
|
SOCKS4AHandler(SOCKS4AServer * parent, boost::asio::ip::tcp::socket * sock) :
|
||||||
m_parent(parent), m_sock(sock), m_stream(nullptr), m_state(INITIAL)
|
m_parent(parent), m_sock(sock), m_stream(nullptr), m_state(GET_VERSION)
|
||||||
{ AsyncSockRead(); }
|
{ AsyncSockRead(); m_destination.reserve(HOST_NAME_MAX+1); }
|
||||||
|
|
||||||
~SOCKS4AHandler() { CloseSock(); CloseStream(); }
|
~SOCKS4AHandler() { CloseSock(); CloseStream(); }
|
||||||
bool isComplete() { return m_state == END; }
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class SOCKS4AServer: public i2p::client::I2PTunnel
|
class SOCKS4AServer: public i2p::client::I2PTunnel
|
||||||
|
Loading…
x
Reference in New Issue
Block a user