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.
240 lines
7.4 KiB
240 lines
7.4 KiB
10 years ago
|
#ifndef BOB_H__
|
||
|
#define BOB_H__
|
||
|
|
||
10 years ago
|
#include <inttypes.h>
|
||
10 years ago
|
#include <thread>
|
||
|
#include <memory>
|
||
10 years ago
|
#include <map>
|
||
|
#include <string>
|
||
10 years ago
|
#include <boost/asio.hpp>
|
||
10 years ago
|
#include "I2PTunnel.h"
|
||
10 years ago
|
#include "I2PService.h"
|
||
10 years ago
|
#include "Identity.h"
|
||
10 years ago
|
#include "LeaseSet.h"
|
||
10 years ago
|
|
||
10 years ago
|
namespace i2p
|
||
|
{
|
||
|
namespace client
|
||
|
{
|
||
10 years ago
|
const size_t BOB_COMMAND_BUFFER_SIZE = 1024;
|
||
|
const char BOB_COMMAND_ZAP[] = "zap";
|
||
10 years ago
|
const char BOB_COMMAND_QUIT[] = "quit";
|
||
|
const char BOB_COMMAND_START[] = "start";
|
||
10 years ago
|
const char BOB_COMMAND_STOP[] = "stop";
|
||
|
const char BOB_COMMAND_SETNICK[] = "setnick";
|
||
|
const char BOB_COMMAND_GETNICK[] = "getnick";
|
||
10 years ago
|
const char BOB_COMMAND_NEWKEYS[] = "newkeys";
|
||
|
const char BOB_COMMAND_GETKEYS[] = "getkeys";
|
||
|
const char BOB_COMMAND_SETKEYS[] = "setkeys";
|
||
|
const char BOB_COMMAND_GETDEST[] = "getdest";
|
||
10 years ago
|
const char BOB_COMMAND_OUTHOST[] = "outhost";
|
||
|
const char BOB_COMMAND_OUTPORT[] = "outport";
|
||
10 years ago
|
const char BOB_COMMAND_INHOST[] = "inhost";
|
||
|
const char BOB_COMMAND_INPORT[] = "inport";
|
||
10 years ago
|
const char BOB_COMMAND_QUIET[] = "quiet";
|
||
10 years ago
|
const char BOB_COMMAND_LOOKUP[] = "lookup";
|
||
10 years ago
|
const char BOB_COMMAND_CLEAR[] = "clear";
|
||
|
const char BOB_COMMAND_LIST[] = "list";
|
||
|
const char BOB_COMMAND_OPTION[] = "option";
|
||
9 years ago
|
const char BOB_COMMAND_STATUS[] = "status";
|
||
10 years ago
|
|
||
10 years ago
|
const char BOB_VERSION[] = "BOB 00.00.10\nOK\n";
|
||
10 years ago
|
const char BOB_REPLY_OK[] = "OK %s\n";
|
||
|
const char BOB_REPLY_ERROR[] = "ERROR %s\n";
|
||
10 years ago
|
const char BOB_DATA[] = "NICKNAME %s\n";
|
||
|
|
||
10 years ago
|
class BOBI2PTunnel: public I2PService
|
||
10 years ago
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
BOBI2PTunnel (std::shared_ptr<ClientDestination> localDestination):
|
||
10 years ago
|
I2PService (localDestination) {};
|
||
10 years ago
|
|
||
10 years ago
|
virtual void Start () {};
|
||
|
virtual void Stop () {};
|
||
|
};
|
||
|
|
||
|
class BOBI2PInboundTunnel: public BOBI2PTunnel
|
||
10 years ago
|
{
|
||
10 years ago
|
struct AddressReceiver
|
||
|
{
|
||
10 years ago
|
std::shared_ptr<boost::asio::ip::tcp::socket> socket;
|
||
10 years ago
|
char buffer[BOB_COMMAND_BUFFER_SIZE + 1]; // for destination base64 address
|
||
10 years ago
|
uint8_t * data; // pointer to buffer
|
||
10 years ago
|
size_t dataLen, bufferOffset;
|
||
|
|
||
|
AddressReceiver (): data (nullptr), dataLen (0), bufferOffset (0) {};
|
||
|
};
|
||
|
|
||
10 years ago
|
public:
|
||
|
|
||
10 years ago
|
BOBI2PInboundTunnel (int port, std::shared_ptr<ClientDestination> localDestination);
|
||
10 years ago
|
~BOBI2PInboundTunnel ();
|
||
|
|
||
|
void Start ();
|
||
|
void Stop ();
|
||
|
|
||
|
private:
|
||
|
|
||
|
void Accept ();
|
||
10 years ago
|
void HandleAccept (const boost::system::error_code& ecode, std::shared_ptr<AddressReceiver> receiver);
|
||
10 years ago
|
|
||
10 years ago
|
void ReceiveAddress (std::shared_ptr<AddressReceiver> receiver);
|
||
10 years ago
|
void HandleReceivedAddress (const boost::system::error_code& ecode, std::size_t bytes_transferred,
|
||
10 years ago
|
std::shared_ptr<AddressReceiver> receiver);
|
||
10 years ago
|
|
||
10 years ago
|
void HandleDestinationRequestComplete (std::shared_ptr<i2p::data::LeaseSet> leaseSet, std::shared_ptr<AddressReceiver> receiver);
|
||
10 years ago
|
|
||
10 years ago
|
void CreateConnection (std::shared_ptr<AddressReceiver> receiver, std::shared_ptr<const i2p::data::LeaseSet> leaseSet);
|
||
10 years ago
|
|
||
|
private:
|
||
|
|
||
|
boost::asio::ip::tcp::acceptor m_Acceptor;
|
||
|
};
|
||
|
|
||
10 years ago
|
class BOBI2POutboundTunnel: public BOBI2PTunnel
|
||
10 years ago
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
BOBI2POutboundTunnel (const std::string& address, int port, std::shared_ptr<ClientDestination> localDestination, bool quiet);
|
||
10 years ago
|
|
||
|
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;
|
||
|
};
|
||
|
|
||
10 years ago
|
|
||
|
class BOBDestination
|
||
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
BOBDestination (std::shared_ptr<ClientDestination> localDestination);
|
||
10 years ago
|
~BOBDestination ();
|
||
|
|
||
|
void Start ();
|
||
|
void Stop ();
|
||
10 years ago
|
void StopTunnels ();
|
||
10 years ago
|
void CreateInboundTunnel (int port);
|
||
|
void CreateOutboundTunnel (const std::string& address, int port, bool quiet);
|
||
10 years ago
|
const i2p::data::PrivateKeys& GetKeys () const { return m_LocalDestination->GetPrivateKeys (); };
|
||
10 years ago
|
std::shared_ptr<ClientDestination> GetLocalDestination () const { return m_LocalDestination; };
|
||
10 years ago
|
|
||
|
private:
|
||
|
|
||
10 years ago
|
std::shared_ptr<ClientDestination> m_LocalDestination;
|
||
10 years ago
|
BOBI2POutboundTunnel * m_OutboundTunnel;
|
||
|
BOBI2PInboundTunnel * m_InboundTunnel;
|
||
|
};
|
||
|
|
||
10 years ago
|
class BOBCommandChannel;
|
||
|
class BOBCommandSession: public std::enable_shared_from_this<BOBCommandSession>
|
||
10 years ago
|
{
|
||
|
public:
|
||
|
|
||
10 years ago
|
BOBCommandSession (BOBCommandChannel& owner);
|
||
|
~BOBCommandSession ();
|
||
10 years ago
|
void Terminate ();
|
||
10 years ago
|
|
||
|
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
|
||
10 years ago
|
void SendVersion ();
|
||
10 years ago
|
|
||
|
// command handlers
|
||
|
void ZapCommandHandler (const char * operand, size_t len);
|
||
10 years ago
|
void QuitCommandHandler (const char * operand, size_t len);
|
||
10 years ago
|
void StartCommandHandler (const char * operand, size_t len);
|
||
10 years ago
|
void StopCommandHandler (const char * operand, size_t len);
|
||
10 years ago
|
void SetNickCommandHandler (const char * operand, size_t len);
|
||
10 years ago
|
void GetNickCommandHandler (const char * operand, size_t len);
|
||
10 years ago
|
void NewkeysCommandHandler (const char * operand, size_t len);
|
||
10 years ago
|
void SetkeysCommandHandler (const char * operand, size_t len);
|
||
|
void GetkeysCommandHandler (const char * operand, size_t len);
|
||
|
void GetdestCommandHandler (const char * operand, size_t len);
|
||
10 years ago
|
void OuthostCommandHandler (const char * operand, size_t len);
|
||
|
void OutportCommandHandler (const char * operand, size_t len);
|
||
10 years ago
|
void InhostCommandHandler (const char * operand, size_t len);
|
||
|
void InportCommandHandler (const char * operand, size_t len);
|
||
10 years ago
|
void QuietCommandHandler (const char * operand, size_t len);
|
||
10 years ago
|
void LookupCommandHandler (const char * operand, size_t len);
|
||
10 years ago
|
void ClearCommandHandler (const char * operand, size_t len);
|
||
10 years ago
|
void ListCommandHandler (const char * operand, size_t len);
|
||
|
void OptionCommandHandler (const char * operand, size_t len);
|
||
9 years ago
|
void StatusCommandHandler (const char * operand, size_t len);
|
||
10 years ago
|
|
||
10 years ago
|
private:
|
||
|
|
||
10 years ago
|
void Receive ();
|
||
10 years ago
|
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);
|
||
10 years ago
|
void SendData (const char * nickname);
|
||
10 years ago
|
|
||
10 years ago
|
private:
|
||
|
|
||
10 years ago
|
BOBCommandChannel& m_Owner;
|
||
|
boost::asio::ip::tcp::socket m_Socket;
|
||
10 years ago
|
char m_ReceiveBuffer[BOB_COMMAND_BUFFER_SIZE + 1], m_SendBuffer[BOB_COMMAND_BUFFER_SIZE + 1];
|
||
10 years ago
|
size_t m_ReceiveBufferOffset;
|
||
9 years ago
|
bool m_IsOpen, m_IsQuiet, m_IsActive;
|
||
10 years ago
|
std::string m_Nickname, m_Address;
|
||
10 years ago
|
int m_InPort, m_OutPort;
|
||
10 years ago
|
i2p::data::PrivateKeys m_Keys;
|
||
10 years ago
|
std::map<std::string, std::string> m_Options;
|
||
10 years ago
|
BOBDestination * m_CurrentDestination;
|
||
10 years ago
|
};
|
||
|
typedef void (BOBCommandSession::*BOBCommandHandler)(const char * operand, size_t len);
|
||
10 years ago
|
|
||
|
class BOBCommandChannel
|
||
|
{
|
||
|
public:
|
||
|
|
||
9 years ago
|
BOBCommandChannel (const std::string& address, int port);
|
||
10 years ago
|
~BOBCommandChannel ();
|
||
10 years ago
|
|
||
|
void Start ();
|
||
|
void Stop ();
|
||
|
|
||
10 years ago
|
boost::asio::io_service& GetService () { return m_Service; };
|
||
10 years ago
|
void AddDestination (const std::string& name, BOBDestination * dest);
|
||
10 years ago
|
void DeleteDestination (const std::string& name);
|
||
10 years ago
|
BOBDestination * FindDestination (const std::string& name);
|
||
10 years ago
|
|
||
10 years ago
|
private:
|
||
|
|
||
|
void Run ();
|
||
|
void Accept ();
|
||
10 years ago
|
void HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<BOBCommandSession> session);
|
||
10 years ago
|
|
||
|
private:
|
||
|
|
||
|
bool m_IsRunning;
|
||
|
std::thread * m_Thread;
|
||
|
boost::asio::io_service m_Service;
|
||
|
boost::asio::ip::tcp::acceptor m_Acceptor;
|
||
10 years ago
|
std::map<std::string, BOBDestination *> m_Destinations;
|
||
10 years ago
|
std::map<std::string, BOBCommandHandler> m_CommandHandlers;
|
||
10 years ago
|
|
||
|
public:
|
||
|
|
||
|
const decltype(m_CommandHandlers)& GetCommandHandlers () const { return m_CommandHandlers; };
|
||
10 years ago
|
const decltype(m_Destinations)& GetDestinations () const { return m_Destinations; };
|
||
10 years ago
|
};
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endif
|
||
|
|