mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-02-07 03:34:15 +00:00
outbound tunnel pool
This commit is contained in:
parent
9ef8ae99e2
commit
c9ba7da0b0
@ -139,6 +139,8 @@ namespace util
|
|||||||
for (auto it: i2p::tunnel::tunnels.GetOutboundTunnels ())
|
for (auto it: i2p::tunnel::tunnels.GetOutboundTunnels ())
|
||||||
{
|
{
|
||||||
it->GetTunnelConfig ()->Print (s);
|
it->GetTunnelConfig ()->Print (s);
|
||||||
|
if (it->GetTunnelPool ())
|
||||||
|
s << " " << "Pool";
|
||||||
s << " " << (int)it->GetNumSentBytes () << "<BR>";
|
s << " " << (int)it->GetNumSentBytes () << "<BR>";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -171,7 +171,7 @@ namespace stream
|
|||||||
CreateDataMessage (this, packet, size), m_LocalDestination->GetLeaseSet ());
|
CreateDataMessage (this, packet, size), m_LocalDestination->GetLeaseSet ());
|
||||||
|
|
||||||
if (!m_OutboundTunnel)
|
if (!m_OutboundTunnel)
|
||||||
m_OutboundTunnel = i2p::tunnel::tunnels.GetNextOutboundTunnel ();
|
m_OutboundTunnel = m_LocalDestination->GetTunnelPool ()->GetNextOutboundTunnel ();
|
||||||
auto leases = m_RemoteLeaseSet.GetNonExpiredLeases ();
|
auto leases = m_RemoteLeaseSet.GetNonExpiredLeases ();
|
||||||
if (m_OutboundTunnel && !leases.empty ())
|
if (m_OutboundTunnel && !leases.empty ())
|
||||||
{
|
{
|
||||||
|
@ -108,6 +108,7 @@ namespace stream
|
|||||||
const i2p::data::Keys& GetKeys () const { return m_Keys; };
|
const i2p::data::Keys& GetKeys () const { return m_Keys; };
|
||||||
const i2p::data::Identity& GetIdentity () const { return m_Identity; };
|
const i2p::data::Identity& GetIdentity () const { return m_Identity; };
|
||||||
I2NPMessage * GetLeaseSet ();
|
I2NPMessage * GetLeaseSet ();
|
||||||
|
i2p::tunnel::TunnelPool * GetTunnelPool () const { return m_Pool; };
|
||||||
void Sign (uint8_t * buf, int len, uint8_t * signature) const;
|
void Sign (uint8_t * buf, int len, uint8_t * signature) const;
|
||||||
|
|
||||||
Stream * CreateNewStream (const i2p::data::LeaseSet& remote);
|
Stream * CreateNewStream (const i2p::data::LeaseSet& remote);
|
||||||
|
@ -390,13 +390,16 @@ namespace tunnel
|
|||||||
if (ts > (*it)->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
|
if (ts > (*it)->GetCreationTime () + TUNNEL_EXPIRATION_TIMEOUT)
|
||||||
{
|
{
|
||||||
LogPrint ("Tunnel ", (*it)->GetTunnelID (), " expired");
|
LogPrint ("Tunnel ", (*it)->GetTunnelID (), " expired");
|
||||||
|
auto pool = (*it)->GetTunnelPool ();
|
||||||
|
if (pool)
|
||||||
|
pool->TunnelExpired (*it);
|
||||||
it = m_OutboundTunnels.erase (it);
|
it = m_OutboundTunnels.erase (it);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
it++;
|
it++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_OutboundTunnels.size () < 10)
|
if (m_OutboundTunnels.size () < 15) // TODO: store exploratory tunnels explicitly
|
||||||
{
|
{
|
||||||
// trying to create one more oubound tunnel
|
// trying to create one more oubound tunnel
|
||||||
if (m_InboundTunnels.empty ()) return;
|
if (m_InboundTunnels.empty ()) return;
|
||||||
@ -520,6 +523,9 @@ namespace tunnel
|
|||||||
void Tunnels::AddOutboundTunnel (OutboundTunnel * newTunnel)
|
void Tunnels::AddOutboundTunnel (OutboundTunnel * newTunnel)
|
||||||
{
|
{
|
||||||
m_OutboundTunnels.push_back (newTunnel);
|
m_OutboundTunnels.push_back (newTunnel);
|
||||||
|
auto pool = newTunnel->GetTunnelPool ();
|
||||||
|
if (pool)
|
||||||
|
pool->TunnelCreated (newTunnel);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Tunnels::AddInboundTunnel (InboundTunnel * newTunnel)
|
void Tunnels::AddInboundTunnel (InboundTunnel * newTunnel)
|
||||||
|
@ -11,7 +11,7 @@ namespace i2p
|
|||||||
namespace tunnel
|
namespace tunnel
|
||||||
{
|
{
|
||||||
TunnelPool::TunnelPool (i2p::data::LocalDestination * localDestination, int numTunnels):
|
TunnelPool::TunnelPool (i2p::data::LocalDestination * localDestination, int numTunnels):
|
||||||
m_LocalDestination (localDestination), m_NumTunnels (numTunnels)
|
m_LocalDestination (localDestination), m_NumTunnels (numTunnels), m_LastOutboundTunnel (nullptr)
|
||||||
{
|
{
|
||||||
CryptoPP::AutoSeededRandomPool rnd;
|
CryptoPP::AutoSeededRandomPool rnd;
|
||||||
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
|
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
|
||||||
@ -22,6 +22,8 @@ namespace tunnel
|
|||||||
{
|
{
|
||||||
for (auto it: m_InboundTunnels)
|
for (auto it: m_InboundTunnels)
|
||||||
it->SetTunnelPool (nullptr);
|
it->SetTunnelPool (nullptr);
|
||||||
|
for (auto it: m_OutboundTunnels)
|
||||||
|
it->SetTunnelPool (nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TunnelPool::TunnelCreated (InboundTunnel * createdTunnel)
|
void TunnelPool::TunnelCreated (InboundTunnel * createdTunnel)
|
||||||
@ -37,6 +39,16 @@ namespace tunnel
|
|||||||
if (m_LocalDestination)
|
if (m_LocalDestination)
|
||||||
m_LocalDestination->UpdateLeaseSet ();
|
m_LocalDestination->UpdateLeaseSet ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TunnelPool::TunnelCreated (OutboundTunnel * createdTunnel)
|
||||||
|
{
|
||||||
|
m_OutboundTunnels.insert (createdTunnel);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TunnelPool::TunnelExpired (OutboundTunnel * expiredTunnel)
|
||||||
|
{
|
||||||
|
m_OutboundTunnels.erase (expiredTunnel);
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<InboundTunnel *> TunnelPool::GetInboundTunnels (int num) const
|
std::vector<InboundTunnel *> TunnelPool::GetInboundTunnels (int num) const
|
||||||
{
|
{
|
||||||
@ -51,16 +63,37 @@ namespace tunnel
|
|||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
OutboundTunnel * TunnelPool::GetNextOutboundTunnel ()
|
||||||
|
{
|
||||||
|
if (m_OutboundTunnels.empty ()) return nullptr;
|
||||||
|
auto tunnel = *m_OutboundTunnels.begin ();
|
||||||
|
if (m_LastOutboundTunnel && tunnel == m_LastOutboundTunnel)
|
||||||
|
{
|
||||||
|
for (auto it: m_OutboundTunnels)
|
||||||
|
if (it != m_LastOutboundTunnel)
|
||||||
|
{
|
||||||
|
tunnel = it;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_LastOutboundTunnel = tunnel;
|
||||||
|
return tunnel;
|
||||||
|
}
|
||||||
|
|
||||||
void TunnelPool::CreateTunnels ()
|
void TunnelPool::CreateTunnels ()
|
||||||
{
|
{
|
||||||
int num = m_InboundTunnels.size ();
|
int num = m_InboundTunnels.size ();
|
||||||
for (int i = num; i < m_NumTunnels; i++)
|
for (int i = num; i < m_NumTunnels; i++)
|
||||||
CreateInboundTunnel ();
|
CreateInboundTunnel ();
|
||||||
|
num = m_OutboundTunnels.size ();
|
||||||
|
for (int i = num; i < m_NumTunnels; i++)
|
||||||
|
CreateOutboundTunnel ();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TunnelPool::CreateInboundTunnel ()
|
void TunnelPool::CreateInboundTunnel ()
|
||||||
{
|
{
|
||||||
OutboundTunnel * outboundTunnel = tunnels.GetNextOutboundTunnel ();
|
OutboundTunnel * outboundTunnel = m_OutboundTunnels.size () > 0 ?
|
||||||
|
*m_OutboundTunnels.begin () : tunnels.GetNextOutboundTunnel ();
|
||||||
LogPrint ("Creating destination inbound tunnel...");
|
LogPrint ("Creating destination inbound tunnel...");
|
||||||
auto firstHop = i2p::data::netdb.GetRandomRouter (outboundTunnel ? outboundTunnel->GetEndpointRouter () : nullptr);
|
auto firstHop = i2p::data::netdb.GetRandomRouter (outboundTunnel ? outboundTunnel->GetEndpointRouter () : nullptr);
|
||||||
auto secondHop = i2p::data::netdb.GetRandomRouter (firstHop);
|
auto secondHop = i2p::data::netdb.GetRandomRouter (firstHop);
|
||||||
@ -75,5 +108,25 @@ namespace tunnel
|
|||||||
outboundTunnel);
|
outboundTunnel);
|
||||||
tunnel->SetTunnelPool (this);
|
tunnel->SetTunnelPool (this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TunnelPool::CreateOutboundTunnel ()
|
||||||
|
{
|
||||||
|
InboundTunnel * inboundTunnel = m_InboundTunnels.size () > 0 ?
|
||||||
|
*m_InboundTunnels.begin () : tunnels.GetNextInboundTunnel ();
|
||||||
|
if (inboundTunnel)
|
||||||
|
{
|
||||||
|
LogPrint ("Creating destination outbound tunnel...");
|
||||||
|
auto firstHop = i2p::data::netdb.GetRandomRouter (&i2p::context.GetRouterInfo ());
|
||||||
|
auto secondHop = i2p::data::netdb.GetRandomRouter (firstHop);
|
||||||
|
auto * tunnel = tunnels.CreateTunnel<OutboundTunnel> (
|
||||||
|
new TunnelConfig (std::vector<const i2p::data::RouterInfo *>
|
||||||
|
{
|
||||||
|
firstHop,
|
||||||
|
secondHop
|
||||||
|
},
|
||||||
|
inboundTunnel->GetTunnelConfig ()));
|
||||||
|
tunnel->SetTunnelPool (this);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
11
TunnelPool.h
11
TunnelPool.h
@ -29,19 +29,24 @@ namespace tunnel
|
|||||||
void CreateTunnels ();
|
void CreateTunnels ();
|
||||||
void TunnelCreated (InboundTunnel * createdTunnel);
|
void TunnelCreated (InboundTunnel * createdTunnel);
|
||||||
void TunnelExpired (InboundTunnel * expiredTunnel);
|
void TunnelExpired (InboundTunnel * expiredTunnel);
|
||||||
|
void TunnelCreated (OutboundTunnel * createdTunnel);
|
||||||
|
void TunnelExpired (OutboundTunnel * expiredTunnel);
|
||||||
std::vector<InboundTunnel *> GetInboundTunnels (int num) const;
|
std::vector<InboundTunnel *> GetInboundTunnels (int num) const;
|
||||||
|
OutboundTunnel * GetNextOutboundTunnel ();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
void CreateInboundTunnel ();
|
void CreateInboundTunnel ();
|
||||||
|
void CreateOutboundTunnel ();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
uint8_t m_EncryptionPublicKey[256], m_EncryptionPrivateKey[256];
|
uint8_t m_EncryptionPublicKey[256], m_EncryptionPrivateKey[256];
|
||||||
i2p::data::LocalDestination * m_LocalDestination;
|
i2p::data::LocalDestination * m_LocalDestination;
|
||||||
int m_NumTunnels;
|
int m_NumTunnels;
|
||||||
std::set<InboundTunnel *, TunnelCreationTimeCmp> m_InboundTunnels; // recent tunnel appears first
|
std::set<InboundTunnel *, TunnelCreationTimeCmp> m_InboundTunnels; // recent tunnel appears first
|
||||||
|
std::set<OutboundTunnel *, TunnelCreationTimeCmp> m_OutboundTunnels;
|
||||||
|
OutboundTunnel * m_LastOutboundTunnel;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user