From a03bf891905c146a30f886eb63b579120bfa3c39 Mon Sep 17 00:00:00 2001 From: Darknet Villain Date: Thu, 2 Feb 2017 15:25:25 -0500 Subject: [PATCH] Refactored code to Reseed module --- NetDb.cpp | 35 +---------------------------- Reseed.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++++++++----- Reseed.h | 1 + 3 files changed, 63 insertions(+), 39 deletions(-) diff --git a/NetDb.cpp b/NetDb.cpp index 1c5758df..e246cb7b 100644 --- a/NetDb.cpp +++ b/NetDb.cpp @@ -328,40 +328,7 @@ namespace data } } - - std::string su3FileName; i2p::config::GetOption("reseed.file", su3FileName); - std::string zipFileName; i2p::config::GetOption("reseed.zipfile", zipFileName); - - if (su3FileName.length() > 0) // bootstrap from SU3 file or URL - { - int num; - if (su3FileName.length() > 8 && su3FileName.substr(0, 8) == "https://") - { - num = m_Reseeder->ReseedFromSU3Url (su3FileName); // from https URL - } - else - { - num = m_Reseeder->ProcessSU3File (su3FileName.c_str ()); - } - if (num == 0) - LogPrint (eLogWarning, "NetDb: failed to reseed from ", su3FileName); - return; - } - else if (zipFileName.length() > 0) // bootstrap from ZIP file - { - int num = m_Reseeder->ProcessZIPFile (zipFileName.c_str ()); - if (num == 0) - LogPrint (eLogWarning, "NetDb: failed to reseed from ", zipFileName); - return; - } - else // bootstrap from reseed servers - { - int reseedRetries = 0; - while (reseedRetries < 10 && !m_Reseeder->ReseedFromServers ()) - reseedRetries++; - if (reseedRetries >= 10) - LogPrint (eLogWarning, "NetDb: failed to reseed after 10 attempts"); - } + m_Reseeder->Bootstrap (); } void NetDb::ReseedFromFloodfill(const RouterInfo & ri, int numRouters, int numFloodfills) diff --git a/Reseed.cpp b/Reseed.cpp index 10f3cf5a..24df13f7 100644 --- a/Reseed.cpp +++ b/Reseed.cpp @@ -32,17 +32,73 @@ namespace data { } + /** @brief tries to bootstrap into I2P network (from local files and servers, with respect of options) + */ + void Reseeder::Bootstrap () + { + std::string su3FileName; i2p::config::GetOption("reseed.file", su3FileName); + std::string zipFileName; i2p::config::GetOption("reseed.zipfile", zipFileName); + + if (su3FileName.length() > 0) // bootstrap from SU3 file or URL + { + int num; + if (su3FileName.length() > 8 && su3FileName.substr(0, 8) == "https://") + { + num = ReseedFromSU3Url (su3FileName); // from https URL + } + else + { + num = ProcessSU3File (su3FileName.c_str ()); + } + if (num == 0) + LogPrint (eLogWarning, "Reseed: failed to reseed from ", su3FileName); + } + else if (zipFileName.length() > 0) // bootstrap from ZIP file + { + int num = ProcessZIPFile (zipFileName.c_str ()); + if (num == 0) + LogPrint (eLogWarning, "Reseed: failed to reseed from ", zipFileName); + } + else // bootstrap from reseed servers + { + int num = ReseedFromServers (); + if (num == 0) + LogPrint (eLogWarning, "Reseed: failed to reseed from servers"); + } + } + + /** @brief bootstrap from random server, retry 10 times + * @return number of entries added to netDb + */ int Reseeder::ReseedFromServers () { std::string reseedURLs; i2p::config::GetOption("reseed.urls", reseedURLs); - std::vector httpsReseedHostList; - boost::split(httpsReseedHostList, reseedURLs, boost::is_any_of(","), boost::token_compress_on); + std::vector httpsReseedHostList; + boost::split(httpsReseedHostList, reseedURLs, boost::is_any_of(","), boost::token_compress_on); + + if (reseedURLs.length () == 0) + { + LogPrint (eLogWarning, "Reseed: No reseed servers specified"); + return 0; + } - auto ind = rand () % httpsReseedHostList.size (); - std::string reseedUrl = httpsReseedHostList[ind] + "i2pseeds.su3"; - return ReseedFromSU3Url (reseedUrl); + int reseedRetries = 0; + while (reseedRetries < 10) + { + auto ind = rand () % httpsReseedHostList.size (); + std::string reseedUrl = httpsReseedHostList[ind] + "i2pseeds.su3"; + auto num = ReseedFromSU3Url (reseedUrl); + if (num > 0) return num; // success + reseedRetries++; + } + LogPrint (eLogWarning, "Reseed: failed to reseed from servers after 10 attempts"); + return 0; } + /** @brief bootstrap from HTTPS URL with SU3 file + * @param url + * @return number of entries added to netDb + */ int Reseeder::ReseedFromSU3Url (const std::string& url) { LogPrint (eLogInfo, "Reseed: Downloading SU3 from ", url); diff --git a/Reseed.h b/Reseed.h index 72ff4c55..6b09f911 100644 --- a/Reseed.h +++ b/Reseed.h @@ -21,6 +21,7 @@ namespace data Reseeder(); ~Reseeder(); + void Bootstrap (); int ReseedFromServers (); int ReseedFromSU3Url (const std::string& url); int ProcessSU3File (const char * filename);