|
|
@ -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 |
|
|
|
{ |
|
|
|
{ |
|
|
@ -73,16 +75,16 @@ namespace proxy |
|
|
|
}; |
|
|
|
}; |
|
|
|
enum authMethods |
|
|
|
enum authMethods |
|
|
|
{ |
|
|
|
{ |
|
|
|
AUTH_NONE = 0, //No authentication, skip to next step
|
|
|
|
AUTH_NONE = 0, // No authentication, skip to next step
|
|
|
|
AUTH_GSSAPI = 1, //GSSAPI authentication
|
|
|
|
AUTH_GSSAPI = 1, // GSSAPI authentication
|
|
|
|
AUTH_USERPASSWD = 2, //Username and password
|
|
|
|
AUTH_USERPASSWD = 2, // Username and password
|
|
|
|
AUTH_UNACCEPTABLE = 0xff //No acceptable method found
|
|
|
|
AUTH_UNACCEPTABLE = 0xff // No acceptable method found
|
|
|
|
}; |
|
|
|
}; |
|
|
|
enum addrTypes |
|
|
|
enum addrTypes |
|
|
|
{ |
|
|
|
{ |
|
|
|
ADDR_IPV4 = 1, //IPv4 address (4 octets)
|
|
|
|
ADDR_IPV4 = 1, // IPv4 address (4 octets)
|
|
|
|
ADDR_DNS = 3, // DNS name (up to 255 octets)
|
|
|
|
ADDR_DNS = 3, // DNS name (up to 255 octets)
|
|
|
|
ADDR_IPV6 = 4 //IPV6 address (16 octets)
|
|
|
|
ADDR_IPV6 = 4 // IPV6 address (16 octets)
|
|
|
|
}; |
|
|
|
}; |
|
|
|
enum errTypes |
|
|
|
enum errTypes |
|
|
|
{ |
|
|
|
{ |
|
|
@ -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); |
|
|
@ -154,9 +156,9 @@ namespace proxy |
|
|
|
std::shared_ptr<i2p::stream::Stream> m_stream; |
|
|
|
std::shared_ptr<i2p::stream::Stream> m_stream; |
|
|
|
uint8_t *m_remaining_data; //Data left to be sent
|
|
|
|
uint8_t *m_remaining_data; //Data left to be sent
|
|
|
|
uint8_t *m_remaining_upstream_data; //upstream data left to be forwarded
|
|
|
|
uint8_t *m_remaining_upstream_data; //upstream data left to be forwarded
|
|
|
|
uint8_t m_response[7+max_socks_hostname_size]; |
|
|
|
uint8_t m_response[7 + max_socks_hostname_size]; |
|
|
|
uint8_t m_upstream_response[SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE]; |
|
|
|
uint8_t m_upstream_response[SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE]; |
|
|
|
uint8_t m_upstream_request[14+max_socks_hostname_size]; |
|
|
|
uint8_t m_upstream_request[14 + max_socks_hostname_size]; |
|
|
|
std::size_t m_upstream_response_len; |
|
|
|
std::size_t m_upstream_response_len; |
|
|
|
address m_address; //Address
|
|
|
|
address m_address; //Address
|
|
|
|
std::size_t m_remaining_data_len; //Size of the data left to be sent
|
|
|
|
std::size_t m_remaining_data_len; //Size of the data left to be sent
|
|
|
@ -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: |
|
|
@ -704,7 +723,7 @@ namespace proxy |
|
|
|
break; |
|
|
|
break; |
|
|
|
case SOCKS5: |
|
|
|
case SOCKS5: |
|
|
|
LogPrint(eLogInfo, "SOCKS: v5 connection success"); |
|
|
|
LogPrint(eLogInfo, "SOCKS: v5 connection success"); |
|
|
|
//HACK only 16 bits passed in port as SOCKS5 doesn't allow for more
|
|
|
|
// HACK only 16 bits passed in port as SOCKS5 doesn't allow for more
|
|
|
|
response = GenerateSOCKS5Response(SOCKS5_OK, ADDR_DNS, m_address, m_port); |
|
|
|
response = GenerateSOCKS5Response(SOCKS5_OK, ADDR_DNS, m_address, m_port); |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
@ -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,14 +741,33 @@ 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) { |
|
|
|
// just right
|
|
|
|
// just right
|
|
|
|
uint8_t resp = m_upstream_response[1]; |
|
|
|
uint8_t resp = m_upstream_response[1]; |
|
|
|
if (resp == SOCKS4_OK) { |
|
|
|
if (resp == SOCKS4_OK) { |
|
|
|
// we have connected !
|
|
|
|
// we have connected!
|
|
|
|
SocksUpstreamSuccess(); |
|
|
|
SocksUpstreamSuccess(); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
// upstream failure
|
|
|
|
// upstream failure
|
|
|
@ -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) |
|
|
|