diff --git a/ClientContext.cpp b/ClientContext.cpp index 57ccc32e..53c442f0 100644 --- a/ClientContext.cpp +++ b/ClientContext.cpp @@ -59,6 +59,8 @@ namespace client std::string socksProxyKeys; i2p::config::GetOption("socksproxy.keys", socksProxyKeys); std::string socksProxyAddr; i2p::config::GetOption("socksproxy.address", socksProxyAddr); uint16_t socksProxyPort; i2p::config::GetOption("socksproxy.port", socksProxyPort); + std::string socksOutProxyAddr; i2p::config::GetOption("socksproxy.outproxy", socksOutProxyAddr); + uint16_t socksOutProxyPort; i2p::config::GetOption("socksproxy.outproxyport", socksOutProxyPort); LogPrint(eLogInfo, "Clients: starting SOCKS Proxy at ", socksProxyAddr, ":", socksProxyPort); if (socksProxyKeys.length () > 0) { @@ -66,7 +68,7 @@ namespace client LoadPrivateKeys (keys, socksProxyKeys); localDestination = CreateNewLocalDestination (keys, false); } - m_SocksProxy = new i2p::proxy::SOCKSProxy(socksProxyAddr, socksProxyPort, localDestination); + m_SocksProxy = new i2p::proxy::SOCKSProxy(socksProxyAddr, socksProxyPort, socksOutProxyAddr, socksOutProxyPort, localDestination); m_SocksProxy->Start(); } @@ -244,6 +246,7 @@ namespace client options[I2CP_PARAM_OUTBOUND_TUNNEL_LENGTH] = GetI2CPOption (section, I2CP_PARAM_OUTBOUND_TUNNEL_LENGTH, DEFAULT_OUTBOUND_TUNNEL_LENGTH); options[I2CP_PARAM_INBOUND_TUNNELS_QUANTITY] = GetI2CPOption (section, I2CP_PARAM_INBOUND_TUNNELS_QUANTITY, DEFAULT_INBOUND_TUNNELS_QUANTITY); options[I2CP_PARAM_OUTBOUND_TUNNELS_QUANTITY] = GetI2CPOption (section, I2CP_PARAM_OUTBOUND_TUNNELS_QUANTITY, DEFAULT_OUTBOUND_TUNNELS_QUANTITY); + options[I2CP_PARAM_TAGS_TO_SEND] = GetI2CPOption (section, I2CP_PARAM_TAGS_TO_SEND, DEFAULT_TAGS_TO_SEND); } void ClientContext::ReadTunnels () @@ -286,7 +289,9 @@ namespace client { i2p::data::PrivateKeys k; LoadPrivateKeys (k, keys, sigType); - localDestination = CreateNewLocalDestination (k, false, &options); + localDestination = FindLocalDestination (k.GetPublic ()->GetIdentHash ()); + if (!localDestination) + localDestination = CreateNewLocalDestination (k, false, &options); } auto clientTunnel = new I2PClientTunnel (name, dest, address, port, localDestination, destinationPort); if (m_ClientTunnels.insert (std::make_pair (port, std::unique_ptr(clientTunnel))).second) @@ -309,9 +314,12 @@ namespace client std::map options; ReadI2CPOptions (section, options); + std::shared_ptr localDestination = nullptr; i2p::data::PrivateKeys k; LoadPrivateKeys (k, keys, sigType); - auto localDestination = CreateNewLocalDestination (k, true, &options); + 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, inPort) : new I2PServerTunnel (name, host, port, localDestination, inPort); diff --git a/Destination.cpp b/Destination.cpp index 765fbdae..233946ad 100644 --- a/Destination.cpp +++ b/Destination.cpp @@ -27,6 +27,7 @@ namespace client int outboundTunnelLen = DEFAULT_OUTBOUND_TUNNEL_LENGTH; int inboundTunnelsQuantity = DEFAULT_INBOUND_TUNNELS_QUANTITY; int outboundTunnelsQuantity = DEFAULT_OUTBOUND_TUNNELS_QUANTITY; + int numTags = DEFAULT_TAGS_TO_SEND; std::shared_ptr > explicitPeers; if (params) { @@ -70,6 +71,16 @@ namespace client LogPrint (eLogInfo, "Destination: Outbound tunnels quantity set to ", quantity); } } + it = params->find (I2CP_PARAM_TAGS_TO_SEND); + if (it != params->end ()) + { + int tagsToSend = boost::lexical_cast(it->second); + if (tagsToSend > 0) + { + numTags = tagsToSend; + LogPrint (eLogInfo, "Destination: Tags to send set to ", tagsToSend); + } + } it = params->find (I2CP_PARAM_EXPLICIT_PEERS); if (it != params->end ()) { @@ -85,6 +96,7 @@ namespace client LogPrint (eLogInfo, "Destination: Explicit peers set to ", it->second); } } + SetNumTags (numTags); m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (inboundTunnelLen, outboundTunnelLen, inboundTunnelsQuantity, outboundTunnelsQuantity); if (explicitPeers) m_Pool->SetExplicitPeers (explicitPeers); diff --git a/Destination.h b/Destination.h index 3de47a81..8ff2dbc8 100644 --- a/Destination.h +++ b/Destination.h @@ -42,7 +42,9 @@ namespace client const int DEFAULT_OUTBOUND_TUNNELS_QUANTITY = 5; const char I2CP_PARAM_EXPLICIT_PEERS[] = "explicitPeers"; 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, diff --git a/Garlic.cpp b/Garlic.cpp index 71688305..1bd5cc3f 100644 --- a/Garlic.cpp +++ b/Garlic.cpp @@ -100,7 +100,7 @@ namespace garlic if (ts >= it->second->tagsCreationTime + OUTGOING_TAGS_EXPIRATION_TIMEOUT) { if (m_Owner) - m_Owner->RemoveCreatedSession (it->first); + m_Owner->RemoveDeliveryStatusSession (it->first); delete it->second; it = m_UnconfirmedTagsMsgs.erase (it); } @@ -546,21 +546,24 @@ namespace garlic std::shared_ptr GarlicDestination::WrapMessage (std::shared_ptr destination, std::shared_ptr msg, bool attachLeaseSet) { - auto session = GetRoutingSession (destination, attachLeaseSet); // 32 tags by default + auto session = GetRoutingSession (destination, attachLeaseSet); return session->WrapSingleMessage (msg); } std::shared_ptr GarlicDestination::GetRoutingSession ( std::shared_ptr destination, bool attachLeaseSet) { - auto it = m_Sessions.find (destination->GetIdentHash ()); - std::shared_ptr session; - if (it != m_Sessions.end ()) - session = it->second; + GarlicRoutingSessionPtr session; + { + std::unique_lock l(m_SessionsMutex); + auto it = m_Sessions.find (destination->GetIdentHash ()); + if (it != m_Sessions.end ()) + session = it->second; + } if (!session) { session = std::make_shared (this, destination, - attachLeaseSet ? 40 : 4, attachLeaseSet); // 40 tags for connections and 4 for LS requests + attachLeaseSet ? m_NumTags : 4, attachLeaseSet); // specified num tags for connections and 4 for LS requests std::unique_lock l(m_SessionsMutex); m_Sessions[destination->GetIdentHash ()] = session; } @@ -582,25 +585,25 @@ namespace garlic } } - void GarlicDestination::RemoveCreatedSession (uint32_t msgID) + void GarlicDestination::RemoveDeliveryStatusSession (uint32_t msgID) { - m_CreatedSessions.erase (msgID); + m_DeliveryStatusSessions.erase (msgID); } - void GarlicDestination::DeliveryStatusSent (std::shared_ptr session, uint32_t msgID) + void GarlicDestination::DeliveryStatusSent (GarlicRoutingSessionPtr session, uint32_t msgID) { - m_CreatedSessions[msgID] = session; + m_DeliveryStatusSessions[msgID] = session; } void GarlicDestination::HandleDeliveryStatusMessage (std::shared_ptr msg) { uint32_t msgID = bufbe32toh (msg->GetPayload ()); { - auto it = m_CreatedSessions.find (msgID); - if (it != m_CreatedSessions.end ()) + auto it = m_DeliveryStatusSessions.find (msgID); + if (it != m_DeliveryStatusSessions.end ()) { it->second->MessageConfirmed (msgID); - m_CreatedSessions.erase (it); + m_DeliveryStatusSessions.erase (it); LogPrint (eLogDebug, "Garlic: message ", msgID, " acknowledged"); } } diff --git a/Garlic.h b/Garlic.h index 716bc466..1bfcab7c 100644 --- a/Garlic.h +++ b/Garlic.h @@ -113,24 +113,30 @@ namespace garlic i2p::crypto::CBCEncryption m_Encryption; std::unique_ptr m_ElGamalEncryption; + + public: + // for HTTP only + size_t GetNumOutgoingTags () const { return m_SessionTags.size (); }; }; - + using GarlicRoutingSessionPtr = std::shared_ptr; + class GarlicDestination: public i2p::data::LocalDestination { public: - GarlicDestination (): m_LastTagsCleanupTime (0) {}; + GarlicDestination (): m_NumTags (32), m_LastTagsCleanupTime (0) {}; // 32 tags by default ~GarlicDestination (); + void SetNumTags (int numTags) { m_NumTags = numTags; }; std::shared_ptr GetRoutingSession (std::shared_ptr destination, bool attachLeaseSet); void CleanupRoutingSessions (); - void RemoveCreatedSession (uint32_t msgID); + void RemoveDeliveryStatusSession (uint32_t msgID); std::shared_ptr WrapMessage (std::shared_ptr destination, std::shared_ptr msg, bool attachLeaseSet = false); void AddSessionKey (const uint8_t * key, const uint8_t * tag); // one tag virtual bool SubmitSessionKey (const uint8_t * key, const uint8_t * tag); // from different thread - void DeliveryStatusSent (std::shared_ptr session, uint32_t msgID); + void DeliveryStatusSent (GarlicRoutingSessionPtr session, uint32_t msgID); virtual void ProcessGarlicMessage (std::shared_ptr msg); virtual void ProcessDeliveryStatusMessage (std::shared_ptr msg); @@ -154,13 +160,20 @@ namespace garlic private: // outgoing sessions + int m_NumTags; std::mutex m_SessionsMutex; - std::map > m_Sessions; + std::map m_Sessions; // incoming std::map> m_Tags; uint32_t m_LastTagsCleanupTime; // DeliveryStatus - std::map > m_CreatedSessions; // msgID -> session + std::map m_DeliveryStatusSessions; // msgID -> session + + public: + + // for HTTP only + size_t GetNumIncomingTags () const { return m_Tags.size (); } + const decltype(m_Sessions)& GetSessions () const { return m_Sessions; }; }; } } diff --git a/HTTPProxy.cpp b/HTTPProxy.cpp index 12d2c765..b4010a37 100644 --- a/HTTPProxy.cpp +++ b/HTTPProxy.cpp @@ -182,7 +182,22 @@ namespace proxy bool HTTPProxyHandler::HandleData(uint8_t *http_buff, std::size_t len) { - assert(len); // This should always be called with a least a byte left to parse + // TODO: we should srtrip 'Referer' better, because it might be inside message body + /*assert(len); // This should always be called with a least a byte left to parse + + // remove "Referer" from http requst + http_buff[len] = 0; + auto start = strstr((char *)http_buff, "\nReferer:"); + if (start) + { + auto end = strchr (start + 1, '\n'); + if (end) + { + strncpy(start, end, (char *)(http_buff + len) - end); + len -= (end - start); + } + }*/ + while (len > 0) { //TODO: fallback to finding HOst: header if needed diff --git a/HTTPServer.cpp b/HTTPServer.cpp index e4a75e1a..2fef6e66 100644 --- a/HTTPServer.cpp +++ b/HTTPServer.cpp @@ -534,6 +534,13 @@ namespace util s << "
\r\n" << std::endl; } } + s << "Tags
Incoming: " << dest->GetNumIncomingTags () << "
Outgoing:
" << std::endl; + for (auto it: dest->GetSessions ()) + { + s << i2p::client::context.GetAddressBook ().ToAddress(it.first) << " "; + s << it.second->GetNumOutgoingTags () << "
" << std::endl; + } + s << "
" << std::endl; // s << "
\r\nStreams:
\r\n"; // for (auto it: dest->GetStreamingDestination ()->GetStreams ()) // { diff --git a/I2PControl.cpp b/I2PControl.cpp index 35f3d0c6..7b63df93 100644 --- a/I2PControl.cpp +++ b/I2PControl.cpp @@ -162,7 +162,7 @@ namespace client { auto request = std::make_shared(); socket->async_read_some ( -#if BOOST_VERSION >= 104900 +#if defined(BOOST_ASIO_HAS_STD_ARRAY) boost::asio::buffer (*request), #else boost::asio::buffer (request->data (), request->size ()), @@ -300,7 +300,7 @@ namespace client auto password = params.get ("Password"); LogPrint (eLogDebug, "I2PControl: Authenticate API=", api, " Password=", password); if (password != m_Password) { - LogPrint (eLogError, "I2PControl: Authenticate Invalid password: ", password); + LogPrint (eLogError, "I2PControl: Authenticate - Invalid password: ", password); return; } InsertParam (results, "API", api); diff --git a/Makefile b/Makefile index 2e86fd88..b11305ce 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,7 @@ else ifeq ($(UNAME),Linux) include Makefile.linux else # win32 mingw DAEMON_SRC += DaemonWin32.cpp Win32/Win32Service.cpp + WINDIR := True include Makefile.mingw endif @@ -30,6 +31,9 @@ all: mk_build_dir $(ARLIB) $(ARLIB_CLIENT) $(I2PD) mk_build_dir: mkdir -p obj + ifeq ($(WINDIR),True) + mkdir -p obj/Win32 + endif api: mk_build_dir $(SHLIB) $(ARLIB) api_client: mk_build_dir $(SHLIB) $(ARLIB) $(SHLIB_CLIENT) $(ARLIB_CLIENT) diff --git a/Makefile.mingw b/Makefile.mingw index c2fd35a9..57fe094b 100644 --- a/Makefile.mingw +++ b/Makefile.mingw @@ -4,4 +4,4 @@ NEEDED_CXXFLAGS = -std=c++11 BOOST_SUFFIX = -mgw48-mt-1_59 INCFLAGS = -I/usr/include/ -I/usr/local/include/ -I/c/dev/openssl/include -I/c/dev/boost/include/boost-1_59 LDFLAGS = -Wl,-rpath,/usr/local/lib -L/usr/local/lib -L/c/dev/openssl -L/c/dev/boost/lib -LDLIBS = -lboost_system$(BOOST_SUFFIX) -lboost_date_time$(BOOST_SUFFIX) -lboost_filesystem$(BOOST_SUFFIX) -lboost_regex$(BOOST_SUFFIX) -lboost_program_options$(BOOST_SUFFIX) -lssl -lcrypto -lz -lwsock32 -lws2_32 -lgdi32 -liphlpapi -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread +LDLIBS = -Wl,-Bstatic -lboost_system$(BOOST_SUFFIX) -Wl,-Bstatic -lboost_date_time$(BOOST_SUFFIX) -Wl,-Bstatic -lboost_filesystem$(BOOST_SUFFIX) -Wl,-Bstatic -lboost_regex$(BOOST_SUFFIX) -Wl,-Bstatic -lboost_program_options$(BOOST_SUFFIX) -Wl,-Bstatic -lssl -Wl,-Bstatic -lcrypto -Wl,-Bstatic -lz -Wl,-Bstatic -lwsock32 -Wl,-Bstatic -lws2_32 -Wl,-Bstatic -lgdi32 -Wl,-Bstatic -liphlpapi -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -Wl,-Bstatic -lpthread diff --git a/SOCKS.cpp b/SOCKS.cpp index 3a971a60..27b23df1 100644 --- a/SOCKS.cpp +++ b/SOCKS.cpp @@ -557,7 +557,8 @@ namespace proxy } } - SOCKSServer::SOCKSServer(const std::string& address, int port, std::shared_ptr localDestination) : + SOCKSServer::SOCKSServer(const std::string& address, int port, const std::string& outAddress, int outPort, + std::shared_ptr localDestination) : TCPIPAcceptor (address, port, localDestination ? localDestination : i2p::client::context.GetSharedLocalDestination ()) { } diff --git a/SOCKS.h b/SOCKS.h index cc2cfa24..82f068fb 100644 --- a/SOCKS.h +++ b/SOCKS.h @@ -15,7 +15,8 @@ namespace proxy { public: - SOCKSServer(const std::string& address, int port, std::shared_ptr localDestination = nullptr); + SOCKSServer(const std::string& address, int port, const std::string& outAddress, int outPort, + std::shared_ptr localDestination = nullptr); ~SOCKSServer() {}; protected: diff --git a/Streaming.cpp b/Streaming.cpp index 6834d4bb..ea90c1f0 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -714,7 +714,7 @@ namespace stream if (m_RemoteLeaseSet) { if (!m_RoutingSession) - m_RoutingSession = m_LocalDestination.GetOwner ()->GetRoutingSession (m_RemoteLeaseSet, 32); + m_RoutingSession = m_LocalDestination.GetOwner ()->GetRoutingSession (m_RemoteLeaseSet, true); auto leases = m_RemoteLeaseSet->GetNonExpiredLeases (false); // try without threshold first if (leases.empty ()) { diff --git a/Tunnel.cpp b/Tunnel.cpp index 267485c8..47b3246c 100644 --- a/Tunnel.cpp +++ b/Tunnel.cpp @@ -482,6 +482,11 @@ namespace tunnel uint16_t len = bufbe16toh(payload + TUNNEL_GATEWAY_HEADER_LENGTH_OFFSET); // we make payload as new I2NP message to send msg->offset += I2NP_HEADER_SIZE + TUNNEL_GATEWAY_HEADER_SIZE; + if (msg->offset + len > msg->len) + { + LogPrint (eLogError, "Tunnel: gateway payload ", (int)len, " exceeds message length ", (int)msg->len); + return; + } msg->len = msg->offset + len; auto typeID = msg->GetTypeID (); LogPrint (eLogDebug, "Tunnel: gateway of ", (int) len, " bytes for tunnel ", tunnel->GetTunnelID (), ", msg type ", (int)typeID); diff --git a/TunnelEndpoint.cpp b/TunnelEndpoint.cpp index 151a4828..0b730d35 100644 --- a/TunnelEndpoint.cpp +++ b/TunnelEndpoint.cpp @@ -90,6 +90,11 @@ namespace tunnel msg->offset = fragment - msg->buf; msg->len = msg->offset + size; + if (msg->len > msg->maxLen) + { + LogPrint (eLogError, "TunnelMessage: fragment is too long ", (int)size); + return; + } if (fragment + size < decrypted + TUNNEL_DATA_ENCRYPTED_SIZE) { // this is not last message. we have to copy it diff --git a/util.cpp b/util.cpp index 5f36f409..1ea5fcd7 100644 --- a/util.cpp +++ b/util.cpp @@ -155,15 +155,15 @@ namespace filesystem boost::filesystem::path GetDefaultDataDir() { - // Windows < Vista: C:\Documents and Settings\Username\.i2pd - // Windows >= Vista: C:\Users\Username\.i2pd + // Windows < Vista: C:\Documents and Settings\Username\Application Data\i2pd + // Windows >= Vista: C:\Users\Username\AppData\Roaming\i2pd // Mac: ~/Library/Application Support/i2pd // Unix: ~/.i2pd or /var/lib/i2pd is system=1 #ifdef WIN32 // Windows char localAppData[MAX_PATH]; - SHGetFolderPath(NULL, CSIDL_PROFILE, 0, 0, localAppData); - return boost::filesystem::path(std::string(localAppData) + "\\" + "." + appName); + SHGetFolderPath(NULL, CSIDL_APPDATA, 0, NULL, localAppData); + return boost::filesystem::path(std::string(localAppData) + "\\" + appName); #else /* UNIX */ bool service; i2p::config::GetOption("service", service); if (service) // use system folder