|
|
|
@ -8,6 +8,7 @@
@@ -8,6 +8,7 @@
|
|
|
|
|
|
|
|
|
|
namespace i2p { |
|
|
|
|
namespace client { |
|
|
|
|
|
|
|
|
|
I2PControlService::I2PControlService(const std::string& address, int port, const std::string& pass) |
|
|
|
|
: m_Session(std::make_shared<I2PControlSession>(m_Service, pass)), |
|
|
|
|
m_IsRunning(false), m_Thread(nullptr), |
|
|
|
@ -25,8 +26,7 @@ namespace client {
@@ -25,8 +26,7 @@ namespace client {
|
|
|
|
|
|
|
|
|
|
void I2PControlService::Start() |
|
|
|
|
{ |
|
|
|
|
if (!m_IsRunning) |
|
|
|
|
{ |
|
|
|
|
if(!m_IsRunning) { |
|
|
|
|
Accept(); |
|
|
|
|
m_Session->start(); |
|
|
|
|
m_IsRunning = true; |
|
|
|
@ -36,8 +36,7 @@ namespace client {
@@ -36,8 +36,7 @@ namespace client {
|
|
|
|
|
|
|
|
|
|
void I2PControlService::Stop() |
|
|
|
|
{ |
|
|
|
|
if (m_IsRunning) |
|
|
|
|
{ |
|
|
|
|
if(m_IsRunning) { |
|
|
|
|
m_IsRunning = false; |
|
|
|
|
m_Acceptor.cancel(); |
|
|
|
|
m_Session->stop(); |
|
|
|
@ -55,14 +54,10 @@ namespace client {
@@ -55,14 +54,10 @@ namespace client {
|
|
|
|
|
|
|
|
|
|
void I2PControlService::Run() |
|
|
|
|
{ |
|
|
|
|
while (m_IsRunning) |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
while(m_IsRunning) { |
|
|
|
|
try { |
|
|
|
|
m_Service.run(); |
|
|
|
|
} |
|
|
|
|
catch (std::exception& ex) |
|
|
|
|
{ |
|
|
|
|
} catch(const std::exception& ex) { |
|
|
|
|
LogPrint(eLogError, "I2PControl: ", ex.what()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -107,24 +102,20 @@ namespace client {
@@ -107,24 +102,20 @@ namespace client {
|
|
|
|
|
size_t bytes_transferred, std::shared_ptr<boost::asio::ip::tcp::socket> socket, |
|
|
|
|
std::shared_ptr<I2PControlBuffer> buf) |
|
|
|
|
{ |
|
|
|
|
if (ecode) |
|
|
|
|
{ |
|
|
|
|
if(ecode) { |
|
|
|
|
LogPrint(eLogError, "I2PControl read error: ", ecode.message()); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
bool isHtml = !memcmp(buf->data(), "POST", 4); |
|
|
|
|
std::stringstream ss; |
|
|
|
|
ss.write(buf->data(), bytes_transferred); |
|
|
|
|
if (isHtml) |
|
|
|
|
{ |
|
|
|
|
if(isHtml) { |
|
|
|
|
std::string header; |
|
|
|
|
while(!ss.eof() && header != "\r") |
|
|
|
|
std::getline(ss, header); |
|
|
|
|
if (ss.eof ()) |
|
|
|
|
{ |
|
|
|
|
if(ss.eof()) { |
|
|
|
|
LogPrint(eLogError, "Malformed I2PControl request. HTTP header expected"); |
|
|
|
|
return; // TODO:
|
|
|
|
|
} |
|
|
|
@ -132,24 +123,18 @@ namespace client {
@@ -132,24 +123,18 @@ namespace client {
|
|
|
|
|
|
|
|
|
|
I2PControlSession::Response response = m_Session->handleRequest(ss); |
|
|
|
|
SendResponse(socket, buf, response.toJsonString(), isHtml); |
|
|
|
|
} |
|
|
|
|
catch (const std::exception& ex) |
|
|
|
|
{ |
|
|
|
|
} catch(const std::exception& ex) { |
|
|
|
|
LogPrint(eLogError, "I2PControl handle request: ", ex.what()); |
|
|
|
|
} |
|
|
|
|
catch (...) |
|
|
|
|
{ |
|
|
|
|
} catch(...) { |
|
|
|
|
LogPrint(eLogError, "I2PControl handle request unknown exception"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void I2PControlService::SendResponse(std::shared_ptr<boost::asio::ip::tcp::socket> socket, |
|
|
|
|
std::shared_ptr<I2PControlBuffer> buf, const std::string& response, bool isHtml) |
|
|
|
|
{ |
|
|
|
|
size_t len = response.length(), offset = 0; |
|
|
|
|
if (isHtml) |
|
|
|
|
{ |
|
|
|
|
if(isHtml) { |
|
|
|
|
std::ostringstream header; |
|
|
|
|
header << "HTTP/1.1 200 OK\r\n"; |
|
|
|
|
header << "Connection: close\r\n"; |
|
|
|
@ -164,10 +149,13 @@ namespace client {
@@ -164,10 +149,13 @@ namespace client {
|
|
|
|
|
memcpy(buf->data(), header.str().c_str(), offset); |
|
|
|
|
} |
|
|
|
|
memcpy(buf->data() + offset, response.c_str(), len); |
|
|
|
|
boost::asio::async_write (*socket, boost::asio::buffer (buf->data (), offset + len), |
|
|
|
|
boost::asio::transfer_all (), |
|
|
|
|
std::bind(&I2PControlService::HandleResponseSent, this, |
|
|
|
|
std::placeholders::_1, std::placeholders::_2, socket, buf)); |
|
|
|
|
boost::asio::async_write( |
|
|
|
|
*socket, boost::asio::buffer(buf->data(), offset + 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, |
|
|
|
|