From 6d892179c89cca378b5f185b62c470adaadabb81 Mon Sep 17 00:00:00 2001 From: orignal Date: Mon, 29 Feb 2016 14:44:15 -0500 Subject: [PATCH] added gzip parameter for server tunnels --- ClientContext.cpp | 14 ++++++------ ClientContext.h | 1 + Destination.cpp | 4 ++-- Destination.h | 4 ++-- I2PTunnel.cpp | 12 +++++----- I2PTunnel.h | 6 ++--- Streaming.cpp | 57 ++++++++++++++++++++++++----------------------- Streaming.h | 6 ++--- 8 files changed, 53 insertions(+), 51 deletions(-) diff --git a/ClientContext.cpp b/ClientContext.cpp index 304b53c6..ea459a13 100644 --- a/ClientContext.cpp +++ b/ClientContext.cpp @@ -313,6 +313,7 @@ namespace client int inPort = section.second.get (I2P_SERVER_TUNNEL_INPORT, 0); std::string accessList = section.second.get (I2P_SERVER_TUNNEL_ACCESS_LIST, ""); std::string hostOverride = section.second.get (I2P_SERVER_TUNNEL_HOST_OVERRIDE, ""); + bool gzip = section.second.get (I2P_SERVER_TUNNEL_GZIP, true); i2p::data::SigningKeyType sigType = section.second.get (I2P_SERVER_TUNNEL_SIGNATURE_TYPE, i2p::data::SIGNING_KEY_TYPE_ECDSA_SHA256_P256); // I2CP std::map options; @@ -326,13 +327,12 @@ namespace client localDestination = CreateNewLocalDestination (k, true, &options); I2PServerTunnel * serverTunnel; - if (type == I2P_TUNNELS_SECTION_TYPE_HTTP) { - 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) { - serverTunnel = new I2PServerTunnelIRC (name, host, port, localDestination, inPort); - } + if (type == I2P_TUNNELS_SECTION_TYPE_HTTP) + serverTunnel = new I2PServerTunnelHTTP (name, host, port, localDestination, hostOverride, inPort, gzip); + else if (type == I2P_TUNNELS_SECTION_TYPE_IRC) + serverTunnel = new I2PServerTunnelIRC (name, host, port, localDestination, inPort, gzip); + else // regular server tunnel by default + serverTunnel = new I2PServerTunnel (name, host, port, localDestination, inPort, gzip); if (accessList.length () > 0) { diff --git a/ClientContext.h b/ClientContext.h index 1be2960b..8126a17a 100644 --- a/ClientContext.h +++ b/ClientContext.h @@ -35,6 +35,7 @@ namespace client const char I2P_SERVER_TUNNEL_SIGNATURE_TYPE[] = "signaturetype"; const char I2P_SERVER_TUNNEL_INPORT[] = "inport"; const char I2P_SERVER_TUNNEL_ACCESS_LIST[] = "accesslist"; + const char I2P_SERVER_TUNNEL_GZIP[] = "gzip"; class ClientContext { diff --git a/Destination.cpp b/Destination.cpp index eefd9700..c53c4ee6 100644 --- a/Destination.cpp +++ b/Destination.cpp @@ -576,9 +576,9 @@ namespace client return false; } - std::shared_ptr ClientDestination::CreateStreamingDestination (int port) + std::shared_ptr ClientDestination::CreateStreamingDestination (int port, bool gzip) { - auto dest = std::make_shared (shared_from_this (), port); + auto dest = std::make_shared (shared_from_this (), port, gzip); if (port) m_StreamingDestinationsByPorts[port] = dest; else // update default diff --git a/Destination.h b/Destination.h index 2da4c8e5..44cb7424 100644 --- a/Destination.h +++ b/Destination.h @@ -46,7 +46,7 @@ namespace client const int STREAM_REQUEST_TIMEOUT = 60; //in seconds const char I2CP_PARAM_TAGS_TO_SEND[] = "crypto.tagsToSend"; const int DEFAULT_TAGS_TO_SEND = 40; - + typedef std::function stream)> StreamRequestComplete; class ClientDestination: public i2p::garlic::GarlicDestination, @@ -82,7 +82,7 @@ namespace client void CancelDestinationRequest (const i2p::data::IdentHash& dest); // streaming - std::shared_ptr CreateStreamingDestination (int port); // additional + std::shared_ptr CreateStreamingDestination (int port, bool gzip = true); // additional std::shared_ptr GetStreamingDestination (int port = 0) const; // following methods operate with default streaming destination void CreateStream (StreamRequestComplete streamRequestComplete, const i2p::data::IdentHash& dest, int port = 0); diff --git a/I2PTunnel.cpp b/I2PTunnel.cpp index f1d4a488..e2b75232 100644 --- a/I2PTunnel.cpp +++ b/I2PTunnel.cpp @@ -371,10 +371,10 @@ namespace client } I2PServerTunnel::I2PServerTunnel (const std::string& name, const std::string& address, - int port, std::shared_ptr localDestination, int inport): + int port, std::shared_ptr localDestination, int inport, bool gzip): I2PService (localDestination), m_Name (name), m_Address (address), m_Port (port), m_IsAccessList (false) { - m_PortDestination = localDestination->CreateStreamingDestination (inport > 0 ? inport : port); + m_PortDestination = localDestination->CreateStreamingDestination (inport > 0 ? inport : port, gzip); } void I2PServerTunnel::Start () @@ -462,8 +462,8 @@ namespace client I2PServerTunnelHTTP::I2PServerTunnelHTTP (const std::string& name, const std::string& address, int port, std::shared_ptr localDestination, - const std::string& host, int inport): - I2PServerTunnel (name, address, port, localDestination, inport), + const std::string& host, int inport, bool gzip): + I2PServerTunnel (name, address, port, localDestination, inport, gzip), m_Host (host.length () > 0 ? host : address) { } @@ -477,8 +477,8 @@ 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) + int port, std::shared_ptr localDestination, int inport, bool gzip): + I2PServerTunnel (name, address, port, localDestination, inport, gzip) { } diff --git a/I2PTunnel.h b/I2PTunnel.h index dc791506..9b704033 100644 --- a/I2PTunnel.h +++ b/I2PTunnel.h @@ -134,7 +134,7 @@ namespace client public: I2PServerTunnel (const std::string& name, const std::string& address, int port, - std::shared_ptr localDestination, int inport = 0); + std::shared_ptr localDestination, int inport = 0, bool gzip = true); void Start (); void Stop (); @@ -173,7 +173,7 @@ namespace client I2PServerTunnelHTTP (const std::string& name, const std::string& address, int port, std::shared_ptr localDestination, const std::string& host, - int inport = 0); + int inport = 0, bool gzip = true); private: @@ -189,7 +189,7 @@ namespace client public: I2PServerTunnelIRC (const std::string& name, const std::string& address, int port, - std::shared_ptr localDestination, int inport = 0); + std::shared_ptr localDestination, int inport = 0, bool gzip = true); private: diff --git a/Streaming.cpp b/Streaming.cpp index 46644cb0..33be58b5 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -628,7 +628,7 @@ namespace stream std::vector msgs; for (auto it: packets) { - auto msg = m_RoutingSession->WrapSingleMessage (CreateDataMessage (it->GetBuffer (), it->GetLength ())); + auto msg = m_RoutingSession->WrapSingleMessage (m_LocalDestination.CreateDataMessage (it->GetBuffer (), it->GetLength (), m_Port)); msgs.push_back (i2p::tunnel::TunnelMessageBlock { i2p::tunnel::eDeliveryTypeTunnel, @@ -779,33 +779,9 @@ namespace stream m_CurrentRemoteLease = nullptr; } - std::shared_ptr Stream::CreateDataMessage (const uint8_t * payload, size_t len) - { - auto msg = NewI2NPShortMessage (); - if (len <= i2p::stream::COMPRESSION_THRESHOLD_SIZE) - m_LocalDestination.m_Deflator.SetCompressionLevel (Z_NO_COMPRESSION); - else - m_LocalDestination.m_Deflator.SetCompressionLevel (Z_DEFAULT_COMPRESSION); - uint8_t * buf = msg->GetPayload (); - buf += 4; // reserve for lengthlength - msg->len += 4; - size_t size = m_LocalDestination.m_Deflator.Deflate (payload, len, buf, msg->maxLen - msg->len); - if (size) - { - htobe32buf (msg->GetPayload (), size); // length - htobe16buf (buf + 4, m_LocalDestination.GetLocalPort ()); // source port - htobe16buf (buf + 6, m_Port); // destination port - buf[9] = i2p::client::PROTOCOL_TYPE_STREAMING; // streaming protocol - msg->len += size; - msg->FillI2NPMessageHeader (eI2NPData); - } - else - msg = nullptr; - return msg; - } - - StreamingDestination::StreamingDestination (std::shared_ptr owner, uint16_t localPort): - m_Owner (owner), m_LocalPort (localPort), m_PendingIncomingTimer (m_Owner->GetService ()) + StreamingDestination::StreamingDestination (std::shared_ptr owner, uint16_t localPort, bool gzip): + m_Owner (owner), m_LocalPort (localPort), m_Gzip (gzip), + m_PendingIncomingTimer (m_Owner->GetService ()) { } @@ -993,5 +969,30 @@ namespace stream else delete uncompressed; } + + std::shared_ptr StreamingDestination::CreateDataMessage (const uint8_t * payload, size_t len, uint16_t toPort) + { + auto msg = NewI2NPShortMessage (); + if (!m_Gzip || len <= i2p::stream::COMPRESSION_THRESHOLD_SIZE) + m_Deflator.SetCompressionLevel (Z_NO_COMPRESSION); + else + m_Deflator.SetCompressionLevel (Z_DEFAULT_COMPRESSION); + uint8_t * buf = msg->GetPayload (); + buf += 4; // reserve for lengthlength + msg->len += 4; + size_t size = m_Deflator.Deflate (payload, len, buf, msg->maxLen - msg->len); + if (size) + { + htobe32buf (msg->GetPayload (), size); // length + htobe16buf (buf + 4, m_LocalPort); // source port + htobe16buf (buf + 6, toPort); // destination port + buf[9] = i2p::client::PROTOCOL_TYPE_STREAMING; // streaming protocol + msg->len += size; + msg->FillI2NPMessageHeader (eI2NPData); + } + else + msg = nullptr; + return msg; + } } } diff --git a/Streaming.h b/Streaming.h index c908c6ec..c29b62f9 100644 --- a/Streaming.h +++ b/Streaming.h @@ -158,8 +158,6 @@ namespace stream void ScheduleResend (); void HandleResendTimer (const boost::system::error_code& ecode); void HandleAckSendTimer (const boost::system::error_code& ecode); - - std::shared_ptr CreateDataMessage (const uint8_t * payload, size_t len); private: @@ -195,7 +193,7 @@ namespace stream typedef std::function)> Acceptor; - StreamingDestination (std::shared_ptr owner, uint16_t localPort = 0); + StreamingDestination (std::shared_ptr owner, uint16_t localPort = 0, bool gzip = true); ~StreamingDestination (); void Start (); @@ -210,6 +208,7 @@ namespace stream uint16_t GetLocalPort () const { return m_LocalPort; }; void HandleDataMessagePayload (const uint8_t * buf, size_t len); + std::shared_ptr CreateDataMessage (const uint8_t * payload, size_t len, uint16_t toPort); private: @@ -221,6 +220,7 @@ namespace stream std::shared_ptr m_Owner; uint16_t m_LocalPort; + bool m_Gzip; // gzip compression of data messages std::mutex m_StreamsMutex; std::map > m_Streams; // sendStreamID->stream Acceptor m_Acceptor;