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.
720 lines
20 KiB
720 lines
20 KiB
10 years ago
|
#include <string.h>
|
||
10 years ago
|
#include "Log.h"
|
||
10 years ago
|
#include "ClientContext.h"
|
||
8 years ago
|
#include "util.h"
|
||
10 years ago
|
#include "BOB.h"
|
||
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace client
|
||
|
{
|
||
10 years ago
|
BOBI2PInboundTunnel::BOBI2PInboundTunnel (int port, std::shared_ptr<ClientDestination> localDestination):
|
||
10 years ago
|
BOBI2PTunnel (localDestination),
|
||
10 years ago
|
m_Acceptor (localDestination->GetService (), boost::asio::ip::tcp::endpoint (boost::asio::ip::tcp::v4(), port))
|
||
10 years ago
|
{
|
||
|
}
|
||
|
|
||
|
BOBI2PInboundTunnel::~BOBI2PInboundTunnel ()
|
||
|
{
|
||
10 years ago
|
Stop ();
|
||
10 years ago
|
}
|
||
|
|
||
|
void BOBI2PInboundTunnel::Start ()
|
||
|
{
|
||
|
m_Acceptor.listen ();
|
||
|
Accept ();
|
||
|
}
|
||
|
|
||
|
void BOBI2PInboundTunnel::Stop ()
|
||
|
{
|
||
|
m_Acceptor.close();
|
||
10 years ago
|
ClearHandlers ();
|
||
10 years ago
|
}
|
||
|
|
||
|
void BOBI2PInboundTunnel::Accept ()
|
||
|
{
|
||
10 years ago
|
auto receiver = std::make_shared<AddressReceiver> ();
|
||
10 years ago
|
receiver->socket = std::make_shared<boost::asio::ip::tcp::socket> (GetService ());
|
||
10 years ago
|
m_Acceptor.async_accept (*receiver->socket, std::bind (&BOBI2PInboundTunnel::HandleAccept, this,
|
||
|
std::placeholders::_1, receiver));
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void BOBI2PInboundTunnel::HandleAccept (const boost::system::error_code& ecode, std::shared_ptr<AddressReceiver> receiver)
|
||
10 years ago
|
{
|
||
|
if (!ecode)
|
||
|
{
|
||
|
Accept ();
|
||
10 years ago
|
ReceiveAddress (receiver);
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void BOBI2PInboundTunnel::ReceiveAddress (std::shared_ptr<AddressReceiver> receiver)
|
||
10 years ago
|
{
|
||
10 years ago
|
receiver->socket->async_read_some (boost::asio::buffer(
|
||
|
receiver->buffer + receiver->bufferOffset,
|
||
|
BOB_COMMAND_BUFFER_SIZE - receiver->bufferOffset),
|
||
10 years ago
|
std::bind(&BOBI2PInboundTunnel::HandleReceivedAddress, this,
|
||
10 years ago
|
std::placeholders::_1, std::placeholders::_2, receiver));
|
||
10 years ago
|
}
|
||
|
|
||
|
void BOBI2PInboundTunnel::HandleReceivedAddress (const boost::system::error_code& ecode, std::size_t bytes_transferred,
|
||
10 years ago
|
std::shared_ptr<AddressReceiver> receiver)
|
||
10 years ago
|
{
|
||
|
if (ecode)
|
||
9 years ago
|
LogPrint (eLogError, "BOB: inbound tunnel read error: ", ecode.message ());
|
||
10 years ago
|
else
|
||
|
{
|
||
10 years ago
|
receiver->bufferOffset += bytes_transferred;
|
||
|
receiver->buffer[receiver->bufferOffset] = 0;
|
||
|
char * eol = strchr (receiver->buffer, '\n');
|
||
10 years ago
|
if (eol)
|
||
|
{
|
||
|
*eol = 0;
|
||
9 years ago
|
if (eol != receiver->buffer && eol[-1] == '\r') eol[-1] = 0; // workaround for Transmission, it sends '\r\n' terminated address
|
||
10 years ago
|
receiver->data = (uint8_t *)eol + 1;
|
||
|
receiver->dataLen = receiver->bufferOffset - (eol - receiver->buffer + 1);
|
||
10 years ago
|
i2p::data::IdentHash ident;
|
||
10 years ago
|
if (!context.GetAddressBook ().GetIdentHash (receiver->buffer, ident))
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogError, "BOB: address ", receiver->buffer, " not found");
|
||
10 years ago
|
return;
|
||
|
}
|
||
10 years ago
|
auto leaseSet = GetLocalDestination ()->FindLeaseSet (ident);
|
||
|
if (leaseSet)
|
||
10 years ago
|
CreateConnection (receiver, leaseSet);
|
||
10 years ago
|
else
|
||
10 years ago
|
GetLocalDestination ()->RequestDestination (ident,
|
||
|
std::bind (&BOBI2PInboundTunnel::HandleDestinationRequestComplete,
|
||
10 years ago
|
this, std::placeholders::_1, receiver));
|
||
10 years ago
|
}
|
||
|
else
|
||
|
{
|
||
10 years ago
|
if (receiver->bufferOffset < BOB_COMMAND_BUFFER_SIZE)
|
||
|
ReceiveAddress (receiver);
|
||
10 years ago
|
else
|
||
9 years ago
|
LogPrint (eLogError, "BOB: missing inbound address");
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void BOBI2PInboundTunnel::HandleDestinationRequestComplete (std::shared_ptr<i2p::data::LeaseSet> leaseSet, std::shared_ptr<AddressReceiver> receiver)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (leaseSet)
|
||
|
CreateConnection (receiver, leaseSet);
|
||
|
else
|
||
9 years ago
|
LogPrint (eLogError, "BOB: LeaseSet for inbound destination not found");
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void BOBI2PInboundTunnel::CreateConnection (std::shared_ptr<AddressReceiver> receiver, std::shared_ptr<const i2p::data::LeaseSet> leaseSet)
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "BOB: New inbound connection");
|
||
10 years ago
|
auto connection = std::make_shared<I2PTunnelConnection>(this, receiver->socket, leaseSet);
|
||
10 years ago
|
AddHandler (connection);
|
||
10 years ago
|
connection->I2PConnect (receiver->data, receiver->dataLen);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
BOBI2POutboundTunnel::BOBI2POutboundTunnel (const std::string& address, int port,
|
||
10 years ago
|
std::shared_ptr<ClientDestination> localDestination, bool quiet): BOBI2PTunnel (localDestination),
|
||
10 years ago
|
m_Endpoint (boost::asio::ip::address::from_string (address), port), m_IsQuiet (quiet)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void BOBI2POutboundTunnel::Start ()
|
||
|
{
|
||
|
Accept ();
|
||
|
}
|
||
|
|
||
|
void BOBI2POutboundTunnel::Stop ()
|
||
|
{
|
||
10 years ago
|
ClearHandlers ();
|
||
10 years ago
|
}
|
||
|
|
||
|
void BOBI2POutboundTunnel::Accept ()
|
||
|
{
|
||
|
auto localDestination = GetLocalDestination ();
|
||
|
if (localDestination)
|
||
|
localDestination->AcceptStreams (std::bind (&BOBI2POutboundTunnel::HandleAccept, this, std::placeholders::_1));
|
||
|
else
|
||
9 years ago
|
LogPrint (eLogError, "BOB: Local destination not set for server tunnel");
|
||
10 years ago
|
}
|
||
|
|
||
|
void BOBI2POutboundTunnel::HandleAccept (std::shared_ptr<i2p::stream::Stream> stream)
|
||
|
{
|
||
|
if (stream)
|
||
|
{
|
||
10 years ago
|
auto conn = std::make_shared<I2PTunnelConnection> (this, stream, std::make_shared<boost::asio::ip::tcp::socket> (GetService ()), m_Endpoint, m_IsQuiet);
|
||
10 years ago
|
AddHandler (conn);
|
||
10 years ago
|
conn->Connect ();
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
BOBDestination::BOBDestination (std::shared_ptr<ClientDestination> localDestination):
|
||
10 years ago
|
m_LocalDestination (localDestination),
|
||
10 years ago
|
m_OutboundTunnel (nullptr), m_InboundTunnel (nullptr)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
BOBDestination::~BOBDestination ()
|
||
|
{
|
||
|
delete m_OutboundTunnel;
|
||
|
delete m_InboundTunnel;
|
||
10 years ago
|
i2p::client::context.DeleteLocalDestination (m_LocalDestination);
|
||
10 years ago
|
}
|
||
|
|
||
|
void BOBDestination::Start ()
|
||
|
{
|
||
|
if (m_OutboundTunnel) m_OutboundTunnel->Start ();
|
||
|
if (m_InboundTunnel) m_InboundTunnel->Start ();
|
||
|
}
|
||
|
|
||
|
void BOBDestination::Stop ()
|
||
|
{
|
||
10 years ago
|
StopTunnels ();
|
||
10 years ago
|
m_LocalDestination->Stop ();
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void BOBDestination::StopTunnels ()
|
||
|
{
|
||
|
if (m_OutboundTunnel)
|
||
|
{
|
||
|
m_OutboundTunnel->Stop ();
|
||
|
delete m_OutboundTunnel;
|
||
|
m_OutboundTunnel = nullptr;
|
||
|
}
|
||
|
if (m_InboundTunnel)
|
||
|
{
|
||
|
m_InboundTunnel->Stop ();
|
||
|
delete m_InboundTunnel;
|
||
|
m_InboundTunnel = nullptr;
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void BOBDestination::CreateInboundTunnel (int port)
|
||
|
{
|
||
10 years ago
|
if (!m_InboundTunnel)
|
||
10 years ago
|
m_InboundTunnel = new BOBI2PInboundTunnel (port, m_LocalDestination);
|
||
10 years ago
|
}
|
||
|
|
||
|
void BOBDestination::CreateOutboundTunnel (const std::string& address, int port, bool quiet)
|
||
|
{
|
||
10 years ago
|
if (!m_OutboundTunnel)
|
||
10 years ago
|
m_OutboundTunnel = new BOBI2POutboundTunnel (address, port, m_LocalDestination, quiet);
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
BOBCommandSession::BOBCommandSession (BOBCommandChannel& owner):
|
||
9 years ago
|
m_Owner (owner), m_Socket (m_Owner.GetService ()),
|
||
9 years ago
|
m_ReceiveBufferOffset (0), m_IsOpen (true), m_IsQuiet (false), m_IsActive (false),
|
||
9 years ago
|
m_InPort (0), m_OutPort (0), m_CurrentDestination (nullptr)
|
||
10 years ago
|
{
|
||
|
}
|
||
|
|
||
10 years ago
|
BOBCommandSession::~BOBCommandSession ()
|
||
|
{
|
||
|
}
|
||
|
|
||
10 years ago
|
void BOBCommandSession::Terminate ()
|
||
|
{
|
||
|
m_Socket.close ();
|
||
|
m_IsOpen = false;
|
||
|
}
|
||
|
|
||
10 years ago
|
void BOBCommandSession::Receive ()
|
||
|
{
|
||
|
m_Socket.async_read_some (boost::asio::buffer(m_ReceiveBuffer + m_ReceiveBufferOffset, BOB_COMMAND_BUFFER_SIZE - m_ReceiveBufferOffset),
|
||
|
std::bind(&BOBCommandSession::HandleReceived, shared_from_this (),
|
||
|
std::placeholders::_1, std::placeholders::_2));
|
||
|
}
|
||
|
|
||
|
void BOBCommandSession::HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
||
|
{
|
||
|
if (ecode)
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogError, "BOB: command channel read error: ", ecode.message ());
|
||
10 years ago
|
if (ecode != boost::asio::error::operation_aborted)
|
||
|
Terminate ();
|
||
10 years ago
|
}
|
||
10 years ago
|
else
|
||
|
{
|
||
|
size_t size = m_ReceiveBufferOffset + bytes_transferred;
|
||
|
m_ReceiveBuffer[size] = 0;
|
||
|
char * eol = strchr (m_ReceiveBuffer, '\n');
|
||
|
if (eol)
|
||
|
{
|
||
|
*eol = 0;
|
||
|
char * operand = strchr (m_ReceiveBuffer, ' ');
|
||
10 years ago
|
if (operand)
|
||
|
{
|
||
|
*operand = 0;
|
||
|
operand++;
|
||
|
}
|
||
|
else
|
||
|
operand = eol;
|
||
10 years ago
|
// process command
|
||
10 years ago
|
auto& handlers = m_Owner.GetCommandHandlers ();
|
||
10 years ago
|
auto it = handlers.find (m_ReceiveBuffer);
|
||
|
if (it != handlers.end ())
|
||
10 years ago
|
(this->*(it->second))(operand, eol - operand);
|
||
|
else
|
||
10 years ago
|
{
|
||
9 years ago
|
LogPrint (eLogError, "BOB: unknown command ", m_ReceiveBuffer);
|
||
10 years ago
|
SendReplyError ("unknown command");
|
||
|
}
|
||
10 years ago
|
|
||
|
m_ReceiveBufferOffset = size - (eol - m_ReceiveBuffer) - 1;
|
||
|
memmove (m_ReceiveBuffer, eol + 1, m_ReceiveBufferOffset);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (size < BOB_COMMAND_BUFFER_SIZE)
|
||
|
m_ReceiveBufferOffset = size;
|
||
|
else
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "BOB: Malformed input of the command channel");
|
||
10 years ago
|
Terminate ();
|
||
10 years ago
|
}
|
||
|
}
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
|
void BOBCommandSession::Send (size_t len)
|
||
|
{
|
||
|
boost::asio::async_write (m_Socket, boost::asio::buffer (m_SendBuffer, len),
|
||
|
boost::asio::transfer_all (),
|
||
|
std::bind(&BOBCommandSession::HandleSent, shared_from_this (),
|
||
|
std::placeholders::_1, std::placeholders::_2));
|
||
|
}
|
||
|
|
||
|
void BOBCommandSession::HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
||
|
{
|
||
|
if (ecode)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "BOB: command channel send error: ", ecode.message ());
|
||
10 years ago
|
if (ecode != boost::asio::error::operation_aborted)
|
||
|
Terminate ();
|
||
10 years ago
|
}
|
||
|
else
|
||
|
{
|
||
10 years ago
|
if (m_IsOpen)
|
||
|
Receive ();
|
||
10 years ago
|
else
|
||
|
Terminate ();
|
||
10 years ago
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void BOBCommandSession::SendReplyOK (const char * msg)
|
||
|
{
|
||
|
#ifdef _MSC_VER
|
||
|
size_t len = sprintf_s (m_SendBuffer, BOB_COMMAND_BUFFER_SIZE, BOB_REPLY_OK, msg);
|
||
|
#else
|
||
|
size_t len = snprintf (m_SendBuffer, BOB_COMMAND_BUFFER_SIZE, BOB_REPLY_OK, msg);
|
||
|
#endif
|
||
|
Send (len);
|
||
|
}
|
||
|
|
||
|
void BOBCommandSession::SendReplyError (const char * msg)
|
||
|
{
|
||
|
#ifdef _MSC_VER
|
||
|
size_t len = sprintf_s (m_SendBuffer, BOB_COMMAND_BUFFER_SIZE, BOB_REPLY_ERROR, msg);
|
||
|
#else
|
||
|
size_t len = snprintf (m_SendBuffer, BOB_COMMAND_BUFFER_SIZE, BOB_REPLY_ERROR, msg);
|
||
|
#endif
|
||
|
Send (len);
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void BOBCommandSession::SendVersion ()
|
||
|
{
|
||
|
size_t len = strlen (BOB_VERSION);
|
||
|
memcpy (m_SendBuffer, BOB_VERSION, len);
|
||
|
Send (len);
|
||
|
}
|
||
|
|
||
10 years ago
|
void BOBCommandSession::SendData (const char * nickname)
|
||
|
{
|
||
|
#ifdef _MSC_VER
|
||
|
size_t len = sprintf_s (m_SendBuffer, BOB_COMMAND_BUFFER_SIZE, BOB_DATA, nickname);
|
||
|
#else
|
||
|
size_t len = snprintf (m_SendBuffer, BOB_COMMAND_BUFFER_SIZE, BOB_DATA, nickname);
|
||
|
#endif
|
||
|
Send (len);
|
||
|
}
|
||
|
|
||
10 years ago
|
void BOBCommandSession::ZapCommandHandler (const char * operand, size_t len)
|
||
|
{
|
||
|
LogPrint (eLogDebug, "BOB: zap");
|
||
10 years ago
|
Terminate ();
|
||
|
}
|
||
|
|
||
|
void BOBCommandSession::QuitCommandHandler (const char * operand, size_t len)
|
||
|
{
|
||
|
LogPrint (eLogDebug, "BOB: quit");
|
||
10 years ago
|
m_IsOpen = false;
|
||
10 years ago
|
SendReplyOK ("Bye!");
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void BOBCommandSession::StartCommandHandler (const char * operand, size_t len)
|
||
|
{
|
||
|
LogPrint (eLogDebug, "BOB: start ", m_Nickname);
|
||
9 years ago
|
if (m_IsActive)
|
||
|
{
|
||
|
SendReplyError ("tunnel is active");
|
||
|
return;
|
||
|
}
|
||
10 years ago
|
if (!m_CurrentDestination)
|
||
|
{
|
||
10 years ago
|
m_CurrentDestination = new BOBDestination (i2p::client::context.CreateNewLocalDestination (m_Keys, true, &m_Options));
|
||
10 years ago
|
m_Owner.AddDestination (m_Nickname, m_CurrentDestination);
|
||
|
}
|
||
10 years ago
|
if (m_InPort)
|
||
10 years ago
|
m_CurrentDestination->CreateInboundTunnel (m_InPort);
|
||
10 years ago
|
if (m_OutPort && !m_Address.empty ())
|
||
10 years ago
|
m_CurrentDestination->CreateOutboundTunnel (m_Address, m_OutPort, m_IsQuiet);
|
||
|
m_CurrentDestination->Start ();
|
||
9 years ago
|
SendReplyOK ("Tunnel starting");
|
||
|
m_IsActive = true;
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
10 years ago
|
void BOBCommandSession::StopCommandHandler (const char * operand, size_t len)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogDebug, "BOB: stop ", m_Nickname);
|
||
|
if (!m_IsActive)
|
||
|
{
|
||
|
SendReplyError ("tunnel is inactive");
|
||
|
return;
|
||
|
}
|
||
10 years ago
|
auto dest = m_Owner.FindDestination (m_Nickname);
|
||
|
if (dest)
|
||
10 years ago
|
{
|
||
10 years ago
|
dest->StopTunnels ();
|
||
9 years ago
|
SendReplyOK ("Tunnel stopping");
|
||
10 years ago
|
}
|
||
|
else
|
||
|
SendReplyError ("tunnel not found");
|
||
9 years ago
|
m_IsActive = false;
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void BOBCommandSession::SetNickCommandHandler (const char * operand, size_t len)
|
||
|
{
|
||
10 years ago
|
LogPrint (eLogDebug, "BOB: setnick ", operand);
|
||
10 years ago
|
m_Nickname = operand;
|
||
10 years ago
|
std::string msg ("Nickname set to ");
|
||
9 years ago
|
msg += m_Nickname;
|
||
10 years ago
|
SendReplyOK (msg.c_str ());
|
||
|
}
|
||
|
|
||
10 years ago
|
void BOBCommandSession::GetNickCommandHandler (const char * operand, size_t len)
|
||
|
{
|
||
10 years ago
|
LogPrint (eLogDebug, "BOB: getnick ", operand);
|
||
10 years ago
|
m_CurrentDestination = m_Owner.FindDestination (operand);
|
||
|
if (m_CurrentDestination)
|
||
10 years ago
|
{
|
||
10 years ago
|
m_Keys = m_CurrentDestination->GetKeys ();
|
||
10 years ago
|
m_Nickname = operand;
|
||
9 years ago
|
}
|
||
|
if (m_Nickname == operand)
|
||
|
{
|
||
10 years ago
|
std::string msg ("Nickname set to ");
|
||
9 years ago
|
msg += m_Nickname;
|
||
10 years ago
|
SendReplyOK (msg.c_str ());
|
||
9 years ago
|
}
|
||
10 years ago
|
else
|
||
9 years ago
|
SendReplyError ("no nickname has been set");
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void BOBCommandSession::NewkeysCommandHandler (const char * operand, size_t len)
|
||
|
{
|
||
|
LogPrint (eLogDebug, "BOB: newkeys");
|
||
|
m_Keys = i2p::data::PrivateKeys::CreateRandomKeys ();
|
||
9 years ago
|
SendReplyOK (m_Keys.GetPublic ()->ToBase64 ().c_str ());
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void BOBCommandSession::SetkeysCommandHandler (const char * operand, size_t len)
|
||
|
{
|
||
10 years ago
|
LogPrint (eLogDebug, "BOB: setkeys ", operand);
|
||
8 years ago
|
if (m_Keys.FromBase64 (operand))
|
||
|
SendReplyOK (m_Keys.GetPublic ()->ToBase64 ().c_str ());
|
||
|
else
|
||
|
SendReplyError ("invalid keys");
|
||
10 years ago
|
}
|
||
|
|
||
|
void BOBCommandSession::GetkeysCommandHandler (const char * operand, size_t len)
|
||
|
{
|
||
|
LogPrint (eLogDebug, "BOB: getkeys");
|
||
8 years ago
|
if (m_Keys.GetPublic ()) // keys are set ?
|
||
|
SendReplyOK (m_Keys.ToBase64 ().c_str ());
|
||
|
else
|
||
|
SendReplyError ("keys are not set");
|
||
|
}
|
||
10 years ago
|
|
||
|
void BOBCommandSession::GetdestCommandHandler (const char * operand, size_t len)
|
||
|
{
|
||
|
LogPrint (eLogDebug, "BOB: getdest");
|
||
9 years ago
|
SendReplyOK (m_Keys.GetPublic ()->ToBase64 ().c_str ());
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void BOBCommandSession::OuthostCommandHandler (const char * operand, size_t len)
|
||
|
{
|
||
10 years ago
|
LogPrint (eLogDebug, "BOB: outhost ", operand);
|
||
10 years ago
|
m_Address = operand;
|
||
|
SendReplyOK ("outhost set");
|
||
|
}
|
||
|
|
||
|
void BOBCommandSession::OutportCommandHandler (const char * operand, size_t len)
|
||
|
{
|
||
10 years ago
|
LogPrint (eLogDebug, "BOB: outport ", operand);
|
||
8 years ago
|
m_OutPort = std::stoi(operand);
|
||
9 years ago
|
if (m_OutPort >= 0)
|
||
|
SendReplyOK ("outbound port set");
|
||
|
else
|
||
|
SendReplyError ("port out of range");
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
void BOBCommandSession::InhostCommandHandler (const char * operand, size_t len)
|
||
|
{
|
||
10 years ago
|
LogPrint (eLogDebug, "BOB: inhost ", operand);
|
||
10 years ago
|
m_Address = operand;
|
||
|
SendReplyOK ("inhost set");
|
||
|
}
|
||
|
|
||
|
void BOBCommandSession::InportCommandHandler (const char * operand, size_t len)
|
||
|
{
|
||
10 years ago
|
LogPrint (eLogDebug, "BOB: inport ", operand);
|
||
8 years ago
|
m_InPort = std::stoi(operand);
|
||
9 years ago
|
if (m_InPort >= 0)
|
||
|
SendReplyOK ("inbound port set");
|
||
|
else
|
||
|
SendReplyError ("port out of range");
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
|
void BOBCommandSession::QuietCommandHandler (const char * operand, size_t len)
|
||
|
{
|
||
|
LogPrint (eLogDebug, "BOB: quiet");
|
||
9 years ago
|
if (m_Nickname.length () > 0)
|
||
|
{
|
||
|
if (!m_IsActive)
|
||
|
{
|
||
|
m_IsQuiet = true;
|
||
|
SendReplyOK ("Quiet set");
|
||
|
}
|
||
|
else
|
||
|
SendReplyError ("tunnel is active");
|
||
|
}
|
||
|
else
|
||
|
SendReplyError ("no nickname has been set");
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void BOBCommandSession::LookupCommandHandler (const char * operand, size_t len)
|
||
|
{
|
||
10 years ago
|
LogPrint (eLogDebug, "BOB: lookup ", operand);
|
||
10 years ago
|
i2p::data::IdentHash ident;
|
||
8 years ago
|
if (!context.GetAddressBook ().GetIdentHash (operand, ident))
|
||
10 years ago
|
{
|
||
|
SendReplyError ("Address Not found");
|
||
|
return;
|
||
10 years ago
|
}
|
||
8 years ago
|
auto localDestination = m_CurrentDestination ? m_CurrentDestination->GetLocalDestination () : i2p::client::context.GetSharedLocalDestination ();
|
||
10 years ago
|
auto leaseSet = localDestination->FindLeaseSet (ident);
|
||
|
if (leaseSet)
|
||
9 years ago
|
SendReplyOK (leaseSet->GetIdentity ()->ToBase64 ().c_str ());
|
||
10 years ago
|
else
|
||
|
{
|
||
|
auto s = shared_from_this ();
|
||
10 years ago
|
localDestination->RequestDestination (ident,
|
||
|
[s](std::shared_ptr<i2p::data::LeaseSet> ls)
|
||
10 years ago
|
{
|
||
10 years ago
|
if (ls)
|
||
9 years ago
|
s->SendReplyOK (ls->GetIdentity ()->ToBase64 ().c_str ());
|
||
10 years ago
|
else
|
||
|
s->SendReplyError ("LeaseSet Not found");
|
||
|
}
|
||
|
);
|
||
|
}
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void BOBCommandSession::ClearCommandHandler (const char * operand, size_t len)
|
||
|
{
|
||
|
LogPrint (eLogDebug, "BOB: clear");
|
||
10 years ago
|
m_Owner.DeleteDestination (m_Nickname);
|
||
9 years ago
|
m_Nickname = "";
|
||
10 years ago
|
SendReplyOK ("cleared");
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void BOBCommandSession::ListCommandHandler (const char * operand, size_t len)
|
||
|
{
|
||
|
LogPrint (eLogDebug, "BOB: list");
|
||
9 years ago
|
const auto& destinations = m_Owner.GetDestinations ();
|
||
|
for (const auto& it: destinations)
|
||
10 years ago
|
SendData (it.first.c_str ());
|
||
|
SendReplyOK ("Listing done");
|
||
|
}
|
||
|
|
||
|
void BOBCommandSession::OptionCommandHandler (const char * operand, size_t len)
|
||
|
{
|
||
|
LogPrint (eLogDebug, "BOB: option ", operand);
|
||
10 years ago
|
const char * value = strchr (operand, '=');
|
||
|
if (value)
|
||
|
{
|
||
9 years ago
|
std::string msg ("option ");
|
||
10 years ago
|
*(const_cast<char *>(value)) = 0;
|
||
|
m_Options[operand] = value + 1;
|
||
9 years ago
|
msg += operand;
|
||
10 years ago
|
*(const_cast<char *>(value)) = '=';
|
||
9 years ago
|
msg += " set to ";
|
||
|
msg += value;
|
||
|
SendReplyOK (msg.c_str ());
|
||
10 years ago
|
}
|
||
|
else
|
||
|
SendReplyError ("malformed");
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
|
void BOBCommandSession::StatusCommandHandler (const char * operand, size_t len)
|
||
|
{
|
||
|
LogPrint (eLogDebug, "BOB: status ", operand);
|
||
9 years ago
|
if (m_Nickname == operand)
|
||
9 years ago
|
{
|
||
|
std::stringstream s;
|
||
9 years ago
|
s << "DATA"; s << " NICKNAME: "; s << m_Nickname;
|
||
8 years ago
|
if (m_CurrentDestination)
|
||
|
{
|
||
|
if (m_CurrentDestination->GetLocalDestination ()->IsReady ())
|
||
|
s << " STARTING: false RUNNING: true STOPPING: false";
|
||
|
else
|
||
|
s << " STARTING: true RUNNING: false STOPPING: false";
|
||
|
}
|
||
9 years ago
|
else
|
||
8 years ago
|
s << " STARTING: false RUNNING: false STOPPING: false";
|
||
9 years ago
|
s << " KEYS: true"; s << " QUIET: "; s << (m_IsQuiet ? "true":"false");
|
||
9 years ago
|
if (m_InPort)
|
||
|
{
|
||
9 years ago
|
s << " INPORT: " << m_InPort;
|
||
|
s << " INHOST: " << (m_Address.length () > 0 ? m_Address : "127.0.0.1");
|
||
9 years ago
|
}
|
||
|
if (m_OutPort)
|
||
|
{
|
||
9 years ago
|
s << " OUTPORT: " << m_OutPort;
|
||
|
s << " OUTHOST: " << (m_Address.length () > 0 ? m_Address : "127.0.0.1");
|
||
9 years ago
|
}
|
||
|
SendReplyOK (s.str().c_str());
|
||
|
}
|
||
9 years ago
|
else
|
||
|
SendReplyError ("no nickname has been set");
|
||
|
}
|
||
10 years ago
|
|
||
9 years ago
|
BOBCommandChannel::BOBCommandChannel (const std::string& address, int port):
|
||
10 years ago
|
m_IsRunning (false), m_Thread (nullptr),
|
||
9 years ago
|
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(address), port))
|
||
10 years ago
|
{
|
||
10 years ago
|
// command -> handler
|
||
10 years ago
|
m_CommandHandlers[BOB_COMMAND_ZAP] = &BOBCommandSession::ZapCommandHandler;
|
||
10 years ago
|
m_CommandHandlers[BOB_COMMAND_QUIT] = &BOBCommandSession::QuitCommandHandler;
|
||
10 years ago
|
m_CommandHandlers[BOB_COMMAND_START] = &BOBCommandSession::StartCommandHandler;
|
||
10 years ago
|
m_CommandHandlers[BOB_COMMAND_STOP] = &BOBCommandSession::StopCommandHandler;
|
||
10 years ago
|
m_CommandHandlers[BOB_COMMAND_SETNICK] = &BOBCommandSession::SetNickCommandHandler;
|
||
10 years ago
|
m_CommandHandlers[BOB_COMMAND_GETNICK] = &BOBCommandSession::GetNickCommandHandler;
|
||
10 years ago
|
m_CommandHandlers[BOB_COMMAND_NEWKEYS] = &BOBCommandSession::NewkeysCommandHandler;
|
||
10 years ago
|
m_CommandHandlers[BOB_COMMAND_GETKEYS] = &BOBCommandSession::GetkeysCommandHandler;
|
||
|
m_CommandHandlers[BOB_COMMAND_SETKEYS] = &BOBCommandSession::SetkeysCommandHandler;
|
||
|
m_CommandHandlers[BOB_COMMAND_GETDEST] = &BOBCommandSession::GetdestCommandHandler;
|
||
10 years ago
|
m_CommandHandlers[BOB_COMMAND_OUTHOST] = &BOBCommandSession::OuthostCommandHandler;
|
||
|
m_CommandHandlers[BOB_COMMAND_OUTPORT] = &BOBCommandSession::OutportCommandHandler;
|
||
10 years ago
|
m_CommandHandlers[BOB_COMMAND_INHOST] = &BOBCommandSession::InhostCommandHandler;
|
||
|
m_CommandHandlers[BOB_COMMAND_INPORT] = &BOBCommandSession::InportCommandHandler;
|
||
10 years ago
|
m_CommandHandlers[BOB_COMMAND_QUIET] = &BOBCommandSession::QuietCommandHandler;
|
||
10 years ago
|
m_CommandHandlers[BOB_COMMAND_LOOKUP] = &BOBCommandSession::LookupCommandHandler;
|
||
10 years ago
|
m_CommandHandlers[BOB_COMMAND_CLEAR] = &BOBCommandSession::ClearCommandHandler;
|
||
10 years ago
|
m_CommandHandlers[BOB_COMMAND_LIST] = &BOBCommandSession::ListCommandHandler;
|
||
|
m_CommandHandlers[BOB_COMMAND_OPTION] = &BOBCommandSession::OptionCommandHandler;
|
||
9 years ago
|
m_CommandHandlers[BOB_COMMAND_STATUS] = &BOBCommandSession::StatusCommandHandler;
|
||
10 years ago
|
}
|
||
|
|
||
|
BOBCommandChannel::~BOBCommandChannel ()
|
||
|
{
|
||
10 years ago
|
Stop ();
|
||
9 years ago
|
for (const auto& it: m_Destinations)
|
||
10 years ago
|
delete it.second;
|
||
10 years ago
|
}
|
||
|
|
||
|
void BOBCommandChannel::Start ()
|
||
|
{
|
||
|
Accept ();
|
||
|
m_IsRunning = true;
|
||
|
m_Thread = new std::thread (std::bind (&BOBCommandChannel::Run, this));
|
||
|
}
|
||
|
|
||
|
void BOBCommandChannel::Stop ()
|
||
|
{
|
||
10 years ago
|
m_IsRunning = false;
|
||
9 years ago
|
for (auto& it: m_Destinations)
|
||
10 years ago
|
it.second->Stop ();
|
||
10 years ago
|
m_Acceptor.cancel ();
|
||
10 years ago
|
m_Service.stop ();
|
||
|
if (m_Thread)
|
||
|
{
|
||
|
m_Thread->join ();
|
||
|
delete m_Thread;
|
||
|
m_Thread = nullptr;
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void BOBCommandChannel::Run ()
|
||
|
{
|
||
|
while (m_IsRunning)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
m_Service.run ();
|
||
|
}
|
||
|
catch (std::exception& ex)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogError, "BOB: runtime exception: ", ex.what ());
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
void BOBCommandChannel::AddDestination (const std::string& name, BOBDestination * dest)
|
||
10 years ago
|
{
|
||
10 years ago
|
m_Destinations[name] = dest;
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void BOBCommandChannel::DeleteDestination (const std::string& name)
|
||
|
{
|
||
|
auto it = m_Destinations.find (name);
|
||
|
if (it != m_Destinations.end ())
|
||
|
{
|
||
|
it->second->Stop ();
|
||
|
delete it->second;
|
||
|
m_Destinations.erase (it);
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
BOBDestination * BOBCommandChannel::FindDestination (const std::string& name)
|
||
10 years ago
|
{
|
||
10 years ago
|
auto it = m_Destinations.find (name);
|
||
|
if (it != m_Destinations.end ())
|
||
10 years ago
|
return it->second;
|
||
|
return nullptr;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
void BOBCommandChannel::Accept ()
|
||
|
{
|
||
10 years ago
|
auto newSession = std::make_shared<BOBCommandSession> (*this);
|
||
|
m_Acceptor.async_accept (newSession->GetSocket (), std::bind (&BOBCommandChannel::HandleAccept, this,
|
||
|
std::placeholders::_1, newSession));
|
||
10 years ago
|
}
|
||
|
|
||
10 years ago
|
void BOBCommandChannel::HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<BOBCommandSession> session)
|
||
10 years ago
|
{
|
||
|
if (ecode != boost::asio::error::operation_aborted)
|
||
|
Accept ();
|
||
|
|
||
|
if (!ecode)
|
||
|
{
|
||
9 years ago
|
LogPrint (eLogInfo, "BOB: New command connection from ", session->GetSocket ().remote_endpoint ());
|
||
10 years ago
|
session->SendVersion ();
|
||
10 years ago
|
}
|
||
|
else
|
||
9 years ago
|
LogPrint (eLogError, "BOB: accept error: ", ecode.message ());
|
||
10 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|