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.
877 lines
28 KiB
877 lines
28 KiB
![]()
10 years ago
|
#include <cassert>
|
||
![]()
9 years ago
|
#include "Base.h"
|
||
![]()
10 years ago
|
#include "Log.h"
|
||
![]()
10 years ago
|
#include "Destination.h"
|
||
![]()
10 years ago
|
#include "ClientContext.h"
|
||
![]()
10 years ago
|
#include "I2PTunnel.h"
|
||
|
|
||
|
namespace i2p
|
||
|
{
|
||
![]()
10 years ago
|
namespace client
|
||
![]()
10 years ago
|
{
|
||
![]()
8 years ago
|
|
||
|
/** set standard socket options */
|
||
|
static void I2PTunnelSetSocketOptions(std::shared_ptr<boost::asio::ip::tcp::socket> socket)
|
||
|
{
|
||
![]()
8 years ago
|
if (socket && socket->is_open())
|
||
|
{
|
||
|
boost::asio::socket_base::receive_buffer_size option(I2P_TUNNEL_CONNECTION_BUFFER_SIZE);
|
||
|
socket->set_option(option);
|
||
|
}
|
||
![]()
8 years ago
|
}
|
||
|
|
||
![]()
10 years ago
|
I2PTunnelConnection::I2PTunnelConnection (I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> socket,
|
||
![]()
10 years ago
|
std::shared_ptr<const i2p::data::LeaseSet> leaseSet, int port):
|
||
![]()
10 years ago
|
I2PServiceHandler(owner), m_Socket (socket), m_RemoteEndpoint (socket->remote_endpoint ()),
|
||
![]()
10 years ago
|
m_IsQuiet (true)
|
||
![]()
10 years ago
|
{
|
||
![]()
10 years ago
|
m_Stream = GetOwner()->GetLocalDestination ()->CreateStream (leaseSet, port);
|
||
![]()
10 years ago
|
}
|
||
|
|
||
![]()
10 years ago
|
I2PTunnelConnection::I2PTunnelConnection (I2PService * owner,
|
||
![]()
10 years ago
|
std::shared_ptr<boost::asio::ip::tcp::socket> socket, std::shared_ptr<i2p::stream::Stream> stream):
|
||
![]()
10 years ago
|
I2PServiceHandler(owner), m_Socket (socket), m_Stream (stream),
|
||
![]()
10 years ago
|
m_RemoteEndpoint (socket->remote_endpoint ()), m_IsQuiet (true)
|
||
|
{
|
||
|
}
|
||
|
|
||
![]()
10 years ago
|
I2PTunnelConnection::I2PTunnelConnection (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream,
|
||
![]()
10 years ago
|
std::shared_ptr<boost::asio::ip::tcp::socket> socket, const boost::asio::ip::tcp::endpoint& target, bool quiet):
|
||
![]()
10 years ago
|
I2PServiceHandler(owner), m_Socket (socket), m_Stream (stream),
|
||
|
m_RemoteEndpoint (target), m_IsQuiet (quiet)
|
||
![]()
10 years ago
|
{
|
||
|
}
|
||
|
|
||
![]()
10 years ago
|
I2PTunnelConnection::~I2PTunnelConnection ()
|
||
|
{
|
||
![]()
10 years ago
|
}
|
||
![]()
8 years ago
|
|
||
![]()
10 years ago
|
void I2PTunnelConnection::I2PConnect (const uint8_t * msg, size_t len)
|
||
![]()
10 years ago
|
{
|
||
![]()
10 years ago
|
if (m_Stream)
|
||
|
{
|
||
|
if (msg)
|
||
|
m_Stream->Send (msg, len); // connect and send
|
||
|
else
|
||
|
m_Stream->Send (m_Buffer, 0); // connect
|
||
|
}
|
||
![]()
10 years ago
|
StreamReceive ();
|
||
|
Receive ();
|
||
|
}
|
||
![]()
8 years ago
|
|
||
|
static boost::asio::ip::address GetLoopbackAddressFor(const i2p::data::IdentHash & addr)
|
||
|
{
|
||
|
boost::asio::ip::address_v4::bytes_type bytes;
|
||
|
const uint8_t * ident = addr;
|
||
|
bytes[0] = 127;
|
||
|
memcpy (bytes.data ()+1, ident, 3);
|
||
|
boost::asio::ip::address ourIP = boost::asio::ip::address_v4 (bytes);
|
||
|
return ourIP;
|
||
|
}
|
||
|
|
||
|
static void MapToLoopback(const std::shared_ptr<boost::asio::ip::tcp::socket> & sock, const i2p::data::IdentHash & addr)
|
||
|
{
|
||
|
|
||
|
// bind to 127.x.x.x address
|
||
|
// where x.x.x are first three bytes from ident
|
||
|
auto ourIP = GetLoopbackAddressFor(addr);
|
||
|
sock->bind (boost::asio::ip::tcp::endpoint (ourIP, 0));
|
||
|
|
||
|
}
|
||
|
|
||
![]()
8 years ago
|
void I2PTunnelConnection::Connect (bool isUniqueLocal)
|
||
![]()
10 years ago
|
{
|
||
![]()
8 years ago
|
I2PTunnelSetSocketOptions(m_Socket);
|
||
![]()
8 years ago
|
if (m_Socket)
|
||
|
{
|
||
![]()
8 years ago
|
#ifdef __linux__
|
||
![]()
8 years ago
|
if (isUniqueLocal && m_RemoteEndpoint.address ().is_v4 () &&
|
||
|
m_RemoteEndpoint.address ().to_v4 ().to_bytes ()[0] == 127)
|
||
![]()
8 years ago
|
{
|
||
|
m_Socket->open (boost::asio::ip::tcp::v4 ());
|
||
![]()
8 years ago
|
auto ident = m_Stream->GetRemoteIdentity()->GetIdentHash();
|
||
|
MapToLoopback(m_Socket, ident);
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
#endif
|
||
![]()
8 years ago
|
m_Socket->async_connect (m_RemoteEndpoint, std::bind (&I2PTunnelConnection::HandleConnect,
|
||
![]()
10 years ago
|
shared_from_this (), std::placeholders::_1));
|
||
![]()
8 years ago
|
}
|
||
![]()
10 years ago
|
}
|
||
|
|
||
![]()
10 years ago
|
void I2PTunnelConnection::Terminate ()
|
||
![]()
10 years ago
|
{
|
||
|
if (Kill()) return;
|
||
![]()
10 years ago
|
if (m_Stream)
|
||
|
{
|
||
|
m_Stream->Close ();
|
||
![]()
10 years ago
|
m_Stream.reset ();
|
||
![]()
10 years ago
|
}
|
||
![]()
8 years ago
|
boost::system::error_code ec;
|
||
|
m_Socket->shutdown(boost::asio::ip::tcp::socket::shutdown_send, ec); // avoid RST
|
||
![]()
10 years ago
|
m_Socket->close ();
|
||
![]()
8 years ago
|
|
||
![]()
10 years ago
|
Done(shared_from_this ());
|
||
![]()
10 years ago
|
}
|
||
|
|
||
|
void I2PTunnelConnection::Receive ()
|
||
|
{
|
||
|
m_Socket->async_read_some (boost::asio::buffer(m_Buffer, I2P_TUNNEL_CONNECTION_BUFFER_SIZE),
|
||
![]()
10 years ago
|
std::bind(&I2PTunnelConnection::HandleReceived, shared_from_this (),
|
||
|
std::placeholders::_1, std::placeholders::_2));
|
||
![]()
10 years ago
|
}
|
||
|
|
||
|
void I2PTunnelConnection::HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
||
|
{
|
||
|
if (ecode)
|
||
![]()
10 years ago
|
{
|
||
![]()
10 years ago
|
if (ecode != boost::asio::error::operation_aborted)
|
||
![]()
8 years ago
|
{
|
||
|
LogPrint (eLogError, "I2PTunnel: read error: ", ecode.message ());
|
||
![]()
10 years ago
|
Terminate ();
|
||
![]()
8 years ago
|
}
|
||
![]()
10 years ago
|
}
|
||
|
else
|
||
![]()
10 years ago
|
{
|
||
![]()
10 years ago
|
if (m_Stream)
|
||
![]()
10 years ago
|
{
|
||
|
auto s = shared_from_this ();
|
||
|
m_Stream->AsyncSend (m_Buffer, bytes_transferred,
|
||
|
[s](const boost::system::error_code& ecode)
|
||
|
{
|
||
|
if (!ecode)
|
||
|
s->Receive ();
|
||
|
else
|
||
|
s->Terminate ();
|
||
|
});
|
||
|
}
|
||
![]()
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void I2PTunnelConnection::HandleWrite (const boost::system::error_code& ecode)
|
||
|
{
|
||
![]()
10 years ago
|
if (ecode)
|
||
|
{
|
||
![]()
9 years ago
|
LogPrint (eLogError, "I2PTunnel: write error: ", ecode.message ());
|
||
![]()
10 years ago
|
if (ecode != boost::asio::error::operation_aborted)
|
||
|
Terminate ();
|
||
![]()
10 years ago
|
}
|
||
|
else
|
||
|
StreamReceive ();
|
||
![]()
10 years ago
|
}
|
||
|
|
||
|
void I2PTunnelConnection::StreamReceive ()
|
||
|
{
|
||
|
if (m_Stream)
|
||
![]()
9 years ago
|
{
|
||
|
if (m_Stream->GetStatus () == i2p::stream::eStreamStatusNew ||
|
||
|
m_Stream->GetStatus () == i2p::stream::eStreamStatusOpen) // regular
|
||
|
{
|
||
|
m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, I2P_TUNNEL_CONNECTION_BUFFER_SIZE),
|
||
|
std::bind (&I2PTunnelConnection::HandleStreamReceive, shared_from_this (),
|
||
|
std::placeholders::_1, std::placeholders::_2),
|
||
|
I2P_TUNNEL_CONNECTION_MAX_IDLE);
|
||
|
}
|
||
|
else // closed by peer
|
||
|
{
|
||
|
// get remaning data
|
||
|
auto len = m_Stream->ReadSome (m_StreamBuffer, I2P_TUNNEL_CONNECTION_BUFFER_SIZE);
|
||
|
if (len > 0) // still some data
|
||
|
Write (m_StreamBuffer, len);
|
||
|
else // no more data
|
||
|
Terminate ();
|
||
|
}
|
||
|
}
|
||
![]()
10 years ago
|
}
|
||
|
|
||
![]()
10 years ago
|
void I2PTunnelConnection::HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
||
|
{
|
||
|
if (ecode)
|
||
|
{
|
||
![]()
10 years ago
|
if (ecode != boost::asio::error::operation_aborted)
|
||
![]()
9 years ago
|
{
|
||
![]()
8 years ago
|
LogPrint (eLogError, "I2PTunnel: stream read error: ", ecode.message ());
|
||
![]()
9 years ago
|
if (bytes_transferred > 0)
|
||
|
Write (m_StreamBuffer, bytes_transferred); // postpone termination
|
||
![]()
8 years ago
|
else if (ecode == boost::asio::error::timed_out && m_Stream && m_Stream->IsOpen ())
|
||
8 years ago
|
StreamReceive ();
|
||
|
else
|
||
![]()
9 years ago
|
Terminate ();
|
||
![]()
8 years ago
|
}
|
||
|
else
|
||
8 years ago
|
Terminate ();
|
||
![]()
10 years ago
|
}
|
||
|
else
|
||
![]()
9 years ago
|
Write (m_StreamBuffer, bytes_transferred);
|
||
|
}
|
||
|
|
||
|
void I2PTunnelConnection::Write (const uint8_t * buf, size_t len)
|
||
|
{
|
||
![]()
9 years ago
|
boost::asio::async_write (*m_Socket, boost::asio::buffer (buf, len), boost::asio::transfer_all (),
|
||
![]()
9 years ago
|
std::bind (&I2PTunnelConnection::HandleWrite, shared_from_this (), std::placeholders::_1));
|
||
![]()
10 years ago
|
}
|
||
|
|
||
![]()
10 years ago
|
void I2PTunnelConnection::HandleConnect (const boost::system::error_code& ecode)
|
||
|
{
|
||
|
if (ecode)
|
||
|
{
|
||
![]()
9 years ago
|
LogPrint (eLogError, "I2PTunnel: connect error: ", ecode.message ());
|
||
![]()
10 years ago
|
Terminate ();
|
||
![]()
10 years ago
|
}
|
||
|
else
|
||
|
{
|
||
![]()
9 years ago
|
LogPrint (eLogDebug, "I2PTunnel: connected");
|
||
![]()
10 years ago
|
if (m_IsQuiet)
|
||
|
StreamReceive ();
|
||
|
else
|
||
|
{
|
||
|
// send destination first like received from I2P
|
||
![]()
9 years ago
|
std::string dest = m_Stream->GetRemoteIdentity ()->ToBase64 ();
|
||
![]()
10 years ago
|
dest += "\n";
|
||
![]()
8 years ago
|
if(sizeof(m_StreamBuffer) >= dest.size()) {
|
||
|
memcpy (m_StreamBuffer, dest.c_str (), dest.size ());
|
||
|
}
|
||
![]()
10 years ago
|
HandleStreamReceive (boost::system::error_code (), dest.size ());
|
||
|
}
|
||
![]()
10 years ago
|
Receive ();
|
||
|
}
|
||
|
}
|
||
|
|
||
![]()
8 years ago
|
void I2PClientTunnelConnectionHTTP::Write (const uint8_t * buf, size_t len)
|
||
|
{
|
||
|
if (m_HeaderSent)
|
||
|
I2PTunnelConnection::Write (buf, len);
|
||
|
else
|
||
|
{
|
||
|
m_InHeader.clear ();
|
||
|
m_InHeader.write ((const char *)buf, len);
|
||
|
std::string line;
|
||
|
bool endOfHeader = false;
|
||
|
while (!endOfHeader)
|
||
|
{
|
||
|
std::getline(m_InHeader, line);
|
||
|
if (!m_InHeader.fail ())
|
||
|
{
|
||
|
if (line == "\r") endOfHeader = true;
|
||
|
else
|
||
|
{
|
||
|
if (!m_ConnectionSent && !line.compare(0, 10, "Connection"))
|
||
|
{
|
||
|
m_OutHeader << "Connection: close\r\n";
|
||
|
m_ConnectionSent = true;
|
||
|
}
|
||
|
else if (!m_ProxyConnectionSent && !line.compare(0, 16, "Proxy-Connection"))
|
||
|
{
|
||
|
m_OutHeader << "Proxy-Connection: close\r\n";
|
||
|
m_ProxyConnectionSent = true;
|
||
|
}
|
||
|
else
|
||
|
m_OutHeader << line << "\n";
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
if (endOfHeader)
|
||
|
{
|
||
|
if (!m_ConnectionSent) m_OutHeader << "Connection: close\r\n";
|
||
|
if (!m_ProxyConnectionSent) m_OutHeader << "Proxy-Connection: close\r\n";
|
||
|
m_OutHeader << "\r\n"; // end of header
|
||
|
m_OutHeader << m_InHeader.str ().substr (m_InHeader.tellg ()); // data right after header
|
||
|
m_InHeader.str ("");
|
||
|
m_HeaderSent = true;
|
||
|
I2PTunnelConnection::Write ((uint8_t *)m_OutHeader.str ().c_str (), m_OutHeader.str ().length ());
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
I2PServerTunnelConnectionHTTP::I2PServerTunnelConnectionHTTP (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream,
|
||
![]()
9 years ago
|
std::shared_ptr<boost::asio::ip::tcp::socket> socket,
|
||
|
const boost::asio::ip::tcp::endpoint& target, const std::string& host):
|
||
![]()
9 years ago
|
I2PTunnelConnection (owner, stream, socket, target), m_Host (host), m_HeaderSent (false), m_From (stream->GetRemoteIdentity ())
|
||
![]()
9 years ago
|
{
|
||
|
}
|
||
|
|
||
![]()
8 years ago
|
void I2PServerTunnelConnectionHTTP::Write (const uint8_t * buf, size_t len)
|
||
![]()
9 years ago
|
{
|
||
|
if (m_HeaderSent)
|
||
|
I2PTunnelConnection::Write (buf, len);
|
||
|
else
|
||
![]()
9 years ago
|
{
|
||
|
m_InHeader.clear ();
|
||
|
m_InHeader.write ((const char *)buf, len);
|
||
|
std::string line;
|
||
|
bool endOfHeader = false;
|
||
|
while (!endOfHeader)
|
||
|
{
|
||
|
std::getline(m_InHeader, line);
|
||
|
if (!m_InHeader.fail ())
|
||
|
{
|
||
|
if (line == "\r") endOfHeader = true;
|
||
![]()
9 years ago
|
else
|
||
|
{
|
||
![]()
8 years ago
|
if (m_Host.length () > 0 && line.find ("Host:") != std::string::npos)
|
||
|
m_OutHeader << "Host: " << m_Host << "\r\n"; // override host
|
||
![]()
9 years ago
|
else
|
||
|
m_OutHeader << line << "\n";
|
||
|
}
|
||
![]()
9 years ago
|
}
|
||
|
else
|
||
|
break;
|
||
|
}
|
||
![]()
9 years ago
|
// add X-I2P fields
|
||
|
if (m_From)
|
||
|
{
|
||
![]()
9 years ago
|
m_OutHeader << X_I2P_DEST_B32 << ": " << context.GetAddressBook ().ToAddress(m_From->GetIdentHash ()) << "\r\n";
|
||
![]()
9 years ago
|
m_OutHeader << X_I2P_DEST_HASH << ": " << m_From->GetIdentHash ().ToBase64 () << "\r\n";
|
||
![]()
9 years ago
|
m_OutHeader << X_I2P_DEST_B64 << ": " << m_From->ToBase64 () << "\r\n";
|
||
![]()
9 years ago
|
}
|
||
![]()
9 years ago
|
|
||
|
if (endOfHeader)
|
||
|
{
|
||
![]()
9 years ago
|
m_OutHeader << "\r\n"; // end of header
|
||
![]()
9 years ago
|
m_OutHeader << m_InHeader.str ().substr (m_InHeader.tellg ()); // data right after header
|
||
![]()
8 years ago
|
m_InHeader.str ("");
|
||
![]()
9 years ago
|
m_HeaderSent = true;
|
||
|
I2PTunnelConnection::Write ((uint8_t *)m_OutHeader.str ().c_str (), m_OutHeader.str ().length ());
|
||
|
}
|
||
![]()
9 years ago
|
}
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
I2PTunnelConnectionIRC::I2PTunnelConnectionIRC (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream,
|
||
|
std::shared_ptr<boost::asio::ip::tcp::socket> socket,
|
||
![]()
9 years ago
|
const boost::asio::ip::tcp::endpoint& target, const std::string& webircpass):
|
||
|
I2PTunnelConnection (owner, stream, socket, target), m_From (stream->GetRemoteIdentity ()),
|
||
![]()
9 years ago
|
m_NeedsWebIrc (webircpass.length() ? true : false), m_WebircPass (webircpass)
|
||
![]()
9 years ago
|
{
|
||
|
}
|
||
|
|
||
|
void I2PTunnelConnectionIRC::Write (const uint8_t * buf, size_t len)
|
||
|
{
|
||
![]()
8 years ago
|
m_OutPacket.str ("");
|
||
|
if (m_NeedsWebIrc)
|
||
|
{
|
||
![]()
9 years ago
|
m_NeedsWebIrc = false;
|
||
![]()
8 years ago
|
m_OutPacket << "WEBIRC " << m_WebircPass << " cgiirc " << context.GetAddressBook ().ToAddress (m_From->GetIdentHash ()) << " " << GetSocket ()->local_endpoint ().address () << std::endl;
|
||
![]()
9 years ago
|
}
|
||
|
|
||
![]()
9 years ago
|
m_InPacket.clear ();
|
||
![]()
9 years ago
|
m_InPacket.write ((const char *)buf, len);
|
||
|
|
||
|
while (!m_InPacket.eof () && !m_InPacket.fail ())
|
||
|
{
|
||
![]()
8 years ago
|
std::string line;
|
||
![]()
9 years ago
|
std::getline (m_InPacket, line);
|
||
![]()
8 years ago
|
if (line.length () == 0 && m_InPacket.eof ())
|
||
![]()
9 years ago
|
m_InPacket.str ("");
|
||
![]()
9 years ago
|
auto pos = line.find ("USER");
|
||
![]()
8 years ago
|
if (!pos) // start of line
|
||
![]()
9 years ago
|
{
|
||
|
pos = line.find (" ");
|
||
|
pos++;
|
||
|
pos = line.find (" ", pos);
|
||
|
pos++;
|
||
|
auto nextpos = line.find (" ", pos);
|
||
|
m_OutPacket << line.substr (0, pos);
|
||
|
m_OutPacket << context.GetAddressBook ().ToAddress (m_From->GetIdentHash ());
|
||
|
m_OutPacket << line.substr (nextpos) << '\n';
|
||
![]()
8 years ago
|
}
|
||
|
else
|
||
![]()
9 years ago
|
m_OutPacket << line << '\n';
|
||
![]()
9 years ago
|
}
|
||
|
I2PTunnelConnection::Write ((uint8_t *)m_OutPacket.str ().c_str (), m_OutPacket.str ().length ());
|
||
|
}
|
||
|
|
||
|
|
||
![]()
10 years ago
|
/* This handler tries to stablish a connection with the desired server and dies if it fails to do so */
|
||
|
class I2PClientTunnelHandler: public I2PServiceHandler, public std::enable_shared_from_this<I2PClientTunnelHandler>
|
||
|
{
|
||
|
public:
|
||
|
I2PClientTunnelHandler (I2PClientTunnel * parent, i2p::data::IdentHash destination,
|
||
![]()
10 years ago
|
int destinationPort, std::shared_ptr<boost::asio::ip::tcp::socket> socket):
|
||
![]()
10 years ago
|
I2PServiceHandler(parent), m_DestinationIdentHash(destination),
|
||
|
m_DestinationPort (destinationPort), m_Socket(socket) {};
|
||
![]()
10 years ago
|
void Handle();
|
||
|
void Terminate();
|
||
|
private:
|
||
|
void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream);
|
||
|
i2p::data::IdentHash m_DestinationIdentHash;
|
||
![]()
10 years ago
|
int m_DestinationPort;
|
||
![]()
10 years ago
|
std::shared_ptr<boost::asio::ip::tcp::socket> m_Socket;
|
||
![]()
10 years ago
|
};
|
||
|
|
||
|
void I2PClientTunnelHandler::Handle()
|
||
|
{
|
||
![]()
10 years ago
|
GetOwner()->GetLocalDestination ()->CreateStream (
|
||
|
std::bind (&I2PClientTunnelHandler::HandleStreamRequestComplete, shared_from_this(), std::placeholders::_1),
|
||
|
m_DestinationIdentHash, m_DestinationPort);
|
||
![]()
10 years ago
|
}
|
||
|
|
||
|
void I2PClientTunnelHandler::HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream)
|
||
|
{
|
||
|
if (stream)
|
||
|
{
|
||
|
if (Kill()) return;
|
||
![]()
9 years ago
|
LogPrint (eLogDebug, "I2PTunnel: new connection");
|
||
![]()
10 years ago
|
auto connection = std::make_shared<I2PTunnelConnection>(GetOwner(), m_Socket, stream);
|
||
|
GetOwner()->AddHandler (connection);
|
||
|
connection->I2PConnect ();
|
||
|
Done(shared_from_this());
|
||
|
}
|
||
|
else
|
||
|
{
|
||
![]()
9 years ago
|
LogPrint (eLogError, "I2PTunnel: Client Tunnel Issue when creating the stream, check the previous warnings for more info.");
|
||
![]()
10 years ago
|
Terminate();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void I2PClientTunnelHandler::Terminate()
|
||
|
{
|
||
|
if (Kill()) return;
|
||
|
if (m_Socket)
|
||
|
{
|
||
|
m_Socket->close();
|
||
|
m_Socket = nullptr;
|
||
|
}
|
||
|
Done(shared_from_this());
|
||
|
}
|
||
|
|
||
![]()
9 years ago
|
I2PClientTunnel::I2PClientTunnel (const std::string& name, const std::string& destination,
|
||
|
const std::string& address, int port, std::shared_ptr<ClientDestination> localDestination, int destinationPort):
|
||
|
TCPIPAcceptor (address, port, localDestination), m_Name (name), m_Destination (destination),
|
||
|
m_DestinationIdentHash (nullptr), m_DestinationPort (destinationPort)
|
||
|
{
|
||
|
}
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
void I2PClientTunnel::Start ()
|
||
|
{
|
||
![]()
10 years ago
|
TCPIPAcceptor::Start ();
|
||
![]()
10 years ago
|
GetIdentHash();
|
||
![]()
10 years ago
|
}
|
||
|
|
||
|
void I2PClientTunnel::Stop ()
|
||
|
{
|
||
![]()
10 years ago
|
TCPIPAcceptor::Stop();
|
||
![]()
10 years ago
|
auto *originalIdentHash = m_DestinationIdentHash;
|
||
![]()
10 years ago
|
m_DestinationIdentHash = nullptr;
|
||
![]()
10 years ago
|
delete originalIdentHash;
|
||
![]()
10 years ago
|
}
|
||
|
|
||
![]()
10 years ago
|
/* HACK: maybe we should create a caching IdentHash provider in AddressBook */
|
||
|
const i2p::data::IdentHash * I2PClientTunnel::GetIdentHash ()
|
||
|
{
|
||
|
if (!m_DestinationIdentHash)
|
||
|
{
|
||
|
i2p::data::IdentHash identHash;
|
||
|
if (i2p::client::context.GetAddressBook ().GetIdentHash (m_Destination, identHash))
|
||
|
m_DestinationIdentHash = new i2p::data::IdentHash (identHash);
|
||
|
else
|
||
![]()
9 years ago
|
LogPrint (eLogWarning, "I2PTunnel: Remote destination ", m_Destination, " not found");
|
||
![]()
10 years ago
|
}
|
||
|
return m_DestinationIdentHash;
|
||
|
}
|
||
|
|
||
![]()
10 years ago
|
std::shared_ptr<I2PServiceHandler> I2PClientTunnel::CreateHandler(std::shared_ptr<boost::asio::ip::tcp::socket> socket)
|
||
![]()
10 years ago
|
{
|
||
![]()
10 years ago
|
const i2p::data::IdentHash *identHash = GetIdentHash();
|
||
|
if (identHash)
|
||
![]()
10 years ago
|
return std::make_shared<I2PClientTunnelHandler>(this, *identHash, m_DestinationPort, socket);
|
||
![]()
10 years ago
|
else
|
||
![]()
10 years ago
|
return nullptr;
|
||
![]()
10 years ago
|
}
|
||
|
|
||
![]()
9 years ago
|
I2PServerTunnel::I2PServerTunnel (const std::string& name, const std::string& address,
|
||
![]()
9 years ago
|
int port, std::shared_ptr<ClientDestination> localDestination, int inport, bool gzip):
|
||
![]()
8 years ago
|
I2PService (localDestination), m_IsUniqueLocal(true), m_Name (name), m_Address (address), m_Port (port), m_IsAccessList (false)
|
||
![]()
10 years ago
|
{
|
||
![]()
9 years ago
|
m_PortDestination = localDestination->CreateStreamingDestination (inport > 0 ? inport : port, gzip);
|
||
![]()
10 years ago
|
}
|
||
![]()
8 years ago
|
|
||
![]()
10 years ago
|
void I2PServerTunnel::Start ()
|
||
|
{
|
||
![]()
9 years ago
|
m_Endpoint.port (m_Port);
|
||
|
boost::system::error_code ec;
|
||
|
auto addr = boost::asio::ip::address::from_string (m_Address, ec);
|
||
|
if (!ec)
|
||
|
{
|
||
|
m_Endpoint.address (addr);
|
||
|
Accept ();
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
auto resolver = std::make_shared<boost::asio::ip::tcp::resolver>(GetService ());
|
||
|
resolver->async_resolve (boost::asio::ip::tcp::resolver::query (m_Address, ""),
|
||
|
std::bind (&I2PServerTunnel::HandleResolve, this,
|
||
|
std::placeholders::_1, std::placeholders::_2, resolver));
|
||
|
}
|
||
![]()
10 years ago
|
}
|
||
|
|
||
|
void I2PServerTunnel::Stop ()
|
||
|
{
|
||
![]()
10 years ago
|
ClearHandlers ();
|
||
![]()
10 years ago
|
}
|
||
|
|
||
![]()
9 years ago
|
void I2PServerTunnel::HandleResolve (const boost::system::error_code& ecode, boost::asio::ip::tcp::resolver::iterator it,
|
||
![]()
8 years ago
|
std::shared_ptr<boost::asio::ip::tcp::resolver> resolver)
|
||
![]()
9 years ago
|
{
|
||
|
if (!ecode)
|
||
|
{
|
||
|
auto addr = (*it).endpoint ().address ();
|
||
![]()
9 years ago
|
LogPrint (eLogInfo, "I2PTunnel: server tunnel ", (*it).host_name (), " has been resolved to ", addr);
|
||
![]()
9 years ago
|
m_Endpoint.address (addr);
|
||
|
Accept ();
|
||
|
}
|
||
|
else
|
||
![]()
9 years ago
|
LogPrint (eLogError, "I2PTunnel: Unable to resolve server tunnel address: ", ecode.message ());
|
||
![]()
9 years ago
|
}
|
||
|
|
||
![]()
10 years ago
|
void I2PServerTunnel::SetAccessList (const std::set<i2p::data::IdentHash>& accessList)
|
||
|
{
|
||
|
m_AccessList = accessList;
|
||
|
m_IsAccessList = true;
|
||
|
}
|
||
|
|
||
![]()
10 years ago
|
void I2PServerTunnel::Accept ()
|
||
|
{
|
||
![]()
10 years ago
|
if (m_PortDestination)
|
||
|
m_PortDestination->SetAcceptor (std::bind (&I2PServerTunnel::HandleAccept, this, std::placeholders::_1));
|
||
|
|
||
![]()
10 years ago
|
auto localDestination = GetLocalDestination ();
|
||
|
if (localDestination)
|
||
![]()
10 years ago
|
{
|
||
|
if (!localDestination->IsAcceptingStreams ()) // set it as default if not set yet
|
||
|
localDestination->AcceptStreams (std::bind (&I2PServerTunnel::HandleAccept, this, std::placeholders::_1));
|
||
|
}
|
||
![]()
10 years ago
|
else
|
||
![]()
9 years ago
|
LogPrint (eLogError, "I2PTunnel: Local destination not set for server tunnel");
|
||
![]()
10 years ago
|
}
|
||
|
|
||
![]()
10 years ago
|
void I2PServerTunnel::HandleAccept (std::shared_ptr<i2p::stream::Stream> stream)
|
||
![]()
10 years ago
|
{
|
||
|
if (stream)
|
||
![]()
10 years ago
|
{
|
||
![]()
10 years ago
|
if (m_IsAccessList)
|
||
|
{
|
||
![]()
9 years ago
|
if (!m_AccessList.count (stream->GetRemoteIdentity ()->GetIdentHash ()))
|
||
![]()
10 years ago
|
{
|
||
![]()
9 years ago
|
LogPrint (eLogWarning, "I2PTunnel: Address ", stream->GetRemoteIdentity ()->GetIdentHash ().ToBase32 (), " is not in white list. Incoming connection dropped");
|
||
![]()
10 years ago
|
stream->Close ();
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
![]()
8 years ago
|
// new connection
|
||
|
auto conn = CreateI2PConnection (stream);
|
||
|
AddHandler (conn);
|
||
|
conn->Connect (m_IsUniqueLocal);
|
||
![]()
10 years ago
|
}
|
||
![]()
10 years ago
|
}
|
||
![]()
9 years ago
|
|
||
![]()
8 years ago
|
std::shared_ptr<I2PTunnelConnection> I2PServerTunnel::CreateI2PConnection (std::shared_ptr<i2p::stream::Stream> stream)
|
||
![]()
9 years ago
|
{
|
||
![]()
8 years ago
|
return std::make_shared<I2PTunnelConnection> (this, stream, std::make_shared<boost::asio::ip::tcp::socket> (GetService ()), GetEndpoint ());
|
||
|
|
||
![]()
9 years ago
|
}
|
||
|
|
||
![]()
9 years ago
|
I2PServerTunnelHTTP::I2PServerTunnelHTTP (const std::string& name, const std::string& address,
|
||
![]()
9 years ago
|
int port, std::shared_ptr<ClientDestination> localDestination,
|
||
![]()
9 years ago
|
const std::string& host, int inport, bool gzip):
|
||
|
I2PServerTunnel (name, address, port, localDestination, inport, gzip),
|
||
![]()
8 years ago
|
m_Host (host)
|
||
![]()
9 years ago
|
{
|
||
|
}
|
||
|
|
||
![]()
8 years ago
|
std::shared_ptr<I2PTunnelConnection> I2PServerTunnelHTTP::CreateI2PConnection (std::shared_ptr<i2p::stream::Stream> stream)
|
||
![]()
9 years ago
|
{
|
||
![]()
8 years ago
|
return std::make_shared<I2PServerTunnelConnectionHTTP> (this, stream,
|
||
![]()
9 years ago
|
std::make_shared<boost::asio::ip::tcp::socket> (GetService ()), GetEndpoint (), m_Host);
|
||
![]()
9 years ago
|
}
|
||
![]()
9 years ago
|
|
||
|
I2PServerTunnelIRC::I2PServerTunnelIRC (const std::string& name, const std::string& address,
|
||
![]()
9 years ago
|
int port, std::shared_ptr<ClientDestination> localDestination,
|
||
|
const std::string& webircpass, int inport, bool gzip):
|
||
|
I2PServerTunnel (name, address, port, localDestination, inport, gzip),
|
||
|
m_WebircPass (webircpass)
|
||
![]()
9 years ago
|
{
|
||
|
}
|
||
|
|
||
![]()
8 years ago
|
std::shared_ptr<I2PTunnelConnection> I2PServerTunnelIRC::CreateI2PConnection (std::shared_ptr<i2p::stream::Stream> stream)
|
||
![]()
9 years ago
|
{
|
||
![]()
8 years ago
|
return std::make_shared<I2PTunnelConnectionIRC> (this, stream, std::make_shared<boost::asio::ip::tcp::socket> (GetService ()), GetEndpoint (), this->m_WebircPass);
|
||
![]()
9 years ago
|
}
|
||
![]()
9 years ago
|
|
||
![]()
8 years ago
|
void I2PUDPServerTunnel::HandleRecvFromI2P(const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len)
|
||
|
{
|
||
|
std::lock_guard<std::mutex> lock(m_SessionsMutex);
|
||
![]()
8 years ago
|
auto session = ObtainUDPSession(from, toPort, fromPort);
|
||
|
session->IPSocket.send_to(boost::asio::buffer(buf, len), m_RemoteEndpoint);
|
||
|
session->LastActivity = i2p::util::GetMillisecondsSinceEpoch();
|
||
![]()
8 years ago
|
}
|
||
|
|
||
|
void I2PUDPServerTunnel::ExpireStale(const uint64_t delta) {
|
||
|
std::lock_guard<std::mutex> lock(m_SessionsMutex);
|
||
|
uint64_t now = i2p::util::GetMillisecondsSinceEpoch();
|
||
![]()
8 years ago
|
auto itr = m_Sessions.begin();
|
||
|
while(itr != m_Sessions.end()) {
|
||
|
if(now - (*itr)->LastActivity >= delta )
|
||
|
itr = m_Sessions.erase(itr);
|
||
|
else
|
||
|
++itr;
|
||
|
}
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
|
||
|
void I2PUDPClientTunnel::ExpireStale(const uint64_t delta) {
|
||
|
std::lock_guard<std::mutex> lock(m_SessionsMutex);
|
||
|
uint64_t now = i2p::util::GetMillisecondsSinceEpoch();
|
||
|
std::vector<uint16_t> removePorts;
|
||
|
for (const auto & s : m_Sessions) {
|
||
![]()
8 years ago
|
if (now - s.second.second >= delta)
|
||
![]()
8 years ago
|
removePorts.push_back(s.first);
|
||
|
}
|
||
|
for(auto port : removePorts) {
|
||
|
m_Sessions.erase(port);
|
||
|
}
|
||
|
}
|
||
|
|
||
![]()
8 years ago
|
UDPSessionPtr I2PUDPServerTunnel::ObtainUDPSession(const i2p::data::IdentityEx& from, uint16_t localPort, uint16_t remotePort)
|
||
![]()
8 years ago
|
{
|
||
|
auto ih = from.GetIdentHash();
|
||
![]()
8 years ago
|
for (auto & s : m_Sessions )
|
||
![]()
8 years ago
|
{
|
||
![]()
8 years ago
|
if ( s->Identity == ih)
|
||
![]()
8 years ago
|
{
|
||
|
/** found existing session */
|
||
![]()
8 years ago
|
LogPrint(eLogDebug, "UDPServer: found session ", s->IPSocket.local_endpoint(), " ", ih.ToBase32());
|
||
![]()
8 years ago
|
return s;
|
||
|
}
|
||
|
}
|
||
![]()
8 years ago
|
boost::asio::ip::address addr;
|
||
|
/** create new udp session */
|
||
![]()
8 years ago
|
if(m_IsUniqueLocal && m_LocalAddress.is_loopback())
|
||
|
{
|
||
![]()
8 years ago
|
auto ident = from.GetIdentHash();
|
||
|
addr = GetLoopbackAddressFor(ident);
|
||
![]()
8 years ago
|
}
|
||
|
else
|
||
![]()
8 years ago
|
addr = m_LocalAddress;
|
||
|
boost::asio::ip::udp::endpoint ep(addr, 0);
|
||
![]()
8 years ago
|
m_Sessions.push_back(std::make_shared<UDPSession>(ep, m_LocalDest, m_RemoteEndpoint, &ih, localPort, remotePort));
|
||
![]()
8 years ago
|
auto & back = m_Sessions.back();
|
||
|
return back;
|
||
![]()
8 years ago
|
}
|
||
|
|
||
![]()
8 years ago
|
UDPSession::UDPSession(boost::asio::ip::udp::endpoint localEndpoint,
|
||
|
const std::shared_ptr<i2p::client::ClientDestination> & localDestination,
|
||
![]()
8 years ago
|
boost::asio::ip::udp::endpoint endpoint, const i2p::data::IdentHash * to,
|
||
![]()
8 years ago
|
uint16_t ourPort, uint16_t theirPort) :
|
||
![]()
8 years ago
|
m_Destination(localDestination->GetDatagramDestination()),
|
||
![]()
8 years ago
|
IPSocket(localDestination->GetService(), localEndpoint),
|
||
|
SendEndpoint(endpoint),
|
||
|
LastActivity(i2p::util::GetMillisecondsSinceEpoch()),
|
||
|
LocalPort(ourPort),
|
||
|
RemotePort(theirPort)
|
||
|
{
|
||
![]()
8 years ago
|
memcpy(Identity, to->data(), 32);
|
||
![]()
8 years ago
|
Receive();
|
||
|
}
|
||
![]()
8 years ago
|
|
||
|
|
||
![]()
8 years ago
|
void UDPSession::Receive() {
|
||
|
LogPrint(eLogDebug, "UDPSession: Receive");
|
||
|
IPSocket.async_receive_from(boost::asio::buffer(m_Buffer, I2P_UDP_MAX_MTU),
|
||
|
FromEndpoint, std::bind(&UDPSession::HandleReceived, this, std::placeholders::_1, std::placeholders::_2));
|
||
|
}
|
||
|
|
||
|
void UDPSession::HandleReceived(const boost::system::error_code & ecode, std::size_t len)
|
||
|
{
|
||
|
if(!ecode)
|
||
|
{
|
||
|
LogPrint(eLogDebug, "UDPSession: forward ", len, "B from ", FromEndpoint);
|
||
|
LastActivity = i2p::util::GetMillisecondsSinceEpoch();
|
||
![]()
8 years ago
|
m_Destination->SendDatagramTo(m_Buffer, len, Identity, LocalPort, RemotePort);
|
||
![]()
8 years ago
|
Receive();
|
||
|
} else {
|
||
|
LogPrint(eLogError, "UDPSession: ", ecode.message());
|
||
|
}
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
|
||
|
|
||
|
I2PUDPServerTunnel::I2PUDPServerTunnel(const std::string & name, std::shared_ptr<i2p::client::ClientDestination> localDestination,
|
||
![]()
8 years ago
|
boost::asio::ip::address localAddress, boost::asio::ip::udp::endpoint forwardTo, uint16_t port) :
|
||
![]()
8 years ago
|
m_IsUniqueLocal(true),
|
||
![]()
8 years ago
|
m_Name(name),
|
||
|
m_LocalAddress(localAddress),
|
||
|
m_RemoteEndpoint(forwardTo)
|
||
|
{
|
||
|
m_LocalDest = localDestination;
|
||
|
m_LocalDest->Start();
|
||
|
auto dgram = m_LocalDest->CreateDatagramDestination();
|
||
|
dgram->SetReceiver(std::bind(&I2PUDPServerTunnel::HandleRecvFromI2P, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5));
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
I2PUDPServerTunnel::~I2PUDPServerTunnel()
|
||
|
{
|
||
|
auto dgram = m_LocalDest->GetDatagramDestination();
|
||
|
if (dgram) dgram->ResetReceiver();
|
||
|
|
||
|
LogPrint(eLogInfo, "UDPServer: done");
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
void I2PUDPServerTunnel::Start() {
|
||
|
m_LocalDest->Start();
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
std::vector<std::shared_ptr<DatagramSessionInfo> > I2PUDPServerTunnel::GetSessions()
|
||
![]()
8 years ago
|
{
|
||
![]()
8 years ago
|
std::vector<std::shared_ptr<DatagramSessionInfo> > sessions;
|
||
![]()
8 years ago
|
std::lock_guard<std::mutex> lock(m_SessionsMutex);
|
||
![]()
8 years ago
|
|
||
|
for ( UDPSessionPtr s : m_Sessions )
|
||
![]()
8 years ago
|
{
|
||
![]()
8 years ago
|
if (!s->m_Destination) continue;
|
||
|
auto info = s->m_Destination->GetInfoForRemote(s->Identity);
|
||
![]()
8 years ago
|
if(!info) continue;
|
||
![]()
8 years ago
|
|
||
|
auto sinfo = std::make_shared<DatagramSessionInfo>();
|
||
|
sinfo->Name = m_Name;
|
||
|
sinfo->LocalIdent = std::make_shared<i2p::data::IdentHash>(m_LocalDest->GetIdentHash().data());
|
||
![]()
8 years ago
|
sinfo->RemoteIdent = std::make_shared<i2p::data::IdentHash>(s->Identity.data());
|
||
![]()
8 years ago
|
sinfo->CurrentIBGW = info->IBGW;
|
||
|
sinfo->CurrentOBEP = info->OBEP;
|
||
|
sessions.push_back(sinfo);
|
||
![]()
8 years ago
|
}
|
||
|
return sessions;
|
||
|
}
|
||
![]()
8 years ago
|
|
||
|
I2PUDPClientTunnel::I2PUDPClientTunnel(const std::string & name, const std::string &remoteDest,
|
||
|
boost::asio::ip::udp::endpoint localEndpoint,
|
||
|
std::shared_ptr<i2p::client::ClientDestination> localDestination,
|
||
|
uint16_t remotePort) :
|
||
|
m_Name(name),
|
||
|
m_RemoteDest(remoteDest),
|
||
|
m_LocalDest(localDestination),
|
||
|
m_LocalEndpoint(localEndpoint),
|
||
|
m_RemoteIdent(nullptr),
|
||
|
m_ResolveThread(nullptr),
|
||
![]()
8 years ago
|
m_LocalSocket(localDestination->GetService(), localEndpoint),
|
||
![]()
8 years ago
|
RemotePort(remotePort),
|
||
|
m_cancel_resolve(false)
|
||
|
{
|
||
|
auto dgram = m_LocalDest->CreateDatagramDestination();
|
||
|
dgram->SetReceiver(std::bind(&I2PUDPClientTunnel::HandleRecvFromI2P, this,
|
||
|
std::placeholders::_1, std::placeholders::_2,
|
||
|
std::placeholders::_3, std::placeholders::_4,
|
||
![]()
8 years ago
|
std::placeholders::_5));
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
|
||
|
|
||
|
void I2PUDPClientTunnel::Start() {
|
||
|
m_LocalDest->Start();
|
||
|
if (m_ResolveThread == nullptr)
|
||
|
m_ResolveThread = new std::thread(std::bind(&I2PUDPClientTunnel::TryResolving, this));
|
||
![]()
8 years ago
|
RecvFromLocal();
|
||
|
}
|
||
|
|
||
|
void I2PUDPClientTunnel::RecvFromLocal()
|
||
|
{
|
||
|
m_LocalSocket.async_receive_from(boost::asio::buffer(m_RecvBuff, I2P_UDP_MAX_MTU),
|
||
|
m_RecvEndpoint, std::bind(&I2PUDPClientTunnel::HandleRecvFromLocal, this, std::placeholders::_1, std::placeholders::_2));
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
void I2PUDPClientTunnel::HandleRecvFromLocal(const boost::system::error_code & ec, std::size_t transferred)
|
||
|
{
|
||
![]()
8 years ago
|
if(ec) {
|
||
|
LogPrint(eLogError, "UDP Client: ", ec.message());
|
||
|
return;
|
||
|
}
|
||
![]()
8 years ago
|
if(!m_RemoteIdent) {
|
||
|
LogPrint(eLogWarning, "UDP Client: remote endpoint not resolved yet");
|
||
![]()
8 years ago
|
RecvFromLocal();
|
||
![]()
8 years ago
|
return; // drop, remote not resolved
|
||
|
}
|
||
![]()
8 years ago
|
auto remotePort = m_RecvEndpoint.port();
|
||
|
auto itr = m_Sessions.find(remotePort);
|
||
|
if (itr == m_Sessions.end()) {
|
||
|
// track new udp convo
|
||
|
m_Sessions[remotePort] = {boost::asio::ip::udp::endpoint(m_RecvEndpoint), 0};
|
||
|
}
|
||
|
// send off to remote i2p destination
|
||
![]()
8 years ago
|
LogPrint(eLogDebug, "UDP Client: send ", transferred, " to ", m_RemoteIdent->ToBase32(), ":", RemotePort);
|
||
![]()
8 years ago
|
m_LocalDest->GetDatagramDestination()->SendDatagramTo(m_RecvBuff, transferred, *m_RemoteIdent, remotePort, RemotePort);
|
||
|
// mark convo as active
|
||
![]()
8 years ago
|
m_Sessions[remotePort].second = i2p::util::GetMillisecondsSinceEpoch();
|
||
![]()
8 years ago
|
RecvFromLocal();
|
||
![]()
8 years ago
|
}
|
||
|
|
||
![]()
8 years ago
|
std::vector<std::shared_ptr<DatagramSessionInfo> > I2PUDPClientTunnel::GetSessions()
|
||
![]()
8 years ago
|
{
|
||
![]()
8 years ago
|
// TODO: implement
|
||
![]()
8 years ago
|
std::vector<std::shared_ptr<DatagramSessionInfo> > infos;
|
||
![]()
8 years ago
|
return infos;
|
||
|
}
|
||
|
|
||
![]()
8 years ago
|
void I2PUDPClientTunnel::TryResolving() {
|
||
|
LogPrint(eLogInfo, "UDP Tunnel: Trying to resolve ", m_RemoteDest);
|
||
![]()
8 years ago
|
i2p::data::IdentHash * h = new i2p::data::IdentHash;
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
while(!context.GetAddressBook().GetIdentHash(m_RemoteDest, *h) && !m_cancel_resolve)
|
||
![]()
8 years ago
|
{
|
||
|
LogPrint(eLogWarning, "UDP Tunnel: failed to lookup ", m_RemoteDest);
|
||
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||
|
}
|
||
|
if(m_cancel_resolve)
|
||
|
{
|
||
|
LogPrint(eLogError, "UDP Tunnel: lookup of ", m_RemoteDest, " was cancelled");
|
||
|
return;
|
||
|
}
|
||
![]()
8 years ago
|
m_RemoteIdent = h;
|
||
![]()
8 years ago
|
LogPrint(eLogInfo, "UDP Tunnel: resolved ", m_RemoteDest, " to ", m_RemoteIdent->ToBase32());
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
void I2PUDPClientTunnel::HandleRecvFromI2P(const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len)
|
||
![]()
8 years ago
|
{
|
||
|
if(m_RemoteIdent && from.GetIdentHash() == *m_RemoteIdent)
|
||
|
{
|
||
![]()
8 years ago
|
auto itr = m_Sessions.find(toPort);
|
||
|
// found convo ?
|
||
|
if(itr != m_Sessions.end())
|
||
![]()
8 years ago
|
{
|
||
![]()
8 years ago
|
// found convo
|
||
|
if (len > 0) {
|
||
|
LogPrint(eLogDebug, "UDP Client: got ", len, "B from ", from.GetIdentHash().ToBase32());
|
||
![]()
8 years ago
|
m_LocalSocket.send_to(boost::asio::buffer(buf, len), itr->second.first);
|
||
![]()
8 years ago
|
// mark convo as active
|
||
![]()
8 years ago
|
itr->second.second = i2p::util::GetMillisecondsSinceEpoch();
|
||
![]()
8 years ago
|
}
|
||
![]()
8 years ago
|
}
|
||
|
else
|
||
![]()
8 years ago
|
LogPrint(eLogWarning, "UDP Client: not tracking udp session using port ", (int) toPort);
|
||
![]()
8 years ago
|
}
|
||
|
else
|
||
|
LogPrint(eLogWarning, "UDP Client: unwarrented traffic from ", from.GetIdentHash().ToBase32());
|
||
|
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
I2PUDPClientTunnel::~I2PUDPClientTunnel() {
|
||
|
auto dgram = m_LocalDest->GetDatagramDestination();
|
||
|
if (dgram) dgram->ResetReceiver();
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
m_Sessions.clear();
|
||
|
|
||
|
if(m_LocalSocket.is_open())
|
||
|
m_LocalSocket.close();
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
m_cancel_resolve = true;
|
||
![]()
8 years ago
|
|
||
![]()
8 years ago
|
if(m_ResolveThread)
|
||
|
{
|
||
|
m_ResolveThread->join();
|
||
|
delete m_ResolveThread;
|
||
|
m_ResolveThread = nullptr;
|
||
|
}
|
||
|
if (m_RemoteIdent) delete m_RemoteIdent;
|
||
|
}
|
||
![]()
8 years ago
|
}
|
||
|
}
|