2015-01-06 22:51:10 +00:00
|
|
|
#include <cassert>
|
2014-08-13 19:25:52 +00:00
|
|
|
#include "base64.h"
|
|
|
|
#include "Log.h"
|
2014-10-05 12:54:59 +00:00
|
|
|
#include "Destination.h"
|
2014-10-16 00:52:17 +00:00
|
|
|
#include "ClientContext.h"
|
2014-08-13 01:14:19 +00:00
|
|
|
#include "I2PTunnel.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2014-10-16 14:28:44 +00:00
|
|
|
namespace client
|
2014-08-13 01:14:19 +00:00
|
|
|
{
|
2015-01-07 18:09:59 +00:00
|
|
|
I2PTunnelConnection::I2PTunnelConnection (I2PService * owner,
|
2014-09-10 02:40:12 +00:00
|
|
|
boost::asio::ip::tcp::socket * socket, const i2p::data::LeaseSet * leaseSet):
|
2015-01-07 18:09:59 +00:00
|
|
|
I2PServiceHandler(owner), m_Socket (socket), m_RemoteEndpoint (socket->remote_endpoint ()),
|
2014-12-05 19:46:59 +00:00
|
|
|
m_IsQuiet (true)
|
2014-08-13 01:14:19 +00:00
|
|
|
{
|
2015-01-07 18:09:59 +00:00
|
|
|
m_Stream = GetOwner()->GetLocalDestination ()->CreateStream (*leaseSet);
|
2014-08-13 01:14:19 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 18:09:59 +00:00
|
|
|
I2PTunnelConnection::I2PTunnelConnection (I2PService * owner,
|
2015-01-03 01:43:59 +00:00
|
|
|
boost::asio::ip::tcp::socket * socket, std::shared_ptr<i2p::stream::Stream> stream):
|
2015-01-07 18:09:59 +00:00
|
|
|
I2PServiceHandler(owner), m_Socket (socket), m_Stream (stream),
|
2015-01-03 01:43:59 +00:00
|
|
|
m_RemoteEndpoint (socket->remote_endpoint ()), m_IsQuiet (true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-01-07 18:09:59 +00:00
|
|
|
I2PTunnelConnection::I2PTunnelConnection (I2PService * owner, std::shared_ptr<i2p::stream::Stream> stream,
|
2014-12-05 19:46:59 +00:00
|
|
|
boost::asio::ip::tcp::socket * socket, const boost::asio::ip::tcp::endpoint& target, bool quiet):
|
2015-01-07 18:09:59 +00:00
|
|
|
I2PServiceHandler(owner), m_Socket (socket), m_Stream (stream),
|
|
|
|
m_RemoteEndpoint (target), m_IsQuiet (quiet)
|
2014-08-20 19:03:10 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-13 01:14:19 +00:00
|
|
|
I2PTunnelConnection::~I2PTunnelConnection ()
|
|
|
|
{
|
|
|
|
delete m_Socket;
|
2014-08-14 01:04:23 +00:00
|
|
|
}
|
|
|
|
|
2014-12-04 01:37:20 +00:00
|
|
|
void I2PTunnelConnection::I2PConnect (const uint8_t * msg, size_t len)
|
2014-11-24 03:23:17 +00:00
|
|
|
{
|
2015-01-06 22:51:10 +00:00
|
|
|
if (m_Stream)
|
|
|
|
{
|
|
|
|
if (msg)
|
|
|
|
m_Stream->Send (msg, len); // connect and send
|
|
|
|
else
|
|
|
|
m_Stream->Send (m_Buffer, 0); // connect
|
|
|
|
}
|
2014-11-24 03:23:17 +00:00
|
|
|
StreamReceive ();
|
|
|
|
Receive ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void I2PTunnelConnection::Connect ()
|
|
|
|
{
|
|
|
|
if (m_Socket)
|
|
|
|
m_Socket->async_connect (m_RemoteEndpoint, std::bind (&I2PTunnelConnection::HandleConnect,
|
|
|
|
shared_from_this (), std::placeholders::_1));
|
|
|
|
}
|
|
|
|
|
2014-08-14 01:04:23 +00:00
|
|
|
void I2PTunnelConnection::Terminate ()
|
2015-01-07 18:09:59 +00:00
|
|
|
{
|
|
|
|
if (Kill()) return;
|
2014-10-08 23:44:12 +00:00
|
|
|
if (m_Stream)
|
|
|
|
{
|
|
|
|
m_Stream->Close ();
|
2014-11-23 16:33:58 +00:00
|
|
|
m_Stream.reset ();
|
2014-10-08 23:44:12 +00:00
|
|
|
}
|
2014-08-17 23:14:40 +00:00
|
|
|
m_Socket->close ();
|
2015-01-07 18:09:59 +00:00
|
|
|
Done(shared_from_this ());
|
2014-08-13 19:25:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void I2PTunnelConnection::Receive ()
|
|
|
|
{
|
|
|
|
m_Socket->async_read_some (boost::asio::buffer(m_Buffer, I2P_TUNNEL_CONNECTION_BUFFER_SIZE),
|
2014-11-24 03:23:17 +00:00
|
|
|
std::bind(&I2PTunnelConnection::HandleReceived, shared_from_this (),
|
|
|
|
std::placeholders::_1, std::placeholders::_2));
|
2014-08-13 19:25:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void I2PTunnelConnection::HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
if (ecode)
|
2015-01-03 02:57:37 +00:00
|
|
|
{
|
2014-08-13 19:25:52 +00:00
|
|
|
LogPrint ("I2PTunnel read error: ", ecode.message ());
|
2014-10-08 23:44:12 +00:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
Terminate ();
|
2014-08-13 19:25:52 +00:00
|
|
|
}
|
|
|
|
else
|
2014-08-14 01:04:23 +00:00
|
|
|
{
|
2014-08-13 19:25:52 +00:00
|
|
|
if (m_Stream)
|
2014-10-02 01:18:41 +00:00
|
|
|
m_Stream->Send (m_Buffer, bytes_transferred);
|
2014-08-13 19:25:52 +00:00
|
|
|
Receive ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void I2PTunnelConnection::HandleWrite (const boost::system::error_code& ecode)
|
|
|
|
{
|
2014-08-17 23:14:40 +00:00
|
|
|
if (ecode)
|
|
|
|
{
|
|
|
|
LogPrint ("I2PTunnel write error: ", ecode.message ());
|
2014-10-08 23:44:12 +00:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
Terminate ();
|
2014-08-17 23:14:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
StreamReceive ();
|
2014-08-13 19:25:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void I2PTunnelConnection::StreamReceive ()
|
|
|
|
{
|
|
|
|
if (m_Stream)
|
|
|
|
m_Stream->AsyncReceive (boost::asio::buffer (m_StreamBuffer, I2P_TUNNEL_CONNECTION_BUFFER_SIZE),
|
2014-11-24 03:23:17 +00:00
|
|
|
std::bind (&I2PTunnelConnection::HandleStreamReceive, shared_from_this (),
|
|
|
|
std::placeholders::_1, std::placeholders::_2),
|
2014-08-13 19:25:52 +00:00
|
|
|
I2P_TUNNEL_CONNECTION_MAX_IDLE);
|
2014-08-13 01:14:19 +00:00
|
|
|
}
|
|
|
|
|
2014-08-13 19:25:52 +00:00
|
|
|
void I2PTunnelConnection::HandleStreamReceive (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
|
|
|
if (ecode)
|
|
|
|
{
|
|
|
|
LogPrint ("I2PTunnel stream read error: ", ecode.message ());
|
2014-10-08 23:44:12 +00:00
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
Terminate ();
|
2014-08-13 19:25:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
boost::asio::async_write (*m_Socket, boost::asio::buffer (m_StreamBuffer, bytes_transferred),
|
2014-11-24 03:23:17 +00:00
|
|
|
std::bind (&I2PTunnelConnection::HandleWrite, shared_from_this (), std::placeholders::_1));
|
2014-08-13 19:25:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 19:03:10 +00:00
|
|
|
void I2PTunnelConnection::HandleConnect (const boost::system::error_code& ecode)
|
|
|
|
{
|
|
|
|
if (ecode)
|
|
|
|
{
|
|
|
|
LogPrint ("I2PTunnel connect error: ", ecode.message ());
|
2014-11-24 03:23:17 +00:00
|
|
|
Terminate ();
|
2014-08-20 19:03:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
LogPrint ("I2PTunnel connected");
|
2014-12-05 19:46:59 +00:00
|
|
|
if (m_IsQuiet)
|
|
|
|
StreamReceive ();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// send destination first like received from I2P
|
|
|
|
std::string dest = m_Stream->GetRemoteIdentity ().ToBase64 ();
|
|
|
|
dest += "\n";
|
|
|
|
memcpy (m_StreamBuffer, dest.c_str (), dest.size ());
|
|
|
|
HandleStreamReceive (boost::system::error_code (), dest.size ());
|
|
|
|
}
|
2014-08-20 19:03:10 +00:00
|
|
|
Receive ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-29 19:29:55 +00:00
|
|
|
I2PClientTunnel::I2PClientTunnel (const std::string& destination, int port, ClientDestination * localDestination):
|
2015-01-07 18:09:59 +00:00
|
|
|
I2PService (localDestination),
|
2014-12-29 19:29:55 +00:00
|
|
|
m_Acceptor (GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port)),
|
2015-01-03 03:56:34 +00:00
|
|
|
m_Timer (GetService ()), m_Destination (destination), m_DestinationIdentHash (nullptr)
|
2014-08-13 01:14:19 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-08-13 19:25:52 +00:00
|
|
|
I2PClientTunnel::~I2PClientTunnel ()
|
|
|
|
{
|
|
|
|
Stop ();
|
|
|
|
}
|
2014-08-13 01:14:19 +00:00
|
|
|
|
2014-08-13 19:25:52 +00:00
|
|
|
void I2PClientTunnel::Start ()
|
|
|
|
{
|
2015-01-03 01:17:01 +00:00
|
|
|
GetIdentHash();
|
2014-08-13 19:25:52 +00:00
|
|
|
m_Acceptor.listen ();
|
|
|
|
Accept ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void I2PClientTunnel::Stop ()
|
|
|
|
{
|
|
|
|
m_Acceptor.close();
|
2014-10-15 16:07:06 +00:00
|
|
|
m_Timer.cancel ();
|
2015-01-07 18:09:59 +00:00
|
|
|
ClearHandlers ();
|
2015-01-03 01:07:55 +00:00
|
|
|
auto *originalIdentHash = m_DestinationIdentHash;
|
2014-08-13 19:25:52 +00:00
|
|
|
m_DestinationIdentHash = nullptr;
|
2015-01-03 01:07:55 +00:00
|
|
|
delete originalIdentHash;
|
2014-08-13 19:25:52 +00:00
|
|
|
}
|
|
|
|
|
2015-01-03 01:17:01 +00:00
|
|
|
/* 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
|
2015-01-03 01:43:59 +00:00
|
|
|
LogPrint (eLogWarning,"Remote destination ", m_Destination, " not found");
|
2015-01-03 01:17:01 +00:00
|
|
|
}
|
|
|
|
return m_DestinationIdentHash;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-13 01:14:19 +00:00
|
|
|
void I2PClientTunnel::Accept ()
|
|
|
|
{
|
2014-09-10 02:40:12 +00:00
|
|
|
auto newSocket = new boost::asio::ip::tcp::socket (GetService ());
|
2014-11-24 03:23:17 +00:00
|
|
|
m_Acceptor.async_accept (*newSocket, std::bind (&I2PClientTunnel::HandleAccept, this,
|
|
|
|
std::placeholders::_1, newSocket));
|
2014-08-13 01:14:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void I2PClientTunnel::HandleAccept (const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket)
|
|
|
|
{
|
|
|
|
if (!ecode)
|
|
|
|
{
|
2015-01-03 01:17:01 +00:00
|
|
|
const i2p::data::IdentHash *identHash = GetIdentHash();
|
|
|
|
if (identHash)
|
2015-01-03 01:43:59 +00:00
|
|
|
GetLocalDestination ()->CreateStream (
|
|
|
|
std::bind (&I2PClientTunnel::HandleStreamRequestComplete,
|
|
|
|
this, std::placeholders::_1, socket), *identHash);
|
2014-08-13 01:14:19 +00:00
|
|
|
else
|
2015-01-03 01:43:59 +00:00
|
|
|
{
|
|
|
|
LogPrint (eLogError,"Closing socket");
|
2014-10-15 16:30:20 +00:00
|
|
|
delete socket;
|
2015-01-03 01:43:59 +00:00
|
|
|
}
|
2014-08-13 01:14:19 +00:00
|
|
|
Accept ();
|
|
|
|
}
|
|
|
|
else
|
2014-10-15 16:07:06 +00:00
|
|
|
{
|
2015-01-03 01:43:59 +00:00
|
|
|
LogPrint (eLogError,"Closing socket on accept because: ", ecode.message ());
|
|
|
|
delete socket;
|
2014-10-15 16:07:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-03 01:43:59 +00:00
|
|
|
void I2PClientTunnel::HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream, boost::asio::ip::tcp::socket * socket)
|
2014-10-15 16:07:06 +00:00
|
|
|
{
|
2015-01-03 01:43:59 +00:00
|
|
|
if (stream)
|
|
|
|
{
|
|
|
|
LogPrint (eLogInfo,"New I2PTunnel connection");
|
|
|
|
auto connection = std::make_shared<I2PTunnelConnection>(this, socket, stream);
|
2015-01-07 18:09:59 +00:00
|
|
|
AddHandler (connection);
|
2014-11-24 03:23:17 +00:00
|
|
|
connection->I2PConnect ();
|
2014-10-15 16:07:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-01-03 01:43:59 +00:00
|
|
|
LogPrint (eLogError,"Issue when creating the stream, check the previous warnings for more info.");
|
2014-10-15 16:07:06 +00:00
|
|
|
delete socket;
|
2015-01-03 01:43:59 +00:00
|
|
|
}
|
2014-10-15 16:07:06 +00:00
|
|
|
}
|
|
|
|
|
2014-12-29 19:29:55 +00:00
|
|
|
I2PServerTunnel::I2PServerTunnel (const std::string& address, int port, ClientDestination * localDestination):
|
2015-01-07 18:09:59 +00:00
|
|
|
I2PService (localDestination), m_Endpoint (boost::asio::ip::address::from_string (address), port)
|
2014-08-20 19:03:10 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void I2PServerTunnel::Start ()
|
|
|
|
{
|
|
|
|
Accept ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void I2PServerTunnel::Stop ()
|
|
|
|
{
|
2015-01-07 18:09:59 +00:00
|
|
|
ClearHandlers ();
|
2014-08-20 19:03:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void I2PServerTunnel::Accept ()
|
|
|
|
{
|
2014-10-01 14:58:28 +00:00
|
|
|
auto localDestination = GetLocalDestination ();
|
|
|
|
if (localDestination)
|
2014-10-22 18:01:23 +00:00
|
|
|
localDestination->AcceptStreams (std::bind (&I2PServerTunnel::HandleAccept, this, std::placeholders::_1));
|
2014-10-01 14:58:28 +00:00
|
|
|
else
|
|
|
|
LogPrint ("Local destination not set for server tunnel");
|
2014-08-20 19:03:10 +00:00
|
|
|
}
|
|
|
|
|
2014-11-23 16:33:58 +00:00
|
|
|
void I2PServerTunnel::HandleAccept (std::shared_ptr<i2p::stream::Stream> stream)
|
2014-08-20 19:03:10 +00:00
|
|
|
{
|
|
|
|
if (stream)
|
2014-11-24 03:23:17 +00:00
|
|
|
{
|
|
|
|
auto conn = std::make_shared<I2PTunnelConnection> (this, stream, new boost::asio::ip::tcp::socket (GetService ()), m_Endpoint);
|
2015-01-07 18:09:59 +00:00
|
|
|
AddHandler (conn);
|
2014-11-24 03:23:17 +00:00
|
|
|
conn->Connect ();
|
|
|
|
}
|
2014-08-20 19:03:10 +00:00
|
|
|
}
|
2014-08-13 01:14:19 +00:00
|
|
|
}
|
|
|
|
}
|