Browse Source

I2PControl Authenticate and Echo

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

69
I2PControl.cpp

@ -1,7 +1,9 @@
#include <sstream> #include <sstream>
#include <boost/lexical_cast.hpp>
#include <boost/property_tree/ptree.hpp> #include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp> #include <boost/property_tree/json_parser.hpp>
#include "Log.h" #include "Log.h"
#include "Timestamp.h"
#include "I2PControl.h" #include "I2PControl.h"
namespace i2p namespace i2p
@ -12,6 +14,8 @@ namespace client
m_IsRunning (false), m_Thread (nullptr), m_IsRunning (false), m_Thread (nullptr),
m_Acceptor (m_Service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)) 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; m_MethodHanders[I2P_CONTROL_METHOD_ROUTER_INFO] = &I2PControlService::RouterInfoHandler;
} }
@ -100,26 +104,37 @@ namespace client
} }
else else
{ {
std::stringstream ss; try
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 ())
{ {
std::map<std::string, std::string> params; std::stringstream ss;
for (auto& v: pt.get_child (I2P_CONTROL_PROPERTY_PARAMS)) 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()) std::map<std::string, std::string> params;
params[v.first] = v.second.data (); for (auto& v: pt.get_child (I2P_CONTROL_PROPERTY_PARAMS))
} {
std::map<std::string, std::string> results; if (!v.first.empty())
(this->*(it->second))(params, results); params[v.first] = v.second.data ();
SendResponse (socket, buf, pt.get<std::string>(I2P_CONTROL_PROPERTY_ID), results); }
} std::map<std::string, std::string> results;
else (this->*(it->second))(params, results);
LogPrint (eLogWarning, "Unknown I2PControl method ", method); 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");
}
} }
} }
@ -154,6 +169,24 @@ namespace client
socket->close (); 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) 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
const char I2P_CONTROL_PROPERTY_ID[] = "id"; const char I2P_CONTROL_PROPERTY_ID[] = "id";
const char I2P_CONTROL_PROPERTY_METHOD[] = "method"; 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_PARAMS[] = "params";
const char I2P_CONTROL_PROPERTY_RESULT[] = "result"; const char I2P_CONTROL_PROPERTY_RESULT[] = "result";
// methods // methods
const char I2P_CONTROL_METHOD_AUTHENTICATE[] = "Authenticate";
const char I2P_CONTROL_METHOD_ECHO[] = "Echo";
const char I2P_CONTROL_METHOD_ROUTER_INFO[] = "RouterInfo"; 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 class I2PControlService
{ {
public: public:
@ -51,6 +59,8 @@ namespace client
private: 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); void RouterInfoHandler (const std::map<std::string, std::string>& params, std::map<std::string, std::string> results);
private: private:

Loading…
Cancel
Save