Browse Source

Merge pull request #55 from chertov/master

Initial download of hosts.txt for AddressBook
pull/59/head
orignal 11 years ago
parent
commit
afe2dceb52
  1. 37
      AddressBook.cpp
  2. 1
      AddressBook.h
  3. 18
      util.cpp
  4. 1
      util.h

37
AddressBook.cpp

@ -7,6 +7,8 @@ @@ -7,6 +7,8 @@
#include "Log.h"
#include "AddressBook.h"
#include <boost/algorithm/string.hpp>
namespace i2p
{
namespace data
@ -27,7 +29,36 @@ const IdentHash * AddressBook::FindAddress (const std::string& address) @@ -27,7 +29,36 @@ const IdentHash * AddressBook::FindAddress (const std::string& address)
else
return nullptr;
}
void AddressBook::LoadHostsFromI2P ()
{
std::string content;
std::stringstream url_ss;
// TODO: hosts link in config
// TODO: url download via HTTPProxy
url_ss << "http://127.0.0.1:" << i2p::util::config::GetArg("-httpport", 7070) << "/udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna/hosts.txt";
while (true)
{
content = i2p::util::http::httpRequest(url_ss.str());
// TODO: check http errors
if (! boost::starts_with(content, "<html>"))
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())
{
f_save << content;
f_save.close();
}
else
LogPrint("Can't write hosts.txt");
m_IsLoaded = false;
return;
}
void AddressBook::LoadHosts ()
{
@ -35,7 +66,9 @@ void AddressBook::LoadHosts () @@ -35,7 +66,9 @@ void AddressBook::LoadHosts ()
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");
LogPrint ("hosts.txt not found. Try to load...");
std::thread load_hosts(&AddressBook::LoadHostsFromI2P, this);
load_hosts.detach();
return;
}
int numAddresses = 0;

1
AddressBook.h

@ -23,6 +23,7 @@ namespace data @@ -23,6 +23,7 @@ namespace data
private:
void LoadHosts ();
void LoadHostsFromI2P ();
std::map<std::string, IdentHash> m_Addresses;
bool m_IsLoaded;

18
util.cpp

@ -204,7 +204,13 @@ namespace http @@ -204,7 +204,13 @@ namespace http
// please don't uncomment following line because it's not compatible with boost 1.46
// 1.46 is default boost for Ubuntu 12.04 LTS
//site.expires_from_now (boost::posix_time::seconds(30));
site.connect(u.host_, "http");
if (u.port_ == 80)
site.connect(u.host_, "http");
else
{
std::stringstream ss; ss << u.port_;
site.connect(u.host_, ss.str());
}
if (site)
{
// User-Agent is needed to get the server list routerInfo files.
@ -265,6 +271,16 @@ namespace http @@ -265,6 +271,16 @@ namespace http
transform(prot_i, path_i,
back_inserter(host_),
std::ptr_fun<int,int>(tolower)); // host is icase
std::string::const_iterator port_i = find(host_.begin(), host_.end(), ':');
if (port_i != host_.end())
{
port_ = std::stoi(std::string(port_i + 1, host_.end()));
host_.assign(host_.begin(), port_i);
}
else
port_ = 80;
std::string::const_iterator query_i = find(path_i, url_s.end(), '?');
path_.assign(path_i, query_i);
if( query_i != url_s.end() )

1
util.h

@ -41,6 +41,7 @@ namespace util @@ -41,6 +41,7 @@ namespace util
void parse(const std::string& url_s);
public:
std::string protocol_, host_, path_, query_;
unsigned int port_;
};
}
}

Loading…
Cancel
Save