Browse Source

added I2PControl to ClientContext

pull/147/head
orignal 10 years ago
parent
commit
efdadfd7c5
  1. 18
      ClientContext.cpp
  2. 2
      ClientContext.h
  3. 40
      I2PControl.cpp
  4. 15
      I2PControl.h
  5. 1
      README.md

18
ClientContext.cpp

@ -12,7 +12,8 @@ namespace client @@ -12,7 +12,8 @@ namespace client
ClientContext::ClientContext (): m_SharedLocalDestination (nullptr),
m_HttpProxy (nullptr), m_SocksProxy (nullptr), m_IrcTunnel (nullptr),
m_ServerTunnel (nullptr), m_SamBridge (nullptr), m_BOBCommandChannel (nullptr)
m_ServerTunnel (nullptr), m_SamBridge (nullptr), m_BOBCommandChannel (nullptr),
m_I2PControlService (nullptr)
{
}
@ -24,6 +25,7 @@ namespace client @@ -24,6 +25,7 @@ namespace client
delete m_ServerTunnel;
delete m_SamBridge;
delete m_BOBCommandChannel;
delete m_I2PControlService;
}
void ClientContext::Start ()
@ -75,6 +77,13 @@ namespace client @@ -75,6 +77,13 @@ namespace client
m_BOBCommandChannel->Start ();
LogPrint("BOB command channel started");
}
int i2pcontrolPort = i2p::util::config::GetArg("-i2pcontrolport", 0);
if (i2pcontrolPort)
{
m_I2PControlService = new I2PControlService (i2pcontrolPort);
m_I2PControlService->Start ();
LogPrint("I2PControl started");
}
m_AddressBook.StartSubscriptions ();
}
@ -117,6 +126,13 @@ namespace client @@ -117,6 +126,13 @@ namespace client
m_BOBCommandChannel = nullptr;
LogPrint("BOB command channel stoped");
}
if (m_I2PControlService)
{
m_I2PControlService->Stop ();
delete m_I2PControlService;
m_I2PControlService = nullptr;
LogPrint("I2PControl stoped");
}
for (auto it: m_Destinations)
{

2
ClientContext.h

@ -9,6 +9,7 @@ @@ -9,6 +9,7 @@
#include "SAM.h"
#include "BOB.h"
#include "AddressBook.h"
#include "I2PControl.h"
namespace i2p
{
@ -49,6 +50,7 @@ namespace client @@ -49,6 +50,7 @@ namespace client
I2PServerTunnel * m_ServerTunnel;
SAMBridge * m_SamBridge;
BOBCommandChannel * m_BOBCommandChannel;
I2PControlService * m_I2PControlService;
public:
// for HTTP

40
I2PControl.cpp

@ -12,6 +12,7 @@ namespace client @@ -12,6 +12,7 @@ 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_ROUTER_INFO] = &I2PControlService::RouterInfoHandler;
}
I2PControlService::~I2PControlService ()
@ -113,11 +114,48 @@ namespace client @@ -113,11 +114,48 @@ namespace client
if (!v.first.empty())
params[v.first] = v.second.data ();
}
(this->*(it->second))(params);
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);
}
}
void I2PControlService::SendResponse (std::shared_ptr<boost::asio::ip::tcp::socket> socket,
std::shared_ptr<I2PControlBuffer> buf, const std::string& id,
const std::map<std::string, std::string>& results)
{
boost::property_tree::ptree ptr;
for (auto& result: results)
ptr.put (result.first, result.second);
boost::property_tree::ptree pt;
pt.put (I2P_CONTROL_PROPERTY_ID, id);
pt.put_child (I2P_CONTROL_PROPERTY_RESULT, ptr);
pt.put ("jsonrpc", "2.0");
std::ostringstream ss;
boost::property_tree::write_json (ss, pt, false);
size_t len = ss.str ().length ();
memcpy (buf->data (), ss.str ().c_str (), len);
boost::asio::async_write (*socket, boost::asio::buffer (buf->data (), len),
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 ();
}
void I2PControlService::RouterInfoHandler (const std::map<std::string, std::string>& params, std::map<std::string, std::string> results)
{
}
}
}

15
I2PControl.h

@ -20,6 +20,10 @@ namespace client @@ -20,6 +20,10 @@ namespace client
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_ROUTER_INFO[] = "RouterInfo";
class I2PControlService
{
@ -39,6 +43,15 @@ namespace client @@ -39,6 +43,15 @@ namespace client
void ReadRequest (std::shared_ptr<boost::asio::ip::tcp::socket> socket);
void 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);
void SendResponse (std::shared_ptr<boost::asio::ip::tcp::socket> socket,
std::shared_ptr<I2PControlBuffer> buf, const std::string& id,
const std::map<std::string, std::string>& results);
void 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);
private:
void RouterInfoHandler (const std::map<std::string, std::string>& params, std::map<std::string, std::string> results);
private:
@ -48,7 +61,7 @@ namespace client @@ -48,7 +61,7 @@ namespace client
boost::asio::io_service m_Service;
boost::asio::ip::tcp::acceptor m_Acceptor;
typedef void (I2PControlService::*MethodHandler)(const std::map<std::string, std::string>& params);
typedef void (I2PControlService::*MethodHandler)(const std::map<std::string, std::string>& params, std::map<std::string, std::string> results);
std::map<std::string, MethodHandler> m_MethodHanders;
};
}

1
README.md

@ -72,6 +72,7 @@ Cmdline options @@ -72,6 +72,7 @@ Cmdline options
* --eepport= - Port incoming trafic forward to. 80 by default
* --samport= - Port of SAM bridge. Usually 7656. SAM is off if not specified
* --bobport= - Port of BOB command channel. Usually 2827. BOB is off if not specified
* --i2pcontrolport= - Port of I2P control service. Usually 7650. I2PControl is off if not specified
* --conf= - Config file (default: ~/.i2pd/i2p.conf or /var/lib/i2pd/i2p.conf)
This parameter will be silently ignored if the specified config file does not exist.
Options specified on the command line take precedence over those in the config file.

Loading…
Cancel
Save