Browse Source

select random port if port not found or specified

pull/1786/head
orignal 2 years ago
parent
commit
9d123fa5ad
  1. 32
      libi2pd/RouterContext.cpp
  2. 1
      libi2pd/RouterContext.h

32
libi2pd/RouterContext.cpp

@ -60,11 +60,7 @@ namespace i2p
i2p::data::LocalRouterInfo routerInfo; i2p::data::LocalRouterInfo routerInfo;
routerInfo.SetRouterIdentity (GetIdentity ()); routerInfo.SetRouterIdentity (GetIdentity ());
uint16_t port; i2p::config::GetOption("port", port); uint16_t port; i2p::config::GetOption("port", port);
if (!port) if (!port) port = SelectRandomPort ();
{
port = rand () % (30777 - 9111) + 9111; // I2P network ports range
if (port == 9150) port = 9151; // Tor browser
}
bool ipv4; i2p::config::GetOption("ipv4", ipv4); bool ipv4; i2p::config::GetOption("ipv4", ipv4);
bool ipv6; i2p::config::GetOption("ipv6", ipv6); bool ipv6; i2p::config::GetOption("ipv6", ipv6);
bool ssu; i2p::config::GetOption("ssu", ssu); bool ssu; i2p::config::GetOption("ssu", ssu);
@ -200,6 +196,13 @@ namespace i2p
m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ()); m_RouterInfo.Update (routerInfo.GetBuffer (), routerInfo.GetBufferLen ());
} }
uint16_t RouterContext::SelectRandomPort () const
{
uint16_t port = rand () % (30777 - 9111) + 9111; // I2P network ports range
if (port == 9150) port = 9151; // Tor browser
return port;
}
void RouterContext::UpdateRouterInfo () void RouterContext::UpdateRouterInfo ()
{ {
m_RouterInfo.CreateBuffer (m_Keys); m_RouterInfo.CreateBuffer (m_Keys);
@ -325,12 +328,7 @@ namespace i2p
} }
if (isAddr) if (isAddr)
{ {
if (!port && !address->port) if (!port && !address->port) port = SelectRandomPort ();
{
// select random port only if address's port is not set
port = rand () % (30777 - 9111) + 9111; // I2P network ports range
if (port == 9150) port = 9151; // Tor browser
}
if (port) address->port = port; if (port) address->port = port;
address->published = publish; address->published = publish;
memcpy (address->i, m_NTCP2Keys->iv, 16); memcpy (address->i, m_NTCP2Keys->iv, 16);
@ -761,7 +759,11 @@ namespace i2p
} }
port = addr->port; port = addr->port;
} }
if (!port) i2p::config::GetOption("port", port); if (!port)
{
i2p::config::GetOption("port", port);
if (!port) port = SelectRandomPort ();
}
// SSU // SSU
bool ssu; i2p::config::GetOption("ssu", ssu); bool ssu; i2p::config::GetOption("ssu", ssu);
if (!foundSSU && ssu) if (!foundSSU && ssu)
@ -847,7 +849,11 @@ namespace i2p
} }
if (addr->port) port = addr->port; if (addr->port) port = addr->port;
} }
if (!port) i2p::config::GetOption("port", port); if (!port)
{
i2p::config::GetOption("port", port);
if (!port) port = SelectRandomPort ();
}
// SSU // SSU
bool ssu; i2p::config::GetOption("ssu", ssu); bool ssu; i2p::config::GetOption("ssu", ssu);
if (!foundSSU && ssu) if (!foundSSU && ssu)

1
libi2pd/RouterContext.h

@ -186,6 +186,7 @@ namespace garlic
bool IsSSU2Only () const; // SSU2 and no SSU bool IsSSU2Only () const; // SSU2 and no SSU
bool Load (); bool Load ();
void SaveKeys (); void SaveKeys ();
uint16_t SelectRandomPort () const;
bool DecryptECIESTunnelBuildRecord (const uint8_t * encrypted, uint8_t * data, size_t clearTextSize); bool DecryptECIESTunnelBuildRecord (const uint8_t * encrypted, uint8_t * data, size_t clearTextSize);

Loading…
Cancel
Save