2020-05-22 13:18:41 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2015-01-07 18:26:44 +00:00
|
|
|
#ifndef I2P_CONTROL_H__
|
|
|
|
#define I2P_CONTROL_H__
|
|
|
|
|
2015-01-07 21:09:32 +00:00
|
|
|
#include <inttypes.h>
|
2015-01-07 18:26:44 +00:00
|
|
|
#include <thread>
|
2015-01-07 21:09:32 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <array>
|
|
|
|
#include <string>
|
2015-03-20 18:45:22 +00:00
|
|
|
#include <sstream>
|
2015-01-07 21:09:32 +00:00
|
|
|
#include <map>
|
2015-03-06 16:33:54 +00:00
|
|
|
#include <set>
|
2015-01-07 18:26:44 +00:00
|
|
|
#include <boost/asio.hpp>
|
2018-01-06 03:48:51 +00:00
|
|
|
#include <boost/asio/ssl.hpp>
|
2015-03-20 15:36:57 +00:00
|
|
|
#include <boost/property_tree/ptree.hpp>
|
2015-01-07 18:26:44 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace client
|
|
|
|
{
|
2015-01-07 21:09:32 +00:00
|
|
|
const size_t I2P_CONTROL_MAX_REQUEST_SIZE = 1024;
|
2016-01-31 19:52:20 +00:00
|
|
|
typedef std::array<char, I2P_CONTROL_MAX_REQUEST_SIZE> I2PControlBuffer;
|
2015-01-07 21:09:32 +00:00
|
|
|
|
2015-11-23 16:56:00 +00:00
|
|
|
const long I2P_CONTROL_CERTIFICATE_VALIDITY = 365*10; // 10 years
|
|
|
|
const char I2P_CONTROL_CERTIFICATE_COMMON_NAME[] = "i2pd.i2pcontrol";
|
|
|
|
const char I2P_CONTROL_CERTIFICATE_ORGANIZATION[] = "Purple I2P";
|
|
|
|
|
2015-01-07 18:26:44 +00:00
|
|
|
class I2PControlService
|
|
|
|
{
|
2015-11-23 18:22:02 +00:00
|
|
|
typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket;
|
2020-03-01 10:25:50 +00:00
|
|
|
|
2015-01-07 18:26:44 +00:00
|
|
|
public:
|
|
|
|
|
2015-11-30 14:44:32 +00:00
|
|
|
I2PControlService (const std::string& address, int port);
|
2015-01-07 21:09:32 +00:00
|
|
|
~I2PControlService ();
|
|
|
|
|
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void Run ();
|
|
|
|
void Accept ();
|
2016-01-31 19:52:20 +00:00
|
|
|
void HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<ssl_socket> socket);
|
2015-11-25 18:11:02 +00:00
|
|
|
void Handshake (std::shared_ptr<ssl_socket> socket);
|
|
|
|
void HandleHandshake (const boost::system::error_code& ecode, std::shared_ptr<ssl_socket> socket);
|
2015-11-23 18:22:02 +00:00
|
|
|
void ReadRequest (std::shared_ptr<ssl_socket> socket);
|
2016-01-31 19:52:20 +00:00
|
|
|
void HandleRequestReceived (const boost::system::error_code& ecode, size_t bytes_transferred,
|
2015-11-23 18:22:02 +00:00
|
|
|
std::shared_ptr<ssl_socket> socket, std::shared_ptr<I2PControlBuffer> buf);
|
|
|
|
void SendResponse (std::shared_ptr<ssl_socket> socket,
|
2017-01-06 14:59:22 +00:00
|
|
|
std::shared_ptr<I2PControlBuffer> buf, std::ostringstream& response, bool isHtml);
|
2015-01-08 18:28:51 +00:00
|
|
|
void HandleResponseSent (const boost::system::error_code& ecode, std::size_t bytes_transferred,
|
2015-11-23 18:22:02 +00:00
|
|
|
std::shared_ptr<ssl_socket> socket, std::shared_ptr<I2PControlBuffer> buf);
|
2015-01-08 18:28:51 +00:00
|
|
|
|
2016-01-22 00:00:00 +00:00
|
|
|
void CreateCertificate (const char *crt_path, const char *key_path);
|
2015-11-23 16:56:00 +00:00
|
|
|
|
2015-01-08 18:28:51 +00:00
|
|
|
private:
|
|
|
|
|
2015-03-20 18:45:22 +00:00
|
|
|
void InsertParam (std::ostringstream& ss, const std::string& name, int value) const;
|
|
|
|
void InsertParam (std::ostringstream& ss, const std::string& name, double value) const;
|
|
|
|
void InsertParam (std::ostringstream& ss, const std::string& name, const std::string& value) const;
|
2018-03-03 10:52:11 +00:00
|
|
|
void InsertParam (std::ostringstream& ss, const std::string& name, const boost::property_tree::ptree& value) const;
|
2015-03-20 15:36:57 +00:00
|
|
|
|
2015-01-12 18:38:16 +00:00
|
|
|
// methods
|
2015-03-20 18:45:22 +00:00
|
|
|
typedef void (I2PControlService::*MethodHandler)(const boost::property_tree::ptree& params, std::ostringstream& results);
|
2015-01-12 18:38:16 +00:00
|
|
|
|
2015-03-20 18:45:22 +00:00
|
|
|
void AuthenticateHandler (const boost::property_tree::ptree& params, std::ostringstream& results);
|
|
|
|
void EchoHandler (const boost::property_tree::ptree& params, std::ostringstream& results);
|
|
|
|
void I2PControlHandler (const boost::property_tree::ptree& params, std::ostringstream& results);
|
|
|
|
void RouterInfoHandler (const boost::property_tree::ptree& params, std::ostringstream& results);
|
|
|
|
void RouterManagerHandler (const boost::property_tree::ptree& params, std::ostringstream& results);
|
2016-01-31 19:52:20 +00:00
|
|
|
void NetworkSettingHandler (const boost::property_tree::ptree& params, std::ostringstream& results);
|
2018-03-03 10:52:11 +00:00
|
|
|
void ClientServicesInfoHandler (const boost::property_tree::ptree& params, std::ostringstream& results);
|
2015-01-12 19:31:45 +00:00
|
|
|
|
2015-01-15 21:42:28 +00:00
|
|
|
// I2PControl
|
|
|
|
typedef void (I2PControlService::*I2PControlRequestHandler)(const std::string& value);
|
2015-12-08 15:40:43 +00:00
|
|
|
void PasswordHandler (const std::string& value);
|
2015-01-15 21:42:28 +00:00
|
|
|
|
2015-01-12 18:38:16 +00:00
|
|
|
// RouterInfo
|
2015-03-20 18:45:22 +00:00
|
|
|
typedef void (I2PControlService::*RouterInfoRequestHandler)(std::ostringstream& results);
|
|
|
|
void UptimeHandler (std::ostringstream& results);
|
|
|
|
void VersionHandler (std::ostringstream& results);
|
|
|
|
void StatusHandler (std::ostringstream& results);
|
|
|
|
void NetDbKnownPeersHandler (std::ostringstream& results);
|
2016-01-31 19:52:20 +00:00
|
|
|
void NetDbActivePeersHandler (std::ostringstream& results);
|
|
|
|
void NetStatusHandler (std::ostringstream& results);
|
2015-03-20 18:45:22 +00:00
|
|
|
void TunnelsParticipatingHandler (std::ostringstream& results);
|
2017-01-31 17:12:39 +00:00
|
|
|
void TunnelsSuccessRateHandler (std::ostringstream& results);
|
2015-03-20 18:45:22 +00:00
|
|
|
void InboundBandwidth1S (std::ostringstream& results);
|
|
|
|
void OutboundBandwidth1S (std::ostringstream& results);
|
2016-01-31 19:52:20 +00:00
|
|
|
void NetTotalReceivedBytes (std::ostringstream& results);
|
|
|
|
void NetTotalSentBytes (std::ostringstream& results);
|
2015-01-12 18:38:16 +00:00
|
|
|
|
|
|
|
// RouterManager
|
2015-03-20 18:45:22 +00:00
|
|
|
typedef void (I2PControlService::*RouterManagerRequestHandler)(std::ostringstream& results);
|
|
|
|
void ShutdownHandler (std::ostringstream& results);
|
|
|
|
void ShutdownGracefulHandler (std::ostringstream& results);
|
|
|
|
void ReseedHandler (std::ostringstream& results);
|
2015-01-12 18:38:16 +00:00
|
|
|
|
2015-01-12 19:31:45 +00:00
|
|
|
// NetworkSetting
|
2016-01-31 19:52:20 +00:00
|
|
|
typedef void (I2PControlService::*NetworkSettingRequestHandler)(const std::string& value, std::ostringstream& results);
|
2016-04-05 00:00:00 +00:00
|
|
|
void InboundBandwidthLimit (const std::string& value, std::ostringstream& results);
|
|
|
|
void OutboundBandwidthLimit (const std::string& value, std::ostringstream& results);
|
2015-01-12 19:31:45 +00:00
|
|
|
|
2018-03-03 10:52:11 +00:00
|
|
|
// ClientServicesInfo
|
|
|
|
typedef void (I2PControlService::*ClientServicesInfoRequestHandler)(std::ostringstream& results);
|
|
|
|
void I2PTunnelInfoHandler (std::ostringstream& results);
|
|
|
|
void HTTPProxyInfoHandler (std::ostringstream& results);
|
|
|
|
void SOCKSInfoHandler (std::ostringstream& results);
|
|
|
|
void SAMInfoHandler (std::ostringstream& results);
|
|
|
|
void BOBInfoHandler (std::ostringstream& results);
|
|
|
|
void I2CPInfoHandler (std::ostringstream& results);
|
|
|
|
|
2015-01-07 18:26:44 +00:00
|
|
|
private:
|
|
|
|
|
2015-01-15 21:42:28 +00:00
|
|
|
std::string m_Password;
|
2015-01-07 18:26:44 +00:00
|
|
|
bool m_IsRunning;
|
2016-01-31 19:52:20 +00:00
|
|
|
std::thread * m_Thread;
|
2015-01-07 18:26:44 +00:00
|
|
|
|
|
|
|
boost::asio::io_service m_Service;
|
2015-01-10 03:27:52 +00:00
|
|
|
boost::asio::ip::tcp::acceptor m_Acceptor;
|
2015-11-23 18:22:02 +00:00
|
|
|
boost::asio::ssl::context m_SSLContext;
|
2015-01-10 03:27:52 +00:00
|
|
|
boost::asio::deadline_timer m_ShutdownTimer;
|
2015-03-06 16:33:54 +00:00
|
|
|
std::set<std::string> m_Tokens;
|
2016-01-31 19:52:20 +00:00
|
|
|
|
2015-01-12 18:38:16 +00:00
|
|
|
std::map<std::string, MethodHandler> m_MethodHandlers;
|
2015-01-15 21:42:28 +00:00
|
|
|
std::map<std::string, I2PControlRequestHandler> m_I2PControlHandlers;
|
2015-01-12 18:38:16 +00:00
|
|
|
std::map<std::string, RouterInfoRequestHandler> m_RouterInfoHandlers;
|
|
|
|
std::map<std::string, RouterManagerRequestHandler> m_RouterManagerHandlers;
|
2015-01-12 19:31:45 +00:00
|
|
|
std::map<std::string, NetworkSettingRequestHandler> m_NetworkSettingHandlers;
|
2018-03-03 10:52:11 +00:00
|
|
|
std::map<std::string, ClientServicesInfoRequestHandler> m_ClientServicesInfoHandlers;
|
2015-01-07 18:26:44 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|