diff --git a/AddressBook.cpp b/AddressBook.cpp index 023e75cf..75ca5761 100644 --- a/AddressBook.cpp +++ b/AddressBook.cpp @@ -7,6 +7,8 @@ #include "Log.h" #include "AddressBook.h" +#include + 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, "")) + 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; diff --git a/AddressBook.h b/AddressBook.h index 8d255a7a..90ea94fc 100644 --- a/AddressBook.h +++ b/AddressBook.h @@ -23,6 +23,7 @@ namespace data private: void LoadHosts (); + void LoadHostsFromI2P (); std::map m_Addresses; bool m_IsLoaded; diff --git a/util.cpp b/util.cpp index ef4c2e86..bc1259d1 100644 --- a/util.cpp +++ b/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(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() ) diff --git a/util.h b/util.h index 6bd367e2..d0ad85f8 100644 --- a/util.h +++ b/util.h @@ -41,6 +41,7 @@ namespace util void parse(const std::string& url_s); public: std::string protocol_, host_, path_, query_; + unsigned int port_; }; } }