i2pd/I2PControl.cpp

564 lines
20 KiB
C++
Raw Normal View History

2015-11-23 19:56:00 +03:00
#include <stdio.h>
2015-01-08 00:09:32 +03:00
#include <sstream>
2016-05-11 23:02:26 +03:00
#include <openssl/x509.h>
#include <openssl/pem.h>
2015-01-17 00:51:52 +03:00
#include <boost/date_time/posix_time/posix_time.hpp>
2015-12-08 18:40:43 +03:00
#include <boost/property_tree/ini_parser.hpp>
// There is bug in boost 1.49 with gcc 4.7 coming with Debian Wheezy
#define GCC47_BOOST149 ((BOOST_VERSION == 104900) && (__GNUC__ == 4) && (__GNUC_MINOR__ >= 7))
#if !GCC47_BOOST149
2015-01-07 21:26:44 +03:00
#include <boost/property_tree/json_parser.hpp>
#endif
2016-05-11 23:02:26 +03:00
#include "Crypto.h"
2016-02-11 03:00:00 +03:00
#include "FS.h"
2015-01-08 00:09:32 +03:00
#include "Log.h"
#include "HTTP.h"
#include "Config.h"
#include "NetDb.h"
#include "RouterContext.h"
#include "Daemon.h"
#include "Tunnel.h"
2015-01-09 00:11:40 +03:00
#include "Timestamp.h"
2015-01-14 21:21:41 +03:00
#include "Transports.h"
2015-03-20 04:51:18 +03:00
#include "version.h"
2016-06-14 21:37:22 +03:00
#include "util.h"
2015-11-23 19:56:00 +03:00
#include "I2PControl.h"
2015-01-07 21:26:44 +03:00
namespace i2p
{
namespace client
{
2015-11-30 17:44:32 +03:00
I2PControlService::I2PControlService (const std::string& address, int port):
m_IsRunning (false), m_Thread (nullptr),
2015-11-30 17:44:32 +03:00
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint(boost::asio::ip::address::from_string(address), port)),
2015-11-23 21:22:02 +03:00
m_SSLContext (m_Service, boost::asio::ssl::context::sslv23),
m_ShutdownTimer (m_Service)
2015-01-07 21:26:44 +03:00
{
2016-01-21 15:56:53 +03:00
i2p::config::GetOption("i2pcontrol.password", m_Password);
// certificate / keys
std::string i2pcp_crt; i2p::config::GetOption("i2pcontrol.cert", i2pcp_crt);
std::string i2pcp_key; i2p::config::GetOption("i2pcontrol.key", i2pcp_key);
2016-02-11 03:00:00 +03:00
if (i2pcp_crt.at(0) != '/')
2016-02-11 03:00:00 +03:00
i2pcp_crt = i2p::fs::DataDirPath(i2pcp_crt);
if (i2pcp_key.at(0) != '/')
2016-02-11 03:00:00 +03:00
i2pcp_key = i2p::fs::DataDirPath(i2pcp_key);
if (!i2p::fs::Exists (i2pcp_crt) || !i2p::fs::Exists (i2pcp_key)) {
LogPrint (eLogInfo, "I2PControl: creating new certificate for control connection");
CreateCertificate (i2pcp_crt.c_str(), i2pcp_key.c_str());
} else {
LogPrint(eLogDebug, "I2PControl: using cert from ", i2pcp_crt);
2015-11-23 22:48:56 +03:00
}
m_SSLContext.set_options (boost::asio::ssl::context::default_workarounds | boost::asio::ssl::context::no_sslv2 | boost::asio::ssl::context::single_dh_use);
m_SSLContext.use_certificate_file (i2pcp_crt, boost::asio::ssl::context::pem);
m_SSLContext.use_private_key_file (i2pcp_key, boost::asio::ssl::context::pem);
2015-11-23 19:56:00 +03:00
2015-11-23 21:22:02 +03:00
// handlers
m_MethodHandlers["Authenticate"] = &I2PControlService::AuthenticateHandler;
m_MethodHandlers["Echo"] = &I2PControlService::EchoHandler;
m_MethodHandlers["I2PControl"] = &I2PControlService::I2PControlHandler;
m_MethodHandlers["RouterInfo"] = &I2PControlService::RouterInfoHandler;
m_MethodHandlers["RouterManager"] = &I2PControlService::RouterManagerHandler;
m_MethodHandlers["NetworkSetting"] = &I2PControlService::NetworkSettingHandler;
2015-01-12 21:38:16 +03:00
2015-12-08 18:40:43 +03:00
// I2PControl
m_I2PControlHandlers["i2pcontrol.password"] = &I2PControlService::PasswordHandler;
2015-12-08 18:40:43 +03:00
2015-01-12 21:38:16 +03:00
// RouterInfo
m_RouterInfoHandlers["i2p.router.uptime"] = &I2PControlService::UptimeHandler;
m_RouterInfoHandlers["i2p.router.version"] = &I2PControlService::VersionHandler;
m_RouterInfoHandlers["i2p.router.status"] = &I2PControlService::StatusHandler;
m_RouterInfoHandlers["i2p.router.netdb.knownpeers"] = &I2PControlService::NetDbKnownPeersHandler;
m_RouterInfoHandlers["i2p.router.netdb.activepeers"] = &I2PControlService::NetDbActivePeersHandler;
m_RouterInfoHandlers["i2p.router.net.bw.inbound.1s"] = &I2PControlService::InboundBandwidth1S;
m_RouterInfoHandlers["i2p.router.net.bw.outbound.1s"] = &I2PControlService::OutboundBandwidth1S;
m_RouterInfoHandlers["i2p.router.net.status"] = &I2PControlService::NetStatusHandler;
m_RouterInfoHandlers["i2p.router.net.tunnels.participating"] = &I2PControlService::TunnelsParticipatingHandler;
m_RouterInfoHandlers["i2p.router.net.total.received.bytes"] = &I2PControlService::NetTotalReceivedBytes;
m_RouterInfoHandlers["i2p.router.net.total.sent.bytes"] = &I2PControlService::NetTotalSentBytes;
2015-01-12 21:38:16 +03:00
// RouterManager
m_RouterManagerHandlers["Reseed"] = &I2PControlService::ReseedHandler;
m_RouterManagerHandlers["Shutdown"] = &I2PControlService::ShutdownHandler;
m_RouterManagerHandlers["ShutdownGraceful"] = &I2PControlService::ShutdownGracefulHandler;
// NetworkSetting
m_NetworkSettingHandlers["i2p.router.net.bw.in"] = &I2PControlService::InboundBandwidthLimit;
m_NetworkSettingHandlers["i2p.router.net.bw.out"] = &I2PControlService::OutboundBandwidthLimit;
2015-01-07 21:26:44 +03:00
}
2015-01-08 00:09:32 +03: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 ();
2015-01-08 00:09:32 +03:00
m_Service.stop ();
if (m_Thread)
{
m_Thread->join ();
2015-01-08 00:09:32 +03:00
delete m_Thread;
m_Thread = nullptr;
}
2015-01-08 00:09:32 +03:00
}
}
void I2PControlService::Run ()
{
2015-01-08 00:09:32 +03:00
while (m_IsRunning)
{
2016-01-22 03:00:00 +03:00
try {
2015-01-08 00:09:32 +03:00
m_Service.run ();
2016-01-22 03:00:00 +03:00
} catch (std::exception& ex) {
LogPrint (eLogError, "I2PControl: runtime exception: ", ex.what ());
2015-01-08 00:09:32 +03:00
}
}
}
void I2PControlService::Accept ()
{
2015-11-23 21:22:02 +03:00
auto newSocket = std::make_shared<ssl_socket> (m_Service, m_SSLContext);
m_Acceptor.async_accept (newSocket->lowest_layer(), std::bind (&I2PControlService::HandleAccept, this,
2015-01-08 00:09:32 +03:00
std::placeholders::_1, newSocket));
}
2015-11-23 21:22:02 +03:00
void I2PControlService::HandleAccept(const boost::system::error_code& ecode, std::shared_ptr<ssl_socket> socket)
2015-01-08 00:09:32 +03:00
{
if (ecode != boost::asio::error::operation_aborted)
Accept ();
2016-01-22 03:00:00 +03:00
if (ecode) {
LogPrint (eLogError, "I2PControl: accept error: ", ecode.message ());
return;
2015-01-08 00:09:32 +03:00
}
2016-01-22 03:00:00 +03:00
LogPrint (eLogDebug, "I2PControl: new request from ", socket->lowest_layer ().remote_endpoint ());
Handshake (socket);
2015-01-08 00:09:32 +03:00
}
2015-11-25 21:11:02 +03:00
void I2PControlService::Handshake (std::shared_ptr<ssl_socket> socket)
{
socket->async_handshake(boost::asio::ssl::stream_base::server,
std::bind( &I2PControlService::HandleHandshake, this, std::placeholders::_1, socket));
}
void I2PControlService::HandleHandshake (const boost::system::error_code& ecode, std::shared_ptr<ssl_socket> socket)
{
2016-01-22 03:00:00 +03:00
if (ecode) {
LogPrint (eLogError, "I2PControl: handshake error: ", ecode.message ());
return;
2015-11-25 21:11:02 +03:00
}
2016-01-22 03:00:00 +03:00
//std::this_thread::sleep_for (std::chrono::milliseconds(5));
ReadRequest (socket);
2015-11-25 21:11:02 +03:00
}
2015-11-23 21:22:02 +03:00
void I2PControlService::ReadRequest (std::shared_ptr<ssl_socket> socket)
2015-01-08 00:09:32 +03:00
{
auto request = std::make_shared<I2PControlBuffer>();
2015-11-24 03:47:08 +03:00
socket->async_read_some (
#if defined(BOOST_ASIO_HAS_STD_ARRAY)
boost::asio::buffer (*request),
#else
boost::asio::buffer (request->data (), request->size ()),
#endif
std::bind(&I2PControlService::HandleRequestReceived, this,
2015-01-08 00:09:32 +03:00
std::placeholders::_1, std::placeholders::_2, socket, request));
}
void I2PControlService::HandleRequestReceived (const boost::system::error_code& ecode,
size_t bytes_transferred, std::shared_ptr<ssl_socket> socket,
2015-01-08 00:09:32 +03:00
std::shared_ptr<I2PControlBuffer> buf)
{
2016-01-22 03:00:00 +03:00
if (ecode) {
LogPrint (eLogError, "I2PControl: read error: ", ecode.message ());
return;
}
/* try to parse received data */
std::stringstream json;
std::string response;
bool isHTTP = false;
if (memcmp (buf->data (), "POST", 4) == 0) {
long int remains = 0;
isHTTP = true;
i2p::http::HTTPReq req;
std::size_t len = req.parse(buf->data(), bytes_transferred);
if (len <= 0) {
LogPrint(eLogError, "I2PControl: incomplete/malformed POST request");
return;
}
/* append to json chunk of data from 1st request */
json.write(buf->begin() + len, bytes_transferred - len);
remains = req.length() - len;
/* if request has Content-Length header, fetch rest of data and store to json buffer */
while (remains > 0) {
len = ((long int) buf->size() < remains) ? buf->size() : remains;
bytes_transferred = boost::asio::read (*socket, boost::asio::buffer (buf->data (), len));
json.write(buf->begin(), bytes_transferred);
remains -= bytes_transferred;
}
2016-01-22 03:00:00 +03:00
} else {
json.write(buf->begin(), bytes_transferred);
}
LogPrint(eLogDebug, "I2PControl: json from request: ", json.str());
#if GCC47_BOOST149
LogPrint (eLogError, "I2PControl: json_read is not supported due bug in boost 1.49 with gcc 4.7");
BuildErrorResponse(response, 32603, "JSON requests is not supported with this version of boost");
#else
/* now try to parse json itself */
try {
boost::property_tree::ptree pt;
boost::property_tree::read_json (json, pt);
std::string id = pt.get<std::string>("id");
std::string method = pt.get<std::string>("method");
auto it = m_MethodHandlers.find (method);
if (it != m_MethodHandlers.end ()) {
std::ostringstream ss;
ss << "{\"id\":" << id << ",\"result\":{";
(this->*(it->second))(pt.get_child ("params"), ss);
ss << "},\"jsonrpc\":\"2.0\"}";
response = ss.str();
} else {
LogPrint (eLogWarning, "I2PControl: unknown method ", method);
BuildErrorResponse(response, 32601, "Method not found");
2015-01-09 00:11:40 +03:00
}
} catch (std::exception& ex) {
LogPrint (eLogError, "I2PControl: exception when handle request: ", ex.what ());
BuildErrorResponse(response, 32603, ex.what());
} catch (...) {
LogPrint (eLogError, "I2PControl: handle request unknown exception");
2015-01-08 00:09:32 +03:00
}
#endif
SendResponse (socket, buf, response, isHTTP);
2015-01-08 00:09:32 +03:00
}
2015-01-08 21:28:51 +03:00
void I2PControlService::InsertParam (std::ostringstream& ss, const std::string& name, int value) const
2015-03-20 18:36:57 +03:00
{
ss << "\"" << name << "\":" << value;
}
2015-03-20 18:36:57 +03: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 21:28:51 +03:00
{
ss << "\"" << name << "\":" << std::fixed << std::setprecision(2) << value;
}
2015-01-08 21:28:51 +03:00
void I2PControlService::BuildErrorResponse (std::string & content, int code, const char *message) {
std::stringstream ss;
ss << "{\"id\":null,\"error\":";
ss << "{\"code\":" << -code << ",\"message\":\"" << message << "\"},";
ss << "\"jsonrpc\":\"2.0\"}";
content = ss.str();
}
2015-11-23 21:22:02 +03:00
void I2PControlService::SendResponse (std::shared_ptr<ssl_socket> socket,
std::shared_ptr<I2PControlBuffer> buf, std::string& content, bool isHTTP)
{
if (isHTTP) {
i2p::http::HTTPRes res;
res.code = 200;
res.add_header("Content-Type", "application/json");
res.add_header("Connection", "close");
res.body = content;
std::string tmp = res.to_string();
content = tmp;
}
std::copy(content.begin(), content.end(), buf->begin());
boost::asio::async_write (*socket, boost::asio::buffer (buf->data (), content.length()),
2015-01-08 21:28:51 +03:00
boost::asio::transfer_all (),
std::bind(&I2PControlService::HandleResponseSent, this,
2015-01-08 21:28:51 +03:00
std::placeholders::_1, std::placeholders::_2, socket, buf));
}
void I2PControlService::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
{
2016-01-22 03:00:00 +03:00
if (ecode) {
LogPrint (eLogError, "I2PControl: write error: ", ecode.message ());
}
2015-01-08 21:28:51 +03:00
}
2015-01-09 00:11:40 +03:00
// handlers
void I2PControlService::AuthenticateHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
2015-01-09 00:11:40 +03:00
{
int api = params.get<int> ("API");
auto password = params.get<std::string> ("Password");
2016-01-22 03:00:00 +03:00
LogPrint (eLogDebug, "I2PControl: Authenticate API=", api, " Password=", password);
2016-01-21 15:40:07 +03:00
if (password != m_Password) {
LogPrint (eLogError, "I2PControl: Authenticate - Invalid password: ", password);
return;
}
InsertParam (results, "API", api);
results << ",";
std::string token = std::to_string(i2p::util::GetSecondsSinceEpoch ());
2015-03-06 19:33:54 +03:00
m_Tokens.insert (token);
InsertParam (results, "Token", token);
2015-01-09 00:11:40 +03:00
}
void I2PControlService::EchoHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
2015-01-09 00:11:40 +03:00
{
auto echo = params.get<std::string> ("Echo");
2015-01-09 00:11:40 +03:00
LogPrint (eLogDebug, "I2PControl Echo Echo=", echo);
InsertParam (results, "Result", echo);
2015-01-09 00:11:40 +03:00
}
2015-01-16 00:42:28 +03:00
// I2PControl
void I2PControlService::I2PControlHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
2015-01-16 00:42:28 +03:00
{
for (auto& it: params)
{
2016-01-22 03:00:00 +03:00
LogPrint (eLogDebug, "I2PControl: I2PControl request: ", it.first);
2015-01-16 00:42:28 +03:00
auto it1 = m_I2PControlHandlers.find (it.first);
if (it1 != m_I2PControlHandlers.end ())
2015-12-08 18:40:43 +03:00
{
(this->*(it1->second))(it.second.data ());
InsertParam (results, it.first, "");
2015-12-08 18:40:43 +03:00
}
2015-01-16 00:42:28 +03:00
else
2016-01-22 03:00:00 +03:00
LogPrint (eLogError, "I2PControl: I2PControl unknown request: ", it.first);
2015-01-16 00:42:28 +03:00
}
}
2015-12-08 18:40:43 +03:00
void I2PControlService::PasswordHandler (const std::string& value)
{
2016-01-22 03:00:00 +03:00
LogPrint (eLogWarning, "I2PControl: new password=", value, ", to make it persistent you should update your config!");
2015-12-08 18:40:43 +03:00
m_Password = value;
m_Tokens.clear ();
}
2015-01-12 21:38:16 +03:00
// RouterInfo
void I2PControlService::RouterInfoHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
2015-01-08 21:28:51 +03:00
{
for (auto it = params.begin (); it != params.end (); it++)
{
2016-01-22 03:00:00 +03:00
LogPrint (eLogDebug, "I2PControl: RouterInfo request: ", it->first);
auto it1 = m_RouterInfoHandlers.find (it->first);
2015-01-12 21:38:16 +03:00
if (it1 != m_RouterInfoHandlers.end ())
2015-12-26 17:41:12 +03:00
{
if (it != params.begin ()) results << ",";
(this->*(it1->second))(results);
}
2015-01-12 21:38:16 +03:00
else
2016-01-22 03:00:00 +03:00
LogPrint (eLogError, "I2PControl: RouterInfo unknown request ", it->first);
}
2015-01-08 21:28:51 +03:00
}
void I2PControlService::UptimeHandler (std::ostringstream& results)
2015-02-23 22:57:57 +03:00
{
InsertParam (results, "i2p.router.uptime", (int)i2p::context.GetUptime ()*1000);
2015-02-23 22:57:57 +03:00
}
void I2PControlService::VersionHandler (std::ostringstream& results)
2015-03-20 04:51:18 +03:00
{
InsertParam (results, "i2p.router.version", VERSION);
2015-03-20 04:51:18 +03:00
}
void I2PControlService::StatusHandler (std::ostringstream& results)
2015-03-20 04:51:18 +03:00
{
InsertParam (results, "i2p.router.status", "???"); // TODO:
2015-03-20 04:51:18 +03:00
}
void I2PControlService::NetDbKnownPeersHandler (std::ostringstream& results)
2015-01-12 21:38:16 +03:00
{
InsertParam (results, "i2p.router.netdb.knownpeers", i2p::data::netdb.GetNumRouters ());
2015-01-12 21:38:16 +03:00
}
void I2PControlService::NetDbActivePeersHandler (std::ostringstream& results)
2015-01-14 21:21:41 +03:00
{
InsertParam (results, "i2p.router.netdb.activepeers", (int)i2p::transport::transports.GetPeers ().size ());
2015-01-14 21:21:41 +03:00
}
void I2PControlService::NetStatusHandler (std::ostringstream& results)
2015-03-05 23:17:16 +03:00
{
InsertParam (results, "i2p.router.net.status", (int)i2p::context.GetStatus ());
2015-03-05 23:17:16 +03:00
}
void I2PControlService::TunnelsParticipatingHandler (std::ostringstream& results)
2015-01-12 22:03:20 +03:00
{
int transit = i2p::tunnel::tunnels.GetTransitTunnels ().size ();
InsertParam (results, "i2p.router.net.tunnels.participating", transit);
2015-01-12 22:03:20 +03:00
}
void I2PControlService::InboundBandwidth1S (std::ostringstream& results)
2015-03-17 22:36:15 +03:00
{
double bw = i2p::transport::transports.GetInBandwidth ();
InsertParam (results, "i2p.router.net.bw.inbound.1s", bw);
2015-03-17 22:36:15 +03:00
}
void I2PControlService::OutboundBandwidth1S (std::ostringstream& results)
2015-03-17 22:36:15 +03:00
{
double bw = i2p::transport::transports.GetOutBandwidth ();
InsertParam (results, "i2p.router.net.bw.outbound.1s", bw);
2015-03-17 22:36:15 +03:00
}
void I2PControlService::NetTotalReceivedBytes (std::ostringstream& results)
{
InsertParam (results, "i2p.router.net.total.received.bytes", (double)i2p::transport::transports.GetTotalReceivedBytes ());
}
void I2PControlService::NetTotalSentBytes (std::ostringstream& results)
{
InsertParam (results, "i2p.router.net.total.sent.bytes", (double)i2p::transport::transports.GetTotalSentBytes ());
}
2015-01-12 21:38:16 +03:00
// RouterManager
void I2PControlService::RouterManagerHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
{
for (auto it = params.begin (); it != params.end (); it++)
{
if (it != params.begin ()) results << ",";
2016-01-22 03:00:00 +03:00
LogPrint (eLogDebug, "I2PControl: RouterManager request: ", it->first);
auto it1 = m_RouterManagerHandlers.find (it->first);
2016-01-22 03:00:00 +03:00
if (it1 != m_RouterManagerHandlers.end ()) {
2015-01-12 21:38:16 +03:00
(this->*(it1->second))(results);
2016-01-22 03:00:00 +03:00
} else
LogPrint (eLogError, "I2PControl: RouterManager unknown request: ", it->first);
}
}
2015-01-12 21:38:16 +03:00
void I2PControlService::ShutdownHandler (std::ostringstream& results)
2015-01-12 21:38:16 +03:00
{
2016-01-22 03:00:00 +03:00
LogPrint (eLogInfo, "I2PControl: Shutdown requested");
InsertParam (results, "Shutdown", "");
2015-01-12 21:38:16 +03: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;
2015-01-12 21:38:16 +03:00
});
}
void I2PControlService::ShutdownGracefulHandler (std::ostringstream& results)
2015-01-12 21:38:16 +03:00
{
i2p::context.SetAcceptsTunnels (false);
int timeout = i2p::tunnel::tunnels.GetTransitTunnelsExpirationTimeout ();
2016-01-22 03:00:00 +03:00
LogPrint (eLogInfo, "I2PControl: Graceful shutdown requested, ", timeout, " seconds remains");
InsertParam (results, "ShutdownGraceful", "");
2015-01-12 21:38:16 +03: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 21:38:16 +03:00
});
}
2015-01-12 22:31:45 +03:00
void I2PControlService::ReseedHandler (std::ostringstream& results)
2015-01-19 21:57:37 +03:00
{
2016-01-22 03:00:00 +03:00
LogPrint (eLogInfo, "I2PControl: Reseed requested");
InsertParam (results, "Reseed", "");
2015-01-19 21:57:37 +03:00
i2p::data::netdb.Reseed ();
}
// network setting
void I2PControlService::NetworkSettingHandler (const boost::property_tree::ptree& params, std::ostringstream& results)
2015-01-12 22:31:45 +03:00
{
for (auto it = params.begin (); it != params.end (); it++)
2015-01-12 22:31:45 +03:00
{
if (it != params.begin ()) results << ",";
2016-01-22 03:00:00 +03:00
LogPrint (eLogDebug, "I2PControl: NetworkSetting request: ", it->first);
auto it1 = m_NetworkSettingHandlers.find (it->first);
2016-01-22 03:00:00 +03:00
if (it1 != m_NetworkSettingHandlers.end ()) {
(this->*(it1->second))(it->second.data (), results);
2016-01-22 03:00:00 +03:00
} else
LogPrint (eLogError, "I2PControl: NetworkSetting unknown request: ", it->first);
2015-01-12 22:31:45 +03:00
}
}
void I2PControlService::InboundBandwidthLimit (const std::string& value, std::ostringstream& results)
{
if (value != "null")
i2p::context.SetBandwidth (std::atoi(value.c_str()));
int bw = i2p::context.GetBandwidthLimit();
InsertParam (results, "i2p.router.net.bw.in", bw);
}
void I2PControlService::OutboundBandwidthLimit (const std::string& value, std::ostringstream& results)
{
if (value != "null")
i2p::context.SetBandwidth (std::atoi(value.c_str()));
int bw = i2p::context.GetBandwidthLimit();
InsertParam (results, "i2p.router.net.bw.out", bw);
}
2015-11-23 19:56:00 +03:00
// certificate
void I2PControlService::CreateCertificate (const char *crt_path, const char *key_path)
2015-11-23 19:56:00 +03:00
{
FILE *f = NULL;
2015-11-23 19:56:00 +03:00
EVP_PKEY * pkey = EVP_PKEY_new ();
2015-11-23 22:48:56 +03:00
RSA * rsa = RSA_new ();
BIGNUM * e = BN_dup (i2p::crypto::GetRSAE ());
RSA_generate_key_ex (rsa, 4096, e, NULL);
BN_free (e);
2015-11-23 19:56:00 +03:00
if (rsa)
{
EVP_PKEY_assign_RSA (pkey, rsa);
X509 * x509 = X509_new ();
ASN1_INTEGER_set (X509_get_serialNumber (x509), 1);
X509_gmtime_adj (X509_get_notBefore (x509), 0);
X509_gmtime_adj (X509_get_notAfter (x509), I2P_CONTROL_CERTIFICATE_VALIDITY*24*60*60); // expiration
X509_set_pubkey (x509, pkey); // public key
2015-11-23 19:56:00 +03:00
X509_NAME * name = X509_get_subject_name (x509);
X509_NAME_add_entry_by_txt (name, "C", MBSTRING_ASC, (unsigned char *)"RU", -1, -1, 0); // country (Russia by default)
X509_NAME_add_entry_by_txt (name, "O", MBSTRING_ASC, (unsigned char *)I2P_CONTROL_CERTIFICATE_ORGANIZATION, -1, -1, 0); // organization
X509_NAME_add_entry_by_txt (name, "CN", MBSTRING_ASC, (unsigned char *)I2P_CONTROL_CERTIFICATE_COMMON_NAME, -1, -1, 0); // common name
X509_set_issuer_name (x509, name); // set issuer to ourselves
X509_sign (x509, pkey, EVP_sha1 ()); // sign
// save cert
if ((f = fopen (crt_path, "wb")) != NULL) {
LogPrint (eLogInfo, "I2PControl: saving new cert to ", crt_path);
PEM_write_X509 (f, x509);
2015-11-23 19:56:00 +03:00
fclose (f);
} else {
LogPrint (eLogError, "I2PControl: can't write cert: ", strerror(errno));
2015-11-23 19:56:00 +03:00
}
// save key
if ((f = fopen (key_path, "wb")) != NULL) {
2016-02-11 03:00:00 +03:00
LogPrint (eLogInfo, "I2PControl: saving cert key to ", key_path);
PEM_write_PrivateKey (f, pkey, NULL, NULL, 0, NULL, NULL);
2015-11-23 19:56:00 +03:00
fclose (f);
} else {
LogPrint (eLogError, "I2PControl: can't write key: ", strerror(errno));
2015-11-23 19:56:00 +03:00
}
X509_free (x509);
} else {
LogPrint (eLogError, "I2PControl: can't create RSA key for certificate");
2015-11-23 19:56:00 +03:00
}
EVP_PKEY_free (pkey);
}
2015-01-07 21:26:44 +03:00
}
}