Browse Source

I2PControl Authenticate and Echo

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

33
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;
} }
@ -99,6 +103,8 @@ namespace client
LogPrint (eLogError, "I2PControl read error: ", ecode.message ()); LogPrint (eLogError, "I2PControl read error: ", ecode.message ());
} }
else else
{
try
{ {
std::stringstream ss; std::stringstream ss;
ss.write (buf->data (), bytes_transferred); ss.write (buf->data (), bytes_transferred);
@ -121,6 +127,15 @@ namespace client
else else
LogPrint (eLogWarning, "Unknown I2PControl method ", method); 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");
}
}
} }
void I2PControlService::SendResponse (std::shared_ptr<boost::asio::ip::tcp::socket> socket, void I2PControlService::SendResponse (std::shared_ptr<boost::asio::ip::tcp::socket> socket,
@ -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