Browse Source

try to find destination at every connect attempt

pull/93/head
orignal 10 years ago
parent
commit
fcb45a778b
  1. 29
      AddressBook.cpp
  2. 2
      AddressBook.h
  3. 22
      I2PTunnel.cpp

29
AddressBook.cpp

@ -14,7 +14,7 @@ namespace i2p @@ -14,7 +14,7 @@ namespace i2p
namespace data
{
AddressBook::AddressBook (): m_IsLoaded (false)
AddressBook::AddressBook (): m_IsLoaded (false), m_IsDowloading (false)
{
}
@ -23,26 +23,21 @@ const IdentHash * AddressBook::FindAddress (const std::string& address) @@ -23,26 +23,21 @@ const IdentHash * AddressBook::FindAddress (const std::string& address)
{
if (!m_IsLoaded)
LoadHosts ();
if (m_IsLoaded)
{
auto it = m_Addresses.find (address);
if (it != m_Addresses.end ())
return &it->second;
else
}
return nullptr;
}
void AddressBook::LoadHostsFromI2P ()
{
std::string content;
while (true)
{
// TODO: hosts link in config
int http_code = i2p::util::http::httpRequestViaI2pProxy("http://udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.i2p/hosts.txt", content);
if (http_code == 200)
if (!boost::starts_with(content, "<html>") && !content.empty()) // TODO: test and remove
break;
std::this_thread::sleep_for(std::chrono::seconds(5));
}
{
std::ofstream f_save(i2p::util::filesystem::GetFullPath("hosts.txt").c_str(), std::ofstream::out);
if (f_save.is_open())
{
@ -52,18 +47,26 @@ void AddressBook::LoadHostsFromI2P () @@ -52,18 +47,26 @@ void AddressBook::LoadHostsFromI2P ()
else
LogPrint("Can't write hosts.txt");
m_IsLoaded = false;
}
else
LogPrint ("Failed to download hosts.txt");
m_IsDowloading = false;
return;
}
void AddressBook::LoadHosts ()
{
m_IsLoaded = true;
std::ifstream f (i2p::util::filesystem::GetFullPath ("hosts.txt").c_str (), std::ofstream::in); // in text mode
if (!f.is_open ())
{
LogPrint ("hosts.txt not found. Try to load...");
if (!m_IsDowloading)
{
m_IsDowloading = true;
std::thread load_hosts(&AddressBook::LoadHostsFromI2P, this);
load_hosts.detach();
}
return;
}
int numAddresses = 0;
@ -85,7 +88,8 @@ void AddressBook::LoadHosts () @@ -85,7 +88,8 @@ void AddressBook::LoadHosts ()
std::string addr = s.substr(pos);
Identity ident;
if (!ident.FromBase64(addr)) {
if (!ident.FromBase64(addr))
{
LogPrint ("hosts.txt: ignore ", name);
continue;
}
@ -94,6 +98,7 @@ void AddressBook::LoadHosts () @@ -94,6 +98,7 @@ void AddressBook::LoadHosts ()
}
}
LogPrint (numAddresses, " addresses loaded");
m_IsLoaded = true;
}
}

2
AddressBook.h

@ -26,7 +26,7 @@ namespace data @@ -26,7 +26,7 @@ namespace data
void LoadHostsFromI2P ();
std::map<std::string, IdentHash> m_Addresses;
bool m_IsLoaded;
bool m_IsLoaded, m_IsDowloading;
};
}
}

22
I2PTunnel.cpp

@ -131,7 +131,6 @@ namespace stream @@ -131,7 +131,6 @@ namespace stream
for (auto it: m_Connections)
delete it;
m_Connections.clear ();
delete m_DestinationIdentHash;
m_DestinationIdentHash = nullptr;
}
@ -146,16 +145,33 @@ namespace stream @@ -146,16 +145,33 @@ namespace stream
{
if (!ecode)
{
if (!m_RemoteLeaseSet && m_DestinationIdentHash)
if (!m_RemoteLeaseSet)
{
// try to get it
if (m_DestinationIdentHash)
m_RemoteLeaseSet = i2p::data::netdb.FindLeaseSet (*m_DestinationIdentHash);
if (m_RemoteLeaseSet)
else
{
auto identHash = i2p::data::netdb.FindAddress (m_Destination);
if (identHash)
{
m_DestinationIdentHash = new i2p::data::IdentHash (*identHash);
i2p::data::netdb.Subscribe (*m_DestinationIdentHash);
}
}
}
if (m_RemoteLeaseSet) // leaseSet found
{
LogPrint ("New I2PTunnel connection");
auto connection = new I2PTunnelConnection (socket, m_RemoteLeaseSet);
m_Connections.insert (connection);
}
else
{
LogPrint ("LeaseSet for I2PTunnel destination not found");
delete socket;
}
Accept ();
}
else

Loading…
Cancel
Save