2015-01-07 21:26:44 +03:00
|
|
|
#ifndef I2P_CONTROL_H__
|
|
|
|
#define I2P_CONTROL_H__
|
|
|
|
|
2015-01-08 00:09:32 +03:00
|
|
|
#include <inttypes.h>
|
2015-01-07 21:26:44 +03:00
|
|
|
#include <thread>
|
2015-01-08 00:09:32 +03:00
|
|
|
#include <memory>
|
|
|
|
#include <array>
|
|
|
|
#include <string>
|
2015-03-20 21:45:22 +03:00
|
|
|
#include <sstream>
|
2015-01-08 00:09:32 +03:00
|
|
|
#include <map>
|
2015-03-06 19:33:54 +03:00
|
|
|
#include <set>
|
2015-01-07 21:26:44 +03:00
|
|
|
#include <boost/asio.hpp>
|
2015-11-23 21:22:02 +03:00
|
|
|
#include <boost/asio/ssl.hpp>
|
2015-03-20 18:36:57 +03:00
|
|
|
#include <boost/property_tree/ptree.hpp>
|
2015-11-23 19:56:00 +03:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include "util.h"
|
2015-01-07 21:26:44 +03:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace client
|
|
|
|
{
|
2015-01-08 00:09:32 +03:00
|
|
|
const size_t I2P_CONTROL_MAX_REQUEST_SIZE = 1024;
|
|
|
|
typedef std::array<char, I2P_CONTROL_MAX_REQUEST_SIZE> I2PControlBuffer;
|
|
|
|
|
2015-11-23 19:56:00 +03:00
|
|
|
const char I2P_CONTROL_PATH[] = "ipcontrol";
|
|
|
|
const char I2P_CONTROL_KEY_FILE[] = "key.pem";
|
|
|
|
const char I2P_CONTROL_CERT_FILE[] = "cert.pem";
|
2015-12-08 18:40:43 +03:00
|
|
|
const char I2P_CONTROL_CONFIG_FILE[] = "i2pcontrol.conf";
|
2015-01-16 00:42:28 +03:00
|
|
|
const char I2P_CONTROL_DEFAULT_PASSWORD[] = "itoopie";
|
|
|
|
|
2015-01-08 00:09:32 +03:00
|
|
|
const char I2P_CONTROL_PROPERTY_ID[] = "id";
|
|
|
|
const char I2P_CONTROL_PROPERTY_METHOD[] = "method";
|
|
|
|
const char I2P_CONTROL_PROPERTY_PARAMS[] = "params";
|
2015-01-08 21:28:51 +03:00
|
|
|
const char I2P_CONTROL_PROPERTY_RESULT[] = "result";
|
|
|
|
|
|
|
|
// methods
|
2015-01-09 00:11:40 +03:00
|
|
|
const char I2P_CONTROL_METHOD_AUTHENTICATE[] = "Authenticate";
|
2015-01-16 00:42:28 +03:00
|
|
|
const char I2P_CONTROL_METHOD_ECHO[] = "Echo";
|
|
|
|
const char I2P_CONTROL_METHOD_I2PCONTROL[] = "I2PControl";
|
2015-01-08 21:28:51 +03:00
|
|
|
const char I2P_CONTROL_METHOD_ROUTER_INFO[] = "RouterInfo";
|
2015-01-10 06:27:52 +03:00
|
|
|
const char I2P_CONTROL_METHOD_ROUTER_MANAGER[] = "RouterManager";
|
2015-01-12 22:31:45 +03:00
|
|
|
const char I2P_CONTROL_METHOD_NETWORK_SETTING[] = "NetworkSetting";
|
|
|
|
|
2015-01-09 00:11:40 +03:00
|
|
|
// params
|
|
|
|
const char I2P_CONTROL_PARAM_API[] = "API";
|
|
|
|
const char I2P_CONTROL_PARAM_PASSWORD[] = "Password";
|
|
|
|
const char I2P_CONTROL_PARAM_TOKEN[] = "Token";
|
|
|
|
const char I2P_CONTROL_PARAM_ECHO[] = "Echo";
|
|
|
|
const char I2P_CONTROL_PARAM_RESULT[] = "Result";
|
|
|
|
|
2015-01-16 00:42:28 +03:00
|
|
|
// I2PControl
|
|
|
|
const char I2P_CONTROL_I2PCONTROL_ADDRESS[] = "i2pcontrol.address";
|
|
|
|
const char I2P_CONTROL_I2PCONTROL_PASSWORD[] = "i2pcontrol.password";
|
|
|
|
const char I2P_CONTROL_I2PCONTROL_PORT[] = "i2pcontrol.port";
|
|
|
|
|
2015-01-12 21:38:16 +03:00
|
|
|
// RouterInfo requests
|
2015-02-23 22:57:57 +03:00
|
|
|
const char I2P_CONTROL_ROUTER_INFO_UPTIME[] = "i2p.router.uptime";
|
2015-03-20 04:51:18 +03:00
|
|
|
const char I2P_CONTROL_ROUTER_INFO_VERSION[] = "i2p.router.version";
|
|
|
|
const char I2P_CONTROL_ROUTER_INFO_STATUS[] = "i2p.router.status";
|
2015-01-12 21:38:16 +03:00
|
|
|
const char I2P_CONTROL_ROUTER_INFO_NETDB_KNOWNPEERS[] = "i2p.router.netdb.knownpeers";
|
2015-01-14 21:21:41 +03:00
|
|
|
const char I2P_CONTROL_ROUTER_INFO_NETDB_ACTIVEPEERS[] = "i2p.router.netdb.activepeers";
|
2015-03-20 04:51:18 +03:00
|
|
|
const char I2P_CONTROL_ROUTER_INFO_NET_STATUS[] = "i2p.router.net.status";
|
2015-03-17 22:36:15 +03:00
|
|
|
const char I2P_CONTROL_ROUTER_INFO_TUNNELS_PARTICIPATING[] = "i2p.router.net.tunnels.participating";
|
|
|
|
const char I2P_CONTROL_ROUTER_INFO_BW_IB_1S[] = "i2p.router.net.bw.inbound.1s";
|
|
|
|
const char I2P_CONTROL_ROUTER_INFO_BW_OB_1S[] = "i2p.router.net.bw.outbound.1s";
|
|
|
|
|
2015-01-12 21:38:16 +03:00
|
|
|
// RouterManager requests
|
|
|
|
const char I2P_CONTROL_ROUTER_MANAGER_SHUTDOWN[] = "Shutdown";
|
|
|
|
const char I2P_CONTROL_ROUTER_MANAGER_SHUTDOWN_GRACEFUL[] = "ShutdownGraceful";
|
2015-01-19 21:57:37 +03:00
|
|
|
const char I2P_CONTROL_ROUTER_MANAGER_RESEED[] = "Reseed";
|
|
|
|
|
2015-11-23 19:56:00 +03:00
|
|
|
// Certificate
|
|
|
|
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 21:26:44 +03:00
|
|
|
class I2PControlService
|
|
|
|
{
|
2015-11-23 21:22:02 +03:00
|
|
|
typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> ssl_socket;
|
2015-01-07 21:26:44 +03:00
|
|
|
public:
|
|
|
|
|
2015-11-30 17:44:32 +03:00
|
|
|
I2PControlService (const std::string& address, int port);
|
2015-01-08 00:09:32 +03:00
|
|
|
~I2PControlService ();
|
|
|
|
|
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2015-12-08 18:40:43 +03:00
|
|
|
void LoadConfig ();
|
|
|
|
void SaveConfig ();
|
|
|
|
|
2015-01-08 00:09:32 +03:00
|
|
|
void Run ();
|
|
|
|
void Accept ();
|
2015-11-23 21:22:02 +03:00
|
|
|
void HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<ssl_socket> socket);
|
2015-11-25 21:11:02 +03: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 21:22:02 +03:00
|
|
|
void ReadRequest (std::shared_ptr<ssl_socket> socket);
|
2015-01-08 00:09:32 +03:00
|
|
|
void HandleRequestReceived (const boost::system::error_code& ecode, size_t bytes_transferred,
|
2015-11-23 21:22:02 +03:00
|
|
|
std::shared_ptr<ssl_socket> socket, std::shared_ptr<I2PControlBuffer> buf);
|
|
|
|
void SendResponse (std::shared_ptr<ssl_socket> socket,
|
2015-03-20 21:45:22 +03:00
|
|
|
std::shared_ptr<I2PControlBuffer> buf, std::ostringstream& response, bool isHtml);
|
2015-01-08 21:28:51 +03:00
|
|
|
void HandleResponseSent (const boost::system::error_code& ecode, std::size_t bytes_transferred,
|
2015-11-23 21:22:02 +03:00
|
|
|
std::shared_ptr<ssl_socket> socket, std::shared_ptr<I2PControlBuffer> buf);
|
2015-01-08 21:28:51 +03:00
|
|
|
|
2015-11-23 19:56:00 +03:00
|
|
|
boost::filesystem::path GetPath () const { return i2p::util::filesystem::GetDefaultDataDir() / I2P_CONTROL_PATH; };
|
|
|
|
void CreateCertificate ();
|
|
|
|
|
|
|
|
|
2015-01-08 21:28:51 +03:00
|
|
|
private:
|
|
|
|
|
2015-03-20 21:45:22 +03: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;
|
2015-03-20 18:36:57 +03:00
|
|
|
|
2015-01-12 21:38:16 +03:00
|
|
|
// methods
|
2015-03-20 21:45:22 +03:00
|
|
|
typedef void (I2PControlService::*MethodHandler)(const boost::property_tree::ptree& params, std::ostringstream& results);
|
2015-01-12 21:38:16 +03:00
|
|
|
|
2015-03-20 21:45:22 +03: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);
|
|
|
|
void NetworkSettingHandler (const boost::property_tree::ptree& params, std::ostringstream& results);
|
2015-01-12 22:31:45 +03:00
|
|
|
|
2015-01-16 00:42:28 +03:00
|
|
|
// I2PControl
|
|
|
|
typedef void (I2PControlService::*I2PControlRequestHandler)(const std::string& value);
|
2015-12-08 18:40:43 +03:00
|
|
|
void PasswordHandler (const std::string& value);
|
2015-01-16 00:42:28 +03:00
|
|
|
|
2015-01-12 21:38:16 +03:00
|
|
|
// RouterInfo
|
2015-03-20 21:45:22 +03: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);
|
|
|
|
void NetDbActivePeersHandler (std::ostringstream& results);
|
|
|
|
void NetStatusHandler (std::ostringstream& results);
|
|
|
|
void TunnelsParticipatingHandler (std::ostringstream& results);
|
|
|
|
void InboundBandwidth1S (std::ostringstream& results);
|
|
|
|
void OutboundBandwidth1S (std::ostringstream& results);
|
2015-01-12 21:38:16 +03:00
|
|
|
|
|
|
|
// RouterManager
|
2015-03-20 21:45:22 +03: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 21:38:16 +03:00
|
|
|
|
2015-01-12 22:31:45 +03:00
|
|
|
// NetworkSetting
|
2015-03-20 21:45:22 +03:00
|
|
|
typedef void (I2PControlService::*NetworkSettingRequestHandler)(const std::string& value, std::ostringstream& results);
|
2015-01-12 22:31:45 +03:00
|
|
|
|
2015-01-07 21:26:44 +03:00
|
|
|
private:
|
|
|
|
|
2015-01-16 00:42:28 +03:00
|
|
|
std::string m_Password;
|
2015-01-07 21:26:44 +03:00
|
|
|
bool m_IsRunning;
|
|
|
|
std::thread * m_Thread;
|
|
|
|
|
|
|
|
boost::asio::io_service m_Service;
|
2015-01-10 06:27:52 +03:00
|
|
|
boost::asio::ip::tcp::acceptor m_Acceptor;
|
2015-11-23 21:22:02 +03:00
|
|
|
boost::asio::ssl::context m_SSLContext;
|
2015-01-10 06:27:52 +03:00
|
|
|
boost::asio::deadline_timer m_ShutdownTimer;
|
2015-03-06 19:33:54 +03:00
|
|
|
std::set<std::string> m_Tokens;
|
2015-01-12 21:38:16 +03:00
|
|
|
|
|
|
|
std::map<std::string, MethodHandler> m_MethodHandlers;
|
2015-01-16 00:42:28 +03:00
|
|
|
std::map<std::string, I2PControlRequestHandler> m_I2PControlHandlers;
|
2015-01-12 21:38:16 +03:00
|
|
|
std::map<std::string, RouterInfoRequestHandler> m_RouterInfoHandlers;
|
|
|
|
std::map<std::string, RouterManagerRequestHandler> m_RouterManagerHandlers;
|
2015-01-12 22:31:45 +03:00
|
|
|
std::map<std::string, NetworkSettingRequestHandler> m_NetworkSettingHandlers;
|
2015-01-07 21:26:44 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|