mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 21:37:17 +01:00
handle b33 addresses in I2P tunnels
This commit is contained in:
parent
00b5fdce03
commit
6f4f0f03d2
|
@ -359,6 +359,24 @@ namespace client
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<const Address> AddressBook::GetAddress (const std::string& address)
|
||||||
|
{
|
||||||
|
auto pos = address.find(".b32.i2p");
|
||||||
|
if (pos != std::string::npos)
|
||||||
|
return std::make_shared<const Address>(address.substr (0, pos));
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pos = address.find (".i2p");
|
||||||
|
if (pos != std::string::npos)
|
||||||
|
return FindAddress (address);
|
||||||
|
}
|
||||||
|
// if not .b32 we assume full base64 address
|
||||||
|
i2p::data::IdentityEx dest;
|
||||||
|
if (!dest.FromBase64 (address))
|
||||||
|
return nullptr;
|
||||||
|
return std::make_shared<const Address>(dest.GetIdentHash ());
|
||||||
|
}
|
||||||
|
|
||||||
std::shared_ptr<const Address> AddressBook::FindAddress (const std::string& address)
|
std::shared_ptr<const Address> AddressBook::FindAddress (const std::string& address)
|
||||||
{
|
{
|
||||||
auto it = m_Addresses.find (address);
|
auto it = m_Addresses.find (address);
|
||||||
|
|
|
@ -75,6 +75,7 @@ namespace client
|
||||||
void StartResolvers ();
|
void StartResolvers ();
|
||||||
void Stop ();
|
void Stop ();
|
||||||
bool GetIdentHash (const std::string& address, i2p::data::IdentHash& ident);
|
bool GetIdentHash (const std::string& address, i2p::data::IdentHash& ident);
|
||||||
|
std::shared_ptr<const Address> GetAddress (const std::string& address);
|
||||||
std::shared_ptr<const i2p::data::IdentityEx> GetFullAddress (const std::string& address);
|
std::shared_ptr<const i2p::data::IdentityEx> GetFullAddress (const std::string& address);
|
||||||
std::shared_ptr<const Address> FindAddress (const std::string& address);
|
std::shared_ptr<const Address> FindAddress (const std::string& address);
|
||||||
void LookupAddress (const std::string& address);
|
void LookupAddress (const std::string& address);
|
||||||
|
|
|
@ -101,9 +101,9 @@ namespace client
|
||||||
|
|
||||||
void I2PService::CreateStream (StreamRequestComplete streamRequestComplete, const std::string& dest, int port) {
|
void I2PService::CreateStream (StreamRequestComplete streamRequestComplete, const std::string& dest, int port) {
|
||||||
assert(streamRequestComplete);
|
assert(streamRequestComplete);
|
||||||
i2p::data::IdentHash identHash;
|
auto address = i2p::client::context.GetAddressBook ().GetAddress (dest);
|
||||||
if (i2p::client::context.GetAddressBook ().GetIdentHash (dest, identHash))
|
if (address)
|
||||||
CreateStream(streamRequestComplete, identHash, port);
|
CreateStream(streamRequestComplete, address, port);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LogPrint (eLogWarning, "I2PService: Remote destination not found: ", dest);
|
LogPrint (eLogWarning, "I2PService: Remote destination not found: ", dest);
|
||||||
|
@ -111,27 +111,31 @@ namespace client
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void I2PService::CreateStream(StreamRequestComplete streamRequestComplete, const i2p::data::IdentHash & identHash, int port)
|
void I2PService::CreateStream(StreamRequestComplete streamRequestComplete, std::shared_ptr<const Address> address, int port)
|
||||||
{
|
{
|
||||||
if(m_ConnectTimeout)
|
if(m_ConnectTimeout && !m_LocalDestination->IsReady())
|
||||||
{
|
{
|
||||||
if(m_LocalDestination->IsReady())
|
AddReadyCallback([this, streamRequestComplete, address, port] (const boost::system::error_code & ec) {
|
||||||
m_LocalDestination->CreateStream (streamRequestComplete, identHash, port);
|
if(ec)
|
||||||
else
|
{
|
||||||
{
|
LogPrint(eLogWarning, "I2PService::CeateStream() ", ec.message());
|
||||||
AddReadyCallback([this, streamRequestComplete, identHash, port] (const boost::system::error_code & ec) {
|
streamRequestComplete(nullptr);
|
||||||
if(ec)
|
}
|
||||||
{
|
else
|
||||||
LogPrint(eLogWarning, "I2PService::CeateStream() ", ec.message());
|
{ if (address->IsIdentHash ())
|
||||||
streamRequestComplete(nullptr);
|
this->m_LocalDestination->CreateStream(streamRequestComplete, address->identHash, port);
|
||||||
}
|
|
||||||
else
|
else
|
||||||
this->m_LocalDestination->CreateStream(streamRequestComplete, identHash, port);
|
this->m_LocalDestination->CreateStream (streamRequestComplete, address->blindedPublicKey, port);
|
||||||
});
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
m_LocalDestination->CreateStream(streamRequestComplete, identHash, port);
|
{
|
||||||
|
if (address->IsIdentHash ())
|
||||||
|
m_LocalDestination->CreateStream (streamRequestComplete, address->identHash, port);
|
||||||
|
else
|
||||||
|
m_LocalDestination->CreateStream (streamRequestComplete, address->blindedPublicKey, port);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TCPIPPipe::TCPIPPipe(I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> upstream, std::shared_ptr<boost::asio::ip::tcp::socket> downstream) : I2PServiceHandler(owner), m_up(upstream), m_down(downstream)
|
TCPIPPipe::TCPIPPipe(I2PService * owner, std::shared_ptr<boost::asio::ip::tcp::socket> upstream, std::shared_ptr<boost::asio::ip::tcp::socket> downstream) : I2PServiceHandler(owner), m_up(upstream), m_down(downstream)
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
#include "Destination.h"
|
#include "Destination.h"
|
||||||
#include "Identity.h"
|
#include "Identity.h"
|
||||||
|
#include "AddressBook.h"
|
||||||
|
|
||||||
namespace i2p
|
namespace i2p
|
||||||
{
|
{
|
||||||
|
@ -49,7 +50,7 @@ namespace client
|
||||||
m_LocalDestination = dest;
|
m_LocalDestination = dest;
|
||||||
}
|
}
|
||||||
void CreateStream (StreamRequestComplete streamRequestComplete, const std::string& dest, int port = 0);
|
void CreateStream (StreamRequestComplete streamRequestComplete, const std::string& dest, int port = 0);
|
||||||
void CreateStream(StreamRequestComplete complete, const i2p::data::IdentHash & ident, int port);
|
void CreateStream(StreamRequestComplete complete, std::shared_ptr<const Address> address, int port);
|
||||||
inline boost::asio::io_service& GetService () { return m_LocalDestination->GetService (); }
|
inline boost::asio::io_service& GetService () { return m_LocalDestination->GetService (); }
|
||||||
|
|
||||||
virtual void Start () = 0;
|
virtual void Start () = 0;
|
||||||
|
|
|
@ -393,15 +393,15 @@ namespace client
|
||||||
class I2PClientTunnelHandler: public I2PServiceHandler, public std::enable_shared_from_this<I2PClientTunnelHandler>
|
class I2PClientTunnelHandler: public I2PServiceHandler, public std::enable_shared_from_this<I2PClientTunnelHandler>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
I2PClientTunnelHandler (I2PClientTunnel * parent, i2p::data::IdentHash destination,
|
I2PClientTunnelHandler (I2PClientTunnel * parent, std::shared_ptr<const Address> address,
|
||||||
int destinationPort, std::shared_ptr<boost::asio::ip::tcp::socket> socket):
|
int destinationPort, std::shared_ptr<boost::asio::ip::tcp::socket> socket):
|
||||||
I2PServiceHandler(parent), m_DestinationIdentHash(destination),
|
I2PServiceHandler(parent), m_Address(address),
|
||||||
m_DestinationPort (destinationPort), m_Socket(socket) {};
|
m_DestinationPort (destinationPort), m_Socket(socket) {};
|
||||||
void Handle();
|
void Handle();
|
||||||
void Terminate();
|
void Terminate();
|
||||||
private:
|
private:
|
||||||
void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream);
|
void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream);
|
||||||
i2p::data::IdentHash m_DestinationIdentHash;
|
std::shared_ptr<const Address> m_Address;
|
||||||
int m_DestinationPort;
|
int m_DestinationPort;
|
||||||
std::shared_ptr<boost::asio::ip::tcp::socket> m_Socket;
|
std::shared_ptr<boost::asio::ip::tcp::socket> m_Socket;
|
||||||
};
|
};
|
||||||
|
@ -410,7 +410,7 @@ namespace client
|
||||||
{
|
{
|
||||||
GetOwner()->CreateStream (
|
GetOwner()->CreateStream (
|
||||||
std::bind (&I2PClientTunnelHandler::HandleStreamRequestComplete, shared_from_this(), std::placeholders::_1),
|
std::bind (&I2PClientTunnelHandler::HandleStreamRequestComplete, shared_from_this(), std::placeholders::_1),
|
||||||
m_DestinationIdentHash, m_DestinationPort);
|
m_Address, m_DestinationPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
void I2PClientTunnelHandler::HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream)
|
void I2PClientTunnelHandler::HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream)
|
||||||
|
@ -445,43 +445,39 @@ namespace client
|
||||||
I2PClientTunnel::I2PClientTunnel (const std::string& name, const std::string& destination,
|
I2PClientTunnel::I2PClientTunnel (const std::string& name, const std::string& destination,
|
||||||
const std::string& address, int port, std::shared_ptr<ClientDestination> localDestination, int destinationPort):
|
const std::string& address, int port, std::shared_ptr<ClientDestination> localDestination, int destinationPort):
|
||||||
TCPIPAcceptor (address, port, localDestination), m_Name (name), m_Destination (destination),
|
TCPIPAcceptor (address, port, localDestination), m_Name (name), m_Destination (destination),
|
||||||
m_DestinationIdentHash (nullptr), m_DestinationPort (destinationPort)
|
m_DestinationPort (destinationPort)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void I2PClientTunnel::Start ()
|
void I2PClientTunnel::Start ()
|
||||||
{
|
{
|
||||||
TCPIPAcceptor::Start ();
|
TCPIPAcceptor::Start ();
|
||||||
GetIdentHash();
|
GetAddress ();
|
||||||
}
|
}
|
||||||
|
|
||||||
void I2PClientTunnel::Stop ()
|
void I2PClientTunnel::Stop ()
|
||||||
{
|
{
|
||||||
TCPIPAcceptor::Stop();
|
TCPIPAcceptor::Stop();
|
||||||
auto *originalIdentHash = m_DestinationIdentHash;
|
m_Address = nullptr;
|
||||||
m_DestinationIdentHash = nullptr;
|
|
||||||
delete originalIdentHash;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* HACK: maybe we should create a caching IdentHash provider in AddressBook */
|
/* HACK: maybe we should create a caching IdentHash provider in AddressBook */
|
||||||
const i2p::data::IdentHash * I2PClientTunnel::GetIdentHash ()
|
std::shared_ptr<const Address> I2PClientTunnel::GetAddress ()
|
||||||
{
|
{
|
||||||
if (!m_DestinationIdentHash)
|
if (!m_Address)
|
||||||
{
|
{
|
||||||
i2p::data::IdentHash identHash;
|
m_Address = i2p::client::context.GetAddressBook ().GetAddress (m_Destination);
|
||||||
if (i2p::client::context.GetAddressBook ().GetIdentHash (m_Destination, identHash))
|
if (!m_Address)
|
||||||
m_DestinationIdentHash = new i2p::data::IdentHash (identHash);
|
|
||||||
else
|
|
||||||
LogPrint (eLogWarning, "I2PTunnel: Remote destination ", m_Destination, " not found");
|
LogPrint (eLogWarning, "I2PTunnel: Remote destination ", m_Destination, " not found");
|
||||||
}
|
}
|
||||||
return m_DestinationIdentHash;
|
return m_Address;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<I2PServiceHandler> I2PClientTunnel::CreateHandler(std::shared_ptr<boost::asio::ip::tcp::socket> socket)
|
std::shared_ptr<I2PServiceHandler> I2PClientTunnel::CreateHandler(std::shared_ptr<boost::asio::ip::tcp::socket> socket)
|
||||||
{
|
{
|
||||||
const i2p::data::IdentHash *identHash = GetIdentHash();
|
auto address = GetAddress ();
|
||||||
if (identHash)
|
if (address)
|
||||||
return std::make_shared<I2PClientTunnelHandler>(this, *identHash, m_DestinationPort, socket);
|
return std::make_shared<I2PClientTunnelHandler>(this, address, m_DestinationPort, socket);
|
||||||
else
|
else
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue