2020-05-22 16:18:41 +03:00
|
|
|
|
/*
|
2025-01-12 12:23:26 -05:00
|
|
|
|
* Copyright (c) 2013-2025, The PurpleI2P Project
|
2020-05-22 16:18:41 +03:00
|
|
|
|
*
|
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
|
*
|
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
|
*/
|
|
|
|
|
|
2014-04-01 23:18:14 +04:00
|
|
|
|
#include <string.h>
|
2014-11-26 16:19:36 -05:00
|
|
|
|
#include <inttypes.h>
|
2014-04-01 23:18:14 +04:00
|
|
|
|
#include <string>
|
|
|
|
|
#include <map>
|
2014-11-26 16:19:36 -05:00
|
|
|
|
#include <fstream>
|
2014-12-19 14:40:02 -05:00
|
|
|
|
#include <chrono>
|
|
|
|
|
#include <condition_variable>
|
2016-03-26 10:31:47 -04:00
|
|
|
|
#include <openssl/rand.h>
|
2016-10-12 10:23:43 +00:00
|
|
|
|
#include <boost/algorithm/string.hpp>
|
2015-11-03 09:15:49 -05:00
|
|
|
|
#include "Base.h"
|
2014-04-01 23:18:14 +04:00
|
|
|
|
#include "util.h"
|
|
|
|
|
#include "Identity.h"
|
2016-02-11 00:00:00 +00:00
|
|
|
|
#include "FS.h"
|
2014-04-01 23:18:14 +04:00
|
|
|
|
#include "Log.h"
|
2016-07-16 00:00:00 +00:00
|
|
|
|
#include "HTTP.h"
|
2017-04-21 20:04:16 -04:00
|
|
|
|
#include "NetDb.hpp"
|
2014-12-19 14:40:02 -05:00
|
|
|
|
#include "ClientContext.h"
|
2014-04-01 23:18:14 +04:00
|
|
|
|
#include "AddressBook.h"
|
2016-10-12 10:23:43 +00:00
|
|
|
|
#include "Config.h"
|
2014-04-01 23:18:14 +04:00
|
|
|
|
|
2024-08-27 21:49:23 -04:00
|
|
|
|
#if STD_FILESYSTEM
|
|
|
|
|
#include <filesystem>
|
|
|
|
|
namespace fs_lib = std::filesystem;
|
|
|
|
|
#else
|
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
namespace fs_lib = boost::filesystem;
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-04-01 23:18:14 +04:00
|
|
|
|
namespace i2p
|
|
|
|
|
{
|
2014-10-24 15:22:36 -04:00
|
|
|
|
namespace client
|
2014-04-01 23:18:14 +04:00
|
|
|
|
{
|
2016-02-11 00:00:00 +00:00
|
|
|
|
// TODO: this is actually proxy class
|
2014-11-26 16:19:36 -05:00
|
|
|
|
class AddressBookFilesystemStorage: public AddressBookStorage
|
|
|
|
|
{
|
|
|
|
|
public:
|
2021-11-27 23:30:35 +03:00
|
|
|
|
|
2020-03-01 13:25:50 +03:00
|
|
|
|
AddressBookFilesystemStorage (): storage("addressbook", "b", "", "b32")
|
2019-03-01 14:42:20 -05:00
|
|
|
|
{
|
|
|
|
|
i2p::config::GetOption("persist.addressbook", m_IsPersist);
|
2021-01-20 19:19:34 -05:00
|
|
|
|
if (m_IsPersist)
|
|
|
|
|
i2p::config::GetOption("addressbook.hostsfile", m_HostsFile);
|
2019-03-01 14:42:20 -05:00
|
|
|
|
}
|
2025-01-12 12:23:26 -05:00
|
|
|
|
std::shared_ptr<const i2p::data::IdentityEx> GetAddress (const i2p::data::IdentHash& ident) const override;
|
|
|
|
|
void AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address) override;
|
|
|
|
|
void RemoveAddress (const i2p::data::IdentHash& ident) override;
|
2014-11-26 16:19:36 -05:00
|
|
|
|
|
2025-01-12 12:23:26 -05:00
|
|
|
|
bool Init () override;
|
|
|
|
|
int Load (Addresses& addresses) override;
|
|
|
|
|
int LoadLocal (Addresses& addresses) override;
|
|
|
|
|
int Save (const Addresses& addresses) override;
|
2016-03-14 16:05:57 -04:00
|
|
|
|
|
2025-01-12 12:23:26 -05:00
|
|
|
|
void SaveEtag (const i2p::data::IdentHash& subsciption, const std::string& etag, const std::string& lastModified) override;
|
|
|
|
|
bool GetEtag (const i2p::data::IdentHash& subscription, std::string& etag, std::string& lastModified) override;
|
|
|
|
|
void ResetEtags () override;
|
2016-03-15 14:37:07 -04:00
|
|
|
|
|
2016-03-16 15:40:29 -04:00
|
|
|
|
private:
|
|
|
|
|
|
2025-01-12 12:23:26 -05:00
|
|
|
|
int LoadFromFile (const std::string& filename, Addresses& addresses); // returns -1 if can't open file, otherwise number of records
|
2016-03-16 15:40:29 -04:00
|
|
|
|
|
2019-03-01 14:42:20 -05:00
|
|
|
|
private:
|
|
|
|
|
|
2021-01-20 19:19:34 -05:00
|
|
|
|
i2p::fs::HashedStorage storage;
|
2021-11-27 23:30:35 +03:00
|
|
|
|
std::string etagsPath, indexPath, localPath;
|
2019-03-01 14:42:20 -05:00
|
|
|
|
bool m_IsPersist;
|
2021-01-20 19:19:34 -05:00
|
|
|
|
std::string m_HostsFile; // file to dump hosts.txt, empty if not used
|
2014-11-26 16:19:36 -05:00
|
|
|
|
};
|
|
|
|
|
|
2016-02-18 00:00:00 +00:00
|
|
|
|
bool AddressBookFilesystemStorage::Init()
|
2017-05-22 10:34:29 -04:00
|
|
|
|
{
|
2016-02-18 00:00:00 +00:00
|
|
|
|
storage.SetPlace(i2p::fs::GetDataDir());
|
2016-03-14 16:05:57 -04:00
|
|
|
|
// init storage
|
2016-03-19 08:07:09 -04:00
|
|
|
|
if (storage.Init(i2p::data::GetBase32SubstitutionTable(), 32))
|
2017-05-22 10:34:29 -04:00
|
|
|
|
{
|
2016-03-19 08:07:09 -04:00
|
|
|
|
// init ETags
|
|
|
|
|
etagsPath = i2p::fs::StorageRootPath (storage, "etags");
|
|
|
|
|
if (!i2p::fs::Exists (etagsPath))
|
|
|
|
|
i2p::fs::CreateDirectory (etagsPath);
|
|
|
|
|
// init address files
|
|
|
|
|
indexPath = i2p::fs::StorageRootPath (storage, "addresses.csv");
|
|
|
|
|
localPath = i2p::fs::StorageRootPath (storage, "local.csv");
|
|
|
|
|
return true;
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
2016-03-19 08:07:09 -04:00
|
|
|
|
return false;
|
2016-02-18 00:00:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-03 09:15:49 -05:00
|
|
|
|
std::shared_ptr<const i2p::data::IdentityEx> AddressBookFilesystemStorage::GetAddress (const i2p::data::IdentHash& ident) const
|
2014-11-26 16:19:36 -05:00
|
|
|
|
{
|
2020-03-01 13:25:50 +03:00
|
|
|
|
if (!m_IsPersist)
|
2019-03-01 14:42:20 -05:00
|
|
|
|
{
|
2019-04-08 22:22:42 +03:00
|
|
|
|
LogPrint(eLogDebug, "Addressbook: Persistence is disabled");
|
2020-03-01 13:25:50 +03:00
|
|
|
|
return nullptr;
|
2019-03-01 14:42:20 -05:00
|
|
|
|
}
|
2016-02-18 00:00:00 +00:00
|
|
|
|
std::string filename = storage.Path(ident.ToBase32());
|
2016-02-11 00:00:00 +00:00
|
|
|
|
std::ifstream f(filename, std::ifstream::binary);
|
|
|
|
|
if (!f.is_open ()) {
|
|
|
|
|
LogPrint(eLogDebug, "Addressbook: Requested, but not found: ", filename);
|
|
|
|
|
return nullptr;
|
2014-11-26 16:19:36 -05:00
|
|
|
|
}
|
2016-02-11 00:00:00 +00:00
|
|
|
|
|
|
|
|
|
f.seekg (0,std::ios::end);
|
|
|
|
|
size_t len = f.tellg ();
|
|
|
|
|
if (len < i2p::data::DEFAULT_IDENTITY_SIZE) {
|
2016-03-11 00:46:52 +00:00
|
|
|
|
LogPrint (eLogError, "Addressbook: File ", filename, " is too short: ", len);
|
2015-11-03 09:15:49 -05:00
|
|
|
|
return nullptr;
|
2016-02-11 00:00:00 +00:00
|
|
|
|
}
|
|
|
|
|
f.seekg(0, std::ios::beg);
|
|
|
|
|
uint8_t * buf = new uint8_t[len];
|
|
|
|
|
f.read((char *)buf, len);
|
|
|
|
|
auto address = std::make_shared<i2p::data::IdentityEx>(buf, len);
|
|
|
|
|
delete[] buf;
|
|
|
|
|
return address;
|
2014-11-26 16:19:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-03 09:15:49 -05:00
|
|
|
|
void AddressBookFilesystemStorage::AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address)
|
2014-11-26 16:19:36 -05:00
|
|
|
|
{
|
2019-03-01 14:42:20 -05:00
|
|
|
|
if (!m_IsPersist) return;
|
2016-02-18 00:00:00 +00:00
|
|
|
|
std::string path = storage.Path( address->GetIdentHash().ToBase32() );
|
2016-02-11 00:00:00 +00:00
|
|
|
|
std::ofstream f (path, std::ofstream::binary | std::ofstream::out);
|
|
|
|
|
if (!f.is_open ()) {
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint (eLogError, "Addressbook: Can't open file ", path);
|
2016-02-11 00:00:00 +00:00
|
|
|
|
return;
|
2014-11-26 16:19:36 -05:00
|
|
|
|
}
|
2016-02-11 00:00:00 +00:00
|
|
|
|
size_t len = address->GetFullLen ();
|
|
|
|
|
uint8_t * buf = new uint8_t[len];
|
|
|
|
|
address->ToBuffer (buf, len);
|
|
|
|
|
f.write ((char *)buf, len);
|
|
|
|
|
delete[] buf;
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
2014-11-26 16:19:36 -05:00
|
|
|
|
|
|
|
|
|
void AddressBookFilesystemStorage::RemoveAddress (const i2p::data::IdentHash& ident)
|
|
|
|
|
{
|
2020-03-01 13:25:50 +03:00
|
|
|
|
if (!m_IsPersist) return;
|
2016-02-18 00:00:00 +00:00
|
|
|
|
storage.Remove( ident.ToBase32() );
|
2014-11-26 16:19:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2025-01-12 12:23:26 -05:00
|
|
|
|
int AddressBookFilesystemStorage::LoadFromFile (const std::string& filename, Addresses& addresses)
|
2014-11-27 16:26:55 -05:00
|
|
|
|
{
|
2017-05-22 10:34:29 -04:00
|
|
|
|
int num = 0;
|
2016-03-16 15:40:29 -04:00
|
|
|
|
std::ifstream f (filename, std::ifstream::in); // in text mode
|
|
|
|
|
if (!f) return -1;
|
2016-02-11 00:00:00 +00:00
|
|
|
|
|
|
|
|
|
addresses.clear ();
|
2017-05-22 10:34:29 -04:00
|
|
|
|
while (!f.eof ())
|
2016-03-16 15:40:29 -04:00
|
|
|
|
{
|
|
|
|
|
std::string s;
|
2016-02-11 00:00:00 +00:00
|
|
|
|
getline(f, s);
|
2016-03-16 15:40:29 -04:00
|
|
|
|
if (!s.length()) continue; // skip empty line
|
2016-02-11 00:00:00 +00:00
|
|
|
|
|
|
|
|
|
std::size_t pos = s.find(',');
|
|
|
|
|
if (pos != std::string::npos)
|
2014-11-27 16:26:55 -05:00
|
|
|
|
{
|
2016-02-11 00:00:00 +00:00
|
|
|
|
std::string name = s.substr(0, pos++);
|
|
|
|
|
std::string addr = s.substr(pos);
|
2014-11-27 16:26:55 -05:00
|
|
|
|
|
2019-03-27 15:19:10 -04:00
|
|
|
|
addresses[name] = std::make_shared<Address>(addr);
|
2016-02-11 00:00:00 +00:00
|
|
|
|
num++;
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
2014-11-27 16:26:55 -05:00
|
|
|
|
}
|
2016-03-16 15:40:29 -04:00
|
|
|
|
return num;
|
|
|
|
|
}
|
2016-02-11 00:00:00 +00:00
|
|
|
|
|
2025-01-12 12:23:26 -05:00
|
|
|
|
int AddressBookFilesystemStorage::Load (Addresses& addresses)
|
2016-03-16 15:40:29 -04:00
|
|
|
|
{
|
2017-05-22 10:34:29 -04:00
|
|
|
|
int num = LoadFromFile (indexPath, addresses);
|
2016-03-16 15:40:29 -04:00
|
|
|
|
if (num < 0)
|
|
|
|
|
{
|
|
|
|
|
LogPrint(eLogWarning, "Addressbook: Can't open ", indexPath);
|
|
|
|
|
return 0;
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint(eLogInfo, "Addressbook: Using index file ", indexPath);
|
2016-02-11 00:00:00 +00:00
|
|
|
|
LogPrint (eLogInfo, "Addressbook: ", num, " addresses loaded from storage");
|
2016-03-16 15:40:29 -04:00
|
|
|
|
|
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-12 12:23:26 -05:00
|
|
|
|
int AddressBookFilesystemStorage::LoadLocal (Addresses& addresses)
|
2016-03-16 15:40:29 -04:00
|
|
|
|
{
|
2017-05-22 10:34:29 -04:00
|
|
|
|
int num = LoadFromFile (localPath, addresses);
|
2016-03-16 15:40:29 -04:00
|
|
|
|
if (num < 0) return 0;
|
2017-05-22 10:34:29 -04:00
|
|
|
|
LogPrint (eLogInfo, "Addressbook: ", num, " local addresses loaded");
|
2014-11-27 16:26:55 -05:00
|
|
|
|
return num;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-12 12:23:26 -05:00
|
|
|
|
int AddressBookFilesystemStorage::Save (const Addresses& addresses)
|
2014-11-27 16:26:55 -05:00
|
|
|
|
{
|
2021-11-27 23:30:35 +03:00
|
|
|
|
if (addresses.empty())
|
2021-01-20 19:19:34 -05:00
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint(eLogWarning, "Addressbook: Not saving empty addressbook");
|
2016-02-11 00:00:00 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-27 16:26:55 -05:00
|
|
|
|
int num = 0;
|
2020-03-01 13:25:50 +03:00
|
|
|
|
{
|
2021-01-20 19:19:34 -05:00
|
|
|
|
// save index file
|
|
|
|
|
std::ofstream f (indexPath, std::ofstream::out); // in text mode
|
|
|
|
|
if (f.is_open ())
|
|
|
|
|
{
|
|
|
|
|
for (const auto& it: addresses)
|
|
|
|
|
{
|
|
|
|
|
if (it.second->IsValid ())
|
2021-11-27 23:30:35 +03:00
|
|
|
|
{
|
2021-01-20 19:19:34 -05:00
|
|
|
|
f << it.first << ",";
|
|
|
|
|
if (it.second->IsIdentHash ())
|
|
|
|
|
f << it.second->identHash.ToBase32 ();
|
|
|
|
|
else
|
|
|
|
|
f << it.second->blindedPublicKey->ToB33 ();
|
|
|
|
|
f << std::endl;
|
|
|
|
|
num++;
|
2021-11-27 23:30:35 +03:00
|
|
|
|
}
|
2021-01-20 19:19:34 -05:00
|
|
|
|
else
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint (eLogWarning, "Addressbook: Invalid address ", it.first);
|
2021-01-20 19:19:34 -05:00
|
|
|
|
}
|
|
|
|
|
LogPrint (eLogInfo, "Addressbook: ", num, " addresses saved");
|
2021-11-27 23:30:35 +03:00
|
|
|
|
}
|
2019-03-28 16:06:53 -04:00
|
|
|
|
else
|
2021-01-20 19:19:34 -05:00
|
|
|
|
LogPrint (eLogWarning, "Addressbook: Can't open ", indexPath);
|
2021-11-27 23:30:35 +03:00
|
|
|
|
}
|
2021-01-20 19:19:34 -05:00
|
|
|
|
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;
|
2021-11-27 23:30:35 +03:00
|
|
|
|
if (it.second->IsIdentHash ())
|
|
|
|
|
{
|
2021-01-20 19:19:34 -05:00
|
|
|
|
addr = GetAddress (it.second->identHash);
|
|
|
|
|
if (addr)
|
|
|
|
|
f << it.first << "=" << addr->ToBase64 () << std::endl;
|
2021-11-27 23:30:35 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-01-20 19:19:34 -05:00
|
|
|
|
else
|
|
|
|
|
LogPrint (eLogWarning, "Addressbook: Can't open ", m_HostsFile);
|
2021-11-27 23:30:35 +03:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-22 10:34:29 -04:00
|
|
|
|
return num;
|
|
|
|
|
}
|
2014-11-27 16:26:55 -05:00
|
|
|
|
|
2016-03-14 16:05:57 -04:00
|
|
|
|
void AddressBookFilesystemStorage::SaveEtag (const i2p::data::IdentHash& subscription, const std::string& etag, const std::string& lastModified)
|
|
|
|
|
{
|
|
|
|
|
std::string fname = etagsPath + i2p::fs::dirSep + subscription.ToBase32 () + ".txt";
|
|
|
|
|
std::ofstream f (fname, std::ofstream::out | std::ofstream::trunc);
|
|
|
|
|
if (f)
|
2017-05-22 10:34:29 -04:00
|
|
|
|
{
|
|
|
|
|
f << etag << std::endl;
|
2016-03-14 22:00:05 -04:00
|
|
|
|
f<< lastModified << std::endl;
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
2016-03-14 16:05:57 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-15 14:37:07 -04:00
|
|
|
|
bool AddressBookFilesystemStorage::GetEtag (const i2p::data::IdentHash& subscription, std::string& etag, std::string& lastModified)
|
|
|
|
|
{
|
|
|
|
|
std::string fname = etagsPath + i2p::fs::dirSep + subscription.ToBase32 () + ".txt";
|
|
|
|
|
std::ifstream f (fname, std::ofstream::in);
|
|
|
|
|
if (!f || f.eof ()) return false;
|
|
|
|
|
std::getline (f, etag);
|
2017-05-22 10:34:29 -04:00
|
|
|
|
if (f.eof ()) return false;
|
2016-03-15 14:37:07 -04:00
|
|
|
|
std::getline (f, lastModified);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-04 18:29:29 +00:00
|
|
|
|
void AddressBookFilesystemStorage::ResetEtags ()
|
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint (eLogError, "Addressbook: Resetting eTags");
|
2024-08-27 21:49:23 -04:00
|
|
|
|
for (fs_lib::directory_iterator it (etagsPath); it != fs_lib::directory_iterator (); ++it)
|
2019-03-04 18:29:29 +00:00
|
|
|
|
{
|
2024-08-27 21:49:23 -04:00
|
|
|
|
if (!fs_lib::is_regular_file (it->status ()))
|
2019-03-04 18:29:29 +00:00
|
|
|
|
continue;
|
2024-08-27 21:49:23 -04:00
|
|
|
|
fs_lib::remove (it->path ());
|
2019-03-04 18:29:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-26 16:19:36 -05:00
|
|
|
|
//---------------------------------------------------------------------
|
2019-03-27 15:19:10 -04:00
|
|
|
|
|
2025-01-12 12:23:26 -05:00
|
|
|
|
Address::Address (std::string_view b32):
|
2019-09-23 13:42:15 -04:00
|
|
|
|
addressType (eAddressInvalid)
|
2019-03-27 15:19:10 -04:00
|
|
|
|
{
|
|
|
|
|
if (b32.length () <= B33_ADDRESS_THRESHOLD)
|
|
|
|
|
{
|
2019-09-23 13:42:15 -04:00
|
|
|
|
if (identHash.FromBase32 (b32) > 0)
|
|
|
|
|
addressType = eAddressIndentHash;
|
2019-03-27 15:19:10 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
blindedPublicKey = std::make_shared<i2p::data::BlindedPublicKey>(b32);
|
2019-09-23 13:42:15 -04:00
|
|
|
|
if (blindedPublicKey->IsValid ())
|
|
|
|
|
addressType = eAddressBlindedPublicKey;
|
2019-03-27 15:19:10 -04:00
|
|
|
|
}
|
2020-03-01 13:25:50 +03:00
|
|
|
|
}
|
2019-03-27 15:19:10 -04:00
|
|
|
|
|
|
|
|
|
Address::Address (const i2p::data::IdentHash& hash)
|
|
|
|
|
{
|
|
|
|
|
addressType = eAddressIndentHash;
|
2020-03-01 13:25:50 +03:00
|
|
|
|
identHash = hash;
|
2019-03-27 15:19:10 -04:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-29 15:32:06 -04:00
|
|
|
|
AddressBook::AddressBook (): m_Storage(nullptr), m_IsLoaded (false),
|
2022-05-27 13:17:06 -04:00
|
|
|
|
m_NumRetries (0), m_DefaultSubscription (nullptr), m_SubscriptionsUpdateTimer (nullptr),
|
|
|
|
|
m_IsEnabled (true)
|
2014-04-06 23:22:33 +04:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-26 16:19:36 -05:00
|
|
|
|
AddressBook::~AddressBook ()
|
2017-05-22 10:34:29 -04:00
|
|
|
|
{
|
2015-03-30 10:21:52 -04:00
|
|
|
|
Stop ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddressBook::Start ()
|
|
|
|
|
{
|
2022-05-27 13:17:06 -04:00
|
|
|
|
i2p::config::GetOption("addressbook.enabled", m_IsEnabled);
|
|
|
|
|
if (m_IsEnabled)
|
2022-10-09 20:24:43 +03:00
|
|
|
|
{
|
2022-05-27 13:17:06 -04:00
|
|
|
|
if (!m_Storage)
|
|
|
|
|
m_Storage = new AddressBookFilesystemStorage;
|
|
|
|
|
m_Storage->Init();
|
|
|
|
|
LoadHosts (); /* try storage, then hosts.txt, then download */
|
|
|
|
|
StartSubscriptions ();
|
|
|
|
|
StartLookups ();
|
2022-10-09 20:24:43 +03:00
|
|
|
|
}
|
2015-03-30 10:21:52 -04:00
|
|
|
|
}
|
2016-03-26 10:31:47 -04:00
|
|
|
|
|
|
|
|
|
void AddressBook::StartResolvers ()
|
|
|
|
|
{
|
|
|
|
|
LoadLocal ();
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-30 10:21:52 -04:00
|
|
|
|
void AddressBook::Stop ()
|
|
|
|
|
{
|
2016-03-26 10:31:47 -04:00
|
|
|
|
StopLookups ();
|
2015-03-30 10:21:52 -04:00
|
|
|
|
StopSubscriptions ();
|
|
|
|
|
if (m_SubscriptionsUpdateTimer)
|
2017-05-22 10:34:29 -04:00
|
|
|
|
{
|
|
|
|
|
delete m_SubscriptionsUpdateTimer;
|
2015-03-30 10:21:52 -04:00
|
|
|
|
m_SubscriptionsUpdateTimer = nullptr;
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
2024-10-29 15:32:06 -04:00
|
|
|
|
bool isDownloading = m_Downloading.valid ();
|
|
|
|
|
if (isDownloading)
|
2014-12-21 09:33:02 -05:00
|
|
|
|
{
|
2024-10-29 15:32:06 -04:00
|
|
|
|
if (m_Downloading.wait_for(std::chrono::seconds(0)) == std::future_status::ready)
|
|
|
|
|
isDownloading = false;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LogPrint (eLogInfo, "Addressbook: Subscriptions are downloading, abort");
|
|
|
|
|
for (int i = 0; i < 30; i++)
|
2014-12-21 09:33:02 -05:00
|
|
|
|
{
|
2024-10-29 15:32:06 -04:00
|
|
|
|
if (m_Downloading.wait_for(std::chrono::seconds(1)) == std::future_status::ready) // wait for 1 seconds
|
|
|
|
|
{
|
|
|
|
|
isDownloading = false;
|
|
|
|
|
LogPrint (eLogInfo, "Addressbook: Subscriptions download complete");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
2024-10-29 15:32:06 -04:00
|
|
|
|
}
|
|
|
|
|
if (!isDownloading)
|
|
|
|
|
m_Downloading.get ();
|
|
|
|
|
else
|
|
|
|
|
LogPrint (eLogError, "Addressbook: Subscription download timeout");
|
2016-08-05 21:23:54 +03:00
|
|
|
|
}
|
2014-11-28 15:08:23 -05:00
|
|
|
|
if (m_Storage)
|
|
|
|
|
{
|
|
|
|
|
m_Storage->Save (m_Addresses);
|
|
|
|
|
delete m_Storage;
|
2015-03-30 10:21:52 -04:00
|
|
|
|
m_Storage = nullptr;
|
2014-11-28 15:08:23 -05:00
|
|
|
|
}
|
2017-05-22 10:34:29 -04:00
|
|
|
|
m_DefaultSubscription = nullptr;
|
|
|
|
|
m_Subscriptions.clear ();
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-12 12:23:26 -05:00
|
|
|
|
std::shared_ptr<const Address> AddressBook::GetAddress (std::string_view address)
|
2019-03-28 09:57:34 -04:00
|
|
|
|
{
|
|
|
|
|
auto pos = address.find(".b32.i2p");
|
|
|
|
|
if (pos != std::string::npos)
|
2020-03-01 13:25:50 +03:00
|
|
|
|
{
|
2019-09-23 13:42:15 -04:00
|
|
|
|
auto addr = std::make_shared<const Address>(address.substr (0, pos));
|
|
|
|
|
return addr->IsValid () ? addr : nullptr;
|
|
|
|
|
}
|
2019-03-28 09:57:34 -04:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pos = address.find (".i2p");
|
|
|
|
|
if (pos != std::string::npos)
|
2019-03-28 12:19:19 -04:00
|
|
|
|
{
|
2022-05-27 13:17:06 -04:00
|
|
|
|
if (!m_IsEnabled) return nullptr;
|
2019-03-28 12:19:19 -04:00
|
|
|
|
auto addr = FindAddress (address);
|
|
|
|
|
if (!addr)
|
2022-05-20 19:56:05 +03:00
|
|
|
|
LookupAddress (address); // TODO:
|
2019-03-28 12:19:19 -04:00
|
|
|
|
return addr;
|
2020-03-01 13:25:50 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-28 09:57:34 -04:00
|
|
|
|
// if not .b32 we assume full base64 address
|
|
|
|
|
i2p::data::IdentityEx dest;
|
|
|
|
|
if (!dest.FromBase64 (address))
|
|
|
|
|
return nullptr;
|
|
|
|
|
return std::make_shared<const Address>(dest.GetIdentHash ());
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-12 12:23:26 -05:00
|
|
|
|
std::shared_ptr<const Address> AddressBook::FindAddress (std::string_view address)
|
2014-04-06 23:22:33 +04:00
|
|
|
|
{
|
2016-02-11 00:00:00 +00:00
|
|
|
|
auto it = m_Addresses.find (address);
|
|
|
|
|
if (it != m_Addresses.end ())
|
2019-03-27 15:19:10 -04:00
|
|
|
|
return it->second;
|
2017-05-22 10:34:29 -04:00
|
|
|
|
return nullptr;
|
2014-04-06 23:22:33 +04:00
|
|
|
|
}
|
2014-04-01 23:18:14 +04:00
|
|
|
|
|
2022-10-09 22:13:49 +03:00
|
|
|
|
bool AddressBook::RecordExists (const std::string& address, const std::string& jump)
|
|
|
|
|
{
|
|
|
|
|
auto addr = FindAddress(address);
|
|
|
|
|
if (!addr)
|
|
|
|
|
return false;
|
|
|
|
|
|
2023-10-29 22:11:38 -04:00
|
|
|
|
auto pos = jump.find(".b32.i2p");
|
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
|
{
|
|
|
|
|
i2p::data::IdentHash identHash;
|
|
|
|
|
if (identHash.FromBase32(jump.substr (0, pos)) && identHash == addr->identHash)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
i2p::data::IdentityEx ident;
|
|
|
|
|
if (ident.FromBase64 (jump) && ident.GetIdentHash () == addr->identHash)
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-09 22:13:49 +03:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-02 13:11:49 -04:00
|
|
|
|
void AddressBook::InsertAddress (const std::string& address, const std::string& jump)
|
2014-09-23 15:38:56 -04:00
|
|
|
|
{
|
2019-04-02 13:11:49 -04:00
|
|
|
|
auto pos = jump.find(".b32.i2p");
|
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
|
{
|
|
|
|
|
m_Addresses[address] = std::make_shared<Address>(jump.substr (0, pos));
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint (eLogInfo, "Addressbook: Added ", address," -> ", jump);
|
2020-03-01 13:25:50 +03:00
|
|
|
|
}
|
2019-04-02 13:11:49 -04:00
|
|
|
|
else
|
2020-03-01 13:25:50 +03:00
|
|
|
|
{
|
|
|
|
|
// assume base64
|
2019-04-02 13:11:49 -04:00
|
|
|
|
auto ident = std::make_shared<i2p::data::IdentityEx>();
|
|
|
|
|
if (ident->FromBase64 (jump))
|
|
|
|
|
{
|
2024-09-11 12:06:55 -04:00
|
|
|
|
if (m_Storage) m_Storage->AddAddress (ident);
|
2019-04-02 13:11:49 -04:00
|
|
|
|
m_Addresses[address] = std::make_shared<Address>(ident->GetIdentHash ());
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint (eLogInfo, "Addressbook: Added ", address," -> ", ToAddress(ident->GetIdentHash ()));
|
2019-04-02 13:11:49 -04:00
|
|
|
|
}
|
|
|
|
|
else
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint (eLogError, "Addressbook: Malformed address ", jump);
|
2019-04-02 13:11:49 -04:00
|
|
|
|
}
|
2014-09-23 15:38:56 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 15:19:10 -04:00
|
|
|
|
void AddressBook::InsertFullAddress (std::shared_ptr<const i2p::data::IdentityEx> address)
|
2014-11-26 16:51:36 -05:00
|
|
|
|
{
|
2024-09-11 12:06:55 -04:00
|
|
|
|
if (m_Storage) m_Storage->AddAddress (address);
|
2014-11-26 16:51:36 -05:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-27 15:19:10 -04:00
|
|
|
|
std::shared_ptr<const i2p::data::IdentityEx> AddressBook::GetFullAddress (const std::string& address)
|
2014-11-26 16:19:36 -05:00
|
|
|
|
{
|
2019-03-28 12:19:19 -04:00
|
|
|
|
auto addr = GetAddress (address);
|
|
|
|
|
if (!addr || !addr->IsIdentHash ()) return nullptr;
|
2024-09-11 12:06:55 -04:00
|
|
|
|
return m_Storage ? m_Storage->GetAddress (addr->identHash) : nullptr;
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
2014-11-26 16:19:36 -05:00
|
|
|
|
|
2014-08-14 14:32:00 -04:00
|
|
|
|
void AddressBook::LoadHosts ()
|
2014-04-01 23:18:14 +04:00
|
|
|
|
{
|
2024-09-11 12:06:55 -04:00
|
|
|
|
if (!m_Storage) return;
|
2014-12-19 22:03:34 -05:00
|
|
|
|
if (m_Storage->Load (m_Addresses) > 0)
|
2014-11-28 09:40:27 -05:00
|
|
|
|
{
|
|
|
|
|
m_IsLoaded = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-05-22 10:34:29 -04:00
|
|
|
|
|
2016-02-11 00:00:00 +00:00
|
|
|
|
// then try hosts.txt
|
|
|
|
|
std::ifstream f (i2p::fs::DataDirPath("hosts.txt"), std::ifstream::in); // in text mode
|
2017-05-22 10:34:29 -04:00
|
|
|
|
if (f.is_open ())
|
2014-08-14 14:32:00 -04:00
|
|
|
|
{
|
2016-07-16 00:00:00 +00:00
|
|
|
|
LoadHostsFromStream (f, false);
|
2014-12-21 09:33:02 -05:00
|
|
|
|
m_IsLoaded = true;
|
|
|
|
|
}
|
2019-03-04 18:29:29 +00:00
|
|
|
|
|
|
|
|
|
// reset eTags, because we don’t know how old hosts.txt is or can't load addressbook
|
|
|
|
|
m_Storage->ResetEtags ();
|
2014-12-19 22:03:34 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-16 00:00:00 +00:00
|
|
|
|
bool AddressBook::LoadHostsFromStream (std::istream& f, bool is_update)
|
2014-12-19 22:03:34 -05:00
|
|
|
|
{
|
2014-12-21 09:33:02 -05:00
|
|
|
|
std::unique_lock<std::mutex> l(m_AddressBookMutex);
|
2014-12-19 22:03:34 -05:00
|
|
|
|
int numAddresses = 0;
|
2016-04-01 12:51:34 -04:00
|
|
|
|
bool incomplete = false;
|
2014-08-14 14:32:00 -04:00
|
|
|
|
std::string s;
|
|
|
|
|
while (!f.eof ())
|
2014-04-01 23:18:14 +04:00
|
|
|
|
{
|
2014-08-14 14:32:00 -04:00
|
|
|
|
getline(f, s);
|
2014-04-02 00:27:40 +04:00
|
|
|
|
|
2017-05-05 13:54:21 -04:00
|
|
|
|
if (!s.length() || s[0] == '#')
|
|
|
|
|
continue; // skip empty or comment line
|
2014-08-14 14:32:00 -04:00
|
|
|
|
|
|
|
|
|
size_t pos = s.find('=');
|
|
|
|
|
|
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
|
{
|
|
|
|
|
std::string name = s.substr(0, pos++);
|
|
|
|
|
std::string addr = s.substr(pos);
|
|
|
|
|
|
2019-08-10 22:16:26 -04:00
|
|
|
|
size_t pos = addr.find('#');
|
2017-05-10 09:36:58 -04:00
|
|
|
|
if (pos != std::string::npos)
|
2019-08-10 22:16:26 -04:00
|
|
|
|
addr = addr.substr(0, pos); // remove comments
|
2017-05-10 09:36:58 -04:00
|
|
|
|
|
2021-09-17 02:53:30 +03:00
|
|
|
|
pos = name.find(".b32.i2p");
|
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint (eLogError, "Addressbook: Skipped adding of b32 address: ", name);
|
2021-09-17 02:53:30 +03:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pos = name.find(".i2p");
|
|
|
|
|
if (pos == std::string::npos)
|
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint (eLogError, "Addressbook: Malformed domain: ", name);
|
2021-09-17 02:53:30 +03:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-03 09:15:49 -05:00
|
|
|
|
auto ident = std::make_shared<i2p::data::IdentityEx> ();
|
2016-07-16 00:00:00 +00:00
|
|
|
|
if (!ident->FromBase64(addr)) {
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint (eLogError, "Addressbook: Malformed address ", addr, " for ", name);
|
2016-04-01 12:51:34 -04:00
|
|
|
|
incomplete = f.eof ();
|
2016-07-16 00:00:00 +00:00
|
|
|
|
continue;
|
2016-04-01 12:51:34 -04:00
|
|
|
|
}
|
2016-07-16 00:00:00 +00:00
|
|
|
|
numAddresses++;
|
2017-07-18 15:58:32 -04:00
|
|
|
|
auto it = m_Addresses.find (name);
|
2018-07-10 17:39:21 +08:00
|
|
|
|
if (it != m_Addresses.end ()) // already exists ?
|
2017-07-18 15:58:32 -04:00
|
|
|
|
{
|
2022-05-20 19:56:05 +03:00
|
|
|
|
if (it->second->IsIdentHash () && it->second->identHash != ident->GetIdentHash () && // address changed?
|
2021-11-27 23:30:35 +03:00
|
|
|
|
ident->GetSigningKeyType () != i2p::data::SIGNING_KEY_TYPE_DSA_SHA1) // don't replace by DSA
|
2017-07-18 15:58:32 -04:00
|
|
|
|
{
|
2019-03-27 15:19:10 -04:00
|
|
|
|
it->second->identHash = ident->GetIdentHash ();
|
2024-09-11 12:06:55 -04:00
|
|
|
|
if (m_Storage)
|
|
|
|
|
{
|
|
|
|
|
m_Storage->AddAddress (ident);
|
|
|
|
|
m_Storage->RemoveAddress (it->second->identHash);
|
|
|
|
|
}
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint (eLogInfo, "Addressbook: Updated host: ", name);
|
2017-07-18 15:58:32 -04:00
|
|
|
|
}
|
2018-01-06 11:48:51 +08:00
|
|
|
|
}
|
2017-07-18 15:58:32 -04:00
|
|
|
|
else
|
|
|
|
|
{
|
2020-11-19 15:41:00 -05:00
|
|
|
|
m_Addresses.emplace (name, std::make_shared<Address>(ident->GetIdentHash ()));
|
2024-09-11 12:06:55 -04:00
|
|
|
|
if (m_Storage) m_Storage->AddAddress (ident);
|
2017-07-18 15:58:32 -04:00
|
|
|
|
if (is_update)
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint (eLogInfo, "Addressbook: Added new host: ", name);
|
2017-07-18 15:58:32 -04:00
|
|
|
|
}
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
2016-04-01 12:51:34 -04:00
|
|
|
|
else
|
|
|
|
|
incomplete = f.eof ();
|
2014-08-14 14:32:00 -04:00
|
|
|
|
}
|
2016-03-11 00:46:52 +00:00
|
|
|
|
LogPrint (eLogInfo, "Addressbook: ", numAddresses, " addresses processed");
|
2014-12-21 09:33:02 -05:00
|
|
|
|
if (numAddresses > 0)
|
2017-05-22 10:34:29 -04:00
|
|
|
|
{
|
2016-04-01 12:51:34 -04:00
|
|
|
|
if (!incomplete) m_IsLoaded = true;
|
2024-09-11 12:06:55 -04:00
|
|
|
|
if (m_Storage) m_Storage->Save (m_Addresses);
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
2016-04-01 12:51:34 -04:00
|
|
|
|
return !incomplete;
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-22 15:06:54 -05:00
|
|
|
|
void AddressBook::LoadSubscriptions ()
|
|
|
|
|
{
|
|
|
|
|
if (!m_Subscriptions.size ())
|
|
|
|
|
{
|
2016-02-11 00:00:00 +00:00
|
|
|
|
std::ifstream f (i2p::fs::DataDirPath ("subscriptions.txt"), std::ifstream::in); // in text mode
|
2014-12-22 15:06:54 -05:00
|
|
|
|
if (f.is_open ())
|
|
|
|
|
{
|
|
|
|
|
std::string s;
|
|
|
|
|
while (!f.eof ())
|
|
|
|
|
{
|
|
|
|
|
getline(f, s);
|
2021-01-20 19:19:34 -05:00
|
|
|
|
if (s.empty () || s[0] == '#') continue; // skip empty line or comment
|
2016-08-09 10:17:40 -04:00
|
|
|
|
m_Subscriptions.push_back (std::make_shared<AddressBookSubscription> (*this, s));
|
2014-12-22 15:06:54 -05:00
|
|
|
|
}
|
2016-01-18 00:00:00 +00:00
|
|
|
|
LogPrint (eLogInfo, "Addressbook: ", m_Subscriptions.size (), " subscriptions urls loaded");
|
2016-10-12 10:23:43 +00:00
|
|
|
|
LogPrint (eLogWarning, "Addressbook: subscriptions.txt usage is deprecated, use config file instead");
|
2014-12-22 15:06:54 -05:00
|
|
|
|
}
|
2021-12-07 23:00:52 +03:00
|
|
|
|
else
|
2020-03-01 13:25:50 +03:00
|
|
|
|
{
|
2024-10-29 13:59:21 -04:00
|
|
|
|
LogPrint (eLogInfo, "Addressbook: Loading subscriptions from config");
|
2020-03-01 13:25:50 +03:00
|
|
|
|
// using config file items
|
|
|
|
|
std::string subscriptionURLs; i2p::config::GetOption("addressbook.subscriptions", subscriptionURLs);
|
|
|
|
|
std::vector<std::string> subsList;
|
|
|
|
|
boost::split(subsList, subscriptionURLs, boost::is_any_of(","), boost::token_compress_on);
|
|
|
|
|
|
2021-01-20 19:19:34 -05:00
|
|
|
|
for (const auto& s: subsList)
|
2024-10-29 13:59:21 -04:00
|
|
|
|
if (!s.empty ())
|
|
|
|
|
m_Subscriptions.push_back (std::make_shared<AddressBookSubscription> (*this, s));
|
2020-03-01 13:25:50 +03:00
|
|
|
|
LogPrint (eLogInfo, "Addressbook: ", m_Subscriptions.size (), " subscriptions urls loaded");
|
|
|
|
|
}
|
2014-12-22 15:06:54 -05:00
|
|
|
|
}
|
|
|
|
|
else
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint (eLogError, "Addressbook: Subscriptions already loaded");
|
2014-12-22 15:06:54 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-24 14:48:07 -04:00
|
|
|
|
void AddressBook::LoadLocal ()
|
|
|
|
|
{
|
2022-08-30 15:18:31 -04:00
|
|
|
|
if (!m_Storage) return;
|
2025-01-12 12:23:26 -05:00
|
|
|
|
AddressBookStorage::Addresses localAddresses;
|
2016-03-24 14:48:07 -04:00
|
|
|
|
m_Storage->LoadLocal (localAddresses);
|
2016-08-05 21:23:54 +03:00
|
|
|
|
for (const auto& it: localAddresses)
|
2016-03-24 14:48:07 -04:00
|
|
|
|
{
|
2019-03-27 15:19:10 -04:00
|
|
|
|
if (!it.second->IsIdentHash ()) continue; // skip blinded for now
|
2016-03-24 14:48:07 -04:00
|
|
|
|
auto dot = it.first.find ('.');
|
|
|
|
|
if (dot != std::string::npos)
|
|
|
|
|
{
|
|
|
|
|
auto domain = it.first.substr (dot + 1);
|
2020-03-01 13:25:50 +03:00
|
|
|
|
auto it1 = m_Addresses.find (domain); // find domain in our addressbook
|
2019-03-27 15:19:10 -04:00
|
|
|
|
if (it1 != m_Addresses.end () && it1->second->IsIdentHash ())
|
2016-03-24 14:48:07 -04:00
|
|
|
|
{
|
2019-03-27 15:19:10 -04:00
|
|
|
|
auto dest = context.FindLocalDestination (it1->second->identHash);
|
2017-05-22 10:34:29 -04:00
|
|
|
|
if (dest)
|
2016-03-24 14:48:07 -04:00
|
|
|
|
{
|
|
|
|
|
// address is ours
|
|
|
|
|
std::shared_ptr<AddressResolver> resolver;
|
2019-03-27 15:19:10 -04:00
|
|
|
|
auto it2 = m_Resolvers.find (it1->second->identHash);
|
2016-03-24 14:48:07 -04:00
|
|
|
|
if (it2 != m_Resolvers.end ())
|
|
|
|
|
resolver = it2->second; // resolver exists
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// create new resolver
|
|
|
|
|
resolver = std::make_shared<AddressResolver>(dest);
|
2019-03-27 15:19:10 -04:00
|
|
|
|
m_Resolvers.insert (std::make_pair(it1->second->identHash, resolver));
|
2016-03-24 14:48:07 -04:00
|
|
|
|
}
|
2019-03-27 15:19:10 -04:00
|
|
|
|
resolver->AddAddress (it.first, it.second->identHash);
|
2016-03-24 14:48:07 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-15 14:37:07 -04:00
|
|
|
|
bool AddressBook::GetEtag (const i2p::data::IdentHash& subscription, std::string& etag, std::string& lastModified)
|
|
|
|
|
{
|
|
|
|
|
if (m_Storage)
|
2017-05-22 10:34:29 -04:00
|
|
|
|
return m_Storage->GetEtag (subscription, etag, lastModified);
|
2016-03-15 14:37:07 -04:00
|
|
|
|
else
|
2017-05-22 10:34:29 -04:00
|
|
|
|
return false;
|
2016-03-15 14:37:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-14 16:05:57 -04:00
|
|
|
|
void AddressBook::DownloadComplete (bool success, const i2p::data::IdentHash& subscription, const std::string& etag, const std::string& lastModified)
|
2014-12-23 13:57:09 -05:00
|
|
|
|
{
|
2018-01-23 15:50:28 -05:00
|
|
|
|
m_NumRetries++;
|
|
|
|
|
int nextUpdateTimeout = m_NumRetries*CONTINIOUS_SUBSCRIPTION_RETRY_TIMEOUT;
|
|
|
|
|
if (m_NumRetries > CONTINIOUS_SUBSCRIPTION_MAX_NUM_RETRIES || nextUpdateTimeout > CONTINIOUS_SUBSCRIPTION_UPDATE_TIMEOUT)
|
|
|
|
|
nextUpdateTimeout = CONTINIOUS_SUBSCRIPTION_UPDATE_TIMEOUT;
|
2016-03-11 16:29:49 -05:00
|
|
|
|
if (success)
|
2017-05-22 10:34:29 -04:00
|
|
|
|
{
|
2018-01-23 15:50:28 -05:00
|
|
|
|
m_NumRetries = 0;
|
2016-08-09 10:17:40 -04:00
|
|
|
|
if (m_DefaultSubscription) m_DefaultSubscription = nullptr;
|
2016-03-11 16:29:49 -05:00
|
|
|
|
if (m_IsLoaded)
|
2017-05-22 10:34:29 -04:00
|
|
|
|
nextUpdateTimeout = CONTINIOUS_SUBSCRIPTION_UPDATE_TIMEOUT;
|
2016-03-11 16:29:49 -05:00
|
|
|
|
else
|
|
|
|
|
m_IsLoaded = true;
|
2016-03-14 22:00:05 -04:00
|
|
|
|
if (m_Storage) m_Storage->SaveEtag (subscription, etag, lastModified);
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
2015-01-21 16:34:50 -05:00
|
|
|
|
if (m_SubscriptionsUpdateTimer)
|
|
|
|
|
{
|
2016-03-11 16:29:49 -05:00
|
|
|
|
m_SubscriptionsUpdateTimer->expires_from_now (boost::posix_time::minutes(nextUpdateTimeout));
|
2015-01-21 16:34:50 -05:00
|
|
|
|
m_SubscriptionsUpdateTimer->async_wait (std::bind (&AddressBook::HandleSubscriptionsUpdateTimer,
|
|
|
|
|
this, std::placeholders::_1));
|
|
|
|
|
}
|
2014-12-23 13:57:09 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddressBook::StartSubscriptions ()
|
|
|
|
|
{
|
|
|
|
|
LoadSubscriptions ();
|
2016-02-15 18:20:01 -05:00
|
|
|
|
if (m_IsLoaded && m_Subscriptions.empty ()) return;
|
2017-05-22 10:34:29 -04:00
|
|
|
|
|
2014-12-23 13:57:09 -05:00
|
|
|
|
auto dest = i2p::client::context.GetSharedLocalDestination ();
|
|
|
|
|
if (dest)
|
|
|
|
|
{
|
|
|
|
|
m_SubscriptionsUpdateTimer = new boost::asio::deadline_timer (dest->GetService ());
|
|
|
|
|
m_SubscriptionsUpdateTimer->expires_from_now (boost::posix_time::minutes(INITIAL_SUBSCRIPTION_UPDATE_TIMEOUT));
|
|
|
|
|
m_SubscriptionsUpdateTimer->async_wait (std::bind (&AddressBook::HandleSubscriptionsUpdateTimer,
|
|
|
|
|
this, std::placeholders::_1));
|
|
|
|
|
}
|
|
|
|
|
else
|
2023-03-31 11:29:04 +00:00
|
|
|
|
LogPrint (eLogCritical, "Addressbook: Can't start subscriptions: missing shared local destination");
|
2014-12-23 13:57:09 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddressBook::StopSubscriptions ()
|
|
|
|
|
{
|
|
|
|
|
if (m_SubscriptionsUpdateTimer)
|
|
|
|
|
m_SubscriptionsUpdateTimer->cancel ();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddressBook::HandleSubscriptionsUpdateTimer (const boost::system::error_code& ecode)
|
|
|
|
|
{
|
|
|
|
|
if (ecode != boost::asio::error::operation_aborted)
|
|
|
|
|
{
|
|
|
|
|
auto dest = i2p::client::context.GetSharedLocalDestination ();
|
2016-02-11 00:00:00 +00:00
|
|
|
|
if (!dest) {
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint(eLogWarning, "Addressbook: Missing local destination, skip subscription update");
|
2016-02-11 00:00:00 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2024-10-29 15:32:06 -04:00
|
|
|
|
bool isDownloading = m_Downloading.valid ();
|
|
|
|
|
if (isDownloading && m_Downloading.wait_for(std::chrono::seconds(0)) == std::future_status::ready) // still active?
|
|
|
|
|
{
|
|
|
|
|
m_Downloading.get ();
|
|
|
|
|
isDownloading = false;
|
|
|
|
|
}
|
|
|
|
|
if (!isDownloading && dest->IsReady ())
|
2014-12-23 13:57:09 -05:00
|
|
|
|
{
|
2016-02-15 18:20:01 -05:00
|
|
|
|
if (!m_IsLoaded)
|
|
|
|
|
{
|
2017-05-22 10:34:29 -04:00
|
|
|
|
// download it from default subscription
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint (eLogInfo, "Addressbook: Trying to download it from default subscription.");
|
2020-03-01 13:25:50 +03:00
|
|
|
|
std::string defaultSubURL; i2p::config::GetOption("addressbook.defaulturl", defaultSubURL);
|
2016-02-15 18:20:01 -05:00
|
|
|
|
if (!m_DefaultSubscription)
|
2016-10-12 10:23:43 +00:00
|
|
|
|
m_DefaultSubscription = std::make_shared<AddressBookSubscription>(*this, defaultSubURL);
|
2024-10-29 15:32:06 -04:00
|
|
|
|
m_Downloading = std::async (std::launch::async,
|
|
|
|
|
std::bind (&AddressBookSubscription::CheckUpdates, m_DefaultSubscription));
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
2016-02-15 18:20:01 -05:00
|
|
|
|
else if (!m_Subscriptions.empty ())
|
2017-05-22 10:34:29 -04:00
|
|
|
|
{
|
2016-02-15 18:20:01 -05:00
|
|
|
|
// pick random subscription
|
2017-05-22 10:34:29 -04:00
|
|
|
|
auto ind = rand () % m_Subscriptions.size();
|
2024-10-29 15:32:06 -04:00
|
|
|
|
m_Downloading = std::async (std::launch::async,
|
|
|
|
|
std::bind (&AddressBookSubscription::CheckUpdates, m_Subscriptions[ind]));
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
2014-12-23 13:57:09 -05:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// try it again later
|
|
|
|
|
m_SubscriptionsUpdateTimer->expires_from_now (boost::posix_time::minutes(INITIAL_SUBSCRIPTION_RETRY_TIMEOUT));
|
|
|
|
|
m_SubscriptionsUpdateTimer->async_wait (std::bind (&AddressBook::HandleSubscriptionsUpdateTimer,
|
|
|
|
|
this, std::placeholders::_1));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-26 10:31:47 -04:00
|
|
|
|
void AddressBook::StartLookups ()
|
|
|
|
|
{
|
|
|
|
|
auto dest = i2p::client::context.GetSharedLocalDestination ();
|
|
|
|
|
if (dest)
|
|
|
|
|
{
|
2016-08-21 22:34:48 -04:00
|
|
|
|
auto datagram = dest->GetDatagramDestination ();
|
2016-09-03 10:24:06 -04:00
|
|
|
|
if (!datagram)
|
|
|
|
|
datagram = dest->CreateDatagramDestination ();
|
|
|
|
|
datagram->SetReceiver (std::bind (&AddressBook::HandleLookupResponse, this,
|
|
|
|
|
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5),
|
|
|
|
|
ADDRESS_RESPONSE_DATAGRAM_PORT);
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
2016-03-26 10:31:47 -04:00
|
|
|
|
}
|
2017-05-22 10:34:29 -04:00
|
|
|
|
|
2016-03-26 10:31:47 -04:00
|
|
|
|
void AddressBook::StopLookups ()
|
|
|
|
|
{
|
|
|
|
|
auto dest = i2p::client::context.GetSharedLocalDestination ();
|
|
|
|
|
if (dest)
|
|
|
|
|
{
|
|
|
|
|
auto datagram = dest->GetDatagramDestination ();
|
2016-09-03 10:24:06 -04:00
|
|
|
|
if (datagram) datagram->ResetReceiver (ADDRESS_RESPONSE_DATAGRAM_PORT);
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
2016-03-26 10:31:47 -04:00
|
|
|
|
}
|
|
|
|
|
|
2025-01-12 12:23:26 -05:00
|
|
|
|
void AddressBook::LookupAddress (std::string_view address)
|
2016-03-26 10:31:47 -04:00
|
|
|
|
{
|
2019-03-27 15:19:10 -04:00
|
|
|
|
std::shared_ptr<const Address> addr;
|
2016-03-26 10:31:47 -04:00
|
|
|
|
auto dot = address.find ('.');
|
|
|
|
|
if (dot != std::string::npos)
|
2019-03-27 15:19:10 -04:00
|
|
|
|
addr = FindAddress (address.substr (dot + 1));
|
|
|
|
|
if (!addr || !addr->IsIdentHash ()) // TODO:
|
2016-03-26 10:31:47 -04:00
|
|
|
|
{
|
2016-07-16 00:00:00 +00:00
|
|
|
|
LogPrint (eLogError, "Addressbook: Can't find domain for ", address);
|
2016-03-26 10:31:47 -04:00
|
|
|
|
return;
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-26 10:31:47 -04:00
|
|
|
|
auto dest = i2p::client::context.GetSharedLocalDestination ();
|
|
|
|
|
if (dest)
|
|
|
|
|
{
|
|
|
|
|
auto datagram = dest->GetDatagramDestination ();
|
|
|
|
|
if (datagram)
|
|
|
|
|
{
|
|
|
|
|
uint32_t nonce;
|
|
|
|
|
RAND_bytes ((uint8_t *)&nonce, 4);
|
|
|
|
|
{
|
|
|
|
|
std::unique_lock<std::mutex> l(m_LookupsMutex);
|
2017-05-22 10:34:29 -04:00
|
|
|
|
m_Lookups[nonce] = address;
|
|
|
|
|
}
|
2019-03-27 15:19:10 -04:00
|
|
|
|
LogPrint (eLogDebug, "Addressbook: Lookup of ", address, " to ", addr->identHash.ToBase32 (), " nonce=", nonce);
|
2016-03-26 10:31:47 -04:00
|
|
|
|
size_t len = address.length () + 9;
|
|
|
|
|
uint8_t * buf = new uint8_t[len];
|
|
|
|
|
memset (buf, 0, 4);
|
|
|
|
|
htobe32buf (buf + 4, nonce);
|
|
|
|
|
buf[8] = address.length ();
|
2025-01-12 12:23:26 -05:00
|
|
|
|
memcpy (buf + 9, address.data (), address.length ());
|
2019-03-27 15:19:10 -04:00
|
|
|
|
datagram->SendDatagramTo (buf, len, addr->identHash, ADDRESS_RESPONSE_DATAGRAM_PORT, ADDRESS_RESOLVER_DATAGRAM_PORT);
|
2016-03-26 10:31:47 -04:00
|
|
|
|
delete[] buf;
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-26 10:31:47 -04:00
|
|
|
|
void AddressBook::HandleLookupResponse (const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len)
|
|
|
|
|
{
|
|
|
|
|
if (len < 44)
|
|
|
|
|
{
|
2016-07-16 00:00:00 +00:00
|
|
|
|
LogPrint (eLogError, "Addressbook: Lookup response is too short ", len);
|
2016-03-26 10:31:47 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
uint32_t nonce = bufbe32toh (buf + 4);
|
2016-07-16 00:00:00 +00:00
|
|
|
|
LogPrint (eLogDebug, "Addressbook: Lookup response received from ", from.GetIdentHash ().ToBase32 (), " nonce=", nonce);
|
2016-03-26 10:31:47 -04:00
|
|
|
|
std::string address;
|
|
|
|
|
{
|
|
|
|
|
std::unique_lock<std::mutex> l(m_LookupsMutex);
|
|
|
|
|
auto it = m_Lookups.find (nonce);
|
|
|
|
|
if (it != m_Lookups.end ())
|
2017-05-22 10:34:29 -04:00
|
|
|
|
{
|
2016-03-26 10:31:47 -04:00
|
|
|
|
address = it->second;
|
|
|
|
|
m_Lookups.erase (it);
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-26 10:31:47 -04:00
|
|
|
|
if (address.length () > 0)
|
|
|
|
|
{
|
|
|
|
|
// TODO: verify from
|
2016-12-26 17:19:54 -05:00
|
|
|
|
i2p::data::IdentHash hash(buf + 8);
|
2017-05-22 10:34:29 -04:00
|
|
|
|
if (!hash.IsZero ())
|
2019-03-27 15:19:10 -04:00
|
|
|
|
m_Addresses[address] = std::make_shared<Address>(hash);
|
2016-12-26 17:19:54 -05:00
|
|
|
|
else
|
|
|
|
|
LogPrint (eLogInfo, "AddressBook: Lookup response: ", address, " not found");
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
2016-03-26 10:31:47 -04:00
|
|
|
|
}
|
2017-05-22 10:34:29 -04:00
|
|
|
|
|
2024-10-29 13:59:21 -04:00
|
|
|
|
AddressBookSubscription::AddressBookSubscription (AddressBook& book, std::string_view link):
|
2014-12-19 14:40:02 -05:00
|
|
|
|
m_Book (book), m_Link (link)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-16 00:00:00 +00:00
|
|
|
|
void AddressBookSubscription::CheckUpdates ()
|
2014-12-19 14:40:02 -05:00
|
|
|
|
{
|
2020-12-07 06:31:46 +03:00
|
|
|
|
i2p::util::SetThreadName("Addressbook");
|
|
|
|
|
|
2016-07-16 00:00:00 +00:00
|
|
|
|
bool result = MakeRequest ();
|
|
|
|
|
m_Book.DownloadComplete (result, m_Ident, m_Etag, m_LastModified);
|
2014-12-19 14:40:02 -05:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-16 00:00:00 +00:00
|
|
|
|
bool AddressBookSubscription::MakeRequest ()
|
2014-12-19 14:40:02 -05:00
|
|
|
|
{
|
2016-07-16 00:00:00 +00:00
|
|
|
|
i2p::http::URL url;
|
2017-05-22 10:34:29 -04:00
|
|
|
|
// must be run in separate thread
|
2016-07-16 00:00:00 +00:00
|
|
|
|
LogPrint (eLogInfo, "Addressbook: Downloading hosts database from ", m_Link);
|
2020-03-01 13:25:50 +03:00
|
|
|
|
if (!url.parse(m_Link))
|
2019-03-28 12:19:19 -04:00
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint(eLogError, "Addressbook: Failed to parse url: ", m_Link);
|
2016-07-16 00:00:00 +00:00
|
|
|
|
return false;
|
2016-07-16 00:00:00 +00:00
|
|
|
|
}
|
2019-03-28 12:19:19 -04:00
|
|
|
|
auto addr = m_Book.GetAddress (url.host);
|
2020-03-01 13:25:50 +03:00
|
|
|
|
if (!addr || !addr->IsIdentHash ())
|
2019-03-28 12:19:19 -04:00
|
|
|
|
{
|
2016-07-16 00:00:00 +00:00
|
|
|
|
LogPrint (eLogError, "Addressbook: Can't resolve ", url.host);
|
2016-07-16 00:00:00 +00:00
|
|
|
|
return false;
|
2016-07-16 00:00:00 +00:00
|
|
|
|
}
|
2019-03-28 12:19:19 -04:00
|
|
|
|
else
|
|
|
|
|
m_Ident = addr->identHash;
|
2023-01-03 21:25:19 +03:00
|
|
|
|
// save url parts for later use
|
2022-11-08 19:52:43 -05:00
|
|
|
|
std::string dest_host = url.host;
|
|
|
|
|
int dest_port = url.port ? url.port : 80;
|
|
|
|
|
// try to create stream to addressbook site
|
|
|
|
|
auto stream = i2p::client::context.GetSharedLocalDestination ()->CreateStream (m_Ident, dest_port);
|
|
|
|
|
if (!stream)
|
|
|
|
|
{
|
2016-07-16 00:00:00 +00:00
|
|
|
|
LogPrint (eLogError, "Addressbook: LeaseSet for address ", url.host, " not found");
|
2016-07-16 00:00:00 +00:00
|
|
|
|
return false;
|
2023-01-03 21:25:19 +03:00
|
|
|
|
}
|
|
|
|
|
if (m_Etag.empty() && m_LastModified.empty())
|
2022-11-08 19:52:43 -05:00
|
|
|
|
{
|
2016-07-16 00:00:00 +00:00
|
|
|
|
m_Book.GetEtag (m_Ident, m_Etag, m_LastModified);
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint (eLogDebug, "Addressbook: Loaded for ", url.host, ": ETag: ", m_Etag, ", Last-Modified: ", m_LastModified);
|
2016-07-16 00:00:00 +00:00
|
|
|
|
}
|
2023-01-03 21:25:19 +03:00
|
|
|
|
// create http request & send it
|
2016-07-16 00:00:00 +00:00
|
|
|
|
i2p::http::HTTPReq req;
|
2017-02-04 22:39:54 -05:00
|
|
|
|
req.AddHeader("Host", dest_host);
|
|
|
|
|
req.AddHeader("User-Agent", "Wget/1.11.4");
|
2020-09-27 17:46:15 -04:00
|
|
|
|
req.AddHeader("Accept-Encoding", "gzip");
|
2018-01-15 16:30:01 +01:00
|
|
|
|
req.AddHeader("X-Accept-Encoding", "x-i2p-gzip;q=1.0, identity;q=0.5, deflate;q=0, gzip;q=0, *;q=0");
|
2017-02-04 22:39:54 -05:00
|
|
|
|
req.AddHeader("Connection", "close");
|
2016-07-16 00:00:00 +00:00
|
|
|
|
if (!m_Etag.empty())
|
2017-02-04 22:39:54 -05:00
|
|
|
|
req.AddHeader("If-None-Match", m_Etag);
|
2016-07-16 00:00:00 +00:00
|
|
|
|
if (!m_LastModified.empty())
|
2017-02-04 22:39:54 -05:00
|
|
|
|
req.AddHeader("If-Modified-Since", m_LastModified);
|
2023-01-03 21:25:19 +03:00
|
|
|
|
// convert url to relative
|
2022-05-20 19:56:05 +03:00
|
|
|
|
url.schema = "";
|
|
|
|
|
url.host = "";
|
|
|
|
|
req.uri = url.to_string();
|
2020-09-27 17:46:15 -04:00
|
|
|
|
req.version = "HTTP/1.1";
|
2016-07-16 00:00:00 +00:00
|
|
|
|
std::string request = req.to_string();
|
|
|
|
|
stream->Send ((const uint8_t *) request.data(), request.length());
|
2022-11-08 19:52:43 -05:00
|
|
|
|
// read response
|
2016-07-16 00:00:00 +00:00
|
|
|
|
std::string response;
|
|
|
|
|
uint8_t recv_buf[4096];
|
|
|
|
|
bool end = false;
|
2018-09-02 07:51:58 -04:00
|
|
|
|
int numAttempts = 0;
|
2017-05-22 10:34:29 -04:00
|
|
|
|
while (!end)
|
2017-02-12 15:11:19 -05:00
|
|
|
|
{
|
2022-11-08 18:34:59 -05:00
|
|
|
|
size_t received = stream->Receive (recv_buf, 4096, SUBSCRIPTION_REQUEST_TIMEOUT);
|
|
|
|
|
if (received)
|
|
|
|
|
{
|
|
|
|
|
response.append ((char *)recv_buf, received);
|
|
|
|
|
if (!stream->IsOpen ()) end = true;
|
2023-01-03 21:25:19 +03:00
|
|
|
|
}
|
2022-11-08 18:34:59 -05:00
|
|
|
|
else if (!stream->IsOpen ())
|
|
|
|
|
end = true;
|
|
|
|
|
else
|
2017-05-22 10:34:29 -04:00
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint (eLogError, "Addressbook: Subscriptions request timeout expired");
|
2017-02-12 15:11:19 -05:00
|
|
|
|
numAttempts++;
|
|
|
|
|
if (numAttempts > 5) end = true;
|
2023-01-03 21:25:19 +03:00
|
|
|
|
}
|
2016-07-16 00:00:00 +00:00
|
|
|
|
}
|
|
|
|
|
// process remaining buffer
|
2017-05-22 10:34:29 -04:00
|
|
|
|
while (size_t len = stream->ReadSome (recv_buf, sizeof(recv_buf)))
|
2016-07-16 00:00:00 +00:00
|
|
|
|
response.append ((char *)recv_buf, len);
|
2023-01-03 21:25:19 +03:00
|
|
|
|
// parse response
|
2016-07-16 00:00:00 +00:00
|
|
|
|
i2p::http::HTTPRes res;
|
|
|
|
|
int res_head_len = res.parse(response);
|
2017-05-22 10:34:29 -04:00
|
|
|
|
if (res_head_len < 0)
|
2017-02-12 15:11:19 -05:00
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint(eLogError, "Addressbook: Can't parse http response from ", dest_host);
|
2016-07-16 00:00:00 +00:00
|
|
|
|
return false;
|
2016-07-16 00:00:00 +00:00
|
|
|
|
}
|
2017-05-22 10:34:29 -04:00
|
|
|
|
if (res_head_len == 0)
|
2017-02-12 15:11:19 -05:00
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint(eLogError, "Addressbook: Incomplete http response from ", dest_host, ", interrupted by timeout");
|
2016-07-16 00:00:00 +00:00
|
|
|
|
return false;
|
2016-07-16 00:00:00 +00:00
|
|
|
|
}
|
2023-01-03 21:25:19 +03:00
|
|
|
|
// assert: res_head_len > 0
|
2016-07-16 00:00:00 +00:00
|
|
|
|
response.erase(0, res_head_len);
|
2017-05-22 10:34:29 -04:00
|
|
|
|
if (res.code == 304)
|
2017-02-12 15:11:19 -05:00
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint (eLogInfo, "Addressbook: No updates from ", dest_host, ", code 304");
|
2016-07-16 00:00:00 +00:00
|
|
|
|
return false;
|
2016-07-16 00:00:00 +00:00
|
|
|
|
}
|
2017-05-22 10:34:29 -04:00
|
|
|
|
if (res.code != 200)
|
2017-02-12 15:11:19 -05:00
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint (eLogWarning, "Adressbook: Can't get updates from ", dest_host, ", response code ", res.code);
|
2016-07-16 00:00:00 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
int len = res.content_length();
|
2017-05-22 10:34:29 -04:00
|
|
|
|
if (response.empty())
|
2017-02-12 15:11:19 -05:00
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint(eLogError, "Addressbook: Empty response from ", dest_host, ", expected ", len, " bytes");
|
2016-07-16 00:00:00 +00:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-05-22 10:34:29 -04:00
|
|
|
|
if (!res.is_gzipped () && len > 0 && len != (int) response.length())
|
2017-02-12 15:11:19 -05:00
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint(eLogError, "Addressbook: Response size mismatch, expected: ", len, ", got: ", response.length(), "bytes");
|
2016-07-16 00:00:00 +00:00
|
|
|
|
return false;
|
2016-07-16 00:00:00 +00:00
|
|
|
|
}
|
2023-01-03 21:25:19 +03:00
|
|
|
|
// assert: res.code == 200
|
2016-07-16 00:00:00 +00:00
|
|
|
|
auto it = res.headers.find("ETag");
|
2017-02-12 15:11:19 -05:00
|
|
|
|
if (it != res.headers.end()) m_Etag = it->second;
|
2020-09-27 19:19:48 -04:00
|
|
|
|
it = res.headers.find("Last-Modified");
|
2017-02-12 15:11:19 -05:00
|
|
|
|
if (it != res.headers.end()) m_LastModified = it->second;
|
2017-05-22 10:34:29 -04:00
|
|
|
|
if (res.is_chunked())
|
2017-02-12 15:11:19 -05:00
|
|
|
|
{
|
2016-07-16 00:00:00 +00:00
|
|
|
|
std::stringstream in(response), out;
|
|
|
|
|
i2p::http::MergeChunkedResponse (in, out);
|
|
|
|
|
response = out.str();
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
2020-09-27 17:46:15 -04:00
|
|
|
|
if (res.is_gzipped())
|
2017-02-12 15:11:19 -05:00
|
|
|
|
{
|
2016-07-16 00:00:00 +00:00
|
|
|
|
std::stringstream out;
|
2016-02-18 13:19:31 -05:00
|
|
|
|
i2p::data::GzipInflator inflator;
|
2016-07-16 00:00:00 +00:00
|
|
|
|
inflator.Inflate ((const uint8_t *) response.data(), response.length(), out);
|
2017-05-22 10:34:29 -04:00
|
|
|
|
if (out.fail())
|
2017-02-12 15:11:19 -05:00
|
|
|
|
{
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint(eLogError, "Addressbook: Can't gunzip http response");
|
2016-02-18 13:19:31 -05:00
|
|
|
|
return false;
|
2016-07-16 00:00:00 +00:00
|
|
|
|
}
|
|
|
|
|
response = out.str();
|
|
|
|
|
}
|
|
|
|
|
std::stringstream ss(response);
|
2021-11-27 22:53:53 +03:00
|
|
|
|
LogPrint (eLogInfo, "Addressbook: Got update from ", dest_host);
|
2016-07-16 00:00:00 +00:00
|
|
|
|
m_Book.LoadHostsFromStream (ss, true);
|
2016-07-16 00:00:00 +00:00
|
|
|
|
return true;
|
2016-02-18 13:19:31 -05:00
|
|
|
|
}
|
2016-03-24 14:48:07 -04:00
|
|
|
|
|
|
|
|
|
AddressResolver::AddressResolver (std::shared_ptr<ClientDestination> destination):
|
|
|
|
|
m_LocalDestination (destination)
|
|
|
|
|
{
|
|
|
|
|
if (m_LocalDestination)
|
|
|
|
|
{
|
|
|
|
|
auto datagram = m_LocalDestination->GetDatagramDestination ();
|
|
|
|
|
if (!datagram)
|
|
|
|
|
datagram = m_LocalDestination->CreateDatagramDestination ();
|
2017-05-22 10:34:29 -04:00
|
|
|
|
datagram->SetReceiver (std::bind (&AddressResolver::HandleRequest, this,
|
|
|
|
|
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4, std::placeholders::_5),
|
2016-03-24 14:48:07 -04:00
|
|
|
|
ADDRESS_RESOLVER_DATAGRAM_PORT);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-26 10:31:47 -04:00
|
|
|
|
AddressResolver::~AddressResolver ()
|
|
|
|
|
{
|
|
|
|
|
if (m_LocalDestination)
|
|
|
|
|
{
|
|
|
|
|
auto datagram = m_LocalDestination->GetDatagramDestination ();
|
|
|
|
|
if (datagram)
|
2020-03-01 13:25:50 +03:00
|
|
|
|
datagram->ResetReceiver (ADDRESS_RESOLVER_DATAGRAM_PORT);
|
2017-05-22 10:34:29 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-24 14:48:07 -04:00
|
|
|
|
void AddressResolver::HandleRequest (const i2p::data::IdentityEx& from, uint16_t fromPort, uint16_t toPort, const uint8_t * buf, size_t len)
|
|
|
|
|
{
|
|
|
|
|
if (len < 9 || len < buf[8] + 9U)
|
|
|
|
|
{
|
2016-07-16 00:00:00 +00:00
|
|
|
|
LogPrint (eLogError, "Addressbook: Address request is too short ", len);
|
2016-03-24 14:48:07 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// read requested address
|
|
|
|
|
uint8_t l = buf[8];
|
|
|
|
|
char address[255];
|
|
|
|
|
memcpy (address, buf + 9, l);
|
2017-05-22 10:34:29 -04:00
|
|
|
|
address[l] = 0;
|
2016-07-16 00:00:00 +00:00
|
|
|
|
LogPrint (eLogDebug, "Addressbook: Address request ", address);
|
2016-03-24 14:48:07 -04:00
|
|
|
|
// send response
|
2016-03-26 10:31:47 -04:00
|
|
|
|
uint8_t response[44];
|
2016-03-24 14:48:07 -04:00
|
|
|
|
memset (response, 0, 4); // reserved
|
2017-05-22 10:34:29 -04:00
|
|
|
|
memcpy (response + 4, buf + 4, 4); // nonce
|
2016-03-24 14:48:07 -04:00
|
|
|
|
auto it = m_LocalAddresses.find (address); // address lookup
|
2017-05-22 10:34:29 -04:00
|
|
|
|
if (it != m_LocalAddresses.end ())
|
|
|
|
|
memcpy (response + 8, it->second, 32); // ident
|
2016-03-24 14:48:07 -04:00
|
|
|
|
else
|
2017-05-22 10:34:29 -04:00
|
|
|
|
memset (response + 8, 0, 32); // not found
|
2016-03-26 10:31:47 -04:00
|
|
|
|
memset (response + 40, 0, 4); // set expiration time to zero
|
2016-12-12 13:40:24 -05:00
|
|
|
|
m_LocalDestination->GetDatagramDestination ()->SendDatagramTo (response, 44, from.GetIdentHash(), toPort, fromPort);
|
2016-03-24 14:48:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddressResolver::AddAddress (const std::string& name, const i2p::data::IdentHash& ident)
|
|
|
|
|
{
|
2017-05-22 10:34:29 -04:00
|
|
|
|
m_LocalAddresses[name] = ident;
|
2016-03-24 14:48:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-01 23:18:14 +04:00
|
|
|
|
}
|
|
|
|
|
}
|