mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-13 11:17:53 +00:00
Initial download of hosts.txt for AddressBook
This commit is contained in:
parent
795176bd8d
commit
006ca11af4
@ -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)
|
||||
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 ()
|
||||
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;
|
||||
|
@ -23,6 +23,7 @@ namespace data
|
||||
private:
|
||||
|
||||
void LoadHosts ();
|
||||
void LoadHostsFromI2P ();
|
||||
|
||||
std::map<std::string, IdentHash> m_Addresses;
|
||||
bool m_IsLoaded;
|
||||
|
18
util.cpp
18
util.cpp
@ -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
|
||||
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() )
|
||||
|
Loading…
Reference in New Issue
Block a user