2014-12-02 18:34:02 +03:00
|
|
|
#ifndef BOB_H__
|
|
|
|
#define BOB_H__
|
|
|
|
|
2014-12-02 23:47:44 +03:00
|
|
|
#include <inttypes.h>
|
2014-12-02 19:42:35 +03:00
|
|
|
#include <thread>
|
|
|
|
#include <memory>
|
2014-12-02 23:47:44 +03:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2014-12-02 19:42:35 +03:00
|
|
|
#include <boost/asio.hpp>
|
2014-12-02 23:47:44 +03:00
|
|
|
#include "I2PTunnel.h"
|
2014-12-03 05:45:01 +03:00
|
|
|
#include "Identity.h"
|
2014-12-03 22:48:41 +03:00
|
|
|
#include "LeaseSet.h"
|
2014-12-02 19:42:35 +03:00
|
|
|
|
2014-12-02 18:34:02 +03:00
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace client
|
|
|
|
{
|
2014-12-02 23:47:44 +03:00
|
|
|
const size_t BOB_COMMAND_BUFFER_SIZE = 1024;
|
|
|
|
const char BOB_COMMAND_ZAP[] = "zap";
|
2014-12-03 05:45:01 +03:00
|
|
|
const char BOB_COMMAND_QUIT[] = "quit";
|
|
|
|
const char BOB_COMMAND_START[] = "start";
|
2014-12-03 23:24:30 +03:00
|
|
|
const char BOB_COMMAND_STOP[] = "stop";
|
|
|
|
const char BOB_COMMAND_SETNICK[] = "setnick";
|
|
|
|
const char BOB_COMMAND_GETNICK[] = "getnick";
|
2014-12-04 05:01:40 +03:00
|
|
|
const char BOB_COMMAND_NEWKEYS[] = "newkeys";
|
|
|
|
const char BOB_COMMAND_GETKEYS[] = "getkeys";
|
|
|
|
const char BOB_COMMAND_SETKEYS[] = "setkeys";
|
|
|
|
const char BOB_COMMAND_GETDEST[] = "getdest";
|
2014-12-03 05:45:01 +03:00
|
|
|
const char BOB_COMMAND_OUTHOST[] = "outhost";
|
|
|
|
const char BOB_COMMAND_OUTPORT[] = "outport";
|
2014-12-03 23:02:19 +03:00
|
|
|
const char BOB_COMMAND_INHOST[] = "inhost";
|
|
|
|
const char BOB_COMMAND_INPORT[] = "inport";
|
2014-12-06 00:03:43 +03:00
|
|
|
const char BOB_COMMAND_QUIET[] = "quiet";
|
2014-12-06 03:16:54 +03:00
|
|
|
const char BOB_COMMAND_LOOKUP[] = "lookup";
|
2014-12-06 06:25:31 +03:00
|
|
|
const char BOB_COMMAND_CLEAR[] = "clear";
|
|
|
|
const char BOB_COMMAND_LIST[] = "list";
|
|
|
|
const char BOB_COMMAND_OPTION[] = "option";
|
|
|
|
|
2014-12-06 00:03:43 +03:00
|
|
|
const char BOB_VERSION[] = "BOB 00.00.10\nOK\n";
|
2014-12-03 00:45:51 +03:00
|
|
|
const char BOB_REPLY_OK[] = "OK %s\n";
|
|
|
|
const char BOB_REPLY_ERROR[] = "ERROR %s\n";
|
2014-12-06 06:25:31 +03:00
|
|
|
const char BOB_DATA[] = "NICKNAME %s\n";
|
|
|
|
|
|
|
|
class BOBI2PTunnel: public I2PTunnel
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
BOBI2PTunnel (boost::asio::io_service& service, ClientDestination * localDestination):
|
|
|
|
I2PTunnel (service, localDestination) {};
|
2014-12-02 23:47:44 +03:00
|
|
|
|
2014-12-06 06:25:31 +03:00
|
|
|
virtual void Start () {};
|
|
|
|
virtual void Stop () {};
|
|
|
|
};
|
|
|
|
|
|
|
|
class BOBI2PInboundTunnel: public BOBI2PTunnel
|
2014-12-03 22:48:41 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
BOBI2PInboundTunnel (boost::asio::io_service& service, int port, ClientDestination * localDestination);
|
|
|
|
~BOBI2PInboundTunnel ();
|
|
|
|
|
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void Accept ();
|
|
|
|
void HandleAccept (const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket);
|
|
|
|
|
|
|
|
void ReceiveAddress (boost::asio::ip::tcp::socket * socket);
|
|
|
|
void HandleReceivedAddress (const boost::system::error_code& ecode, std::size_t bytes_transferred,
|
|
|
|
boost::asio::ip::tcp::socket * socket);
|
|
|
|
|
|
|
|
void HandleDestinationRequestTimer (const boost::system::error_code& ecode, boost::asio::ip::tcp::socket * socket, i2p::data::IdentHash ident);
|
|
|
|
|
|
|
|
void CreateConnection (boost::asio::ip::tcp::socket * socket, const i2p::data::LeaseSet * leaseSet);
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
boost::asio::ip::tcp::acceptor m_Acceptor;
|
|
|
|
boost::asio::deadline_timer m_Timer;
|
|
|
|
char m_ReceiveBuffer[BOB_COMMAND_BUFFER_SIZE + 1]; // for destination base64 address
|
2014-12-04 04:37:20 +03:00
|
|
|
uint8_t * m_ReceivedData;
|
2014-12-06 23:31:39 +03:00
|
|
|
size_t m_ReceivedDataLen, m_ReceiveBufferOffset;
|
2014-12-03 22:48:41 +03:00
|
|
|
};
|
|
|
|
|
2014-12-06 06:25:31 +03:00
|
|
|
class BOBI2POutboundTunnel: public BOBI2PTunnel
|
2014-12-05 22:13:16 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
BOBI2POutboundTunnel (boost::asio::io_service& service, const std::string& address, int port,
|
|
|
|
ClientDestination * localDestination, bool quiet);
|
|
|
|
|
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
|
|
|
|
|
|
|
void SetQuiet () { m_IsQuiet = true; };
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void Accept ();
|
|
|
|
void HandleAccept (std::shared_ptr<i2p::stream::Stream> stream);
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
boost::asio::ip::tcp::endpoint m_Endpoint;
|
|
|
|
bool m_IsQuiet;
|
|
|
|
};
|
|
|
|
|
2014-12-02 23:47:44 +03:00
|
|
|
class BOBCommandChannel;
|
|
|
|
class BOBCommandSession: public std::enable_shared_from_this<BOBCommandSession>
|
2014-12-02 19:42:35 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-12-02 23:47:44 +03:00
|
|
|
BOBCommandSession (BOBCommandChannel& owner);
|
|
|
|
~BOBCommandSession ();
|
2014-12-03 00:45:51 +03:00
|
|
|
void Terminate ();
|
2014-12-02 23:47:44 +03:00
|
|
|
|
|
|
|
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
|
2014-12-06 00:03:43 +03:00
|
|
|
void SendVersion ();
|
2014-12-02 23:47:44 +03:00
|
|
|
|
|
|
|
// command handlers
|
|
|
|
void ZapCommandHandler (const char * operand, size_t len);
|
2014-12-03 00:45:51 +03:00
|
|
|
void QuitCommandHandler (const char * operand, size_t len);
|
2014-12-03 05:45:01 +03:00
|
|
|
void StartCommandHandler (const char * operand, size_t len);
|
2014-12-03 23:24:30 +03:00
|
|
|
void StopCommandHandler (const char * operand, size_t len);
|
2014-12-03 05:45:01 +03:00
|
|
|
void SetNickCommandHandler (const char * operand, size_t len);
|
2014-12-03 23:24:30 +03:00
|
|
|
void GetNickCommandHandler (const char * operand, size_t len);
|
2014-12-03 05:45:01 +03:00
|
|
|
void NewkeysCommandHandler (const char * operand, size_t len);
|
2014-12-04 05:01:40 +03:00
|
|
|
void SetkeysCommandHandler (const char * operand, size_t len);
|
|
|
|
void GetkeysCommandHandler (const char * operand, size_t len);
|
|
|
|
void GetdestCommandHandler (const char * operand, size_t len);
|
2014-12-03 05:45:01 +03:00
|
|
|
void OuthostCommandHandler (const char * operand, size_t len);
|
|
|
|
void OutportCommandHandler (const char * operand, size_t len);
|
2014-12-03 23:02:19 +03:00
|
|
|
void InhostCommandHandler (const char * operand, size_t len);
|
|
|
|
void InportCommandHandler (const char * operand, size_t len);
|
2014-12-05 22:13:16 +03:00
|
|
|
void QuietCommandHandler (const char * operand, size_t len);
|
2014-12-06 00:03:43 +03:00
|
|
|
void LookupCommandHandler (const char * operand, size_t len);
|
2014-12-06 03:16:54 +03:00
|
|
|
void ClearCommandHandler (const char * operand, size_t len);
|
2014-12-06 06:25:31 +03:00
|
|
|
void ListCommandHandler (const char * operand, size_t len);
|
|
|
|
void OptionCommandHandler (const char * operand, size_t len);
|
2014-12-06 03:16:54 +03:00
|
|
|
|
2014-12-03 00:45:51 +03:00
|
|
|
private:
|
|
|
|
|
2014-12-06 00:03:43 +03:00
|
|
|
void Receive ();
|
2014-12-03 00:45:51 +03:00
|
|
|
void HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
|
|
|
|
|
|
|
void Send (size_t len);
|
|
|
|
void HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
|
|
|
|
void SendReplyOK (const char * msg);
|
|
|
|
void SendReplyError (const char * msg);
|
2014-12-06 06:25:31 +03:00
|
|
|
void SendData (const char * nickname);
|
2014-12-06 00:03:43 +03:00
|
|
|
|
2014-12-02 19:42:35 +03:00
|
|
|
private:
|
|
|
|
|
2014-12-02 23:47:44 +03:00
|
|
|
BOBCommandChannel& m_Owner;
|
|
|
|
boost::asio::ip::tcp::socket m_Socket;
|
2014-12-03 00:45:51 +03:00
|
|
|
char m_ReceiveBuffer[BOB_COMMAND_BUFFER_SIZE + 1], m_SendBuffer[BOB_COMMAND_BUFFER_SIZE + 1];
|
2014-12-02 23:47:44 +03:00
|
|
|
size_t m_ReceiveBufferOffset;
|
2014-12-05 22:13:16 +03:00
|
|
|
bool m_IsOpen, m_IsOutbound, m_IsQuiet;
|
2014-12-03 05:45:01 +03:00
|
|
|
std::string m_Nickname, m_Address;
|
|
|
|
int m_Port;
|
|
|
|
i2p::data::PrivateKeys m_Keys;
|
2014-12-06 23:31:39 +03:00
|
|
|
std::map<std::string, std::string> m_Options;
|
2014-12-07 00:06:46 +03:00
|
|
|
|
2014-12-02 23:47:44 +03:00
|
|
|
};
|
|
|
|
typedef void (BOBCommandSession::*BOBCommandHandler)(const char * operand, size_t len);
|
2014-12-02 18:34:02 +03:00
|
|
|
|
|
|
|
class BOBCommandChannel
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
BOBCommandChannel (int port);
|
|
|
|
~BOBCommandChannel ();
|
2014-12-02 19:42:35 +03:00
|
|
|
|
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
|
|
|
|
2014-12-02 23:47:44 +03:00
|
|
|
boost::asio::io_service& GetService () { return m_Service; };
|
2014-12-06 06:25:31 +03:00
|
|
|
void AddTunnel (const std::string& name, BOBI2PTunnel * tunnel);
|
|
|
|
BOBI2PTunnel * FindTunnel (const std::string& name);
|
2014-12-03 05:45:01 +03:00
|
|
|
|
2014-12-02 19:42:35 +03:00
|
|
|
private:
|
|
|
|
|
|
|
|
void Run ();
|
|
|
|
void Accept ();
|
2014-12-02 23:47:44 +03:00
|
|
|
void HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<BOBCommandSession> session);
|
2014-12-02 19:42:35 +03:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
bool m_IsRunning;
|
|
|
|
std::thread * m_Thread;
|
|
|
|
boost::asio::io_service m_Service;
|
|
|
|
boost::asio::ip::tcp::acceptor m_Acceptor;
|
2014-12-06 06:25:31 +03:00
|
|
|
std::map<std::string, BOBI2PTunnel *> m_Tunnels;
|
2014-12-02 23:47:44 +03:00
|
|
|
std::map<std::string, BOBCommandHandler> m_CommandHandlers;
|
2014-12-06 06:25:31 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
const decltype(m_CommandHandlers)& GetCommandHandlers () const { return m_CommandHandlers; };
|
|
|
|
const decltype(m_Tunnels)& GetTunnels () const { return m_Tunnels; };
|
2014-12-02 18:34:02 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|