modulize client protocols

Signed-off-by: R4SAS <r4sas@i2pmail.org>
This commit is contained in:
R4SAS 2019-03-04 22:52:21 +03:00
parent 18cb3912e5
commit 3ab5ac66b6
No known key found for this signature in database
GPG key ID: 66F6C87B98EBCFE2
15 changed files with 184 additions and 72 deletions

View file

@ -6,6 +6,8 @@
* See full license text in LICENSE file at top of project tree
*/
#ifdef WITH_BOB
#include <string.h>
#include "Log.h"
#include "ClientContext.h"
@ -884,3 +886,4 @@ namespace client
}
}
}
#endif // WITH_BOB

View file

@ -6,6 +6,8 @@
* See full license text in LICENSE file at top of project tree
*/
#ifdef WITH_BOB
#ifndef BOB_H__
#define BOB_H__
@ -277,5 +279,5 @@ namespace client
};
}
}
#endif
#endif // WITH_BOB

View file

@ -26,8 +26,16 @@ namespace client
ClientContext context;
ClientContext::ClientContext (): m_SharedLocalDestination (nullptr),
m_HttpProxy (nullptr), m_SocksProxy (nullptr), m_SamBridge (nullptr),
m_BOBCommandChannel (nullptr), m_I2CPServer (nullptr)
m_HttpProxy (nullptr), m_SocksProxy (nullptr)
#ifdef WITH_SAM
, m_SamBridge (nullptr)
#endif
#ifdef WITH_BOB
, m_BOBCommandChannel (nullptr)
#endif
#ifdef WITH_I2CP
, m_I2CPServer (nullptr)
#endif
{
}
@ -35,9 +43,15 @@ namespace client
{
delete m_HttpProxy;
delete m_SocksProxy;
#ifdef WITH_SAM
delete m_SamBridge;
#endif
#ifdef WITH_BOB
delete m_BOBCommandChannel;
#endif
#ifdef WITH_I2CP
delete m_I2CPServer;
#endif
}
void ClientContext::Start ()
@ -58,6 +72,7 @@ namespace client
// I2P tunnels
ReadTunnels ();
#ifdef WITH_SAM
// SAM
bool sam; i2p::config::GetOption("sam.enabled", sam);
if (sam)
@ -77,7 +92,9 @@ namespace client
ThrowFatal ("Unable to start SAM bridge at ", samAddr, ":", samPort, ": ", e.what ());
}
}
#endif
#ifdef WITH_BOB
// BOB
bool bob; i2p::config::GetOption("bob.enabled", bob);
if (bob) {
@ -95,7 +112,9 @@ namespace client
ThrowFatal ("Unable to start BOB bridge at ", bobAddr, ":", bobPort, ": ", e.what ());
}
}
#endif
#ifdef WITH_I2CP
// I2CP
bool i2cp; i2p::config::GetOption("i2cp.enabled", i2cp);
if (i2cp)
@ -115,6 +134,7 @@ namespace client
ThrowFatal ("Unable to start I2CP at ", i2cpAddr, ":", i2cpPort, ": ", e.what ());
}
}
#endif
m_AddressBook.StartResolvers ();
@ -158,6 +178,7 @@ namespace client
}
m_ServerTunnels.clear ();
#ifdef WITH_SAM
if (m_SamBridge)
{
LogPrint(eLogInfo, "Clients: Stopping SAM bridge");
@ -165,7 +186,9 @@ namespace client
delete m_SamBridge;
m_SamBridge = nullptr;
}
#endif
#ifdef WITH_BOB
if (m_BOBCommandChannel)
{
LogPrint(eLogInfo, "Clients: Stopping BOB command channel");
@ -173,7 +196,9 @@ namespace client
delete m_BOBCommandChannel;
m_BOBCommandChannel = nullptr;
}
#endif
#ifdef WITH_I2CP
if (m_I2CPServer)
{
LogPrint(eLogInfo, "Clients: Stopping I2CP");
@ -181,6 +206,7 @@ namespace client
delete m_I2CPServer;
m_I2CPServer = nullptr;
}
#endif
LogPrint(eLogInfo, "Clients: Stopping AddressBook");
m_AddressBook.Stop ();

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2013-2021, The PurpleI2P Project
* Copyright (c) 2013-2022, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
@ -18,9 +18,19 @@
#include "HTTPProxy.h"
#include "SOCKS.h"
#include "I2PTunnel.h"
#ifdef WITH_SAM
#include "SAM.h"
#endif
#ifdef WITH_BOB
#include "BOB.h"
#endif
#ifdef WITH_I2CP
#include "I2CP.h"
#endif
#include "AddressBook.h"
#include "I18N_langs.h"
@ -76,31 +86,45 @@ namespace client
void ReloadConfig ();
std::shared_ptr<ClientDestination> GetSharedLocalDestination () const { return m_SharedLocalDestination; };
std::shared_ptr<ClientDestination> CreateNewLocalDestination (bool isPublic = false, // transient
std::shared_ptr<ClientDestination> CreateNewLocalDestination (
bool isPublic = false, // transient
i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519,
i2p::data::CryptoKeyType cryptoType = i2p::data::CRYPTO_KEY_TYPE_ELGAMAL,
const std::map<std::string, std::string> * params = nullptr); // used by SAM only
std::shared_ptr<ClientDestination> CreateNewLocalDestination (boost::asio::io_service& service,
bool isPublic = false, i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519,
i2p::data::CryptoKeyType cryptoType = i2p::data::CRYPTO_KEY_TYPE_ELGAMAL,
const std::map<std::string, std::string> * params = nullptr); // same as previous but on external io_service
std::shared_ptr<ClientDestination> CreateNewLocalDestination (const i2p::data::PrivateKeys& keys, bool isPublic = true,
const std::map<std::string, std::string> * params = nullptr);
std::shared_ptr<ClientDestination> CreateNewLocalDestination (boost::asio::io_service& service,
const i2p::data::PrivateKeys& keys, bool isPublic = true,
const std::map<std::string, std::string> * params = nullptr); // same as previous but on external io_service
std::shared_ptr<ClientDestination> CreateNewMatchedTunnelDestination(const i2p::data::PrivateKeys &keys,
const std::string & name, const std::map<std::string, std::string> * params = nullptr);
void DeleteLocalDestination (std::shared_ptr<ClientDestination> destination);
std::shared_ptr<ClientDestination> FindLocalDestination (const i2p::data::IdentHash& destination) const;
bool LoadPrivateKeys (i2p::data::PrivateKeys& keys, const std::string& filename,
i2p::data::SigningKeyType sigType = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519,
i2p::data::CryptoKeyType cryptoType = i2p::data::CRYPTO_KEY_TYPE_ELGAMAL);
AddressBook& GetAddressBook () { return m_AddressBook; };
#ifdef WITH_BOB
const BOBCommandChannel * GetBOBCommandChannel () const { return m_BOBCommandChannel; };
#endif
#ifdef WITH_SAM
const SAMBridge * GetSAMBridge () const { return m_SamBridge; };
#endif
#ifdef WITH_I2CP
const I2CPServer * GetI2CPServer () const { return m_I2CPServer; };
#endif
std::vector<std::shared_ptr<DatagramSessionInfo> > GetForwardInfosFor(const i2p::data::IdentHash & destination);
@ -149,9 +173,15 @@ namespace client
std::map<boost::asio::ip::udp::endpoint, std::shared_ptr<I2PUDPClientTunnel> > m_ClientForwards; // local endpoint -> udp tunnel
std::map<std::pair<i2p::data::IdentHash, int>, std::shared_ptr<I2PUDPServerTunnel> > m_ServerForwards; // <destination,port> -> udp tunnel
#ifdef WITH_SAM
SAMBridge * m_SamBridge;
#endif
#ifdef WITH_BOB
BOBCommandChannel * m_BOBCommandChannel;
#endif
#ifdef WITH_I2CP
I2CPServer * m_I2CPServer;
#endif
std::unique_ptr<boost::asio::deadline_timer> m_CleanupUDPTimer;

View file

@ -6,6 +6,8 @@
* See full license text in LICENSE file at top of project tree
*/
#ifdef WITH_I2CP
#include <string.h>
#include <stdlib.h>
#include <openssl/rand.h>
@ -208,12 +210,12 @@ namespace client
if (leases.empty ())
leases = remote->GetNonExpiredLeases (true); // with threshold
if (!leases.empty ())
{
{
remoteLease = leases[rand () % leases.size ()];
auto leaseRouter = i2p::data::netdb.FindRouter (remoteLease->tunnelGateway);
outboundTunnel = GetTunnelPool ()->GetNextOutboundTunnel (nullptr,
leaseRouter ? leaseRouter->GetCompatibleTransports (false) : (i2p::data::RouterInfo::CompatibleTransports)i2p::data::RouterInfo::eAllTransports);
}
}
if (remoteLease && outboundTunnel)
remoteSession->SetSharedRoutingPath (std::make_shared<i2p::garlic::GarlicRoutingPath> (
i2p::garlic::GarlicRoutingPath{outboundTunnel, remoteLease, 10000, 0, 0})); // 10 secs RTT
@ -534,21 +536,21 @@ namespace client
RAND_bytes ((uint8_t *)&m_SessionID, 2);
auto identity = std::make_shared<i2p::data::IdentityEx>();
size_t offset = identity->FromBuffer (buf, len);
if (!offset)
{
LogPrint (eLogError, "I2CP: Create session malformed identity");
SendSessionStatusMessage (eI2CPSessionStatusInvalid); // invalid
return;
}
if (m_Owner.FindSessionByIdentHash (identity->GetIdentHash ()))
{
LogPrint (eLogError, "I2CP: Create session duplicate address ", identity->GetIdentHash ().ToBase32 ());
SendSessionStatusMessage (eI2CPSessionStatusInvalid); // invalid
return;
}
}
uint16_t optionsSize = bufbe16toh (buf + offset);
offset += 2;
if (optionsSize > len - offset)
@ -557,7 +559,7 @@ namespace client
SendSessionStatusMessage (eI2CPSessionStatusInvalid); // invalid
return;
}
std::map<std::string, std::string> params;
ExtractMapping (buf + offset, optionsSize, params);
offset += optionsSize; // options
@ -1025,13 +1027,14 @@ namespace client
for (const auto& it: m_Sessions)
{
if (it.second)
{
{
auto dest = it.second->GetDestination ();
if (dest && dest->GetIdentHash () == ident)
return it.second;
}
}
}
return nullptr;
}
}
}
}
#endif // WITH_I2CP

View file

@ -6,6 +6,8 @@
* See full license text in LICENSE file at top of project tree
*/
#ifdef WITH_I2CP
#ifndef I2CP_H__
#define I2CP_H__
@ -249,3 +251,4 @@ namespace client
}
#endif
#endif // WITH_I2CP

View file

@ -1,11 +1,13 @@
/*
* Copyright (c) 2013-2021, The PurpleI2P Project
* Copyright (c) 2013-2022, The PurpleI2P Project
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/
#ifdef WITH_SAM
#include <string.h>
#include <stdio.h>
#ifdef _MSC_VER
@ -154,11 +156,7 @@ namespace client
if (SAMVersionAcceptable(version))
{
#ifdef _MSC_VER
size_t l = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_HANDSHAKE_REPLY, version.c_str ());
#else
size_t l = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_HANDSHAKE_REPLY, version.c_str ());
#endif
boost::asio::async_write (m_Socket, boost::asio::buffer (m_Buffer, l), boost::asio::transfer_all (),
std::bind(&SAMSocket::HandleHandshakeReplySent, shared_from_this (),
std::placeholders::_1, std::placeholders::_2));
@ -465,11 +463,7 @@ namespace client
size_t l = session->GetLocalDestination ()->GetPrivateKeys ().ToBuffer (buf, 1024);
size_t l1 = i2p::data::ByteStreamToBase64 (buf, l, priv, 1024);
priv[l1] = 0;
#ifdef _MSC_VER
size_t l2 = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_CREATE_REPLY_OK, priv);
#else
size_t l2 = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_CREATE_REPLY_OK, priv);
#endif
SendMessageReply (m_Buffer, l2, false);
}
}
@ -710,13 +704,8 @@ namespace client
}
}
auto keys = i2p::data::PrivateKeys::CreateRandomKeys (signatureType, cryptoType);
#ifdef _MSC_VER
size_t l = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_DEST_REPLY,
keys.GetPublic ()->ToBase64 ().c_str (), keys.ToBase64 ().c_str ());
#else
size_t l = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_DEST_REPLY,
keys.GetPublic ()->ToBase64 ().c_str (), keys.ToBase64 ().c_str ());
#endif
SendMessageReply (m_Buffer, l, false);
}
@ -754,11 +743,7 @@ namespace client
else
{
LogPrint (eLogError, "SAM: Naming failed, unknown address ", name);
#ifdef _MSC_VER
size_t len = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY, name.c_str());
#else
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY, name.c_str());
#endif
SendMessageReply (m_Buffer, len, false);
}
}
@ -833,11 +818,7 @@ namespace client
void SAMSocket::SendI2PError(const std::string & msg)
{
LogPrint (eLogError, "SAM: I2P error: ", msg);
#ifdef _MSC_VER
size_t len = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_STATUS_I2P_ERROR, msg.c_str());
#else
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_SESSION_STATUS_I2P_ERROR, msg.c_str());
#endif
SendMessageReply (m_Buffer, len, true);
}
@ -851,11 +832,7 @@ namespace client
else
{
LogPrint (eLogError, "SAM: Naming lookup failed. LeaseSet for ", name, " not found");
#ifdef _MSC_VER
size_t len = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY, name.c_str());
#else
size_t len = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY_INVALID_KEY, name.c_str());
#endif
SendMessageReply (m_Buffer, len, false);
}
}
@ -863,11 +840,7 @@ namespace client
void SAMSocket::SendNamingLookupReply (const std::string& name, std::shared_ptr<const i2p::data::IdentityEx> identity)
{
auto base64 = identity->ToBase64 ();
#ifdef _MSC_VER
size_t l = sprintf_s (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY, name.c_str (), base64.c_str ());
#else
size_t l = snprintf (m_Buffer, SAM_SOCKET_BUFFER_SIZE, SAM_NAMING_REPLY, name.c_str (), base64.c_str ());
#endif
SendMessageReply (m_Buffer, l, false);
}
@ -1121,11 +1094,7 @@ namespace client
}
else
{
#ifdef _MSC_VER
size_t l = sprintf_s ((char *)m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE, SAM_DATAGRAM_RECEIVED, base64.c_str (), (long unsigned int)len);
#else
size_t l = snprintf ((char *)m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE, SAM_DATAGRAM_RECEIVED, base64.c_str (), (long unsigned int)len);
#endif
if (len < SAM_SOCKET_BUFFER_SIZE - l)
{
memcpy (m_StreamBuffer + l, buf, len);
@ -1149,11 +1118,7 @@ namespace client
m_Owner.SendTo({ {buf, len} }, *ep);
else
{
#ifdef _MSC_VER
size_t l = sprintf_s ((char *)m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE, SAM_RAW_RECEIVED, (long unsigned int)len);
#else
size_t l = snprintf ((char *)m_StreamBuffer, SAM_SOCKET_BUFFER_SIZE, SAM_RAW_RECEIVED, (long unsigned int)len);
#endif
if (len < SAM_SOCKET_BUFFER_SIZE - l)
{
memcpy (m_StreamBuffer + l, buf, len);
@ -1528,3 +1493,4 @@ namespace client
}
}
}
#endif // WITH_SAM

View file

@ -6,6 +6,8 @@
* See full license text in LICENSE file at top of project tree
*/
#ifdef WITH_SAM
#ifndef SAM_H__
#define SAM_H__
@ -286,3 +288,4 @@ namespace client
}
#endif
#endif // WITH_SAM