mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
793 lines
25 KiB
793 lines
25 KiB
10 years ago
|
#include <cstring>
|
||
|
#include <cassert>
|
||
10 years ago
|
#include <string>
|
||
|
#include <atomic>
|
||
10 years ago
|
#include "SOCKS.h"
|
||
|
#include "Identity.h"
|
||
10 years ago
|
#include "Streaming.h"
|
||
10 years ago
|
#include "Destination.h"
|
||
10 years ago
|
#include "ClientContext.h"
|
||
10 years ago
|
#include "I2PEndian.h"
|
||
10 years ago
|
#include "I2PTunnel.h"
|
||
9 years ago
|
#include "I2PService.h"
|
||
8 years ago
|
#include "util.h"
|
||
10 years ago
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace proxy
|
||
|
{
|
||
10 years ago
|
static const size_t socks_buffer_size = 8192;
|
||
|
static const size_t max_socks_hostname_size = 255; // Limit for socks5 and bad idea to traverse
|
||
|
|
||
9 years ago
|
static const size_t SOCKS_FORWARDER_BUFFER_SIZE = 8192;
|
||
|
|
||
|
static const size_t SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE = 8;
|
||
|
|
||
10 years ago
|
struct SOCKSDnsAddress
|
||
|
{
|
||
10 years ago
|
uint8_t size;
|
||
|
char value[max_socks_hostname_size];
|
||
8 years ago
|
void FromString (const std::string& str)
|
||
10 years ago
|
{
|
||
10 years ago
|
size = str.length();
|
||
|
if (str.length() > max_socks_hostname_size) size = max_socks_hostname_size;
|
||
|
memcpy(value,str.c_str(),size);
|
||
|
}
|
||
|
std::string ToString() { return std::string(value, size); }
|
||
|
void push_back (char c) { value[size++] = c; }
|
||
|
};
|
||
|
|
||
|
class SOCKSServer;
|
||
10 years ago
|
class SOCKSHandler: public i2p::client::I2PServiceHandler, public std::enable_shared_from_this<SOCKSHandler>
|
||
|
{
|
||
10 years ago
|
private:
|
||
10 years ago
|
enum state
|
||
|
{
|
||
10 years ago
|
GET_SOCKSV,
|
||
|
GET_COMMAND,
|
||
|
GET_PORT,
|
||
|
GET_IPV4,
|
||
|
GET4_IDENT,
|
||
|
GET4A_HOST,
|
||
|
GET5_AUTHNUM,
|
||
|
GET5_AUTH,
|
||
|
GET5_REQUESTV,
|
||
|
GET5_GETRSV,
|
||
|
GET5_GETADDRTYPE,
|
||
|
GET5_IPV6,
|
||
|
GET5_HOST_SIZE,
|
||
|
GET5_HOST,
|
||
9 years ago
|
READY,
|
||
|
UPSTREAM_RESOLVE,
|
||
|
UPSTREAM_CONNECT,
|
||
|
UPSTREAM_HANDSHAKE
|
||
10 years ago
|
};
|
||
10 years ago
|
enum authMethods
|
||
|
{
|
||
10 years ago
|
AUTH_NONE = 0, //No authentication, skip to next step
|
||
|
AUTH_GSSAPI = 1, //GSSAPI authentication
|
||
|
AUTH_USERPASSWD = 2, //Username and password
|
||
|
AUTH_UNACCEPTABLE = 0xff //No acceptable method found
|
||
|
};
|
||
10 years ago
|
enum addrTypes
|
||
|
{
|
||
10 years ago
|
ADDR_IPV4 = 1, //IPv4 address (4 octets)
|
||
|
ADDR_DNS = 3, // DNS name (up to 255 octets)
|
||
|
ADDR_IPV6 = 4 //IPV6 address (16 octets)
|
||
|
};
|
||
10 years ago
|
enum errTypes
|
||
|
{
|
||
10 years ago
|
SOCKS5_OK = 0, // No error for SOCKS5
|
||
|
SOCKS5_GEN_FAIL = 1, // General server failure
|
||
|
SOCKS5_RULE_DENIED = 2, // Connection disallowed by ruleset
|
||
|
SOCKS5_NET_UNREACH = 3, // Network unreachable
|
||
|
SOCKS5_HOST_UNREACH = 4, // Host unreachable
|
||
|
SOCKS5_CONN_REFUSED = 5, // Connection refused by the peer
|
||
|
SOCKS5_TTL_EXPIRED = 6, // TTL Expired
|
||
|
SOCKS5_CMD_UNSUP = 7, // Command unsuported
|
||
|
SOCKS5_ADDR_UNSUP = 8, // Address type unsuported
|
||
|
SOCKS4_OK = 90, // No error for SOCKS4
|
||
|
SOCKS4_FAIL = 91, // Failed establishing connecting or not allowed
|
||
|
SOCKS4_IDENTD_MISSING = 92, // Couldn't connect to the identd server
|
||
|
SOCKS4_IDENTD_DIFFER = 93 // The ID reported by the application and by identd differ
|
||
|
};
|
||
10 years ago
|
enum cmdTypes
|
||
|
{
|
||
10 years ago
|
CMD_CONNECT = 1, // TCP Connect
|
||
|
CMD_BIND = 2, // TCP Bind
|
||
|
CMD_UDP = 3 // UDP associate
|
||
|
};
|
||
10 years ago
|
enum socksVersions
|
||
|
{
|
||
10 years ago
|
SOCKS4 = 4, // SOCKS4
|
||
|
SOCKS5 = 5 // SOCKS5
|
||
|
};
|
||
10 years ago
|
union address
|
||
|
{
|
||
10 years ago
|
uint32_t ip;
|
||
|
SOCKSDnsAddress dns;
|
||
|
uint8_t ipv6[16];
|
||
|
};
|
||
|
|
||
|
void EnterState(state nstate, uint8_t parseleft = 1);
|
||
|
bool HandleData(uint8_t *sock_buff, std::size_t len);
|
||
|
bool ValidateSOCKSRequest();
|
||
|
void HandleSockRecv(const boost::system::error_code & ecode, std::size_t bytes_transfered);
|
||
|
void Terminate();
|
||
|
void AsyncSockRead();
|
||
|
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 GenerateSOCKS5Response(errTypes error, addrTypes type, const address &addr, uint16_t port);
|
||
9 years ago
|
boost::asio::const_buffers_1 GenerateUpstreamRequest();
|
||
10 years ago
|
bool Socks5ChooseAuth();
|
||
|
void SocksRequestFailed(errTypes error);
|
||
|
void SocksRequestSuccess();
|
||
|
void SentSocksFailed(const boost::system::error_code & ecode);
|
||
|
void SentSocksDone(const boost::system::error_code & ecode);
|
||
|
void SentSocksResponse(const boost::system::error_code & ecode);
|
||
|
void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream);
|
||
9 years ago
|
void ForwardSOCKS();
|
||
10 years ago
|
|
||
9 years ago
|
void SocksUpstreamSuccess();
|
||
|
void AsyncUpstreamSockRead();
|
||
|
void SendUpstreamRequest();
|
||
|
void HandleUpstreamData(uint8_t * buff, std::size_t len);
|
||
|
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 HandleUpstreamConnected(const boost::system::error_code & ecode,
|
||
|
boost::asio::ip::tcp::resolver::iterator itr);
|
||
|
void HandleUpstreamResolved(const boost::system::error_code & ecode,
|
||
|
boost::asio::ip::tcp::resolver::iterator itr);
|
||
|
|
||
|
boost::asio::ip::tcp::resolver m_proxy_resolver;
|
||
|
uint8_t m_sock_buff[socks_buffer_size];
|
||
|
std::shared_ptr<boost::asio::ip::tcp::socket> m_sock, m_upstreamSock;
|
||
10 years ago
|
std::shared_ptr<i2p::stream::Stream> m_stream;
|
||
|
uint8_t *m_remaining_data; //Data left to be sent
|
||
9 years ago
|
uint8_t *m_remaining_upstream_data; //upstream data left to be forwarded
|
||
10 years ago
|
uint8_t m_response[7+max_socks_hostname_size];
|
||
9 years ago
|
uint8_t m_upstream_response[SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE];
|
||
|
uint8_t m_upstream_request[14+max_socks_hostname_size];
|
||
|
std::size_t m_upstream_response_len;
|
||
10 years ago
|
address m_address; //Address
|
||
|
std::size_t m_remaining_data_len; //Size of the data left to be sent
|
||
|
uint32_t m_4aip; //Used in 4a requests
|
||
|
uint16_t m_port;
|
||
|
uint8_t m_command;
|
||
|
uint8_t m_parseleft; //Octets left to parse
|
||
|
authMethods m_authchosen; //Authentication chosen
|
||
|
addrTypes m_addrtype; //Address type chosen
|
||
|
socksVersions m_socksv; //Socks version
|
||
|
cmdTypes m_cmd; // Command requested
|
||
|
state m_state;
|
||
9 years ago
|
const bool m_UseUpstreamProxy; // do we want to use the upstream proxy for non i2p addresses?
|
||
|
const std::string m_UpstreamProxyAddress;
|
||
|
const uint16_t m_UpstreamProxyPort;
|
||
|
|
||
10 years ago
|
public:
|
||
9 years ago
|
SOCKSHandler(SOCKSServer * parent, std::shared_ptr<boost::asio::ip::tcp::socket> sock, const std::string & upstreamAddr, const uint16_t upstreamPort, const bool useUpstream) :
|
||
|
I2PServiceHandler(parent),
|
||
|
m_proxy_resolver(parent->GetService()),
|
||
|
m_sock(sock), m_stream(nullptr),
|
||
|
m_authchosen(AUTH_UNACCEPTABLE), m_addrtype(ADDR_IPV4),
|
||
|
m_UseUpstreamProxy(useUpstream),
|
||
|
m_UpstreamProxyAddress(upstreamAddr),
|
||
|
m_UpstreamProxyPort(upstreamPort)
|
||
10 years ago
|
{ m_address.ip = 0; EnterState(GET_SOCKSV); }
|
||
9 years ago
|
|
||
10 years ago
|
~SOCKSHandler() { Terminate(); }
|
||
10 years ago
|
void Handle() { AsyncSockRead(); }
|
||
10 years ago
|
};
|
||
9 years ago
|
|
||
10 years ago
|
void SOCKSHandler::AsyncSockRead()
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint(eLogDebug, "SOCKS: async sock read");
|
||
|
if (m_sock) {
|
||
10 years ago
|
m_sock->async_receive(boost::asio::buffer(m_sock_buff, socks_buffer_size),
|
||
10 years ago
|
std::bind(&SOCKSHandler::HandleSockRecv, shared_from_this(),
|
||
10 years ago
|
std::placeholders::_1, std::placeholders::_2));
|
||
9 years ago
|
} else {
|
||
|
LogPrint(eLogError,"SOCKS: no socket for read");
|
||
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void SOCKSHandler::Terminate()
|
||
|
{
|
||
10 years ago
|
if (Kill()) return;
|
||
10 years ago
|
if (m_sock)
|
||
|
{
|
||
9 years ago
|
LogPrint(eLogDebug, "SOCKS: closing socket");
|
||
10 years ago
|
m_sock->close();
|
||
|
m_sock = nullptr;
|
||
|
}
|
||
9 years ago
|
if (m_upstreamSock)
|
||
|
{
|
||
|
LogPrint(eLogDebug, "SOCKS: closing upstream socket");
|
||
|
m_upstreamSock->close();
|
||
|
m_upstreamSock = nullptr;
|
||
|
}
|
||
10 years ago
|
if (m_stream)
|
||
|
{
|
||
9 years ago
|
LogPrint(eLogDebug, "SOCKS: closing stream");
|
||
10 years ago
|
m_stream.reset ();
|
||
|
}
|
||
10 years ago
|
Done(shared_from_this());
|
||
10 years ago
|
}
|
||
|
|
||
|
boost::asio::const_buffers_1 SOCKSHandler::GenerateSOCKS4Response(SOCKSHandler::errTypes error, uint32_t ip, uint16_t port)
|
||
|
{
|
||
|
assert(error >= SOCKS4_OK);
|
||
10 years ago
|
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);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
boost::asio::const_buffers_1 SOCKSHandler::GenerateSOCKS5Response(SOCKSHandler::errTypes error, SOCKSHandler::addrTypes type, const SOCKSHandler::address &addr, uint16_t port)
|
||
10 years ago
|
{
|
||
10 years ago
|
size_t size = 6;
|
||
10 years ago
|
assert(error <= SOCKS5_ADDR_UNSUP);
|
||
10 years ago
|
m_response[0] = '\x05'; //Version
|
||
|
m_response[1] = error; //Response code
|
||
|
m_response[2] = '\x00'; //RSV
|
||
|
m_response[3] = type; //Address type
|
||
10 years ago
|
switch (type)
|
||
|
{
|
||
10 years ago
|
case ADDR_IPV4:
|
||
|
size = 10;
|
||
10 years ago
|
htobe32buf(m_response+4,addr.ip);
|
||
10 years ago
|
break;
|
||
|
case ADDR_IPV6:
|
||
|
size = 22;
|
||
10 years ago
|
memcpy(m_response+4,addr.ipv6, 16);
|
||
10 years ago
|
break;
|
||
|
case ADDR_DNS:
|
||
|
size = 7+addr.dns.size;
|
||
10 years ago
|
m_response[4] = addr.dns.size;
|
||
|
memcpy(m_response+5,addr.dns.value, addr.dns.size);
|
||
10 years ago
|
break;
|
||
|
}
|
||
10 years ago
|
htobe16buf(m_response+size-2,port); //Port
|
||
|
return boost::asio::const_buffers_1(m_response,size);
|
||
10 years ago
|
}
|
||
|
|
||
9 years ago
|
boost::asio::const_buffers_1 SOCKSHandler::GenerateUpstreamRequest()
|
||
|
{
|
||
|
size_t upstreamRequestSize = 0;
|
||
|
// TODO: negotiate with upstream
|
||
|
// SOCKS 4a
|
||
|
m_upstream_request[0] = '\x04'; //version
|
||
|
m_upstream_request[1] = m_cmd;
|
||
|
htobe16buf(m_upstream_request+2, m_port);
|
||
|
m_upstream_request[4] = 0;
|
||
|
m_upstream_request[5] = 0;
|
||
|
m_upstream_request[6] = 0;
|
||
|
m_upstream_request[7] = 1;
|
||
|
// user id
|
||
|
m_upstream_request[8] = 'i';
|
||
|
m_upstream_request[9] = '2';
|
||
|
m_upstream_request[10] = 'p';
|
||
|
m_upstream_request[11] = 'd';
|
||
|
m_upstream_request[12] = 0;
|
||
|
upstreamRequestSize += 13;
|
||
|
if (m_address.dns.size <= max_socks_hostname_size - ( upstreamRequestSize + 1) ) {
|
||
|
// bounds check okay
|
||
|
memcpy(m_upstream_request + upstreamRequestSize, m_address.dns.value, m_address.dns.size);
|
||
|
upstreamRequestSize += m_address.dns.size;
|
||
|
// null terminate
|
||
|
m_upstream_request[++upstreamRequestSize] = 0;
|
||
|
} else {
|
||
|
LogPrint(eLogError, "SOCKS: BUG!!! m_addr.dns.sizs > max_socks_hostname - ( upstreamRequestSize + 1 ) )");
|
||
|
}
|
||
|
return boost::asio::const_buffers_1(m_upstream_request, upstreamRequestSize);
|
||
|
}
|
||
|
|
||
10 years ago
|
bool SOCKSHandler::Socks5ChooseAuth()
|
||
10 years ago
|
{
|
||
10 years ago
|
m_response[0] = '\x05'; //Version
|
||
|
m_response[1] = m_authchosen; //Response code
|
||
|
boost::asio::const_buffers_1 response(m_response,2);
|
||
10 years ago
|
if (m_authchosen == AUTH_UNACCEPTABLE)
|
||
|
{
|
||
9 years ago
|
LogPrint(eLogWarning, "SOCKS: v5 authentication negotiation failed");
|
||
10 years ago
|
boost::asio::async_write(*m_sock, response, std::bind(&SOCKSHandler::SentSocksFailed,
|
||
9 years ago
|
shared_from_this(), std::placeholders::_1));
|
||
10 years ago
|
return false;
|
||
10 years ago
|
}
|
||
|
else
|
||
|
{
|
||
9 years ago
|
LogPrint(eLogDebug, "SOCKS: v5 choosing authentication method: ", m_authchosen);
|
||
10 years ago
|
boost::asio::async_write(*m_sock, response, std::bind(&SOCKSHandler::SentSocksResponse,
|
||
9 years ago
|
shared_from_this(), std::placeholders::_1));
|
||
10 years ago
|
return true;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
/* All hope is lost beyond this point */
|
||
|
void SOCKSHandler::SocksRequestFailed(SOCKSHandler::errTypes error)
|
||
10 years ago
|
{
|
||
10 years ago
|
boost::asio::const_buffers_1 response(nullptr,0);
|
||
|
assert(error != SOCKS4_OK && error != SOCKS5_OK);
|
||
10 years ago
|
switch (m_socksv)
|
||
|
{
|
||
10 years ago
|
case SOCKS4:
|
||
9 years ago
|
LogPrint(eLogWarning, "SOCKS: v4 request failed: ", error);
|
||
10 years ago
|
if (error < SOCKS4_OK) error = SOCKS4_FAIL; //Transparently map SOCKS5 errors
|
||
|
response = GenerateSOCKS4Response(error, m_4aip, m_port);
|
||
10 years ago
|
break;
|
||
10 years ago
|
case SOCKS5:
|
||
9 years ago
|
LogPrint(eLogWarning, "SOCKS: v5 request failed: ", error);
|
||
10 years ago
|
response = GenerateSOCKS5Response(error, m_addrtype, m_address, m_port);
|
||
10 years ago
|
break;
|
||
10 years ago
|
}
|
||
10 years ago
|
boost::asio::async_write(*m_sock, response, std::bind(&SOCKSHandler::SentSocksFailed,
|
||
9 years ago
|
shared_from_this(), std::placeholders::_1));
|
||
10 years ago
|
}
|
||
|
|
||
|
void SOCKSHandler::SocksRequestSuccess()
|
||
|
{
|
||
10 years ago
|
boost::asio::const_buffers_1 response(nullptr,0);
|
||
|
//TODO: this should depend on things like the command type and callbacks may change
|
||
10 years ago
|
switch (m_socksv)
|
||
|
{
|
||
10 years ago
|
case SOCKS4:
|
||
9 years ago
|
LogPrint(eLogInfo, "SOCKS: v4 connection success");
|
||
10 years ago
|
response = GenerateSOCKS4Response(SOCKS4_OK, m_4aip, m_port);
|
||
10 years ago
|
break;
|
||
10 years ago
|
case SOCKS5:
|
||
9 years ago
|
LogPrint(eLogInfo, "SOCKS: v5 connection success");
|
||
10 years ago
|
auto s = i2p::client::context.GetAddressBook().ToAddress(GetOwner()->GetLocalDestination()->GetIdentHash());
|
||
10 years ago
|
address ad; ad.dns.FromString(s);
|
||
|
//HACK only 16 bits passed in port as SOCKS5 doesn't allow for more
|
||
10 years ago
|
response = GenerateSOCKS5Response(SOCKS5_OK, ADDR_DNS, ad, m_stream->GetRecvStreamID());
|
||
10 years ago
|
break;
|
||
10 years ago
|
}
|
||
10 years ago
|
boost::asio::async_write(*m_sock, response, std::bind(&SOCKSHandler::SentSocksDone,
|
||
9 years ago
|
shared_from_this(), std::placeholders::_1));
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void SOCKSHandler::EnterState(SOCKSHandler::state nstate, uint8_t parseleft) {
|
||
10 years ago
|
switch (nstate)
|
||
|
{
|
||
10 years ago
|
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:;
|
||
10 years ago
|
}
|
||
10 years ago
|
m_parseleft = parseleft;
|
||
|
m_state = nstate;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
bool SOCKSHandler::ValidateSOCKSRequest()
|
||
|
{
|
||
|
if ( m_cmd != CMD_CONNECT )
|
||
|
{
|
||
10 years ago
|
//TODO: we need to support binds and other shit!
|
||
9 years ago
|
LogPrint(eLogError, "SOCKS: unsupported command: ", m_cmd);
|
||
10 years ago
|
SocksRequestFailed(SOCKS5_CMD_UNSUP);
|
||
10 years ago
|
return false;
|
||
10 years ago
|
}
|
||
|
//TODO: we may want to support other address types!
|
||
10 years ago
|
if ( m_addrtype != ADDR_DNS )
|
||
|
{
|
||
|
switch (m_socksv)
|
||
|
{
|
||
10 years ago
|
case SOCKS5:
|
||
9 years ago
|
LogPrint(eLogError, "SOCKS: v5 unsupported address type: ", m_addrtype);
|
||
10 years ago
|
break;
|
||
10 years ago
|
case SOCKS4:
|
||
9 years ago
|
LogPrint(eLogError, "SOCKS: request with v4a rejected because it's actually SOCKS4");
|
||
10 years ago
|
break;
|
||
10 years ago
|
}
|
||
10 years ago
|
SocksRequestFailed(SOCKS5_ADDR_UNSUP);
|
||
10 years ago
|
return false;
|
||
10 years ago
|
}
|
||
10 years ago
|
return true;
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
bool SOCKSHandler::HandleData(uint8_t *sock_buff, std::size_t len)
|
||
10 years ago
|
{
|
||
10 years ago
|
assert(len); // This should always be called with a least a byte left to parse
|
||
10 years ago
|
while (len > 0)
|
||
|
{
|
||
|
switch (m_state)
|
||
|
{
|
||
10 years ago
|
case GET_SOCKSV:
|
||
|
m_socksv = (SOCKSHandler::socksVersions) *sock_buff;
|
||
10 years ago
|
switch (*sock_buff)
|
||
|
{
|
||
10 years ago
|
case SOCKS4:
|
||
|
EnterState(GET_COMMAND); //Initialize the parser at the right position
|
||
10 years ago
|
break;
|
||
10 years ago
|
case SOCKS5:
|
||
|
EnterState(GET5_AUTHNUM); //Initialize the parser at the right position
|
||
10 years ago
|
break;
|
||
10 years ago
|
default:
|
||
9 years ago
|
LogPrint(eLogError, "SOCKS: rejected invalid version: ", ((int)*sock_buff));
|
||
10 years ago
|
Terminate();
|
||
|
return false;
|
||
|
}
|
||
10 years ago
|
break;
|
||
10 years ago
|
case GET5_AUTHNUM:
|
||
|
EnterState(GET5_AUTH, *sock_buff);
|
||
10 years ago
|
break;
|
||
10 years ago
|
case GET5_AUTH:
|
||
|
m_parseleft --;
|
||
|
if (*sock_buff == AUTH_NONE)
|
||
|
m_authchosen = AUTH_NONE;
|
||
10 years ago
|
if ( m_parseleft == 0 )
|
||
|
{
|
||
10 years ago
|
if (!Socks5ChooseAuth()) return false;
|
||
|
EnterState(GET5_REQUESTV);
|
||
|
}
|
||
10 years ago
|
break;
|
||
10 years ago
|
case GET_COMMAND:
|
||
10 years ago
|
switch (*sock_buff)
|
||
|
{
|
||
10 years ago
|
case CMD_CONNECT:
|
||
|
case CMD_BIND:
|
||
10 years ago
|
break;
|
||
10 years ago
|
case CMD_UDP:
|
||
|
if (m_socksv == SOCKS5) break;
|
||
|
default:
|
||
9 years ago
|
LogPrint(eLogError, "SOCKS: invalid command: ", ((int)*sock_buff));
|
||
10 years ago
|
SocksRequestFailed(SOCKS5_GEN_FAIL);
|
||
10 years ago
|
return false;
|
||
10 years ago
|
}
|
||
|
m_cmd = (SOCKSHandler::cmdTypes)*sock_buff;
|
||
10 years ago
|
switch (m_socksv)
|
||
|
{
|
||
10 years ago
|
case SOCKS5: EnterState(GET5_GETRSV); break;
|
||
|
case SOCKS4: EnterState(GET_PORT); break;
|
||
10 years ago
|
}
|
||
10 years ago
|
break;
|
||
10 years ago
|
case GET_PORT:
|
||
|
m_port = (m_port << 8)|((uint16_t)*sock_buff);
|
||
10 years ago
|
m_parseleft--;
|
||
10 years ago
|
if (m_parseleft == 0)
|
||
|
{
|
||
|
switch (m_socksv)
|
||
|
{
|
||
9 years ago
|
case SOCKS5: EnterState(READY); break;
|
||
10 years ago
|
case SOCKS4: EnterState(GET_IPV4); break;
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
break;
|
||
10 years ago
|
case GET_IPV4:
|
||
10 years ago
|
m_address.ip = (m_address.ip << 8)|((uint32_t)*sock_buff);
|
||
10 years ago
|
m_parseleft--;
|
||
10 years ago
|
if (m_parseleft == 0)
|
||
|
{
|
||
|
switch (m_socksv)
|
||
|
{
|
||
10 years ago
|
case SOCKS5: EnterState(GET_PORT); break;
|
||
|
case SOCKS4: EnterState(GET4_IDENT); m_4aip = m_address.ip; break;
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
break;
|
||
10 years ago
|
case GET4_IDENT:
|
||
10 years ago
|
if (!*sock_buff)
|
||
|
{
|
||
|
if( m_4aip == 0 || m_4aip > 255 )
|
||
9 years ago
|
EnterState(READY);
|
||
10 years ago
|
else
|
||
10 years ago
|
EnterState(GET4A_HOST);
|
||
10 years ago
|
}
|
||
10 years ago
|
break;
|
||
10 years ago
|
case GET4A_HOST:
|
||
10 years ago
|
if (!*sock_buff)
|
||
|
{
|
||
9 years ago
|
EnterState(READY);
|
||
10 years ago
|
break;
|
||
|
}
|
||
10 years ago
|
if (m_address.dns.size >= max_socks_hostname_size)
|
||
|
{
|
||
9 years ago
|
LogPrint(eLogError, "SOCKS: v4a req failed: destination is too large");
|
||
10 years ago
|
SocksRequestFailed(SOCKS4_FAIL);
|
||
10 years ago
|
return false;
|
||
10 years ago
|
}
|
||
10 years ago
|
m_address.dns.push_back(*sock_buff);
|
||
10 years ago
|
break;
|
||
10 years ago
|
case GET5_REQUESTV:
|
||
10 years ago
|
if (*sock_buff != SOCKS5)
|
||
|
{
|
||
9 years ago
|
LogPrint(eLogError,"SOCKS: v5 rejected unknown request version: ", ((int)*sock_buff));
|
||
10 years ago
|
SocksRequestFailed(SOCKS5_GEN_FAIL);
|
||
10 years ago
|
return false;
|
||
10 years ago
|
}
|
||
10 years ago
|
EnterState(GET_COMMAND);
|
||
10 years ago
|
break;
|
||
10 years ago
|
case GET5_GETRSV:
|
||
10 years ago
|
if ( *sock_buff != 0 )
|
||
|
{
|
||
9 years ago
|
LogPrint(eLogError, "SOCKS: v5 unknown reserved field: ", ((int)*sock_buff));
|
||
10 years ago
|
SocksRequestFailed(SOCKS5_GEN_FAIL);
|
||
10 years ago
|
return false;
|
||
10 years ago
|
}
|
||
10 years ago
|
EnterState(GET5_GETADDRTYPE);
|
||
10 years ago
|
break;
|
||
10 years ago
|
case GET5_GETADDRTYPE:
|
||
10 years ago
|
switch (*sock_buff)
|
||
|
{
|
||
10 years ago
|
case ADDR_IPV4: EnterState(GET_IPV4); break;
|
||
|
case ADDR_IPV6: EnterState(GET5_IPV6); break;
|
||
|
case ADDR_DNS : EnterState(GET5_HOST_SIZE); break;
|
||
10 years ago
|
default:
|
||
9 years ago
|
LogPrint(eLogError, "SOCKS: v5 unknown address type: ", ((int)*sock_buff));
|
||
10 years ago
|
SocksRequestFailed(SOCKS5_GEN_FAIL);
|
||
10 years ago
|
return false;
|
||
10 years ago
|
}
|
||
10 years ago
|
break;
|
||
10 years ago
|
case GET5_IPV6:
|
||
10 years ago
|
m_address.ipv6[16-m_parseleft] = *sock_buff;
|
||
|
m_parseleft--;
|
||
10 years ago
|
if (m_parseleft == 0) EnterState(GET_PORT);
|
||
10 years ago
|
break;
|
||
10 years ago
|
case GET5_HOST_SIZE:
|
||
10 years ago
|
EnterState(GET5_HOST, *sock_buff);
|
||
10 years ago
|
break;
|
||
10 years ago
|
case GET5_HOST:
|
||
10 years ago
|
m_address.dns.push_back(*sock_buff);
|
||
10 years ago
|
m_parseleft--;
|
||
10 years ago
|
if (m_parseleft == 0) EnterState(GET_PORT);
|
||
10 years ago
|
break;
|
||
10 years ago
|
default:
|
||
9 years ago
|
LogPrint(eLogError, "SOCKS: parse state?? ", m_state);
|
||
10 years ago
|
Terminate();
|
||
10 years ago
|
return false;
|
||
10 years ago
|
}
|
||
10 years ago
|
sock_buff++;
|
||
|
len--;
|
||
9 years ago
|
if (m_state == READY)
|
||
10 years ago
|
{
|
||
10 years ago
|
m_remaining_data_len = len;
|
||
|
m_remaining_data = sock_buff;
|
||
|
return ValidateSOCKSRequest();
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
return true;
|
||
10 years ago
|
}
|
||
|
|
||
|
void SOCKSHandler::HandleSockRecv(const boost::system::error_code & ecode, std::size_t len)
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint(eLogDebug, "SOCKS: recieved ", len, " bytes");
|
||
10 years ago
|
if(ecode)
|
||
|
{
|
||
9 years ago
|
LogPrint(eLogWarning, "SOCKS: recv got error: ", ecode);
|
||
|
Terminate();
|
||
10 years ago
|
return;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
if (HandleData(m_sock_buff, len))
|
||
|
{
|
||
9 years ago
|
if (m_state == READY)
|
||
10 years ago
|
{
|
||
9 years ago
|
const std::string addr = m_address.dns.ToString();
|
||
|
LogPrint(eLogInfo, "SOCKS: requested ", addr, ":" , m_port);
|
||
|
const size_t addrlen = addr.size();
|
||
|
// does it end with .i2p?
|
||
|
if ( addr.rfind(".i2p") == addrlen - 4) {
|
||
|
// yes it does, make an i2p session
|
||
|
GetOwner()->CreateStream ( std::bind (&SOCKSHandler::HandleStreamRequestComplete,
|
||
|
shared_from_this(), std::placeholders::_1), m_address.dns.ToString(), m_port);
|
||
|
} else if (m_UseUpstreamProxy) {
|
||
|
// forward it to upstream proxy
|
||
|
ForwardSOCKS();
|
||
|
} else {
|
||
|
// no upstream proxy
|
||
|
SocksRequestFailed(SOCKS5_ADDR_UNSUP);
|
||
|
}
|
||
10 years ago
|
}
|
||
|
else
|
||
10 years ago
|
AsyncSockRead();
|
||
10 years ago
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void SOCKSHandler::SentSocksFailed(const boost::system::error_code & ecode)
|
||
10 years ago
|
{
|
||
9 years ago
|
if (ecode)
|
||
|
LogPrint (eLogError, "SOCKS: closing socket after sending failure because: ", ecode.message ());
|
||
|
Terminate();
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
void SOCKSHandler::SentSocksDone(const boost::system::error_code & ecode)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (!ecode)
|
||
|
{
|
||
|
if (Kill()) return;
|
||
9 years ago
|
LogPrint (eLogInfo, "SOCKS: new I2PTunnel connection");
|
||
10 years ago
|
auto connection = std::make_shared<i2p::client::I2PTunnelConnection>(GetOwner(), m_sock, m_stream);
|
||
|
GetOwner()->AddHandler (connection);
|
||
10 years ago
|
connection->I2PConnect (m_remaining_data,m_remaining_data_len);
|
||
10 years ago
|
Done(shared_from_this());
|
||
10 years ago
|
}
|
||
|
else
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SOCKS: closing socket after completion reply because: ", ecode.message ());
|
||
10 years ago
|
Terminate();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void SOCKSHandler::SentSocksResponse(const boost::system::error_code & ecode)
|
||
|
{
|
||
10 years ago
|
if (ecode)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SOCKS: closing socket after sending reply because: ", ecode.message ());
|
||
10 years ago
|
Terminate();
|
||
|
}
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void SOCKSHandler::HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (stream)
|
||
|
{
|
||
10 years ago
|
m_stream = stream;
|
||
10 years ago
|
SocksRequestSuccess();
|
||
10 years ago
|
}
|
||
|
else
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "SOCKS: error when creating the stream, check the previous warnings for more info");
|
||
10 years ago
|
SocksRequestFailed(SOCKS5_HOST_UNREACH);
|
||
10 years ago
|
}
|
||
|
}
|
||
9 years ago
|
|
||
|
void SOCKSHandler::ForwardSOCKS()
|
||
|
{
|
||
|
LogPrint(eLogInfo, "SOCKS: forwarding to upstream");
|
||
|
EnterState(UPSTREAM_RESOLVE);
|
||
8 years ago
|
boost::asio::ip::tcp::resolver::query q(m_UpstreamProxyAddress, std::to_string(m_UpstreamProxyPort));
|
||
9 years ago
|
m_proxy_resolver.async_resolve(q, std::bind(&SOCKSHandler::HandleUpstreamResolved, shared_from_this(),
|
||
9 years ago
|
std::placeholders::_1, std::placeholders::_2));
|
||
9 years ago
|
}
|
||
|
|
||
|
void SOCKSHandler::AsyncUpstreamSockRead()
|
||
|
{
|
||
|
LogPrint(eLogDebug, "SOCKS: async upstream sock read");
|
||
|
if (m_upstreamSock) {
|
||
|
m_upstreamSock->async_read_some(boost::asio::buffer(m_upstream_response, SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE),
|
||
|
std::bind(&SOCKSHandler::HandleUpstreamSockRecv, shared_from_this(),
|
||
|
std::placeholders::_1, std::placeholders::_2));
|
||
|
} else {
|
||
|
LogPrint(eLogError, "SOCKS: no upstream socket for read");
|
||
|
SocksRequestFailed(SOCKS5_GEN_FAIL);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void SOCKSHandler::HandleUpstreamSockRecv(const boost::system::error_code & ecode, std::size_t bytes_transfered)
|
||
|
{
|
||
|
if (ecode) {
|
||
|
if (m_state == UPSTREAM_HANDSHAKE ) {
|
||
|
// we are trying to handshake but it failed
|
||
|
SocksRequestFailed(SOCKS5_NET_UNREACH);
|
||
|
} else {
|
||
|
LogPrint(eLogError, "SOCKS: bad state when reading from upstream: ", (int) m_state);
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
HandleUpstreamData(m_upstream_response, bytes_transfered);
|
||
|
}
|
||
|
|
||
|
void SOCKSHandler::SocksUpstreamSuccess()
|
||
|
{
|
||
|
LogPrint(eLogInfo, "SOCKS: upstream success");
|
||
|
boost::asio::const_buffers_1 response(nullptr, 0);
|
||
|
switch (m_socksv)
|
||
|
{
|
||
|
case SOCKS4:
|
||
|
LogPrint(eLogInfo, "SOCKS: v4 connection success");
|
||
|
response = GenerateSOCKS4Response(SOCKS4_OK, m_4aip, m_port);
|
||
|
break;
|
||
|
case SOCKS5:
|
||
|
LogPrint(eLogInfo, "SOCKS: v5 connection success");
|
||
|
//HACK only 16 bits passed in port as SOCKS5 doesn't allow for more
|
||
|
response = GenerateSOCKS5Response(SOCKS5_OK, ADDR_DNS, m_address, m_port);
|
||
|
break;
|
||
|
}
|
||
|
m_sock->send(response);
|
||
|
auto forwarder = std::make_shared<i2p::client::TCPIPPipe>(GetOwner(), m_sock, m_upstreamSock);
|
||
|
m_upstreamSock = nullptr;
|
||
|
m_sock = nullptr;
|
||
|
GetOwner()->AddHandler(forwarder);
|
||
|
forwarder->Start();
|
||
|
Terminate();
|
||
|
|
||
|
}
|
||
|
|
||
|
void SOCKSHandler::HandleUpstreamData(uint8_t * dataptr, std::size_t len)
|
||
|
{
|
||
|
if (m_state == UPSTREAM_HANDSHAKE) {
|
||
|
m_upstream_response_len += len;
|
||
|
// handle handshake data
|
||
|
if (m_upstream_response_len < SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE) {
|
||
|
// too small, continue reading
|
||
|
AsyncUpstreamSockRead();
|
||
|
} else if (len == SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE) {
|
||
|
// just right
|
||
|
uint8_t resp = m_upstream_response[1];
|
||
|
if (resp == SOCKS4_OK) {
|
||
|
// we have connected !
|
||
|
SocksUpstreamSuccess();
|
||
|
} else {
|
||
|
// upstream failure
|
||
|
LogPrint(eLogError, "SOCKS: upstream proxy failure: ", (int) resp);
|
||
|
// TODO: runtime error?
|
||
|
SocksRequestFailed(SOCKS5_GEN_FAIL);
|
||
|
}
|
||
|
} else {
|
||
|
// too big
|
||
|
SocksRequestFailed(SOCKS5_GEN_FAIL);
|
||
|
}
|
||
|
} else {
|
||
|
// invalid state
|
||
|
LogPrint(eLogError, "SOCKS: invalid state reading from upstream: ", (int) m_state);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void SOCKSHandler::SendUpstreamRequest()
|
||
|
{
|
||
|
LogPrint(eLogInfo, "SOCKS: negotiating with upstream proxy");
|
||
|
EnterState(UPSTREAM_HANDSHAKE);
|
||
|
if (m_upstreamSock) {
|
||
|
boost::asio::write(*m_upstreamSock,
|
||
|
GenerateUpstreamRequest());
|
||
|
AsyncUpstreamSockRead();
|
||
|
} else {
|
||
|
LogPrint(eLogError, "SOCKS: no upstream socket to send handshake to");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void SOCKSHandler::HandleUpstreamConnected(const boost::system::error_code & ecode, boost::asio::ip::tcp::resolver::iterator itr)
|
||
|
{
|
||
|
if (ecode) {
|
||
|
LogPrint(eLogWarning, "SOCKS: could not connect to upstream proxy: ", ecode.message());
|
||
|
SocksRequestFailed(SOCKS5_NET_UNREACH);
|
||
|
return;
|
||
|
}
|
||
|
LogPrint(eLogInfo, "SOCKS: connected to upstream proxy");
|
||
|
SendUpstreamRequest();
|
||
|
}
|
||
|
|
||
|
void SOCKSHandler::HandleUpstreamResolved(const boost::system::error_code & ecode, boost::asio::ip::tcp::resolver::iterator itr)
|
||
|
{
|
||
|
if (ecode) {
|
||
|
// error resolving
|
||
|
LogPrint(eLogWarning, "SOCKS: upstream proxy", m_UpstreamProxyAddress, " not resolved: ", ecode.message());
|
||
|
SocksRequestFailed(SOCKS5_NET_UNREACH);
|
||
|
return;
|
||
|
}
|
||
|
LogPrint(eLogInfo, "SOCKS: upstream proxy resolved");
|
||
|
EnterState(UPSTREAM_CONNECT);
|
||
|
auto & service = GetOwner()->GetService();
|
||
|
m_upstreamSock = std::make_shared<boost::asio::ip::tcp::socket>(service);
|
||
|
boost::asio::async_connect(*m_upstreamSock, itr,
|
||
|
std::bind(&SOCKSHandler::HandleUpstreamConnected,
|
||
|
shared_from_this(), std::placeholders::_1, std::placeholders::_2));
|
||
|
}
|
||
|
|
||
|
SOCKSServer::SOCKSServer(const std::string& address, int port, const std::string& outAddress, uint16_t outPort,
|
||
|
std::shared_ptr<i2p::client::ClientDestination> localDestination) :
|
||
9 years ago
|
TCPIPAcceptor (address, port, localDestination ? localDestination : i2p::client::context.GetSharedLocalDestination ())
|
||
10 years ago
|
{
|
||
9 years ago
|
m_UseUpstreamProxy = false;
|
||
|
if (outAddress.length() > 0)
|
||
|
SetUpstreamProxy(outAddress, outPort);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
std::shared_ptr<i2p::client::I2PServiceHandler> SOCKSServer::CreateHandler(std::shared_ptr<boost::asio::ip::tcp::socket> socket)
|
||
10 years ago
|
{
|
||
9 years ago
|
return std::make_shared<SOCKSHandler> (this, socket, m_UpstreamProxyAddress, m_UpstreamProxyPort, m_UseUpstreamProxy);
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
9 years ago
|
void SOCKSServer::SetUpstreamProxy(const std::string & addr, const uint16_t port)
|
||
|
{
|
||
|
m_UpstreamProxyAddress = addr;
|
||
|
m_UpstreamProxyPort = port;
|
||
|
m_UseUpstreamProxy = true;
|
||
|
}
|
||
10 years ago
|
}
|
||
|
}
|