Browse Source

download hosts.txt without proxy

pull/118/head
orignal 10 years ago
parent
commit
db2d0df2c4
  1. 79
      AddressBook.cpp
  2. 10
      AddressBook.h

79
AddressBook.cpp

@ -142,7 +142,7 @@ namespace client @@ -142,7 +142,7 @@ namespace client
f << it.first << "," << it.second.ToBase32 () << std::endl;
num++;
}
LogPrint (eLogInfo, num, " addresses save");
LogPrint (eLogInfo, num, " addresses saved");
}
else
LogPrint (eLogError, "Can't open file ", filename);
@ -150,17 +150,33 @@ namespace client @@ -150,17 +150,33 @@ namespace client
}
//---------------------------------------------------------------------
AddressBook::AddressBook (): m_IsLoaded (false), m_IsDowloading (false)
AddressBook::AddressBook (): m_IsLoaded (false), m_IsDownloading (false),
m_DefaultSubscription (nullptr)
{
}
AddressBook::~AddressBook ()
{
if (m_IsDownloading)
{
LogPrint (eLogInfo, "Subscription is downloading. Waiting for temination...");
for (int i = 0; i < 30; i++)
{
if (!m_IsDownloading)
{
LogPrint (eLogInfo, "Subscription download complete");
break;
}
std::this_thread::sleep_for (std::chrono::seconds (1)); // wait for 1 seconds
}
LogPrint (eLogError, "Subscription download hangs");
}
if (m_Storage)
{
m_Storage->Save (m_Addresses);
delete m_Storage;
}
delete m_DefaultSubscription;
}
AddressBookStorage * AddressBook::CreateStorage ()
@ -239,29 +255,6 @@ namespace client @@ -239,29 +255,6 @@ namespace client
return m_Storage->GetAddress (ident, identity);
}
void AddressBook::LoadHostsFromI2P ()
{
std::string content;
int http_code = i2p::util::http::httpRequestViaI2pProxy("http://udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.i2p/hosts.txt", content);
if (http_code == 200)
{
std::ofstream f_save(i2p::util::filesystem::GetFullPath("hosts.txt").c_str(), std::ofstream::out);
if (f_save.is_open())
{
f_save << content;
f_save.close();
}
else
LogPrint (eLogError, "Can't write hosts.txt");
m_IsLoaded = false;
}
else
LogPrint (eLogError, "Failed to download hosts.txt");
m_IsDowloading = false;
return;
}
void AddressBook::LoadHosts ()
{
if (!m_Storage)
@ -272,27 +265,31 @@ namespace client @@ -272,27 +265,31 @@ namespace client
return;
}
// otherwise try hosts.txt
// try hosts.txt first
std::ifstream f (i2p::util::filesystem::GetFullPath ("hosts.txt").c_str (), std::ofstream::in); // in text mode
if (!f.is_open ())
if (f.is_open ())
{
LoadHostsFromStream (f);
m_IsLoaded = true;
}
else
{
LogPrint (eLogInfo, "hosts.txt not found. Try to load...");
if (!m_IsDowloading)
// if not found download it from http://i2p-projekt.i2p/hosts.txt
LogPrint (eLogInfo, "hosts.txt not found. Try to download it from default subscription...");
if (!m_IsDownloading)
{
m_IsDowloading = true;
std::thread load_hosts(&AddressBook::LoadHostsFromI2P, this);
load_hosts.detach();
m_IsDownloading = true;
if (!m_DefaultSubscription)
m_DefaultSubscription = new AddressBookSubscription (*this, DEFAULT_SUBSCRIPTION_ADDRESS);
m_DefaultSubscription->CheckSubscription ();
}
return;
}
LoadHostsFromStream (f);
m_Storage->Save (m_Addresses);
m_IsLoaded = true;
}
void AddressBook::LoadHostsFromStream (std::istream& f)
{
std::unique_lock<std::mutex> l(m_AddressBookMutex);
int numAddresses = 0;
std::string s;
while (!f.eof ())
@ -316,7 +313,12 @@ namespace client @@ -316,7 +313,12 @@ namespace client
numAddresses++;
}
}
LogPrint (eLogInfo, numAddresses, " addresses loaded");
LogPrint (eLogInfo, numAddresses, " addresses processed");
if (numAddresses > 0)
{
m_IsLoaded = true;
m_Storage->Save (m_Addresses);
}
}
AddressBookSubscription::AddressBookSubscription (AddressBook& book, const std::string& link):
@ -327,7 +329,7 @@ namespace client @@ -327,7 +329,7 @@ namespace client
void AddressBookSubscription::CheckSubscription ()
{
std::thread load_hosts(&AddressBookSubscription::Request, this);
load_hosts.detach();
load_hosts.detach(); // TODO: use join
}
void AddressBookSubscription::Request ()
@ -397,6 +399,7 @@ namespace client @@ -397,6 +399,7 @@ namespace client
}
else
LogPrint (eLogError, "Can't resolve ", u.host_);
m_Book.SetIsDownloading (false);
}
}
}

10
AddressBook.h

@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
#include <string>
#include <map>
#include <iostream>
#include <mutex>
#include "base64.h"
#include "util.h"
#include "Identity.h"
@ -14,6 +15,8 @@ namespace i2p @@ -14,6 +15,8 @@ namespace i2p
{
namespace client
{
const char DEFAULT_SUBSCRIPTION_ADDRESS[] = "http://udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.i2p/hosts.txt";
class AddressBookStorage // interface for storage
{
public:
@ -41,19 +44,20 @@ namespace client @@ -41,19 +44,20 @@ namespace client
void InsertAddress (const i2p::data::IdentityEx& address);
void LoadHostsFromStream (std::istream& f);
void SetIsDownloading (bool isDownloading) { m_IsDownloading = isDownloading; };
private:
AddressBookStorage * CreateStorage ();
void LoadHosts ();
void LoadHostsFromI2P ();
private:
std::mutex m_AddressBookMutex;
std::map<std::string, i2p::data::IdentHash> m_Addresses;
AddressBookStorage * m_Storage;
bool m_IsLoaded, m_IsDowloading;
volatile bool m_IsLoaded, m_IsDownloading;
AddressBookSubscription * m_DefaultSubscription; // in case if we don't know any addresses yet
};
class AddressBookSubscription

Loading…
Cancel
Save