1
0
mirror of https://github.com/PurpleI2P/i2pd.git synced 2025-01-22 04:04:16 +00:00

try to find destination at every connect attempt

This commit is contained in:
orignal 2014-08-14 14:32:00 -04:00
parent d8918edb51
commit fcb45a778b
3 changed files with 94 additions and 73 deletions

View File

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

View File

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

View File

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