Browse Source

inbound and outbound BOB tunnels per destination

pull/113/head
orignal 10 years ago
parent
commit
07ad7fea9e
  1. 113
      BOB.cpp
  2. 35
      BOB.h

113
BOB.cpp

@ -168,10 +168,44 @@ namespace client
} }
} }
BOBDestination::BOBDestination (boost::asio::io_service& service, ClientDestination& localDestination):
m_Service (service), m_LocalDestination (localDestination),
m_OutboundTunnel (nullptr), m_InboundTunnel (nullptr)
{
}
BOBDestination::~BOBDestination ()
{
delete m_OutboundTunnel;
delete m_InboundTunnel;
}
void BOBDestination::Start ()
{
if (m_OutboundTunnel) m_OutboundTunnel->Start ();
if (m_InboundTunnel) m_InboundTunnel->Start ();
}
void BOBDestination::Stop ()
{
if (m_OutboundTunnel) m_OutboundTunnel->Stop ();
if (m_InboundTunnel) m_InboundTunnel->Stop ();
m_LocalDestination.Stop ();
}
void BOBDestination::CreateInboundTunnel (int port)
{
m_InboundTunnel = new BOBI2PInboundTunnel (m_Service, port, &m_LocalDestination);
}
void BOBDestination::CreateOutboundTunnel (const std::string& address, int port, bool quiet)
{
m_OutboundTunnel = new BOBI2POutboundTunnel (m_Service, address, port, &m_LocalDestination, quiet);
}
BOBCommandSession::BOBCommandSession (BOBCommandChannel& owner): BOBCommandSession::BOBCommandSession (BOBCommandChannel& owner):
m_Owner (owner), m_Socket (m_Owner.GetService ()), m_ReceiveBufferOffset (0), m_Owner (owner), m_Socket (m_Owner.GetService ()), m_ReceiveBufferOffset (0),
m_IsOpen (true), m_IsOutbound (false), m_IsQuiet (false), m_Port (0) m_IsOpen (true), m_IsQuiet (false), m_InPort (0), m_OutPort (0)
{ {
} }
@ -192,7 +226,6 @@ namespace client
std::placeholders::_1, std::placeholders::_2)); std::placeholders::_1, std::placeholders::_2));
} }
void BOBCommandSession::HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred) void BOBCommandSession::HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred)
{ {
if (ecode) if (ecode)
@ -322,29 +355,23 @@ namespace client
void BOBCommandSession::StartCommandHandler (const char * operand, size_t len) void BOBCommandSession::StartCommandHandler (const char * operand, size_t len)
{ {
LogPrint (eLogDebug, "BOB: start ", m_Nickname); LogPrint (eLogDebug, "BOB: start ", m_Nickname);
auto dest = context.CreateNewLocalDestination (m_Keys, true, &m_Options); BOBDestination * dest = new BOBDestination (m_Owner.GetService (),
BOBI2PTunnel * tunnel = nullptr; *context.CreateNewLocalDestination (m_Keys, true, &m_Options));
if (m_IsOutbound) if (m_InPort)
tunnel = new BOBI2POutboundTunnel (m_Owner.GetService (), m_Address, m_Port, dest, m_IsQuiet); dest->CreateInboundTunnel (m_InPort);
else if (m_OutPort && !m_Address.empty ())
tunnel = new BOBI2PInboundTunnel (m_Owner.GetService (), m_Port, dest); dest->CreateOutboundTunnel (m_Address, m_OutPort, m_IsQuiet);
if (tunnel) m_Owner.AddDestination (m_Nickname, dest);
{ dest->Start ();
m_Owner.AddTunnel (m_Nickname, tunnel);
tunnel->Start ();
SendReplyOK ("tunnel starting"); SendReplyOK ("tunnel starting");
} }
else
SendReplyError ("failed to create tunnel");
}
void BOBCommandSession::StopCommandHandler (const char * operand, size_t len) void BOBCommandSession::StopCommandHandler (const char * operand, size_t len)
{ {
auto tunnel = m_Owner.FindTunnel (m_Nickname); auto dest = m_Owner.FindDestination (m_Nickname);
if (tunnel) if (dest)
{ {
tunnel->Stop (); dest->Stop ();
tunnel->GetLocalDestination ()->Stop ();
SendReplyOK ("tunnel stopping"); SendReplyOK ("tunnel stopping");
} }
else else
@ -353,7 +380,7 @@ namespace client
void BOBCommandSession::SetNickCommandHandler (const char * operand, size_t len) void BOBCommandSession::SetNickCommandHandler (const char * operand, size_t len)
{ {
LogPrint (eLogDebug, "BOB: setnick"); LogPrint (eLogDebug, "BOB: setnick ", operand);
m_Nickname = operand; m_Nickname = operand;
std::string msg ("Nickname set to "); std::string msg ("Nickname set to ");
msg += operand; msg += operand;
@ -362,11 +389,11 @@ namespace client
void BOBCommandSession::GetNickCommandHandler (const char * operand, size_t len) void BOBCommandSession::GetNickCommandHandler (const char * operand, size_t len)
{ {
LogPrint (eLogDebug, "BOB: getnick"); LogPrint (eLogDebug, "BOB: getnick ", operand);
auto tunnel = m_Owner.FindTunnel (operand); auto dest = m_Owner.FindDestination (operand);
if (tunnel) if (dest)
{ {
m_Keys = tunnel->GetLocalDestination ()->GetPrivateKeys (); m_Keys = dest->GetKeys ();
m_Nickname = operand; m_Nickname = operand;
std::string msg ("Nickname set to "); std::string msg ("Nickname set to ");
msg += operand; msg += operand;
@ -385,7 +412,7 @@ namespace client
void BOBCommandSession::SetkeysCommandHandler (const char * operand, size_t len) void BOBCommandSession::SetkeysCommandHandler (const char * operand, size_t len)
{ {
LogPrint (eLogDebug, "BOB: setkeys"); LogPrint (eLogDebug, "BOB: setkeys ", operand);
m_Keys.FromBase64 (operand); m_Keys.FromBase64 (operand);
SendReplyOK ("keys set"); SendReplyOK ("keys set");
} }
@ -404,33 +431,29 @@ namespace client
void BOBCommandSession::OuthostCommandHandler (const char * operand, size_t len) void BOBCommandSession::OuthostCommandHandler (const char * operand, size_t len)
{ {
LogPrint (eLogDebug, "BOB: outhost"); LogPrint (eLogDebug, "BOB: outhost ", operand);
m_IsOutbound = true;
m_Address = operand; m_Address = operand;
SendReplyOK ("outhost set"); SendReplyOK ("outhost set");
} }
void BOBCommandSession::OutportCommandHandler (const char * operand, size_t len) void BOBCommandSession::OutportCommandHandler (const char * operand, size_t len)
{ {
LogPrint (eLogDebug, "BOB: outport"); LogPrint (eLogDebug, "BOB: outport ", operand);
m_IsOutbound = true; m_OutPort = boost::lexical_cast<int>(operand);
m_Port = boost::lexical_cast<int>(operand);
SendReplyOK ("outbound port set"); SendReplyOK ("outbound port set");
} }
void BOBCommandSession::InhostCommandHandler (const char * operand, size_t len) void BOBCommandSession::InhostCommandHandler (const char * operand, size_t len)
{ {
LogPrint (eLogDebug, "BOB: inhost"); LogPrint (eLogDebug, "BOB: inhost ", operand);
m_IsOutbound = false;
m_Address = operand; m_Address = operand;
SendReplyOK ("inhost set"); SendReplyOK ("inhost set");
} }
void BOBCommandSession::InportCommandHandler (const char * operand, size_t len) void BOBCommandSession::InportCommandHandler (const char * operand, size_t len)
{ {
LogPrint (eLogDebug, "BOB: inport"); LogPrint (eLogDebug, "BOB: inport ", operand);
m_IsOutbound = false; m_InPort = boost::lexical_cast<int>(operand);
m_Port = boost::lexical_cast<int>(operand);
SendReplyOK ("inbound port set"); SendReplyOK ("inbound port set");
} }
@ -443,7 +466,7 @@ namespace client
void BOBCommandSession::LookupCommandHandler (const char * operand, size_t len) void BOBCommandSession::LookupCommandHandler (const char * operand, size_t len)
{ {
LogPrint (eLogDebug, "BOB: lookup"); LogPrint (eLogDebug, "BOB: lookup ", operand);
i2p::data::IdentityEx addr; i2p::data::IdentityEx addr;
if (!context.GetAddressBook ().GetAddress (operand, addr)) if (!context.GetAddressBook ().GetAddress (operand, addr))
{ {
@ -463,8 +486,8 @@ namespace client
void BOBCommandSession::ListCommandHandler (const char * operand, size_t len) void BOBCommandSession::ListCommandHandler (const char * operand, size_t len)
{ {
LogPrint (eLogDebug, "BOB: list"); LogPrint (eLogDebug, "BOB: list");
auto& tunnels = m_Owner.GetTunnels (); auto& destinations = m_Owner.GetDestinations ();
for (auto it: tunnels) for (auto it: destinations)
SendData (it.first.c_str ()); SendData (it.first.c_str ());
SendReplyOK ("Listing done"); SendReplyOK ("Listing done");
} }
@ -513,7 +536,7 @@ namespace client
BOBCommandChannel::~BOBCommandChannel () BOBCommandChannel::~BOBCommandChannel ()
{ {
Stop (); Stop ();
for (auto it: m_Tunnels) for (auto it: m_Destinations)
delete it.second; delete it.second;
} }
@ -526,7 +549,7 @@ namespace client
void BOBCommandChannel::Stop () void BOBCommandChannel::Stop ()
{ {
for (auto it: m_Tunnels) for (auto it: m_Destinations)
it.second->Stop (); it.second->Stop ();
m_IsRunning = false; m_IsRunning = false;
m_Service.stop (); m_Service.stop ();
@ -553,15 +576,15 @@ namespace client
} }
} }
void BOBCommandChannel::AddTunnel (const std::string& name, BOBI2PTunnel * tunnel) void BOBCommandChannel::AddDestination (const std::string& name, BOBDestination * dest)
{ {
m_Tunnels[name] = tunnel; m_Destinations[name] = dest;
} }
BOBI2PTunnel * BOBCommandChannel::FindTunnel (const std::string& name) BOBDestination * BOBCommandChannel::FindDestination (const std::string& name)
{ {
auto it = m_Tunnels.find (name); auto it = m_Destinations.find (name);
if (it != m_Tunnels.end ()) if (it != m_Destinations.end ())
return it->second; return it->second;
return nullptr; return nullptr;
} }

35
BOB.h

@ -107,6 +107,28 @@ namespace client
bool m_IsQuiet; bool m_IsQuiet;
}; };
class BOBDestination
{
public:
BOBDestination (boost::asio::io_service& service, ClientDestination& localDestination);
~BOBDestination ();
void Start ();
void Stop ();
void CreateInboundTunnel (int port);
void CreateOutboundTunnel (const std::string& address, int port, bool quiet);
const i2p::data::PrivateKeys& GetKeys () const { return m_LocalDestination.GetPrivateKeys (); };
private:
boost::asio::io_service& m_Service;
ClientDestination& m_LocalDestination;
BOBI2POutboundTunnel * m_OutboundTunnel;
BOBI2PInboundTunnel * m_InboundTunnel;
};
class BOBCommandChannel; class BOBCommandChannel;
class BOBCommandSession: public std::enable_shared_from_this<BOBCommandSession> class BOBCommandSession: public std::enable_shared_from_this<BOBCommandSession>
{ {
@ -157,12 +179,11 @@ namespace client
boost::asio::ip::tcp::socket m_Socket; boost::asio::ip::tcp::socket m_Socket;
char m_ReceiveBuffer[BOB_COMMAND_BUFFER_SIZE + 1], m_SendBuffer[BOB_COMMAND_BUFFER_SIZE + 1]; char m_ReceiveBuffer[BOB_COMMAND_BUFFER_SIZE + 1], m_SendBuffer[BOB_COMMAND_BUFFER_SIZE + 1];
size_t m_ReceiveBufferOffset; size_t m_ReceiveBufferOffset;
bool m_IsOpen, m_IsOutbound, m_IsQuiet; bool m_IsOpen, m_IsQuiet;
std::string m_Nickname, m_Address; std::string m_Nickname, m_Address;
int m_Port; int m_InPort, m_OutPort;
i2p::data::PrivateKeys m_Keys; i2p::data::PrivateKeys m_Keys;
std::map<std::string, std::string> m_Options; std::map<std::string, std::string> m_Options;
}; };
typedef void (BOBCommandSession::*BOBCommandHandler)(const char * operand, size_t len); typedef void (BOBCommandSession::*BOBCommandHandler)(const char * operand, size_t len);
@ -177,8 +198,8 @@ namespace client
void Stop (); void Stop ();
boost::asio::io_service& GetService () { return m_Service; }; boost::asio::io_service& GetService () { return m_Service; };
void AddTunnel (const std::string& name, BOBI2PTunnel * tunnel); void AddDestination (const std::string& name, BOBDestination * dest);
BOBI2PTunnel * FindTunnel (const std::string& name); BOBDestination * FindDestination (const std::string& name);
private: private:
@ -192,13 +213,13 @@ namespace client
std::thread * m_Thread; std::thread * m_Thread;
boost::asio::io_service m_Service; boost::asio::io_service m_Service;
boost::asio::ip::tcp::acceptor m_Acceptor; boost::asio::ip::tcp::acceptor m_Acceptor;
std::map<std::string, BOBI2PTunnel *> m_Tunnels; std::map<std::string, BOBDestination *> m_Destinations;
std::map<std::string, BOBCommandHandler> m_CommandHandlers; std::map<std::string, BOBCommandHandler> m_CommandHandlers;
public: public:
const decltype(m_CommandHandlers)& GetCommandHandlers () const { return m_CommandHandlers; }; const decltype(m_CommandHandlers)& GetCommandHandlers () const { return m_CommandHandlers; };
const decltype(m_Tunnels)& GetTunnels () const { return m_Tunnels; }; const decltype(m_Destinations)& GetDestinations () const { return m_Destinations; };
}; };
} }
} }

Loading…
Cancel
Save