i2pd/I2PControl.cpp

417 lines
14 KiB
C++
Raw Normal View History

// There is bug in boost 1.49 with gcc 4.7 coming with Debian Wheezy
2015-02-15 02:00:40 +00:00
#define GCC47_BOOST149 ((BOOST_VERSION == 104900) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 7))
#include "I2PControl.h"
2015-01-07 21:09:32 +00:00
#include <sstream>
2015-01-08 21:11:40 +00:00
#include <boost/lexical_cast.hpp>
2015-01-16 21:51:52 +00:00
#include <boost/date_time/local_time/local_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#if !GCC47_BOOST149
2015-01-07 18:26:44 +00:00
#include <boost/property_tree/json_parser.hpp>
#endif
2015-01-07 21:09:32 +00:00
#include "Log.h"
#include "NetDb.h"
#include "RouterContext.h"
#include "Daemon.h"
#include "Tunnel.h"
2015-01-08 21:11:40 +00:00
#include "Timestamp.h"
2015-01-14 18:21:41 +00:00
#include "Transports.h"
2015-03-20 01:51:18 +00:00
#include "version.h"
2015-01-07 18:26:44 +00:00
namespace i2p
{
namespace client
{
I2PControlService::I2PControlService (int port):
2015-01-15 21:42:28 +00:00
m_Password (I2P_CONTROL_DEFAULT_PASSWORD), m_IsRunning (false), m_Thread (nullptr),
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)),
m_ShutdownTimer (m_Service)
2015-01-07 18:26:44 +00:00
{
2015-01-12 18:38:16 +00:00
m_MethodHandlers[I2P_CONTROL_METHOD_AUTHENTICATE] = &I2PControlService::AuthenticateHandler;
m_MethodHandlers[I2P_CONTROL_METHOD_ECHO] = &I2PControlService::EchoHandler;
2015-01-15 21:42:28 +00:00
m_MethodHandlers[I2P_CONTROL_METHOD_I2PCONTROL] = &I2PControlService::I2PControlHandler;
2015-01-12 18:38:16 +00:00
m_MethodHandlers[I2P_CONTROL_METHOD_ROUTER_INFO] = &I2PControlService::RouterInfoHandler;
m_MethodHandlers[I2P_CONTROL_METHOD_ROUTER_MANAGER] = &I2PControlService::RouterManagerHandler;
2015-01-12 19:31:45 +00:00
m_MethodHandlers[I2P_CONTROL_METHOD_NETWORK_SETTING] = &I2PControlService::NetworkSettingHandler;
2015-01-12 18:38:16 +00:00
// RouterInfo
2015-02-23 19:57:57 +00:00
m_RouterInfoHandlers[I2P_CONTROL_ROUTER_INFO_UPTIME] = &I2PControlService::UptimeHandler;
2015-03-20 01:51:18 +00:00
m_RouterInfoHandlers[I2P_CONTROL_ROUTER_INFO_VERSION] = &I2PControlService::VersionHandler;
m_RouterInfoHandlers[I2P_CONTROL_ROUTER_INFO_STATUS] = &I2PControlService::StatusHandler;
2015-01-12 18:38:16 +00:00
m_RouterInfoHandlers[I2P_CONTROL_ROUTER_INFO_NETDB_KNOWNPEERS] = &I2PControlService::NetDbKnownPeersHandler;
2015-01-14 18:21:41 +00:00
m_RouterInfoHandlers[I2P_CONTROL_ROUTER_INFO_NETDB_ACTIVEPEERS] = &I2PControlService::NetDbActivePeersHandler;
2015-03-20 01:51:18 +00:00
m_RouterInfoHandlers[I2P_CONTROL_ROUTER_INFO_NET_STATUS] = &I2PControlService::NetStatusHandler;
2015-01-12 19:03:20 +00:00
m_RouterInfoHandlers[I2P_CONTROL_ROUTER_INFO_TUNNELS_PARTICIPATING] = &I2PControlService::TunnelsParticipatingHandler;
2015-03-17 19:36:15 +00:00
m_RouterInfoHandlers[I2P_CONTROL_ROUTER_INFO_BW_IB_1S] = &I2PControlService::InboundBandwidth1S ;
m_RouterInfoHandlers[I2P_CONTROL_ROUTER_INFO_BW_OB_1S] = &I2PControlService::OutboundBandwidth1S ;
2015-01-12 18:38:16 +00:00
// RouterManager
m_RouterManagerHandlers[I2P_CONTROL_ROUTER_MANAGER_SHUTDOWN] = &I2PControlService::ShutdownHandler;
m_RouterManagerHandlers[I2P_CONTROL_ROUTER_MANAGER_SHUTDOWN_GRACEFUL] = &I2PControlService::ShutdownGracefulHandler;
2015-01-19 18:57:37 +00:00
m_RouterManagerHandlers[I2P_CONTROL_ROUTER_MANAGER_RESEED] = &I2PControlService::ReseedHandler;
2015-01-07 18:26:44 +00:00
}
2015-01-07 21:09:32 +00:00
I2PControlService::~I2PControlService ()
{
Stop ();
}
void I2PControlService::Start ()
{
if (!m_IsRunning)
{
Accept ();
m_IsRunning = true;
m_Thread = new std::thread (std::bind (&I2PControlService::Run, this));
}
}
void I2PControlService::Stop ()
{
if (m_IsRunning)
{
m_IsRunning = false;
m_Acceptor.cancel ();
m_Service.stop ();
if (m_Thread)
{
m_Thread->join ();
delete m_Thread;
m_Thread = nullptr;
}
}
}
void I2PControlService::Run ()
{
while (m_IsRunning)
{
try
{
m_Service.run ();
}
catch (std::exception& ex)
{
LogPrint (eLogError, "I2PControl: ", ex.what ());
}
}
}
void I2PControlService::Accept ()
{
auto newSocket = std::make_shared<boost::asio::ip::tcp::socket> (m_Service);
m_Acceptor.async_accept (*newSocket, std::bind (&I2PControlService::HandleAccept, this,
std::placeholders::_1, newSocket));
}
void I2PControlService::HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<boost::asio::ip::tcp::socket> socket)
{
if (ecode != boost::asio::error::operation_aborted)
Accept ();
if (!ecode)
{
LogPrint (eLogInfo, "New I2PControl request from ", socket->remote_endpoint ());
2015-03-20 01:51:18 +00:00
std::this_thread::sleep_for (std::chrono::milliseconds(5));
2015-01-07 21:09:32 +00:00
ReadRequest (socket);
}
else
LogPrint (eLogError, "I2PControl accept error: ", ecode.message ());
}
void I2PControlService::ReadRequest (std::shared_ptr<boost::asio::ip::tcp::socket> socket)
{
auto request = std::make_shared<I2PControlBuffer>();
socket->async_read_some (
#if BOOST_VERSION >= 104900
boost::asio::buffer (*request),
#else
boost::asio::buffer (request->data (), request->size ()),
#endif
2015-01-07 21:09:32 +00:00
std::bind(&I2PControlService::HandleRequestReceived, this,
std::placeholders::_1, std::placeholders::_2, socket, request));
}
void I2PControlService::HandleRequestReceived (const boost::system::error_code& ecode,
size_t bytes_transferred, std::shared_ptr<boost::asio::ip::tcp::socket> socket,
std::shared_ptr<I2PControlBuffer> buf)
{
if (ecode)
{
LogPrint (eLogError, "I2PControl read error: ", ecode.message ());
}
else
{
2015-01-08 21:11:40 +00:00
try
2015-01-07 21:41:11 +00:00
{
2015-01-16 21:19:17 +00:00
bool isHtml = !memcmp (buf->data (), "POST", 4);
2015-01-08 21:11:40 +00:00
std::stringstream ss;
ss.write (buf->data (), bytes_transferred);
2015-01-16 21:19:17 +00:00
if (isHtml)
{
std::string header;
while (!ss.eof () && header != "\r")
std::getline(ss, header);
if (ss.eof ())
{
LogPrint (eLogError, "Malformed I2PControl request. HTTP header expected");
return; // TODO:
}
}
#if GCC47_BOOST149
LogPrint (eLogError, "json_read is not supported due bug in boost 1.49 with gcc 4.7");
#else
2015-01-08 21:11:40 +00:00
boost::property_tree::ptree pt;
boost::property_tree::read_json (ss, pt);
2015-01-08 21:11:40 +00:00
std::string method = pt.get<std::string>(I2P_CONTROL_PROPERTY_METHOD);
2015-01-12 18:38:16 +00:00
auto it = m_MethodHandlers.find (method);
if (it != m_MethodHandlers.end ())
2015-01-07 21:41:11 +00:00
{
std::ostringstream response;
response << "{\"id\":" << pt.get<std::string>(I2P_CONTROL_PROPERTY_ID) << ",\"result\":{";
(this->*(it->second))(pt.get_child (I2P_CONTROL_PROPERTY_PARAMS), response);
response << "},\"jsonrpc\":\"2.0\"}";
SendResponse (socket, buf, response, isHtml);
2015-01-08 21:11:40 +00:00
}
else
LogPrint (eLogWarning, "Unknown I2PControl method ", method);
#endif
2015-01-08 21:11:40 +00:00
}
catch (std::exception& ex)
{
LogPrint (eLogError, "I2PControl handle request: ", ex.what ());
}
catch (...)
{
LogPrint (eLogError, "I2PControl handle request unknown exception");
}
2015-01-07 21:09:32 +00:00
}
}
2015-01-08 18:28:51 +00:00
void I2PControlService::InsertParam (std::ostringstream& ss, const std::string& name, int value) const
2015-03-20 15:36:57 +00:00
{
ss << "\"" << name << "\":" << value;
2015-03-20 15:36:57 +00:00
}
void I2PControlService::InsertParam (std::ostringstream& ss, const std::string& name, const std::string& value) const
{
ss << "\"" << name << "\":";
if (value.length () > 0)
ss << "\"" << value << "\"";
else
ss << "null";
}
void I2PControlService::InsertParam (std::ostringstream& ss, const std::string& name, double value) const
2015-01-08 18:28:51 +00:00
{
ss << "\"" << name << "\":" << std::fixed << std::setprecision(2) << value;
}
2015-01-08 18:28:51 +00:00
void I2PControlService::SendResponse (std::shared_ptr<boost::asio::ip::tcp::socket> socket,
std::shared_ptr<I2PControlBuffer> buf, std::ostringstream& response, bool isHtml)
{
size_t len = response.str ().length (), offset = 0;
2015-01-16 21:51:52 +00:00
if (isHtml)
{
std::ostringstream header;
header << "HTTP/1.1 200 OK\r\n";
header << "Connection: close\r\n";
header << "Content-Length: " << boost::lexical_cast<std::string>(len) << "\r\n";
header << "Content-Type: application/json\r\n";
header << "Date: ";
auto facet = new boost::local_time::local_time_facet ("%a, %d %b %Y %H:%M:%S GMT");
header.imbue(std::locale (header.getloc(), facet));
header << boost::posix_time::second_clock::local_time() << "\r\n";
header << "\r\n";
offset = header.str ().size ();
memcpy (buf->data (), header.str ().c_str (), offset);
}
memcpy (buf->data () + offset, response.str ().c_str (), len);
2015-03-20 02:18:18 +00:00
boost::asio::async_write (*socket, boost::asio::buffer (buf->data (), offset + len),
2015-01-08 18:28:51 +00:00
boost::asio::transfer_all (),
std::bind(&I2PControlService::HandleResponseSent, this,
std::placeholders::_1, std::placeholders::_2, socket, buf));
}
void I2PControlService::HandleResponseSent (const boost::system::error_code& ecode, std::size_t bytes_transferred,
std::shared_ptr<boost::asio::ip::tcp::socket> socket, std::shared_ptr<I2PControlBuffer> buf)
{
if (ecode)
LogPrint (eLogError, "I2PControl write error: ", ecode.message ());
socket->close ();
}
2015-01-08 21:11:40 +00:00
// handlers
void I2PControlService::AuthenticateHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
2015-01-08 21:11:40 +00:00
{
2015-03-20 15:36:57 +00:00
int api = params.get<int> (I2P_CONTROL_PARAM_API);
auto password = params.get<std::string> (I2P_CONTROL_PARAM_PASSWORD);
2015-01-08 21:11:40 +00:00
LogPrint (eLogDebug, "I2PControl Authenticate API=", api, " Password=", password);
2015-01-15 21:42:28 +00:00
if (password != m_Password)
LogPrint (eLogError, "I2PControl Authenticate Invalid password ", password, " expected ", m_Password);
2015-03-20 15:36:57 +00:00
InsertParam (results, I2P_CONTROL_PARAM_API, api);
results << ",";
2015-03-06 16:33:54 +00:00
std::string token = boost::lexical_cast<std::string>(i2p::util::GetSecondsSinceEpoch ());
m_Tokens.insert (token);
2015-03-20 15:36:57 +00:00
InsertParam (results, I2P_CONTROL_PARAM_TOKEN, token);
2015-01-08 21:11:40 +00:00
}
void I2PControlService::EchoHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
2015-01-08 21:11:40 +00:00
{
2015-03-20 15:36:57 +00:00
auto echo = params.get<std::string> (I2P_CONTROL_PARAM_ECHO);
2015-01-08 21:11:40 +00:00
LogPrint (eLogDebug, "I2PControl Echo Echo=", echo);
2015-03-20 15:36:57 +00:00
InsertParam (results, I2P_CONTROL_PARAM_RESULT, echo);
2015-01-08 21:11:40 +00:00
}
2015-01-15 21:42:28 +00:00
// I2PControl
void I2PControlService::I2PControlHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
2015-01-15 21:42:28 +00:00
{
LogPrint (eLogDebug, "I2PControl I2PControl");
for (auto& it: params)
{
LogPrint (eLogDebug, it.first);
auto it1 = m_I2PControlHandlers.find (it.first);
if (it1 != m_I2PControlHandlers.end ())
2015-03-20 15:36:57 +00:00
(this->*(it1->second))(it.second.data ());
2015-01-15 21:42:28 +00:00
else
LogPrint (eLogError, "I2PControl NetworkSetting unknown request ", it.first);
}
}
2015-01-12 18:38:16 +00:00
// RouterInfo
void I2PControlService::RouterInfoHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
2015-01-08 18:28:51 +00:00
{
LogPrint (eLogDebug, "I2PControl RouterInfo");
for (auto it = params.begin (); it != params.end (); it++)
{
if (it != params.begin ()) results << ",";
LogPrint (eLogDebug, it->first);
auto it1 = m_RouterInfoHandlers.find (it->first);
2015-01-12 18:38:16 +00:00
if (it1 != m_RouterInfoHandlers.end ())
(this->*(it1->second))(results);
else
LogPrint (eLogError, "I2PControl RouterInfo unknown request ", it->first);
}
2015-01-08 18:28:51 +00:00
}
void I2PControlService::UptimeHandler (std::ostringstream& results)
2015-02-23 19:57:57 +00:00
{
InsertParam (results, I2P_CONTROL_ROUTER_INFO_UPTIME, (int)i2p::context.GetUptime ()*1000);
2015-02-23 19:57:57 +00:00
}
void I2PControlService::VersionHandler (std::ostringstream& results)
2015-03-20 01:51:18 +00:00
{
2015-03-20 15:36:57 +00:00
InsertParam (results, I2P_CONTROL_ROUTER_INFO_VERSION, VERSION);
2015-03-20 01:51:18 +00:00
}
void I2PControlService::StatusHandler (std::ostringstream& results)
2015-03-20 01:51:18 +00:00
{
2015-03-20 15:36:57 +00:00
InsertParam (results, I2P_CONTROL_ROUTER_INFO_STATUS, "???"); // TODO:
2015-03-20 01:51:18 +00:00
}
void I2PControlService::NetDbKnownPeersHandler (std::ostringstream& results)
2015-01-12 18:38:16 +00:00
{
2015-03-20 15:36:57 +00:00
InsertParam (results, I2P_CONTROL_ROUTER_INFO_NETDB_KNOWNPEERS, i2p::data::netdb.GetNumRouters ());
2015-01-12 18:38:16 +00:00
}
void I2PControlService::NetDbActivePeersHandler (std::ostringstream& results)
2015-01-14 18:21:41 +00:00
{
InsertParam (results, I2P_CONTROL_ROUTER_INFO_NETDB_ACTIVEPEERS, (int)i2p::transport::transports.GetPeers ().size ());
2015-01-14 18:21:41 +00:00
}
void I2PControlService::NetStatusHandler (std::ostringstream& results)
2015-03-05 20:17:16 +00:00
{
2015-03-20 15:36:57 +00:00
InsertParam (results, I2P_CONTROL_ROUTER_INFO_NET_STATUS, (int)i2p::context.GetStatus ());
2015-03-05 20:17:16 +00:00
}
void I2PControlService::TunnelsParticipatingHandler (std::ostringstream& results)
2015-01-12 19:03:20 +00:00
{
InsertParam (results, I2P_CONTROL_ROUTER_INFO_TUNNELS_PARTICIPATING, (int)i2p::tunnel::tunnels.GetTransitTunnels ().size ());
2015-01-12 19:03:20 +00:00
}
void I2PControlService::InboundBandwidth1S (std::ostringstream& results)
2015-03-17 19:36:15 +00:00
{
InsertParam (results, I2P_CONTROL_ROUTER_INFO_BW_IB_1S, (double)i2p::transport::transports.GetInBandwidth ());
2015-03-17 19:36:15 +00:00
}
void I2PControlService::OutboundBandwidth1S (std::ostringstream& results)
2015-03-17 19:36:15 +00:00
{
InsertParam (results, I2P_CONTROL_ROUTER_INFO_BW_OB_1S, (double)i2p::transport::transports.GetOutBandwidth ());
2015-03-17 19:36:15 +00:00
}
2015-01-12 18:38:16 +00:00
// RouterManager
void I2PControlService::RouterManagerHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
{
LogPrint (eLogDebug, "I2PControl RouterManager");
for (auto it = params.begin (); it != params.end (); it++)
{
if (it != params.begin ()) results << ",";
LogPrint (eLogDebug, it->first);
auto it1 = m_RouterManagerHandlers.find (it->first);
2015-01-12 18:38:16 +00:00
if (it1 != m_RouterManagerHandlers.end ())
(this->*(it1->second))(results);
else
LogPrint (eLogError, "I2PControl RouterManager unknown request ", it->first);
}
}
2015-01-12 18:38:16 +00:00
void I2PControlService::ShutdownHandler (std::ostringstream& results)
2015-01-12 18:38:16 +00:00
{
LogPrint (eLogInfo, "Shutdown requested");
2015-03-20 15:36:57 +00:00
InsertParam (results, I2P_CONTROL_ROUTER_MANAGER_SHUTDOWN, "");
2015-01-12 18:38:16 +00:00
m_ShutdownTimer.expires_from_now (boost::posix_time::seconds(1)); // 1 second to make sure response has been sent
m_ShutdownTimer.async_wait (
[](const boost::system::error_code& ecode)
{
Daemon.running = 0;
});
}
void I2PControlService::ShutdownGracefulHandler (std::ostringstream& results)
2015-01-12 18:38:16 +00:00
{
i2p::context.SetAcceptsTunnels (false);
int timeout = i2p::tunnel::tunnels.GetTransitTunnelsExpirationTimeout ();
LogPrint (eLogInfo, "Graceful shutdown requested. Will shutdown after ", timeout, " seconds");
2015-03-20 15:36:57 +00:00
InsertParam (results, I2P_CONTROL_ROUTER_MANAGER_SHUTDOWN_GRACEFUL, "");
2015-01-12 18:38:16 +00:00
m_ShutdownTimer.expires_from_now (boost::posix_time::seconds(timeout + 1)); // + 1 second
m_ShutdownTimer.async_wait (
[](const boost::system::error_code& ecode)
{
Daemon.running = 0;
});
}
2015-01-12 19:31:45 +00:00
void I2PControlService::ReseedHandler (std::ostringstream& results)
2015-01-19 18:57:37 +00:00
{
LogPrint (eLogInfo, "Reseed requested");
2015-03-20 15:36:57 +00:00
InsertParam (results, I2P_CONTROL_ROUTER_MANAGER_SHUTDOWN, "");
2015-01-19 18:57:37 +00:00
i2p::data::netdb.Reseed ();
}
// network setting
void I2PControlService::NetworkSettingHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
2015-01-12 19:31:45 +00:00
{
LogPrint (eLogDebug, "I2PControl NetworkSetting");
for (auto it = params.begin (); it != params.end (); it++)
2015-01-12 19:31:45 +00:00
{
if (it != params.begin ()) results << ",";
LogPrint (eLogDebug, it->first);
auto it1 = m_NetworkSettingHandlers.find (it->first);
2015-01-12 19:31:45 +00:00
if (it1 != m_NetworkSettingHandlers.end ())
(this->*(it1->second))(it->second.data (), results);
2015-01-12 19:31:45 +00:00
else
LogPrint (eLogError, "I2PControl NetworkSetting unknown request ", it->first);
2015-01-12 19:31:45 +00:00
}
}
2015-01-07 18:26:44 +00:00
}
}