Browse Source

BOB outgoing tunnels

pull/113/head
orignal 10 years ago
parent
commit
60e4e52373
  1. 77
      BOB.cpp
  2. 25
      BOB.h

77
BOB.cpp

@ -1,5 +1,7 @@
#include <string.h> #include <string.h>
#include <boost/lexical_cast.hpp>
#include "Log.h" #include "Log.h"
#include "ClientContext.h"
#include "BOB.h" #include "BOB.h"
namespace i2p namespace i2p
@ -8,7 +10,7 @@ namespace client
{ {
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_IsOpen (true), m_IsOutgoing (false), m_Port (0)
{ {
} }
@ -45,14 +47,20 @@ namespace client
{ {
*eol = 0; *eol = 0;
char * operand = strchr (m_ReceiveBuffer, ' '); char * operand = strchr (m_ReceiveBuffer, ' ');
if (operand) *operand = 0; if (operand)
{
*operand = 0;
operand++;
}
else
operand = eol;
// process command // process command
auto handlers = m_Owner.GetCommandHandlers (); auto& handlers = m_Owner.GetCommandHandlers ();
auto it = handlers.find (m_ReceiveBuffer); auto it = handlers.find (m_ReceiveBuffer);
if (it != handlers.end ()) if (it != handlers.end ())
(this->*(it->second))(operand, operand ? eol - operand : 0); (this->*(it->second))(operand, eol - operand);
else else
LogPrint (eLogError, "BOB unknown command", m_ReceiveBuffer); LogPrint (eLogError, "BOB unknown command ", m_ReceiveBuffer);
m_ReceiveBufferOffset = size - (eol - m_ReceiveBuffer) - 1; m_ReceiveBufferOffset = size - (eol - m_ReceiveBuffer) - 1;
memmove (m_ReceiveBuffer, eol + 1, m_ReceiveBufferOffset); memmove (m_ReceiveBuffer, eol + 1, m_ReceiveBufferOffset);
@ -127,12 +135,64 @@ namespace client
SendReplyOK ("Bye!"); SendReplyOK ("Bye!");
} }
void BOBCommandSession::StartCommandHandler (const char * operand, size_t len)
{
LogPrint (eLogDebug, "BOB: start ", m_Nickname);
if (m_IsOutgoing)
{
auto dest = context.CreateNewLocalDestination (m_Keys, true);
I2PTunnel * tunnel = new I2PServerTunnel (m_Owner.GetService (), m_Address, m_Port, dest);
m_Owner.AddTunnel (m_Nickname, tunnel);
SendReplyOK ("tunnel starting");
}
else
SendReplyError ("not implemented");
}
void BOBCommandSession::SetNickCommandHandler (const char * operand, size_t len)
{
LogPrint (eLogDebug, "BOB: setnick");
m_Nickname = operand;
std::string msg ("Nickname set to");
msg += operand;
SendReplyOK (msg.c_str ());
}
void BOBCommandSession::NewkeysCommandHandler (const char * operand, size_t len)
{
LogPrint (eLogDebug, "BOB: newkeys");
m_Keys = i2p::data::PrivateKeys::CreateRandomKeys ();
SendReplyOK (m_Keys.ToBase64 ().c_str ());
}
void BOBCommandSession::OuthostCommandHandler (const char * operand, size_t len)
{
LogPrint (eLogDebug, "BOB: outhost");
m_IsOutgoing = true;
m_Address = operand;
SendReplyOK ("outhost set");
}
void BOBCommandSession::OutportCommandHandler (const char * operand, size_t len)
{
LogPrint (eLogDebug, "BOB: outport");
m_IsOutgoing = true;
m_Port = boost::lexical_cast<int>(operand);
SendReplyOK ("outbound port set");
}
BOBCommandChannel::BOBCommandChannel (int port): BOBCommandChannel::BOBCommandChannel (int port):
m_IsRunning (false), m_Thread (nullptr), m_IsRunning (false), m_Thread (nullptr),
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)) m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port))
{ {
// command -> handler
m_CommandHandlers[BOB_COMMAND_ZAP] = &BOBCommandSession::ZapCommandHandler; m_CommandHandlers[BOB_COMMAND_ZAP] = &BOBCommandSession::ZapCommandHandler;
m_CommandHandlers[BOB_COMMAND_QUIT] = &BOBCommandSession::QuitCommandHandler; m_CommandHandlers[BOB_COMMAND_QUIT] = &BOBCommandSession::QuitCommandHandler;
m_CommandHandlers[BOB_COMMAND_START] = &BOBCommandSession::StartCommandHandler;
m_CommandHandlers[BOB_COMMAND_SETNICK] = &BOBCommandSession::SetNickCommandHandler;
m_CommandHandlers[BOB_COMMAND_NEWKEYS] = &BOBCommandSession::NewkeysCommandHandler;
m_CommandHandlers[BOB_COMMAND_OUTHOST] = &BOBCommandSession::OuthostCommandHandler;
m_CommandHandlers[BOB_COMMAND_OUTPORT] = &BOBCommandSession::OutportCommandHandler;
} }
BOBCommandChannel::~BOBCommandChannel () BOBCommandChannel::~BOBCommandChannel ()
@ -177,6 +237,11 @@ namespace client
} }
} }
void BOBCommandChannel::AddTunnel (const std::string& name, I2PTunnel * tunnel)
{
m_Tunnels[name] = tunnel;
}
void BOBCommandChannel::Accept () void BOBCommandChannel::Accept ()
{ {
auto newSession = std::make_shared<BOBCommandSession> (*this); auto newSession = std::make_shared<BOBCommandSession> (*this);

25
BOB.h

@ -8,6 +8,7 @@
#include <string> #include <string>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include "I2PTunnel.h" #include "I2PTunnel.h"
#include "Identity.h"
namespace i2p namespace i2p
{ {
@ -15,8 +16,13 @@ namespace client
{ {
const size_t BOB_COMMAND_BUFFER_SIZE = 1024; const size_t BOB_COMMAND_BUFFER_SIZE = 1024;
const char BOB_COMMAND_ZAP[] = "zap"; const char BOB_COMMAND_ZAP[] = "zap";
const char BOB_COMMAND_QUIT[] = "quit"; const char BOB_COMMAND_QUIT[] = "quit";
const char BOB_COMMAND_START[] = "start";
const char BOB_COMMAND_SETNICK[] = "setnick";
const char BOB_COMMAND_NEWKEYS[] = "newkeys";
const char BOB_COMMAND_OUTHOST[] = "outhost";
const char BOB_COMMAND_OUTPORT[] = "outport";
const char BOB_REPLY_OK[] = "OK %s\n"; const char BOB_REPLY_OK[] = "OK %s\n";
const char BOB_REPLY_ERROR[] = "ERROR %s\n"; const char BOB_REPLY_ERROR[] = "ERROR %s\n";
@ -35,7 +41,12 @@ namespace client
// command handlers // command handlers
void ZapCommandHandler (const char * operand, size_t len); void ZapCommandHandler (const char * operand, size_t len);
void QuitCommandHandler (const char * operand, size_t len); void QuitCommandHandler (const char * operand, size_t len);
void StartCommandHandler (const char * operand, size_t len);
void SetNickCommandHandler (const char * operand, size_t len);
void NewkeysCommandHandler (const char * operand, size_t len);
void OuthostCommandHandler (const char * operand, size_t len);
void OutportCommandHandler (const char * operand, size_t len);
private: private:
void HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred); void HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
@ -51,7 +62,10 @@ 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; bool m_IsOpen, m_IsOutgoing;
std::string m_Nickname, m_Address;
int m_Port;
i2p::data::PrivateKeys m_Keys;
}; };
typedef void (BOBCommandSession::*BOBCommandHandler)(const char * operand, size_t len); typedef void (BOBCommandSession::*BOBCommandHandler)(const char * operand, size_t len);
@ -67,7 +81,8 @@ namespace client
boost::asio::io_service& GetService () { return m_Service; }; boost::asio::io_service& GetService () { return m_Service; };
std::map<std::string, BOBCommandHandler>& GetCommandHandlers () { return m_CommandHandlers; }; std::map<std::string, BOBCommandHandler>& GetCommandHandlers () { return m_CommandHandlers; };
void AddTunnel (const std::string& name, I2PTunnel * tunnel);
private: private:
void Run (); void Run ();

Loading…
Cancel
Save