i2pd/libi2pd_client/BOB.h

284 lines
10 KiB
C
Raw Normal View History

/*
* Copyright (c) 2013-2020, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/
2014-12-02 15:34:02 +00:00
#ifndef BOB_H__
#define BOB_H__
2014-12-02 20:47:44 +00:00
#include <inttypes.h>
2014-12-02 16:42:35 +00:00
#include <thread>
#include <memory>
2014-12-02 20:47:44 +00:00
#include <map>
#include <string>
2014-12-02 16:42:35 +00:00
#include <boost/asio.hpp>
#include "util.h"
2014-12-02 20:47:44 +00:00
#include "I2PTunnel.h"
#include "I2PService.h"
2014-12-03 02:45:01 +00:00
#include "Identity.h"
2014-12-03 19:48:41 +00:00
#include "LeaseSet.h"
2014-12-02 16:42:35 +00:00
2014-12-02 15:34:02 +00:00
namespace i2p
{
namespace client
{
2014-12-02 20:47:44 +00:00
const size_t BOB_COMMAND_BUFFER_SIZE = 1024;
const char BOB_COMMAND_ZAP[] = "zap";
2014-12-03 02:45:01 +00:00
const char BOB_COMMAND_QUIT[] = "quit";
const char BOB_COMMAND_START[] = "start";
2018-01-06 03:48:51 +00:00
const char BOB_COMMAND_STOP[] = "stop";
2014-12-03 20:24:30 +00:00
const char BOB_COMMAND_SETNICK[] = "setnick";
2018-01-06 03:48:51 +00:00
const char BOB_COMMAND_GETNICK[] = "getnick";
2014-12-04 02:01:40 +00: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";
2018-01-06 03:48:51 +00:00
const char BOB_COMMAND_OUTHOST[] = "outhost";
2014-12-03 02:45:01 +00:00
const char BOB_COMMAND_OUTPORT[] = "outport";
2018-01-06 03:48:51 +00:00
const char BOB_COMMAND_INHOST[] = "inhost";
2014-12-03 20:02:19 +00:00
const char BOB_COMMAND_INPORT[] = "inport";
2014-12-05 21:03:43 +00:00
const char BOB_COMMAND_QUIET[] = "quiet";
2018-01-06 03:48:51 +00:00
const char BOB_COMMAND_LOOKUP[] = "lookup";
2021-02-21 21:20:57 +00:00
const char BOB_COMMAND_LOOKUP_LOCAL[] = "lookuplocal";
2014-12-06 03:25:31 +00:00
const char BOB_COMMAND_CLEAR[] = "clear";
const char BOB_COMMAND_LIST[] = "list";
const char BOB_COMMAND_OPTION[] = "option";
2018-01-06 03:48:51 +00:00
const char BOB_COMMAND_STATUS[] = "status";
2019-05-29 16:05:03 +00:00
const char BOB_COMMAND_HELP[] = "help";
2019-05-29 16:05:03 +00:00
const char BOB_HELP_ZAP[] = "zap - Shuts down BOB.";
const char BOB_HELP_QUIT[] = "quit - Quits this session with BOB.";
const char BOB_HELP_START[] = "start - Starts the current nicknamed tunnel.";
const char BOB_HELP_STOP[] = "stop - Stops the current nicknamed tunnel.";
const char BOB_HELP_SETNICK[] = "setnick <NICKNAME> - Creates a new nickname.";
const char BOB_HELP_GETNICK[] = "getnick <TUNNELNAME> - Sets the nickname from the database.";
const char BOB_HELP_NEWKEYS[] = "newkeys - Generate a new keypair for the current nickname.";
const char BOB_HELP_GETKEYS[] = "getkeys - Return the keypair for the current nickname.";
const char BOB_HELP_SETKEYS[] = "setkeys <BASE64_KEYPAIR> - Sets the keypair for the current nickname.";
const char BOB_HELP_GETDEST[] = "getdest - Return the destination for the current nickname.";
const char BOB_HELP_OUTHOST[] = "outhost <HOSTNAME|IP> - Set the outhound hostname or IP.";
const char BOB_HELP_OUTPORT[] = "outport <PORT_NUMBER> - Set the outbound port that nickname contacts.";
const char BOB_HELP_INHOST[] = "inhost <HOSTNAME|IP> - Set the inbound hostname or IP.";
const char BOB_HELP_INPORT[] = "inport <PORT_NUMBER> - Set the inbound port number nickname listens on.";
2021-11-12 16:33:51 +00:00
const char BOB_HELP_QUIET[] = "quiet <True|False> - Whether to send the incoming destination.";
2019-05-29 16:05:03 +00:00
const char BOB_HELP_LOOKUP[] = "lookup <I2P_HOSTNAME> - Look up an I2P hostname.";
const char BOB_HELP_CLEAR[] = "clear - Clear the current nickname out of the list.";
const char BOB_HELP_LIST[] = "list - List all tunnels.";
const char BOB_HELP_OPTION[] = "option <KEY>=<VALUE> - Set an option. NOTE: Don't use any spaces.";
const char BOB_HELP_STATUS[] = "status <NICKNAME> - Display status of a nicknamed tunnel.";
const char BOB_HELP_HELP [] = "help <COMMAND> - Get help on a command.";
2014-12-06 03:25:31 +00:00
class BOBI2PTunnel: public I2PService
2014-12-06 03:25:31 +00:00
{
public:
2018-01-06 03:48:51 +00:00
BOBI2PTunnel (std::shared_ptr<ClientDestination> localDestination):
I2PService (localDestination) {};
2014-12-02 20:47:44 +00:00
2014-12-06 03:25:31 +00:00
virtual void Start () {};
2018-01-06 03:48:51 +00:00
virtual void Stop () {};
};
2014-12-06 03:25:31 +00:00
class BOBI2PInboundTunnel: public BOBI2PTunnel
2014-12-03 19:48:41 +00:00
{
struct AddressReceiver
{
std::shared_ptr<boost::asio::ip::tcp::socket> socket;
char buffer[BOB_COMMAND_BUFFER_SIZE + 1]; // for destination base64 address
uint8_t * data; // pointer to buffer
size_t dataLen, bufferOffset;
2014-12-07 14:48:03 +00:00
AddressReceiver (): data (nullptr), dataLen (0), bufferOffset (0) {};
};
2018-01-06 03:48:51 +00:00
2014-12-03 19:48:41 +00:00
public:
BOBI2PInboundTunnel (const boost::asio::ip::tcp::endpoint& ep, std::shared_ptr<ClientDestination> localDestination);
2014-12-03 19:48:41 +00:00
~BOBI2PInboundTunnel ();
void Start ();
void Stop ();
private:
void Accept ();
2015-04-06 19:02:37 +00:00
void HandleAccept (const boost::system::error_code& ecode, std::shared_ptr<AddressReceiver> receiver);
2014-12-03 19:48:41 +00:00
2015-04-06 19:02:37 +00:00
void ReceiveAddress (std::shared_ptr<AddressReceiver> receiver);
2014-12-03 19:48:41 +00:00
void HandleReceivedAddress (const boost::system::error_code& ecode, std::size_t bytes_transferred,
2015-04-06 19:02:37 +00:00
std::shared_ptr<AddressReceiver> receiver);
2014-12-03 19:48:41 +00:00
void HandleDestinationRequestComplete (std::shared_ptr<i2p::data::LeaseSet> leaseSet, std::shared_ptr<AddressReceiver> receiver);
2014-12-03 19:48:41 +00:00
2015-04-06 19:02:37 +00:00
void CreateConnection (std::shared_ptr<AddressReceiver> receiver, std::shared_ptr<const i2p::data::LeaseSet> leaseSet);
2014-12-03 19:48:41 +00:00
private:
2018-01-06 03:48:51 +00:00
boost::asio::ip::tcp::acceptor m_Acceptor;
2014-12-03 19:48:41 +00:00
};
2014-12-06 03:25:31 +00:00
class BOBI2POutboundTunnel: public BOBI2PTunnel
2014-12-05 19:13:16 +00:00
{
public:
BOBI2POutboundTunnel (const std::string& outhost, int port, std::shared_ptr<ClientDestination> localDestination, bool quiet);
2014-12-05 19:13:16 +00:00
void Start ();
void Stop ();
void SetQuiet () { m_IsQuiet = true; };
private:
void Accept ();
void HandleAccept (std::shared_ptr<i2p::stream::Stream> stream);
private:
2018-01-06 03:48:51 +00:00
boost::asio::ip::tcp::endpoint m_Endpoint;
bool m_IsQuiet;
2014-12-05 19:13:16 +00:00
};
class BOBDestination
{
public:
2019-05-29 16:05:03 +00:00
BOBDestination (std::shared_ptr<ClientDestination> localDestination,
const std::string &nickname, const std::string &inhost, const std::string &outhost,
const int inport, const int outport, const bool quiet);
~BOBDestination ();
void Start ();
void Stop ();
2014-12-08 02:04:02 +00:00
void StopTunnels ();
2019-05-29 16:05:03 +00:00
void CreateInboundTunnel (int port, const std::string& inhost);
void CreateOutboundTunnel (const std::string& outhost, int port, bool quiet);
const std::string& GetNickname() const { return m_Nickname; }
const std::string& GetInHost() const { return m_InHost; }
const std::string& GetOutHost() const { return m_OutHost; }
int GetInPort() const { return m_InPort; }
int GetOutPort() const { return m_OutPort; }
bool GetQuiet() const { return m_Quiet; }
bool IsRunning() const { return m_IsRunning; }
2015-02-24 20:40:50 +00:00
const i2p::data::PrivateKeys& GetKeys () const { return m_LocalDestination->GetPrivateKeys (); };
std::shared_ptr<ClientDestination> GetLocalDestination () const { return m_LocalDestination; };
2018-01-06 03:48:51 +00:00
private:
2015-02-24 20:40:50 +00:00
std::shared_ptr<ClientDestination> m_LocalDestination;
BOBI2POutboundTunnel * m_OutboundTunnel;
BOBI2PInboundTunnel * m_InboundTunnel;
2019-05-29 16:05:03 +00:00
std::string m_Nickname;
std::string m_InHost, m_OutHost;
int m_InPort, m_OutPort;
bool m_Quiet;
bool m_IsRunning;
2018-01-06 03:48:51 +00:00
};
2014-12-02 20:47:44 +00:00
class BOBCommandChannel;
class BOBCommandSession: public std::enable_shared_from_this<BOBCommandSession>
2014-12-02 16:42:35 +00:00
{
public:
2014-12-02 20:47:44 +00:00
BOBCommandSession (BOBCommandChannel& owner);
2018-01-06 03:48:51 +00:00
~BOBCommandSession ();
2014-12-02 21:45:51 +00:00
void Terminate ();
2014-12-02 20:47:44 +00:00
boost::asio::ip::tcp::socket& GetSocket () { return m_Socket; };
2014-12-05 21:03:43 +00:00
void SendVersion ();
2014-12-02 20:47:44 +00:00
// command handlers
void ZapCommandHandler (const char * operand, size_t len);
2014-12-02 21:45:51 +00:00
void QuitCommandHandler (const char * operand, size_t len);
2014-12-03 02:45:01 +00:00
void StartCommandHandler (const char * operand, size_t len);
2014-12-03 20:24:30 +00:00
void StopCommandHandler (const char * operand, size_t len);
2014-12-03 02:45:01 +00:00
void SetNickCommandHandler (const char * operand, size_t len);
2014-12-03 20:24:30 +00:00
void GetNickCommandHandler (const char * operand, size_t len);
2014-12-03 02:45:01 +00:00
void NewkeysCommandHandler (const char * operand, size_t len);
2014-12-04 02:01:40 +00: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 02:45:01 +00:00
void OuthostCommandHandler (const char * operand, size_t len);
void OutportCommandHandler (const char * operand, size_t len);
2014-12-03 20:02:19 +00:00
void InhostCommandHandler (const char * operand, size_t len);
2018-01-06 03:48:51 +00:00
void InportCommandHandler (const char * operand, size_t len);
void QuietCommandHandler (const char * operand, size_t len);
2014-12-05 21:03:43 +00:00
void LookupCommandHandler (const char * operand, size_t len);
2021-02-21 21:20:57 +00:00
void LookupLocalCommandHandler (const char * operand, size_t len);
2014-12-06 00:16:54 +00:00
void ClearCommandHandler (const char * operand, size_t len);
2014-12-06 03:25:31 +00:00
void ListCommandHandler (const char * operand, size_t len);
void OptionCommandHandler (const char * operand, size_t len);
2016-07-16 13:31:33 +00:00
void StatusCommandHandler (const char * operand, size_t len);
2019-05-29 16:05:03 +00:00
void HelpCommandHandler (const char * operand, size_t len);
2018-01-06 03:48:51 +00:00
2014-12-02 21:45:51 +00:00
private:
2014-12-05 21:03:43 +00:00
void Receive ();
2019-05-29 16:05:03 +00:00
void HandleReceivedLine(const boost::system::error_code& ecode, std::size_t bytes_transferred);
2014-12-02 21:45:51 +00:00
void HandleReceived (const boost::system::error_code& ecode, std::size_t bytes_transferred);
2019-05-29 16:05:03 +00:00
void Send ();
2014-12-02 21:45:51 +00:00
void HandleSent (const boost::system::error_code& ecode, std::size_t bytes_transferred);
2019-05-29 16:05:03 +00:00
void SendReplyOK (const char * msg = nullptr);
2014-12-02 21:45:51 +00:00
void SendReplyError (const char * msg);
void SendRaw (const char * data);
2019-05-29 16:05:03 +00:00
void BuildStatusLine(bool currentTunnel, BOBDestination *destination, std::string &out);
2014-12-05 21:03:43 +00:00
2014-12-02 16:42:35 +00:00
private:
2014-12-02 20:47:44 +00:00
BOBCommandChannel& m_Owner;
boost::asio::ip::tcp::socket m_Socket;
boost::asio::streambuf m_ReceiveBuffer, m_SendBuffer;
2016-07-21 18:02:13 +00:00
bool m_IsOpen, m_IsQuiet, m_IsActive;
2019-05-29 16:05:03 +00:00
std::string m_Nickname, m_InHost, m_OutHost;
int m_InPort, m_OutPort;
2014-12-03 02:45:01 +00:00
i2p::data::PrivateKeys m_Keys;
2018-01-06 03:48:51 +00:00
std::map<std::string, std::string> m_Options;
2014-12-08 02:04:02 +00:00
BOBDestination * m_CurrentDestination;
2014-12-02 20:47:44 +00:00
};
typedef void (BOBCommandSession::*BOBCommandHandler)(const char * operand, size_t len);
2014-12-02 15:34:02 +00:00
class BOBCommandChannel: private i2p::util::RunnableService
2014-12-02 15:34:02 +00:00
{
public:
2015-11-30 14:44:32 +00:00
BOBCommandChannel (const std::string& address, int port);
2014-12-02 15:34:02 +00:00
~BOBCommandChannel ();
2014-12-02 16:42:35 +00:00
void Start ();
void Stop ();
boost::asio::io_service& GetService () { return GetIOService (); };
void AddDestination (const std::string& name, BOBDestination * dest);
2014-12-08 02:04:02 +00:00
void DeleteDestination (const std::string& name);
BOBDestination * FindDestination (const std::string& name);
2018-01-06 03:48:51 +00:00
2014-12-02 16:42:35 +00:00
private:
void Accept ();
2014-12-02 20:47:44 +00:00
void HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<BOBCommandSession> session);
2014-12-02 16:42:35 +00:00
private:
boost::asio::ip::tcp::acceptor m_Acceptor;
std::map<std::string, BOBDestination *> m_Destinations;
2014-12-02 20:47:44 +00:00
std::map<std::string, BOBCommandHandler> m_CommandHandlers;
2019-05-29 16:05:03 +00:00
std::map<std::string, std::string> m_HelpStrings;
2014-12-06 03:25:31 +00:00
public:
const decltype(m_CommandHandlers)& GetCommandHandlers () const { return m_CommandHandlers; };
2019-05-29 16:05:03 +00:00
const decltype(m_HelpStrings)& GetHelpStrings () const { return m_HelpStrings; };
const decltype(m_Destinations)& GetDestinations () const { return m_Destinations; };
2018-01-06 03:48:51 +00:00
};
2014-12-02 15:34:02 +00:00
}
}
#endif