mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-03-21 16:49:10 +01:00
added I2PControl to ClientContext
This commit is contained in:
parent
e82507ca4e
commit
efdadfd7c5
5 changed files with 73 additions and 3 deletions
|
@ -12,7 +12,8 @@ namespace client
|
||||||
|
|
||||||
ClientContext::ClientContext (): m_SharedLocalDestination (nullptr),
|
ClientContext::ClientContext (): m_SharedLocalDestination (nullptr),
|
||||||
m_HttpProxy (nullptr), m_SocksProxy (nullptr), m_IrcTunnel (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
|
||||||
delete m_ServerTunnel;
|
delete m_ServerTunnel;
|
||||||
delete m_SamBridge;
|
delete m_SamBridge;
|
||||||
delete m_BOBCommandChannel;
|
delete m_BOBCommandChannel;
|
||||||
|
delete m_I2PControlService;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientContext::Start ()
|
void ClientContext::Start ()
|
||||||
|
@ -75,6 +77,13 @@ namespace client
|
||||||
m_BOBCommandChannel->Start ();
|
m_BOBCommandChannel->Start ();
|
||||||
LogPrint("BOB command channel started");
|
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 ();
|
m_AddressBook.StartSubscriptions ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,6 +126,13 @@ namespace client
|
||||||
m_BOBCommandChannel = nullptr;
|
m_BOBCommandChannel = nullptr;
|
||||||
LogPrint("BOB command channel stoped");
|
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)
|
for (auto it: m_Destinations)
|
||||||
{
|
{
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include "SAM.h"
|
#include "SAM.h"
|
||||||
#include "BOB.h"
|
#include "BOB.h"
|
||||||
#include "AddressBook.h"
|
#include "AddressBook.h"
|
||||||
|
#include "I2PControl.h"
|
||||||
|
|
||||||
namespace i2p
|
namespace i2p
|
||||||
{
|
{
|
||||||
|
@ -49,6 +50,7 @@ namespace client
|
||||||
I2PServerTunnel * m_ServerTunnel;
|
I2PServerTunnel * m_ServerTunnel;
|
||||||
SAMBridge * m_SamBridge;
|
SAMBridge * m_SamBridge;
|
||||||
BOBCommandChannel * m_BOBCommandChannel;
|
BOBCommandChannel * m_BOBCommandChannel;
|
||||||
|
I2PControlService * m_I2PControlService;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// for HTTP
|
// for HTTP
|
||||||
|
|
|
@ -12,6 +12,7 @@ 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_ROUTER_INFO] = &I2PControlService::RouterInfoHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
I2PControlService::~I2PControlService ()
|
I2PControlService::~I2PControlService ()
|
||||||
|
@ -113,11 +114,48 @@ namespace client
|
||||||
if (!v.first.empty())
|
if (!v.first.empty())
|
||||||
params[v.first] = v.second.data ();
|
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
|
else
|
||||||
LogPrint (eLogWarning, "Unknown I2PControl method ", method);
|
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
15
I2PControl.h
|
@ -20,6 +20,10 @@ namespace client
|
||||||
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_TOKEN[] = "Token";
|
||||||
const char I2P_CONTROL_PROPERTY_PARAMS[] = "params";
|
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
|
class I2PControlService
|
||||||
{
|
{
|
||||||
|
@ -39,6 +43,15 @@ namespace client
|
||||||
void ReadRequest (std::shared_ptr<boost::asio::ip::tcp::socket> socket);
|
void ReadRequest (std::shared_ptr<boost::asio::ip::tcp::socket> socket);
|
||||||
void HandleRequestReceived (const boost::system::error_code& ecode, size_t bytes_transferred,
|
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);
|
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:
|
private:
|
||||||
|
|
||||||
|
@ -48,7 +61,7 @@ namespace client
|
||||||
boost::asio::io_service m_Service;
|
boost::asio::io_service m_Service;
|
||||||
boost::asio::ip::tcp::acceptor m_Acceptor;
|
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;
|
std::map<std::string, MethodHandler> m_MethodHanders;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,6 +72,7 @@ Cmdline options
|
||||||
* --eepport= - Port incoming trafic forward to. 80 by default
|
* --eepport= - Port incoming trafic forward to. 80 by default
|
||||||
* --samport= - Port of SAM bridge. Usually 7656. SAM is off if not specified
|
* --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
|
* --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)
|
* --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.
|
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.
|
Options specified on the command line take precedence over those in the config file.
|
||||||
|
|
Loading…
Add table
Reference in a new issue