Browse Source

I2PControl Authenticate and Echo

pull/147/head
orignal 10 years ago
parent
commit
f3548daede
  1. 65
      I2PControl.cpp
  2. 12
      I2PControl.h

65
I2PControl.cpp

@ -1,7 +1,9 @@ @@ -1,7 +1,9 @@
#include <sstream>
#include <boost/lexical_cast.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include "Log.h"
#include "Timestamp.h"
#include "I2PControl.h"
namespace i2p
@ -12,6 +14,8 @@ namespace client @@ -12,6 +14,8 @@ namespace client
m_IsRunning (false), m_Thread (nullptr),
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port))
{
m_MethodHanders[I2P_CONTROL_METHOD_AUTHENTICATE] = &I2PControlService::AuthenticateHandler;
m_MethodHanders[I2P_CONTROL_METHOD_ECHO] = &I2PControlService::EchoHandler;
m_MethodHanders[I2P_CONTROL_METHOD_ROUTER_INFO] = &I2PControlService::RouterInfoHandler;
}
@ -100,26 +104,37 @@ namespace client @@ -100,26 +104,37 @@ namespace client
}
else
{
std::stringstream ss;
ss.write (buf->data (), bytes_transferred);
boost::property_tree::ptree pt;
boost::property_tree::read_json (ss, pt);
std::string method = pt.get<std::string>(I2P_CONTROL_PROPERTY_METHOD);
auto it = m_MethodHanders.find (method);
if (it != m_MethodHanders.end ())
try
{
std::map<std::string, std::string> params;
for (auto& v: pt.get_child (I2P_CONTROL_PROPERTY_PARAMS))
std::stringstream ss;
ss.write (buf->data (), bytes_transferred);
boost::property_tree::ptree pt;
boost::property_tree::read_json (ss, pt);
std::string method = pt.get<std::string>(I2P_CONTROL_PROPERTY_METHOD);
auto it = m_MethodHanders.find (method);
if (it != m_MethodHanders.end ())
{
if (!v.first.empty())
params[v.first] = v.second.data ();
std::map<std::string, std::string> params;
for (auto& v: pt.get_child (I2P_CONTROL_PROPERTY_PARAMS))
{
if (!v.first.empty())
params[v.first] = v.second.data ();
}
std::map<std::string, std::string> results;
(this->*(it->second))(params, results);
SendResponse (socket, buf, pt.get<std::string>(I2P_CONTROL_PROPERTY_ID), results);
}
std::map<std::string, std::string> results;
(this->*(it->second))(params, results);
SendResponse (socket, buf, pt.get<std::string>(I2P_CONTROL_PROPERTY_ID), results);
else
LogPrint (eLogWarning, "Unknown I2PControl method ", method);
}
catch (std::exception& ex)
{
LogPrint (eLogError, "I2PControl handle request: ", ex.what ());
}
catch (...)
{
LogPrint (eLogError, "I2PControl handle request unknown exception");
}
else
LogPrint (eLogWarning, "Unknown I2PControl method ", method);
}
}
@ -154,6 +169,24 @@ namespace client @@ -154,6 +169,24 @@ namespace client
socket->close ();
}
// handlers
void I2PControlService::AuthenticateHandler (const std::map<std::string, std::string>& params, std::map<std::string, std::string> results)
{
const std::string& api = params.at (I2P_CONTROL_PARAM_API);
const std::string& password = params.at (I2P_CONTROL_PARAM_PASSWORD);
LogPrint (eLogDebug, "I2PControl Authenticate API=", api, " Password=", password);
results[I2P_CONTROL_PARAM_API] = api;
results[I2P_CONTROL_PARAM_TOKEN] = boost::lexical_cast<std::string>(i2p::util::GetSecondsSinceEpoch ());
}
void I2PControlService::EchoHandler (const std::map<std::string, std::string>& params, std::map<std::string, std::string> results)
{
const std::string& echo = params.at (I2P_CONTROL_PARAM_ECHO);
LogPrint (eLogDebug, "I2PControl Echo Echo=", echo);
results[I2P_CONTROL_PARAM_RESULT] = echo;
}
void I2PControlService::RouterInfoHandler (const std::map<std::string, std::string>& params, std::map<std::string, std::string> results)
{
}

12
I2PControl.h

@ -18,13 +18,21 @@ namespace client @@ -18,13 +18,21 @@ namespace client
const char I2P_CONTROL_PROPERTY_ID[] = "id";
const char I2P_CONTROL_PROPERTY_METHOD[] = "method";
const char I2P_CONTROL_PROPERTY_TOKEN[] = "Token";
const char I2P_CONTROL_PROPERTY_PARAMS[] = "params";
const char I2P_CONTROL_PROPERTY_RESULT[] = "result";
// methods
const char I2P_CONTROL_METHOD_AUTHENTICATE[] = "Authenticate";
const char I2P_CONTROL_METHOD_ECHO[] = "Echo";
const char I2P_CONTROL_METHOD_ROUTER_INFO[] = "RouterInfo";
// 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";
class I2PControlService
{
public:
@ -51,6 +59,8 @@ namespace client @@ -51,6 +59,8 @@ namespace client
private:
void AuthenticateHandler (const std::map<std::string, std::string>& params, std::map<std::string, std::string> results);
void EchoHandler (const std::map<std::string, std::string>& params, std::map<std::string, std::string> results);
void RouterInfoHandler (const std::map<std::string, std::string>& params, std::map<std::string, std::string> results);
private:

Loading…
Cancel
Save