From e45cfe7d0cc444058228f584d353608988bbfdf9 Mon Sep 17 00:00:00 2001 From: xcps Date: Tue, 23 Feb 2016 00:33:21 +0500 Subject: [PATCH 1/5] init --- ClientContext.cpp | 15 ++++++++++---- ClientContext.h | 1 + I2PTunnel.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++++++ I2PTunnel.h | 33 ++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 4 deletions(-) diff --git a/ClientContext.cpp b/ClientContext.cpp index 19d841d3..3e4e0c05 100644 --- a/ClientContext.cpp +++ b/ClientContext.cpp @@ -303,7 +303,7 @@ namespace client LogPrint (eLogError, "Clients: I2P client tunnel with port ", port, " already exists"); numClientTunnels++; } - else if (type == I2P_TUNNELS_SECTION_TYPE_SERVER || type == I2P_TUNNELS_SECTION_TYPE_HTTP) + else if (type == I2P_TUNNELS_SECTION_TYPE_SERVER || type == I2P_TUNNELS_SECTION_TYPE_HTTP || type == I2P_TUNNELS_SECTION_TYPE_IRC) { // mandatory params std::string host = section.second.get (I2P_SERVER_TUNNEL_HOST); @@ -324,9 +324,16 @@ namespace client localDestination = FindLocalDestination (k.GetPublic ()->GetIdentHash ()); if (!localDestination) localDestination = CreateNewLocalDestination (k, true, &options); - I2PServerTunnel * serverTunnel = (type == I2P_TUNNELS_SECTION_TYPE_HTTP) ? - new I2PServerTunnelHTTP (name, host, port, localDestination, hostOverride, inPort) : - new I2PServerTunnel (name, host, port, localDestination, inPort); + + I2PServerTunnel * serverTunnel; + if (type == I2P_TUNNELS_SECTION_TYPE_HTTP) { + serverTunnel = new I2PServerTunnelHTTP (name, host, port, localDestination, inPort); + } else if (type == I2P_TUNNELS_SECTION_TYPE_SERVER) { + serverTunnel = new I2PServerTunnel (name, host, port, localDestination, inPort); + } else if (type == I2P_TUNNELS_SECTION_TYPE_IRC) { + serverTunnel = new I2PServerTunnelIRC (name, host, port, localDestination, inPort); + } + if (accessList.length () > 0) { std::set idents; diff --git a/ClientContext.h b/ClientContext.h index 30e495da..1be2960b 100644 --- a/ClientContext.h +++ b/ClientContext.h @@ -21,6 +21,7 @@ namespace client const char I2P_TUNNELS_SECTION_TYPE_CLIENT[] = "client"; const char I2P_TUNNELS_SECTION_TYPE_SERVER[] = "server"; const char I2P_TUNNELS_SECTION_TYPE_HTTP[] = "http"; + const char I2P_TUNNELS_SECTION_TYPE_IRC[] = "irc"; const char I2P_CLIENT_TUNNEL_PORT[] = "port"; const char I2P_CLIENT_TUNNEL_ADDRESS[] = "address"; const char I2P_CLIENT_TUNNEL_DESTINATION[] = "destination"; diff --git a/I2PTunnel.cpp b/I2PTunnel.cpp index 4eb1c573..51266eb8 100644 --- a/I2PTunnel.cpp +++ b/I2PTunnel.cpp @@ -234,6 +234,44 @@ namespace client } } + I2PTunnelConnectionIRC::I2PTunnelConnectionIRC (I2PService * owner, std::shared_ptr stream, + std::shared_ptr socket, + const boost::asio::ip::tcp::endpoint& target, const std::string& host): + I2PTunnelConnection (owner, stream, socket, target), m_Host (host), m_From (stream->GetRemoteIdentity ()) + { + } + + void I2PTunnelConnectionIRC::Write (const uint8_t * buf, size_t len) + { + std::string line; + m_OutPacket.str (""); + m_InPacket.write ((const char *)buf, len); + + while (!m_InPacket.eof () && !m_InPacket.fail ()) + { + std::getline (m_InPacket, line); + auto pos = line.find ("USER"); + if (pos != std::string::npos && pos == 0) + { + pos = line.find (" "); + pos++; + pos = line.find (" ", pos); + pos++; + pos = line.find (" ", pos); + pos++; + auto nextpos = line.find (" ", pos); + + m_OutPacket << line.substr (0, pos); + m_OutPacket << context.GetAddressBook ().ToAddress (m_From->GetIdentHash ()); + m_OutPacket << line.substr (nextpos) << '\n'; + } else { + m_OutPacket << line << '\n'; + } + } + I2PTunnelConnection::Write ((uint8_t *)m_OutPacket.str ().c_str (), m_OutPacket.str ().length ()); + } + + /* This handler tries to stablish a connection with the desired server and dies if it fails to do so */ class I2PClientTunnelHandler: public I2PServiceHandler, public std::enable_shared_from_this { @@ -436,5 +474,18 @@ namespace client AddHandler (conn); conn->Connect (); } + + I2PServerTunnelIRC::I2PServerTunnelIRC (const std::string& name, const std::string& address, + int port, std::shared_ptr localDestination, int inport): + I2PServerTunnel (name, address, port, localDestination, inport) + { + } + + void I2PServerTunnelIRC::CreateI2PConnection (std::shared_ptr stream) + { + auto conn = std::make_shared (this, stream, std::make_shared (GetService ()), GetEndpoint (), GetAddress ()); + AddHandler (conn); + conn->Connect (); + } } } diff --git a/I2PTunnel.h b/I2PTunnel.h index 834897f7..dc791506 100644 --- a/I2PTunnel.h +++ b/I2PTunnel.h @@ -80,6 +80,26 @@ namespace client std::shared_ptr m_From; }; + class I2PTunnelConnectionIRC: public I2PTunnelConnection + { + public: + + I2PTunnelConnectionIRC (I2PService * owner, std::shared_ptr stream, + std::shared_ptr socket, + const boost::asio::ip::tcp::endpoint& target, const std::string& host); + + protected: + + void Write (const uint8_t * buf, size_t len); + + private: + + std::string m_Host; + std::shared_ptr m_From; + std::stringstream m_OutPacket, m_InPacket; + }; + + class I2PClientTunnel: public TCPIPAcceptor { protected: @@ -163,6 +183,19 @@ namespace client std::string m_Host; }; + + class I2PServerTunnelIRC: public I2PServerTunnel + { + public: + + I2PServerTunnelIRC (const std::string& name, const std::string& address, int port, + std::shared_ptr localDestination, int inport = 0); + + private: + + void CreateI2PConnection (std::shared_ptr stream); + }; + } } From a26dc39a6d481c642c3d182e1e4fea8d1248c47e Mon Sep 17 00:00:00 2001 From: xcps Date: Tue, 23 Feb 2016 09:48:46 +0500 Subject: [PATCH 2/5] ident fix --- I2PTunnel.cpp | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/I2PTunnel.cpp b/I2PTunnel.cpp index 51266eb8..014e7028 100644 --- a/I2PTunnel.cpp +++ b/I2PTunnel.cpp @@ -249,25 +249,26 @@ namespace client while (!m_InPacket.eof () && !m_InPacket.fail ()) { - std::getline (m_InPacket, line); - auto pos = line.find ("USER"); - if (pos != std::string::npos && pos == 0) - { - pos = line.find (" "); - pos++; - pos = line.find (" ", pos); - pos++; - pos = line.find (" ", pos); - pos++; - auto nextpos = line.find (" ", pos); + std::getline (m_InPacket, line); + auto pos = line.find ("USER"); + if (pos != std::string::npos && pos == 0) + { + pos = line.find (" "); + pos++; + pos = line.find (" ", pos); + pos++; + pos = line.find (" ", pos); + pos++; + auto nextpos = line.find (" ", pos); - m_OutPacket << line.substr (0, pos); - m_OutPacket << context.GetAddressBook ().ToAddress (m_From->GetIdentHash ()); - m_OutPacket << line.substr (nextpos) << '\n'; - } else { - m_OutPacket << line << '\n'; - } + m_OutPacket << line.substr (0, pos); + m_OutPacket << context.GetAddressBook ().ToAddress (m_From->GetIdentHash ()); + m_OutPacket << line.substr (nextpos) << '\n'; + } else { + m_OutPacket << line << '\n'; + } } + LogPrint (eLogError, m_OutPacket.str ().substr (0, m_OutPacket.str ().length ())); I2PTunnelConnection::Write ((uint8_t *)m_OutPacket.str ().c_str (), m_OutPacket.str ().length ()); } @@ -477,15 +478,15 @@ namespace client I2PServerTunnelIRC::I2PServerTunnelIRC (const std::string& name, const std::string& address, int port, std::shared_ptr localDestination, int inport): - I2PServerTunnel (name, address, port, localDestination, inport) + I2PServerTunnel (name, address, port, localDestination, inport) { } void I2PServerTunnelIRC::CreateI2PConnection (std::shared_ptr stream) { - auto conn = std::make_shared (this, stream, std::make_shared (GetService ()), GetEndpoint (), GetAddress ()); - AddHandler (conn); - conn->Connect (); + auto conn = std::make_shared (this, stream, std::make_shared (GetService ()), GetEndpoint (), GetAddress ()); + AddHandler (conn); + conn->Connect (); } } } From 0fe7bdf849cff5cd572b7c1a263d3449e0ed15f9 Mon Sep 17 00:00:00 2001 From: xcps Date: Sun, 28 Feb 2016 18:03:12 +0500 Subject: [PATCH 3/5] init --- I2PTunnel.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/I2PTunnel.cpp b/I2PTunnel.cpp index 014e7028..eec57928 100644 --- a/I2PTunnel.cpp +++ b/I2PTunnel.cpp @@ -243,9 +243,14 @@ namespace client void I2PTunnelConnectionIRC::Write (const uint8_t * buf, size_t len) { + char *p = (char*)(buf + len); + *p = '\0'; + LogPrint (eLogError, "======= packet received =====\n", buf, "==============\n"); std::string line; m_OutPacket.str (""); + m_InPacket.clear (); m_InPacket.write ((const char *)buf, len); + LogPrint (eLogError, "======= inpacket =====\n", m_InPacket.str ().c_str (), "==============\n"); while (!m_InPacket.eof () && !m_InPacket.fail ()) { @@ -260,7 +265,6 @@ namespace client pos = line.find (" ", pos); pos++; auto nextpos = line.find (" ", pos); - m_OutPacket << line.substr (0, pos); m_OutPacket << context.GetAddressBook ().ToAddress (m_From->GetIdentHash ()); m_OutPacket << line.substr (nextpos) << '\n'; @@ -268,7 +272,7 @@ namespace client m_OutPacket << line << '\n'; } } - LogPrint (eLogError, m_OutPacket.str ().substr (0, m_OutPacket.str ().length ())); + LogPrint (eLogError, "======= outpacket =====\n", m_OutPacket.str ().substr (0, m_OutPacket.str ().length ()), "============\n"); I2PTunnelConnection::Write ((uint8_t *)m_OutPacket.str ().c_str (), m_OutPacket.str ().length ()); } From 57a53b4b6c8b8a1f64ccfac8991bc7903962ff77 Mon Sep 17 00:00:00 2001 From: xcps Date: Sun, 28 Feb 2016 21:13:01 +0500 Subject: [PATCH 4/5] fixed I2PServerTunnelHTTP call --- ClientContext.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ClientContext.cpp b/ClientContext.cpp index 3e4e0c05..304b53c6 100644 --- a/ClientContext.cpp +++ b/ClientContext.cpp @@ -327,7 +327,7 @@ namespace client I2PServerTunnel * serverTunnel; if (type == I2P_TUNNELS_SECTION_TYPE_HTTP) { - serverTunnel = new I2PServerTunnelHTTP (name, host, port, localDestination, inPort); + serverTunnel = new I2PServerTunnelHTTP (name, host, port, localDestination, hostOverride, inPort); } else if (type == I2P_TUNNELS_SECTION_TYPE_SERVER) { serverTunnel = new I2PServerTunnel (name, host, port, localDestination, inPort); } else if (type == I2P_TUNNELS_SECTION_TYPE_IRC) { From 7b39a12396a0650c2286bbbfaeba0451b33e425c Mon Sep 17 00:00:00 2001 From: xcps Date: Sun, 28 Feb 2016 22:32:34 +0500 Subject: [PATCH 5/5] ready --- I2PTunnel.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/I2PTunnel.cpp b/I2PTunnel.cpp index eec57928..0904ed35 100644 --- a/I2PTunnel.cpp +++ b/I2PTunnel.cpp @@ -245,12 +245,11 @@ namespace client { char *p = (char*)(buf + len); *p = '\0'; - LogPrint (eLogError, "======= packet received =====\n", buf, "==============\n"); - std::string line; + std::string line; m_OutPacket.str (""); + m_InPacket.str (""); m_InPacket.clear (); m_InPacket.write ((const char *)buf, len); - LogPrint (eLogError, "======= inpacket =====\n", m_InPacket.str ().c_str (), "==============\n"); while (!m_InPacket.eof () && !m_InPacket.fail ()) { @@ -272,7 +271,6 @@ namespace client m_OutPacket << line << '\n'; } } - LogPrint (eLogError, "======= outpacket =====\n", m_OutPacket.str ().substr (0, m_OutPacket.str ().length ()), "============\n"); I2PTunnelConnection::Write ((uint8_t *)m_OutPacket.str ().c_str (), m_OutPacket.str ().length ()); }