mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 21:37:17 +01:00
dump addressbook in hosts.txt format
This commit is contained in:
parent
e0cec79ad6
commit
6fc5f88a3b
|
@ -212,7 +212,8 @@ namespace config {
|
|||
("addressbook.defaulturl", value<std::string>()->default_value(
|
||||
"http://joajgazyztfssty4w2on5oaqksz6tqoxbduy553y34mf4byv6gpq.b32.i2p/export/alive-hosts.txt"
|
||||
), "AddressBook subscription URL for initial setup")
|
||||
("addressbook.subscriptions", value<std::string>()->default_value(""), "AddressBook subscriptions URLs, separated by comma");
|
||||
("addressbook.subscriptions", value<std::string>()->default_value(""), "AddressBook subscriptions URLs, separated by comma")
|
||||
("addressbook.hostsfile", value<std::string>()->default_value(""), "File to dump addresses in hosts.txt format");
|
||||
|
||||
options_description trust("Trust options");
|
||||
trust.add_options()
|
||||
|
|
|
@ -34,14 +34,13 @@ namespace client
|
|||
// TODO: this is actually proxy class
|
||||
class AddressBookFilesystemStorage: public AddressBookStorage
|
||||
{
|
||||
private:
|
||||
i2p::fs::HashedStorage storage;
|
||||
std::string etagsPath, indexPath, localPath;
|
||||
|
||||
public:
|
||||
|
||||
AddressBookFilesystemStorage (): storage("addressbook", "b", "", "b32")
|
||||
{
|
||||
i2p::config::GetOption("persist.addressbook", m_IsPersist);
|
||||
if (m_IsPersist)
|
||||
i2p::config::GetOption("addressbook.hostsfile", m_HostsFile);
|
||||
}
|
||||
std::shared_ptr<const i2p::data::IdentityEx> GetAddress (const i2p::data::IdentHash& ident) const;
|
||||
void AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address);
|
||||
|
@ -62,7 +61,10 @@ namespace client
|
|||
|
||||
private:
|
||||
|
||||
i2p::fs::HashedStorage storage;
|
||||
std::string etagsPath, indexPath, localPath;
|
||||
bool m_IsPersist;
|
||||
std::string m_HostsFile; // file to dump hosts.txt, empty if not used
|
||||
};
|
||||
|
||||
bool AddressBookFilesystemStorage::Init()
|
||||
|
@ -183,19 +185,18 @@ namespace client
|
|||
|
||||
int AddressBookFilesystemStorage::Save (const std::map<std::string, std::shared_ptr<Address> >& addresses)
|
||||
{
|
||||
if (addresses.empty()) {
|
||||
if (addresses.empty())
|
||||
{
|
||||
LogPrint(eLogWarning, "Addressbook: not saving empty addressbook");
|
||||
return 0;
|
||||
}
|
||||
|
||||
int num = 0;
|
||||
{
|
||||
// save index file
|
||||
std::ofstream f (indexPath, std::ofstream::out); // in text mode
|
||||
|
||||
if (!f.is_open ()) {
|
||||
LogPrint (eLogWarning, "Addressbook: Can't open ", indexPath);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (f.is_open ())
|
||||
{
|
||||
for (const auto& it: addresses)
|
||||
{
|
||||
if (it.second->IsValid ())
|
||||
|
@ -212,6 +213,31 @@ namespace client
|
|||
LogPrint (eLogWarning, "Addressbook: invalid address ", it.first);
|
||||
}
|
||||
LogPrint (eLogInfo, "Addressbook: ", num, " addresses saved");
|
||||
}
|
||||
else
|
||||
LogPrint (eLogWarning, "Addressbook: Can't open ", indexPath);
|
||||
}
|
||||
if (!m_HostsFile.empty ())
|
||||
{
|
||||
// dump full hosts.txt
|
||||
std::ofstream f (m_HostsFile, std::ofstream::out); // in text mode
|
||||
if (f.is_open ())
|
||||
{
|
||||
for (const auto& it: addresses)
|
||||
{
|
||||
std::shared_ptr<const i2p::data::IdentityEx> addr;
|
||||
if (it.second->IsIdentHash ())
|
||||
{
|
||||
addr = GetAddress (it.second->identHash);
|
||||
if (addr)
|
||||
f << it.first << "=" << addr->ToBase64 () << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
LogPrint (eLogWarning, "Addressbook: Can't open ", m_HostsFile);
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
|
@ -494,7 +520,7 @@ namespace client
|
|||
while (!f.eof ())
|
||||
{
|
||||
getline(f, s);
|
||||
if (!s.length()) continue; // skip empty line
|
||||
if (s.empty () || s[0] == '#') continue; // skip empty line or comment
|
||||
m_Subscriptions.push_back (std::make_shared<AddressBookSubscription> (*this, s));
|
||||
}
|
||||
LogPrint (eLogInfo, "Addressbook: ", m_Subscriptions.size (), " subscriptions urls loaded");
|
||||
|
@ -507,9 +533,10 @@ namespace client
|
|||
std::vector<std::string> subsList;
|
||||
boost::split(subsList, subscriptionURLs, boost::is_any_of(","), boost::token_compress_on);
|
||||
|
||||
for (size_t i = 0; i < subsList.size (); i++)
|
||||
for (const auto& s: subsList)
|
||||
{
|
||||
m_Subscriptions.push_back (std::make_shared<AddressBookSubscription> (*this, subsList[i]));
|
||||
if (s.empty () || s[0] == '#') continue; // skip empty line or comment
|
||||
m_Subscriptions.push_back (std::make_shared<AddressBookSubscription> (*this, s));
|
||||
}
|
||||
LogPrint (eLogInfo, "Addressbook: ", m_Subscriptions.size (), " subscriptions urls loaded");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue