mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-04-27 11:17:49 +02:00
remove trailing whitespaces
This commit is contained in:
parent
b91efaa973
commit
46f62e1af9
117 changed files with 3039 additions and 3040 deletions
|
@ -22,8 +22,8 @@ namespace proxy
|
|||
static const size_t SOCKS_FORWARDER_BUFFER_SIZE = 8192;
|
||||
|
||||
static const size_t SOCKS_UPSTREAM_SOCKS4A_REPLY_SIZE = 8;
|
||||
|
||||
struct SOCKSDnsAddress
|
||||
|
||||
struct SOCKSDnsAddress
|
||||
{
|
||||
uint8_t size;
|
||||
char value[max_socks_hostname_size];
|
||||
|
@ -38,10 +38,10 @@ namespace proxy
|
|||
};
|
||||
|
||||
class SOCKSServer;
|
||||
class SOCKSHandler: public i2p::client::I2PServiceHandler, public std::enable_shared_from_this<SOCKSHandler>
|
||||
class SOCKSHandler: public i2p::client::I2PServiceHandler, public std::enable_shared_from_this<SOCKSHandler>
|
||||
{
|
||||
private:
|
||||
enum state
|
||||
enum state
|
||||
{
|
||||
GET_SOCKSV,
|
||||
GET_COMMAND,
|
||||
|
@ -62,20 +62,20 @@ namespace proxy
|
|||
UPSTREAM_CONNECT,
|
||||
UPSTREAM_HANDSHAKE
|
||||
};
|
||||
enum authMethods
|
||||
enum authMethods
|
||||
{
|
||||
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
|
||||
};
|
||||
enum addrTypes
|
||||
enum addrTypes
|
||||
{
|
||||
ADDR_IPV4 = 1, //IPv4 address (4 octets)
|
||||
ADDR_DNS = 3, // DNS name (up to 255 octets)
|
||||
ADDR_IPV6 = 4 //IPV6 address (16 octets)
|
||||
};
|
||||
enum errTypes
|
||||
enum errTypes
|
||||
{
|
||||
SOCKS5_OK = 0, // No error for SOCKS5
|
||||
SOCKS5_GEN_FAIL = 1, // General server failure
|
||||
|
@ -91,18 +91,18 @@ namespace proxy
|
|||
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
|
||||
};
|
||||
enum cmdTypes
|
||||
enum cmdTypes
|
||||
{
|
||||
CMD_CONNECT = 1, // TCP Connect
|
||||
CMD_BIND = 2, // TCP Bind
|
||||
CMD_UDP = 3 // UDP associate
|
||||
};
|
||||
enum socksVersions
|
||||
enum socksVersions
|
||||
{
|
||||
SOCKS4 = 4, // SOCKS4
|
||||
SOCKS5 = 5 // SOCKS5
|
||||
};
|
||||
union address
|
||||
union address
|
||||
{
|
||||
uint32_t ip;
|
||||
SOCKSDnsAddress dns;
|
||||
|
@ -138,7 +138,7 @@ namespace proxy
|
|||
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;
|
||||
|
@ -163,7 +163,7 @@ namespace proxy
|
|||
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;
|
||||
|
||||
|
||||
public:
|
||||
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),
|
||||
|
@ -172,13 +172,13 @@ namespace proxy
|
|||
m_authchosen(AUTH_UNACCEPTABLE), m_addrtype(ADDR_IPV4),
|
||||
m_UseUpstreamProxy(useUpstream),
|
||||
m_UpstreamProxyAddress(upstreamAddr),
|
||||
m_UpstreamProxyPort(upstreamPort)
|
||||
m_UpstreamProxyPort(upstreamPort)
|
||||
{ m_address.ip = 0; EnterState(GET_SOCKSV); }
|
||||
|
||||
|
||||
~SOCKSHandler() { Terminate(); }
|
||||
void Handle() { AsyncSockRead(); }
|
||||
};
|
||||
|
||||
|
||||
void SOCKSHandler::AsyncSockRead()
|
||||
{
|
||||
LogPrint(eLogDebug, "SOCKS: async sock read");
|
||||
|
@ -191,10 +191,10 @@ namespace proxy
|
|||
}
|
||||
}
|
||||
|
||||
void SOCKSHandler::Terminate()
|
||||
void SOCKSHandler::Terminate()
|
||||
{
|
||||
if (Kill()) return;
|
||||
if (m_sock)
|
||||
if (m_sock)
|
||||
{
|
||||
LogPrint(eLogDebug, "SOCKS: closing socket");
|
||||
m_sock->close();
|
||||
|
@ -206,7 +206,7 @@ namespace proxy
|
|||
m_upstreamSock->close();
|
||||
m_upstreamSock = nullptr;
|
||||
}
|
||||
if (m_stream)
|
||||
if (m_stream)
|
||||
{
|
||||
LogPrint(eLogDebug, "SOCKS: closing stream");
|
||||
m_stream.reset ();
|
||||
|
@ -232,7 +232,7 @@ namespace proxy
|
|||
m_response[1] = error; //Response code
|
||||
m_response[2] = '\x00'; //RSV
|
||||
m_response[3] = type; //Address type
|
||||
switch (type)
|
||||
switch (type)
|
||||
{
|
||||
case ADDR_IPV4:
|
||||
size = 10;
|
||||
|
@ -288,14 +288,14 @@ namespace proxy
|
|||
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)
|
||||
if (m_authchosen == AUTH_UNACCEPTABLE)
|
||||
{
|
||||
LogPrint(eLogWarning, "SOCKS: v5 authentication negotiation failed");
|
||||
boost::asio::async_write(*m_sock, response, std::bind(&SOCKSHandler::SentSocksFailed,
|
||||
shared_from_this(), std::placeholders::_1));
|
||||
return false;
|
||||
}
|
||||
else
|
||||
}
|
||||
else
|
||||
{
|
||||
LogPrint(eLogDebug, "SOCKS: v5 choosing authentication method: ", m_authchosen);
|
||||
boost::asio::async_write(*m_sock, response, std::bind(&SOCKSHandler::SentSocksResponse,
|
||||
|
@ -309,7 +309,7 @@ namespace proxy
|
|||
{
|
||||
boost::asio::const_buffers_1 response(nullptr,0);
|
||||
assert(error != SOCKS4_OK && error != SOCKS5_OK);
|
||||
switch (m_socksv)
|
||||
switch (m_socksv)
|
||||
{
|
||||
case SOCKS4:
|
||||
LogPrint(eLogWarning, "SOCKS: v4 request failed: ", error);
|
||||
|
@ -329,7 +329,7 @@ namespace proxy
|
|||
{
|
||||
boost::asio::const_buffers_1 response(nullptr,0);
|
||||
//TODO: this should depend on things like the command type and callbacks may change
|
||||
switch (m_socksv)
|
||||
switch (m_socksv)
|
||||
{
|
||||
case SOCKS4:
|
||||
LogPrint(eLogInfo, "SOCKS: v4 connection success");
|
||||
|
@ -348,7 +348,7 @@ namespace proxy
|
|||
}
|
||||
|
||||
void SOCKSHandler::EnterState(SOCKSHandler::state nstate, uint8_t parseleft) {
|
||||
switch (nstate)
|
||||
switch (nstate)
|
||||
{
|
||||
case GET_PORT: parseleft = 2; break;
|
||||
case GET_IPV4: m_addrtype = ADDR_IPV4; m_address.ip = 0; parseleft = 4; break;
|
||||
|
@ -362,9 +362,9 @@ namespace proxy
|
|||
m_state = nstate;
|
||||
}
|
||||
|
||||
bool SOCKSHandler::ValidateSOCKSRequest()
|
||||
bool SOCKSHandler::ValidateSOCKSRequest()
|
||||
{
|
||||
if ( m_cmd != CMD_CONNECT )
|
||||
if ( m_cmd != CMD_CONNECT )
|
||||
{
|
||||
//TODO: we need to support binds and other shit!
|
||||
LogPrint(eLogError, "SOCKS: unsupported command: ", m_cmd);
|
||||
|
@ -372,9 +372,9 @@ namespace proxy
|
|||
return false;
|
||||
}
|
||||
//TODO: we may want to support other address types!
|
||||
if ( m_addrtype != ADDR_DNS )
|
||||
if ( m_addrtype != ADDR_DNS )
|
||||
{
|
||||
switch (m_socksv)
|
||||
switch (m_socksv)
|
||||
{
|
||||
case SOCKS5:
|
||||
LogPrint(eLogError, "SOCKS: v5 unsupported address type: ", m_addrtype);
|
||||
|
@ -392,13 +392,13 @@ namespace proxy
|
|||
bool SOCKSHandler::HandleData(uint8_t *sock_buff, std::size_t len)
|
||||
{
|
||||
assert(len); // This should always be called with a least a byte left to parse
|
||||
while (len > 0)
|
||||
while (len > 0)
|
||||
{
|
||||
switch (m_state)
|
||||
switch (m_state)
|
||||
{
|
||||
case GET_SOCKSV:
|
||||
m_socksv = (SOCKSHandler::socksVersions) *sock_buff;
|
||||
switch (*sock_buff)
|
||||
switch (*sock_buff)
|
||||
{
|
||||
case SOCKS4:
|
||||
EnterState(GET_COMMAND); //Initialize the parser at the right position
|
||||
|
@ -419,14 +419,14 @@ namespace proxy
|
|||
m_parseleft --;
|
||||
if (*sock_buff == AUTH_NONE)
|
||||
m_authchosen = AUTH_NONE;
|
||||
if ( m_parseleft == 0 )
|
||||
if ( m_parseleft == 0 )
|
||||
{
|
||||
if (!Socks5ChooseAuth()) return false;
|
||||
EnterState(GET5_REQUESTV);
|
||||
}
|
||||
break;
|
||||
case GET_COMMAND:
|
||||
switch (*sock_buff)
|
||||
switch (*sock_buff)
|
||||
{
|
||||
case CMD_CONNECT:
|
||||
case CMD_BIND:
|
||||
|
@ -439,7 +439,7 @@ namespace proxy
|
|||
return false;
|
||||
}
|
||||
m_cmd = (SOCKSHandler::cmdTypes)*sock_buff;
|
||||
switch (m_socksv)
|
||||
switch (m_socksv)
|
||||
{
|
||||
case SOCKS5: EnterState(GET5_GETRSV); break;
|
||||
case SOCKS4: EnterState(GET_PORT); break;
|
||||
|
@ -448,9 +448,9 @@ namespace proxy
|
|||
case GET_PORT:
|
||||
m_port = (m_port << 8)|((uint16_t)*sock_buff);
|
||||
m_parseleft--;
|
||||
if (m_parseleft == 0)
|
||||
if (m_parseleft == 0)
|
||||
{
|
||||
switch (m_socksv)
|
||||
switch (m_socksv)
|
||||
{
|
||||
case SOCKS5: EnterState(READY); break;
|
||||
case SOCKS4: EnterState(GET_IPV4); break;
|
||||
|
@ -460,9 +460,9 @@ namespace proxy
|
|||
case GET_IPV4:
|
||||
m_address.ip = (m_address.ip << 8)|((uint32_t)*sock_buff);
|
||||
m_parseleft--;
|
||||
if (m_parseleft == 0)
|
||||
if (m_parseleft == 0)
|
||||
{
|
||||
switch (m_socksv)
|
||||
switch (m_socksv)
|
||||
{
|
||||
case SOCKS5: EnterState(GET_PORT); break;
|
||||
case SOCKS4: EnterState(GET4_IDENT); m_4aip = m_address.ip; break;
|
||||
|
@ -470,7 +470,7 @@ namespace proxy
|
|||
}
|
||||
break;
|
||||
case GET4_IDENT:
|
||||
if (!*sock_buff)
|
||||
if (!*sock_buff)
|
||||
{
|
||||
if( m_4aip == 0 || m_4aip > 255 )
|
||||
EnterState(READY);
|
||||
|
@ -479,12 +479,12 @@ namespace proxy
|
|||
}
|
||||
break;
|
||||
case GET4A_HOST:
|
||||
if (!*sock_buff)
|
||||
if (!*sock_buff)
|
||||
{
|
||||
EnterState(READY);
|
||||
break;
|
||||
}
|
||||
if (m_address.dns.size >= max_socks_hostname_size)
|
||||
if (m_address.dns.size >= max_socks_hostname_size)
|
||||
{
|
||||
LogPrint(eLogError, "SOCKS: v4a req failed: destination is too large");
|
||||
SocksRequestFailed(SOCKS4_FAIL);
|
||||
|
@ -493,7 +493,7 @@ namespace proxy
|
|||
m_address.dns.push_back(*sock_buff);
|
||||
break;
|
||||
case GET5_REQUESTV:
|
||||
if (*sock_buff != SOCKS5)
|
||||
if (*sock_buff != SOCKS5)
|
||||
{
|
||||
LogPrint(eLogError,"SOCKS: v5 rejected unknown request version: ", ((int)*sock_buff));
|
||||
SocksRequestFailed(SOCKS5_GEN_FAIL);
|
||||
|
@ -502,7 +502,7 @@ namespace proxy
|
|||
EnterState(GET_COMMAND);
|
||||
break;
|
||||
case GET5_GETRSV:
|
||||
if ( *sock_buff != 0 )
|
||||
if ( *sock_buff != 0 )
|
||||
{
|
||||
LogPrint(eLogError, "SOCKS: v5 unknown reserved field: ", ((int)*sock_buff));
|
||||
SocksRequestFailed(SOCKS5_GEN_FAIL);
|
||||
|
@ -511,7 +511,7 @@ namespace proxy
|
|||
EnterState(GET5_GETADDRTYPE);
|
||||
break;
|
||||
case GET5_GETADDRTYPE:
|
||||
switch (*sock_buff)
|
||||
switch (*sock_buff)
|
||||
{
|
||||
case ADDR_IPV4: EnterState(GET_IPV4); break;
|
||||
case ADDR_IPV6: EnterState(GET5_IPV6); break;
|
||||
|
@ -542,7 +542,7 @@ namespace proxy
|
|||
}
|
||||
sock_buff++;
|
||||
len--;
|
||||
if (m_state == READY)
|
||||
if (m_state == READY)
|
||||
{
|
||||
m_remaining_data_len = len;
|
||||
m_remaining_data = sock_buff;
|
||||
|
@ -555,16 +555,16 @@ namespace proxy
|
|||
void SOCKSHandler::HandleSockRecv(const boost::system::error_code & ecode, std::size_t len)
|
||||
{
|
||||
LogPrint(eLogDebug, "SOCKS: received ", len, " bytes");
|
||||
if(ecode)
|
||||
if(ecode)
|
||||
{
|
||||
LogPrint(eLogWarning, "SOCKS: recv got error: ", ecode);
|
||||
Terminate();
|
||||
return;
|
||||
}
|
||||
|
||||
if (HandleData(m_sock_buff, len))
|
||||
if (HandleData(m_sock_buff, len))
|
||||
{
|
||||
if (m_state == READY)
|
||||
if (m_state == READY)
|
||||
{
|
||||
const std::string addr = m_address.dns.ToString();
|
||||
LogPrint(eLogInfo, "SOCKS: requested ", addr, ":" , m_port);
|
||||
|
@ -578,10 +578,10 @@ namespace proxy
|
|||
// forward it to upstream proxy
|
||||
ForwardSOCKS();
|
||||
} else {
|
||||
// no upstream proxy
|
||||
// no upstream proxy
|
||||
SocksRequestFailed(SOCKS5_ADDR_UNSUP);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
AsyncSockRead();
|
||||
}
|
||||
|
@ -596,7 +596,7 @@ namespace proxy
|
|||
|
||||
void SOCKSHandler::SentSocksDone(const boost::system::error_code & ecode)
|
||||
{
|
||||
if (!ecode)
|
||||
if (!ecode)
|
||||
{
|
||||
if (Kill()) return;
|
||||
LogPrint (eLogInfo, "SOCKS: new I2PTunnel connection");
|
||||
|
@ -614,7 +614,7 @@ namespace proxy
|
|||
|
||||
void SOCKSHandler::SentSocksResponse(const boost::system::error_code & ecode)
|
||||
{
|
||||
if (ecode)
|
||||
if (ecode)
|
||||
{
|
||||
LogPrint (eLogError, "SOCKS: closing socket after sending reply because: ", ecode.message ());
|
||||
Terminate();
|
||||
|
@ -623,18 +623,18 @@ namespace proxy
|
|||
|
||||
void SOCKSHandler::HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream)
|
||||
{
|
||||
if (stream)
|
||||
if (stream)
|
||||
{
|
||||
m_stream = stream;
|
||||
SocksRequestSuccess();
|
||||
}
|
||||
else
|
||||
}
|
||||
else
|
||||
{
|
||||
LogPrint (eLogError, "SOCKS: error when creating the stream, check the previous warnings for more info");
|
||||
SocksRequestFailed(SOCKS5_HOST_UNREACH);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void SOCKSHandler::ForwardSOCKS()
|
||||
{
|
||||
LogPrint(eLogInfo, "SOCKS: forwarding to upstream");
|
||||
|
@ -656,7 +656,7 @@ namespace proxy
|
|||
SocksRequestFailed(SOCKS5_GEN_FAIL);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void SOCKSHandler::HandleUpstreamSockRecv(const boost::system::error_code & ecode, std::size_t bytes_transfered)
|
||||
{
|
||||
if (ecode) {
|
||||
|
@ -675,7 +675,7 @@ namespace proxy
|
|||
{
|
||||
LogPrint(eLogInfo, "SOCKS: upstream success");
|
||||
boost::asio::const_buffers_1 response(nullptr, 0);
|
||||
switch (m_socksv)
|
||||
switch (m_socksv)
|
||||
{
|
||||
case SOCKS4:
|
||||
LogPrint(eLogInfo, "SOCKS: v4 connection success");
|
||||
|
@ -694,9 +694,9 @@ namespace proxy
|
|||
GetOwner()->AddHandler(forwarder);
|
||||
forwarder->Start();
|
||||
Terminate();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
void SOCKSHandler::HandleUpstreamData(uint8_t * dataptr, std::size_t len)
|
||||
{
|
||||
if (m_state == UPSTREAM_HANDSHAKE) {
|
||||
|
@ -726,7 +726,7 @@ namespace proxy
|
|||
LogPrint(eLogError, "SOCKS: invalid state reading from upstream: ", (int) m_state);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void SOCKSHandler::SendUpstreamRequest()
|
||||
{
|
||||
LogPrint(eLogInfo, "SOCKS: negotiating with upstream proxy");
|
||||
|
@ -739,7 +739,7 @@ namespace proxy
|
|||
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) {
|
||||
|
@ -750,7 +750,7 @@ namespace proxy
|
|||
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) {
|
||||
|
@ -768,9 +768,9 @@ namespace proxy
|
|||
shared_from_this(), std::placeholders::_1, std::placeholders::_2));
|
||||
}
|
||||
|
||||
SOCKSServer::SOCKSServer(const std::string& name, const std::string& address, int port,
|
||||
bool outEnable, const std::string& outAddress, uint16_t outPort,
|
||||
std::shared_ptr<i2p::client::ClientDestination> localDestination) :
|
||||
SOCKSServer::SOCKSServer(const std::string& name, const std::string& address, int port,
|
||||
bool outEnable, const std::string& outAddress, uint16_t outPort,
|
||||
std::shared_ptr<i2p::client::ClientDestination> localDestination) :
|
||||
TCPIPAcceptor (address, port, localDestination ? localDestination : i2p::client::context.GetSharedLocalDestination ()), m_Name (name)
|
||||
{
|
||||
m_UseUpstreamProxy = false;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue