2014-04-01 23:18:14 +04:00
|
|
|
#include <string.h>
|
2014-11-27 00:19:36 +03:00
|
|
|
#include <inttypes.h>
|
2014-04-01 23:18:14 +04:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
2014-11-27 00:19:36 +03:00
|
|
|
#include <fstream>
|
2014-12-19 22:40:02 +03:00
|
|
|
#include <chrono>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <boost/lexical_cast.hpp>
|
2015-11-03 17:15:49 +03:00
|
|
|
#include "Base.h"
|
2014-04-01 23:18:14 +04:00
|
|
|
#include "util.h"
|
|
|
|
#include "Identity.h"
|
2016-02-11 03:00:00 +03:00
|
|
|
#include "FS.h"
|
2014-04-01 23:18:14 +04:00
|
|
|
#include "Log.h"
|
2014-12-19 22:40:02 +03:00
|
|
|
#include "NetDb.h"
|
|
|
|
#include "ClientContext.h"
|
2014-04-01 23:18:14 +04:00
|
|
|
#include "AddressBook.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2014-10-24 23:22:36 +04:00
|
|
|
namespace client
|
2014-04-01 23:18:14 +04:00
|
|
|
{
|
2016-02-11 03:00:00 +03:00
|
|
|
// TODO: this is actually proxy class
|
2014-11-27 00:19:36 +03:00
|
|
|
class AddressBookFilesystemStorage: public AddressBookStorage
|
|
|
|
{
|
2016-02-18 03:00:00 +03:00
|
|
|
private:
|
|
|
|
i2p::fs::HashedStorage storage;
|
|
|
|
std::string indexPath;
|
|
|
|
|
2014-11-27 00:19:36 +03:00
|
|
|
public:
|
2016-02-18 03:00:00 +03:00
|
|
|
AddressBookFilesystemStorage (): storage("addressbook", "b", "", "b32") {};
|
2015-11-03 17:15:49 +03:00
|
|
|
std::shared_ptr<const i2p::data::IdentityEx> GetAddress (const i2p::data::IdentHash& ident) const;
|
|
|
|
void AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address);
|
2014-11-27 00:19:36 +03:00
|
|
|
void RemoveAddress (const i2p::data::IdentHash& ident);
|
|
|
|
|
2016-02-18 03:00:00 +03:00
|
|
|
bool Init ();
|
2014-11-28 17:40:27 +03:00
|
|
|
int Load (std::map<std::string, i2p::data::IdentHash>& addresses);
|
|
|
|
int Save (const std::map<std::string, i2p::data::IdentHash>& addresses);
|
2014-11-27 00:19:36 +03:00
|
|
|
};
|
|
|
|
|
2016-02-18 03:00:00 +03:00
|
|
|
bool AddressBookFilesystemStorage::Init()
|
2016-02-18 03:00:00 +03:00
|
|
|
{
|
|
|
|
storage.SetPlace(i2p::fs::GetDataDir());
|
|
|
|
indexPath = storage.GetRoot() + i2p::fs::dirSep + "addresses.csv";
|
2016-02-18 03:00:00 +03:00
|
|
|
return storage.Init(i2p::data::GetBase32SubstitutionTable(), 32);
|
2016-02-18 03:00:00 +03:00
|
|
|
}
|
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
std::shared_ptr<const i2p::data::IdentityEx> AddressBookFilesystemStorage::GetAddress (const i2p::data::IdentHash& ident) const
|
2014-11-27 00:19:36 +03:00
|
|
|
{
|
2016-02-18 03:00:00 +03:00
|
|
|
std::string filename = storage.Path(ident.ToBase32());
|
2016-02-11 03:00:00 +03:00
|
|
|
std::ifstream f(filename, std::ifstream::binary);
|
|
|
|
if (!f.is_open ()) {
|
|
|
|
LogPrint(eLogDebug, "Addressbook: Requested, but not found: ", filename);
|
|
|
|
return nullptr;
|
2014-11-27 00:19:36 +03:00
|
|
|
}
|
2016-02-11 03:00:00 +03:00
|
|
|
|
|
|
|
f.seekg (0,std::ios::end);
|
|
|
|
size_t len = f.tellg ();
|
|
|
|
if (len < i2p::data::DEFAULT_IDENTITY_SIZE) {
|
2016-03-11 03:46:52 +03:00
|
|
|
LogPrint (eLogError, "Addressbook: File ", filename, " is too short: ", len);
|
2015-11-03 17:15:49 +03:00
|
|
|
return nullptr;
|
2016-02-11 03:00:00 +03: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-27 00:19:36 +03:00
|
|
|
}
|
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
void AddressBookFilesystemStorage::AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address)
|
2014-11-27 00:19:36 +03:00
|
|
|
{
|
2016-02-18 03:00:00 +03:00
|
|
|
std::string path = storage.Path( address->GetIdentHash().ToBase32() );
|
2016-02-11 03:00:00 +03:00
|
|
|
std::ofstream f (path, std::ofstream::binary | std::ofstream::out);
|
|
|
|
if (!f.is_open ()) {
|
2016-03-11 03:46:52 +03:00
|
|
|
LogPrint (eLogError, "Addressbook: can't open file ", path);
|
2016-02-11 03:00:00 +03:00
|
|
|
return;
|
2014-11-27 00:19:36 +03:00
|
|
|
}
|
2016-02-11 03:00:00 +03:00
|
|
|
size_t len = address->GetFullLen ();
|
|
|
|
uint8_t * buf = new uint8_t[len];
|
|
|
|
address->ToBuffer (buf, len);
|
|
|
|
f.write ((char *)buf, len);
|
|
|
|
delete[] buf;
|
2014-11-27 00:19:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddressBookFilesystemStorage::RemoveAddress (const i2p::data::IdentHash& ident)
|
|
|
|
{
|
2016-02-18 03:00:00 +03:00
|
|
|
storage.Remove( ident.ToBase32() );
|
2014-11-27 00:19:36 +03:00
|
|
|
}
|
|
|
|
|
2014-11-28 17:40:27 +03:00
|
|
|
int AddressBookFilesystemStorage::Load (std::map<std::string, i2p::data::IdentHash>& addresses)
|
2014-11-28 00:26:55 +03:00
|
|
|
{
|
|
|
|
int num = 0;
|
2016-02-11 03:00:00 +03:00
|
|
|
std::string s;
|
2016-02-18 03:00:00 +03:00
|
|
|
std::ifstream f (indexPath, std::ifstream::in); // in text mode
|
2016-02-11 03:00:00 +03:00
|
|
|
|
|
|
|
if (f.is_open ()) {
|
2016-02-18 03:00:00 +03:00
|
|
|
LogPrint(eLogInfo, "Addressbook: using index file ", indexPath);
|
2016-02-11 03:00:00 +03:00
|
|
|
} else {
|
2016-02-18 03:00:00 +03:00
|
|
|
LogPrint(eLogWarning, "Addressbook: Can't open ", indexPath);
|
2016-02-11 03:00:00 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
addresses.clear ();
|
|
|
|
while (!f.eof ()) {
|
|
|
|
getline(f, s);
|
|
|
|
if (!s.length())
|
|
|
|
continue; // skip empty line
|
|
|
|
|
|
|
|
std::size_t pos = s.find(',');
|
|
|
|
if (pos != std::string::npos)
|
2014-11-28 00:26:55 +03:00
|
|
|
{
|
2016-02-11 03:00:00 +03:00
|
|
|
std::string name = s.substr(0, pos++);
|
|
|
|
std::string addr = s.substr(pos);
|
2014-11-28 00:26:55 +03:00
|
|
|
|
2016-02-11 03:00:00 +03:00
|
|
|
i2p::data::IdentHash ident;
|
|
|
|
ident.FromBase32 (addr);
|
|
|
|
addresses[name] = ident;
|
|
|
|
num++;
|
|
|
|
}
|
2014-11-28 00:26:55 +03:00
|
|
|
}
|
2016-02-11 03:00:00 +03:00
|
|
|
|
|
|
|
LogPrint (eLogInfo, "Addressbook: ", num, " addresses loaded from storage");
|
2014-11-28 00:26:55 +03:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2014-11-28 17:40:27 +03:00
|
|
|
int AddressBookFilesystemStorage::Save (const std::map<std::string, i2p::data::IdentHash>& addresses)
|
2014-11-28 00:26:55 +03:00
|
|
|
{
|
2016-02-11 03:00:00 +03:00
|
|
|
if (addresses.size() == 0) {
|
|
|
|
LogPrint(eLogWarning, "Addressbook: not saving empty addressbook");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-28 00:26:55 +03:00
|
|
|
int num = 0;
|
2016-02-18 03:00:00 +03:00
|
|
|
std::ofstream f (indexPath, std::ofstream::out); // in text mode
|
2016-02-11 03:00:00 +03:00
|
|
|
|
|
|
|
if (!f.is_open ()) {
|
2016-02-18 03:00:00 +03:00
|
|
|
LogPrint (eLogWarning, "Addressbook: Can't open ", indexPath);
|
2016-02-11 03:00:00 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto it: addresses) {
|
|
|
|
f << it.first << "," << it.second.ToBase32 () << std::endl;
|
|
|
|
num++;
|
2014-11-28 00:26:55 +03:00
|
|
|
}
|
2016-02-11 03:00:00 +03:00
|
|
|
LogPrint (eLogInfo, "Addressbook: ", num, " addresses saved");
|
2014-11-28 00:26:55 +03:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
2014-11-27 00:19:36 +03:00
|
|
|
//---------------------------------------------------------------------
|
2016-02-11 03:00:00 +03:00
|
|
|
AddressBook::AddressBook (): m_Storage(new AddressBookFilesystemStorage), m_IsLoaded (false), m_IsDownloading (false),
|
2014-12-23 21:57:09 +03:00
|
|
|
m_DefaultSubscription (nullptr), m_SubscriptionsUpdateTimer (nullptr)
|
2014-04-06 23:22:33 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-11-27 00:19:36 +03:00
|
|
|
AddressBook::~AddressBook ()
|
2014-12-23 21:57:09 +03:00
|
|
|
{
|
2015-03-30 17:21:52 +03:00
|
|
|
Stop ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddressBook::Start ()
|
|
|
|
{
|
2016-02-18 03:00:00 +03:00
|
|
|
m_Storage->Init();
|
2016-02-11 03:00:00 +03:00
|
|
|
LoadHosts (); /* try storage, then hosts.txt, then download */
|
2015-03-30 17:21:52 +03:00
|
|
|
StartSubscriptions ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddressBook::Stop ()
|
|
|
|
{
|
|
|
|
StopSubscriptions ();
|
|
|
|
if (m_SubscriptionsUpdateTimer)
|
|
|
|
{
|
|
|
|
delete m_SubscriptionsUpdateTimer;
|
|
|
|
m_SubscriptionsUpdateTimer = nullptr;
|
|
|
|
}
|
2014-12-21 17:33:02 +03:00
|
|
|
if (m_IsDownloading)
|
|
|
|
{
|
2016-03-11 03:46:52 +03:00
|
|
|
LogPrint (eLogInfo, "Addressbook: subscriptions is downloading, abort");
|
2014-12-21 17:33:02 +03:00
|
|
|
for (int i = 0; i < 30; i++)
|
|
|
|
{
|
|
|
|
if (!m_IsDownloading)
|
|
|
|
{
|
2016-03-11 03:46:52 +03:00
|
|
|
LogPrint (eLogInfo, "Addressbook: subscriptions download complete");
|
2014-12-21 17:33:02 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
std::this_thread::sleep_for (std::chrono::seconds (1)); // wait for 1 seconds
|
|
|
|
}
|
2016-03-11 03:46:52 +03:00
|
|
|
LogPrint (eLogError, "Addressbook: subscription download timeout");
|
2015-03-30 17:21:52 +03:00
|
|
|
m_IsDownloading = false;
|
2014-12-21 17:33:02 +03:00
|
|
|
}
|
2014-11-28 23:08:23 +03:00
|
|
|
if (m_Storage)
|
|
|
|
{
|
|
|
|
m_Storage->Save (m_Addresses);
|
|
|
|
delete m_Storage;
|
2015-03-30 17:21:52 +03:00
|
|
|
m_Storage = nullptr;
|
2014-11-28 23:08:23 +03:00
|
|
|
}
|
2016-02-16 02:20:01 +03:00
|
|
|
m_DefaultSubscription = nullptr;
|
2014-12-22 23:06:54 +03:00
|
|
|
for (auto it: m_Subscriptions)
|
|
|
|
delete it;
|
2015-03-30 17:21:52 +03:00
|
|
|
m_Subscriptions.clear ();
|
|
|
|
}
|
|
|
|
|
2014-10-24 23:22:36 +04:00
|
|
|
bool AddressBook::GetIdentHash (const std::string& address, i2p::data::IdentHash& ident)
|
2014-10-03 22:22:32 +04:00
|
|
|
{
|
|
|
|
auto pos = address.find(".b32.i2p");
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
Base32ToByteStream (address.c_str(), pos, ident, 32);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pos = address.find (".i2p");
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
auto identHash = FindAddress (address);
|
|
|
|
if (identHash)
|
|
|
|
{
|
|
|
|
ident = *identHash;
|
|
|
|
return true;
|
|
|
|
}
|
2014-12-04 22:16:33 +03:00
|
|
|
else
|
|
|
|
return false;
|
2014-10-03 22:22:32 +04:00
|
|
|
}
|
|
|
|
}
|
2014-12-04 22:16:33 +03:00
|
|
|
// if not .b32 we assume full base64 address
|
|
|
|
i2p::data::IdentityEx dest;
|
2014-12-05 03:28:20 +03:00
|
|
|
if (!dest.FromBase64 (address))
|
|
|
|
return false;
|
2014-12-04 22:16:33 +03:00
|
|
|
ident = dest.GetIdentHash ();
|
|
|
|
return true;
|
2014-10-03 22:22:32 +04:00
|
|
|
}
|
2014-08-14 22:32:00 +04:00
|
|
|
|
2014-10-24 23:22:36 +04:00
|
|
|
const i2p::data::IdentHash * AddressBook::FindAddress (const std::string& address)
|
2014-04-06 23:22:33 +04:00
|
|
|
{
|
2016-02-11 03:00:00 +03:00
|
|
|
auto it = m_Addresses.find (address);
|
|
|
|
if (it != m_Addresses.end ())
|
|
|
|
return &it->second;
|
2014-08-14 22:32:00 +04:00
|
|
|
return nullptr;
|
2014-04-06 23:22:33 +04:00
|
|
|
}
|
2014-04-01 23:18:14 +04:00
|
|
|
|
2014-09-23 23:38:56 +04:00
|
|
|
void AddressBook::InsertAddress (const std::string& address, const std::string& base64)
|
|
|
|
{
|
2015-11-03 17:15:49 +03:00
|
|
|
auto ident = std::make_shared<i2p::data::IdentityEx>();
|
|
|
|
ident->FromBase64 (base64);
|
2014-11-27 00:51:36 +03:00
|
|
|
m_Storage->AddAddress (ident);
|
2015-11-03 17:15:49 +03:00
|
|
|
m_Addresses[address] = ident->GetIdentHash ();
|
2016-01-13 02:32:32 +03:00
|
|
|
LogPrint (eLogInfo, "Addressbook: added ", address," -> ", ToAddress(ident->GetIdentHash ()));
|
2014-09-23 23:38:56 +04:00
|
|
|
}
|
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
void AddressBook::InsertAddress (std::shared_ptr<const i2p::data::IdentityEx> address)
|
2014-11-27 00:51:36 +03:00
|
|
|
{
|
|
|
|
m_Storage->AddAddress (address);
|
|
|
|
}
|
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
std::shared_ptr<const i2p::data::IdentityEx> AddressBook::GetAddress (const std::string& address)
|
2014-11-27 00:19:36 +03:00
|
|
|
{
|
2014-11-28 23:08:23 +03:00
|
|
|
i2p::data::IdentHash ident;
|
2015-11-03 17:15:49 +03:00
|
|
|
if (!GetIdentHash (address, ident)) return nullptr;
|
|
|
|
return m_Storage->GetAddress (ident);
|
2014-11-27 00:19:36 +03:00
|
|
|
}
|
|
|
|
|
2014-08-14 22:32:00 +04:00
|
|
|
void AddressBook::LoadHosts ()
|
2014-04-01 23:18:14 +04:00
|
|
|
{
|
2014-12-20 06:03:34 +03:00
|
|
|
if (m_Storage->Load (m_Addresses) > 0)
|
2014-11-28 17:40:27 +03:00
|
|
|
{
|
|
|
|
m_IsLoaded = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-11 03:00:00 +03:00
|
|
|
// then try hosts.txt
|
|
|
|
std::ifstream f (i2p::fs::DataDirPath("hosts.txt"), std::ifstream::in); // in text mode
|
2014-12-21 17:33:02 +03:00
|
|
|
if (f.is_open ())
|
2014-08-14 22:32:00 +04:00
|
|
|
{
|
2014-12-21 17:33:02 +03:00
|
|
|
LoadHostsFromStream (f);
|
|
|
|
m_IsLoaded = true;
|
|
|
|
}
|
2014-12-20 06:03:34 +03:00
|
|
|
}
|
|
|
|
|
2014-12-23 05:20:39 +03:00
|
|
|
void AddressBook::LoadHostsFromStream (std::istream& f)
|
2014-12-20 06:03:34 +03:00
|
|
|
{
|
2014-12-21 17:33:02 +03:00
|
|
|
std::unique_lock<std::mutex> l(m_AddressBookMutex);
|
2014-12-20 06:03:34 +03:00
|
|
|
int numAddresses = 0;
|
2014-08-14 22:32:00 +04:00
|
|
|
std::string s;
|
|
|
|
while (!f.eof ())
|
2014-04-01 23:18:14 +04:00
|
|
|
{
|
2014-08-14 22:32:00 +04:00
|
|
|
getline(f, s);
|
2014-04-02 00:27:40 +04:00
|
|
|
|
2014-08-14 22:32:00 +04:00
|
|
|
if (!s.length())
|
|
|
|
continue; // skip empty line
|
|
|
|
|
|
|
|
size_t pos = s.find('=');
|
|
|
|
|
|
|
|
if (pos != std::string::npos)
|
|
|
|
{
|
|
|
|
std::string name = s.substr(0, pos++);
|
|
|
|
std::string addr = s.substr(pos);
|
|
|
|
|
2015-11-03 17:15:49 +03:00
|
|
|
auto ident = std::make_shared<i2p::data::IdentityEx> ();
|
|
|
|
if (ident->FromBase64(addr))
|
2014-12-25 18:42:49 +03:00
|
|
|
{
|
2015-11-03 17:15:49 +03:00
|
|
|
m_Addresses[name] = ident->GetIdentHash ();
|
2014-12-25 18:42:49 +03:00
|
|
|
m_Storage->AddAddress (ident);
|
|
|
|
numAddresses++;
|
|
|
|
}
|
|
|
|
else
|
2016-03-11 03:46:52 +03:00
|
|
|
LogPrint (eLogError, "Addressbook: malformed address ", addr, " for ", name);
|
2014-08-14 22:32:00 +04:00
|
|
|
}
|
|
|
|
}
|
2016-03-11 03:46:52 +03:00
|
|
|
LogPrint (eLogInfo, "Addressbook: ", numAddresses, " addresses processed");
|
2014-12-21 17:33:02 +03:00
|
|
|
if (numAddresses > 0)
|
|
|
|
{
|
|
|
|
m_IsLoaded = true;
|
|
|
|
m_Storage->Save (m_Addresses);
|
|
|
|
}
|
2014-12-20 06:03:34 +03:00
|
|
|
}
|
|
|
|
|
2014-12-22 23:06:54 +03:00
|
|
|
void AddressBook::LoadSubscriptions ()
|
|
|
|
{
|
|
|
|
if (!m_Subscriptions.size ())
|
|
|
|
{
|
2016-02-11 03:00:00 +03:00
|
|
|
std::ifstream f (i2p::fs::DataDirPath ("subscriptions.txt"), std::ifstream::in); // in text mode
|
2014-12-22 23:06:54 +03:00
|
|
|
if (f.is_open ())
|
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
while (!f.eof ())
|
|
|
|
{
|
|
|
|
getline(f, s);
|
|
|
|
if (!s.length()) continue; // skip empty line
|
|
|
|
m_Subscriptions.push_back (new AddressBookSubscription (*this, s));
|
|
|
|
}
|
2016-01-18 03:00:00 +03:00
|
|
|
LogPrint (eLogInfo, "Addressbook: ", m_Subscriptions.size (), " subscriptions urls loaded");
|
2014-12-22 23:06:54 +03:00
|
|
|
}
|
2014-12-23 21:57:09 +03:00
|
|
|
else
|
2016-03-11 03:46:52 +03:00
|
|
|
LogPrint (eLogWarning, "Addressbook: subscriptions.txt not found in datadir");
|
2014-12-22 23:06:54 +03:00
|
|
|
}
|
|
|
|
else
|
2016-01-13 02:32:32 +03:00
|
|
|
LogPrint (eLogError, "Addressbook: subscriptions already loaded");
|
2014-12-22 23:06:54 +03:00
|
|
|
}
|
|
|
|
|
2014-12-23 21:57:09 +03:00
|
|
|
void AddressBook::DownloadComplete (bool success)
|
|
|
|
{
|
|
|
|
m_IsDownloading = false;
|
2016-02-16 02:20:01 +03:00
|
|
|
if (success && m_DefaultSubscription)
|
|
|
|
{
|
|
|
|
m_DefaultSubscription.reset (nullptr);
|
|
|
|
m_IsLoaded = true;
|
|
|
|
}
|
2015-01-22 00:34:50 +03:00
|
|
|
if (m_SubscriptionsUpdateTimer)
|
|
|
|
{
|
|
|
|
m_SubscriptionsUpdateTimer->expires_from_now (boost::posix_time::minutes(
|
|
|
|
success ? CONTINIOUS_SUBSCRIPTION_UPDATE_TIMEOUT : CONTINIOUS_SUBSCRIPTION_RETRY_TIMEOUT));
|
|
|
|
m_SubscriptionsUpdateTimer->async_wait (std::bind (&AddressBook::HandleSubscriptionsUpdateTimer,
|
|
|
|
this, std::placeholders::_1));
|
|
|
|
}
|
2014-12-23 21:57:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddressBook::StartSubscriptions ()
|
|
|
|
{
|
|
|
|
LoadSubscriptions ();
|
2016-02-16 02:20:01 +03:00
|
|
|
if (m_IsLoaded && m_Subscriptions.empty ()) return;
|
|
|
|
|
2014-12-23 21:57:09 +03: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
|
2016-03-11 03:46:52 +03:00
|
|
|
LogPrint (eLogError, "Addressbook: can't start subscriptions: missing shared local destination");
|
2014-12-23 21:57:09 +03: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 03:00:00 +03:00
|
|
|
if (!dest) {
|
|
|
|
LogPrint(eLogWarning, "Addressbook: missing local destination, skip subscription update");
|
|
|
|
return;
|
|
|
|
}
|
2016-02-16 02:20:01 +03:00
|
|
|
if (!m_IsDownloading && dest->IsReady ())
|
2014-12-23 21:57:09 +03:00
|
|
|
{
|
2016-02-16 02:20:01 +03:00
|
|
|
if (!m_IsLoaded)
|
|
|
|
{
|
|
|
|
// download it from http://i2p-projekt.i2p/hosts.txt
|
|
|
|
LogPrint (eLogInfo, "Addressbook: trying to download it from default subscription.");
|
|
|
|
if (!m_DefaultSubscription)
|
|
|
|
m_DefaultSubscription.reset (new AddressBookSubscription (*this, DEFAULT_SUBSCRIPTION_ADDRESS));
|
|
|
|
m_IsDownloading = true;
|
|
|
|
m_DefaultSubscription->CheckSubscription ();
|
|
|
|
}
|
|
|
|
else if (!m_Subscriptions.empty ())
|
|
|
|
{
|
|
|
|
// pick random subscription
|
|
|
|
auto ind = rand () % m_Subscriptions.size();
|
|
|
|
m_IsDownloading = true;
|
|
|
|
m_Subscriptions[ind]->CheckSubscription ();
|
|
|
|
}
|
2014-12-23 21:57:09 +03: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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 22:40:02 +03:00
|
|
|
AddressBookSubscription::AddressBookSubscription (AddressBook& book, const std::string& link):
|
|
|
|
m_Book (book), m_Link (link)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddressBookSubscription::CheckSubscription ()
|
|
|
|
{
|
|
|
|
std::thread load_hosts(&AddressBookSubscription::Request, this);
|
2014-12-21 17:33:02 +03:00
|
|
|
load_hosts.detach(); // TODO: use join
|
2014-12-19 22:40:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddressBookSubscription::Request ()
|
|
|
|
{
|
|
|
|
// must be run in separate thread
|
2016-03-11 03:46:52 +03:00
|
|
|
LogPrint (eLogInfo, "Addressbook: Downloading hosts database from ", m_Link, " ETag: ", m_Etag, " Last-Modified: ", m_LastModified);
|
2014-12-23 21:57:09 +03:00
|
|
|
bool success = false;
|
2014-12-19 22:40:02 +03:00
|
|
|
i2p::util::http::url u (m_Link);
|
|
|
|
i2p::data::IdentHash ident;
|
|
|
|
if (m_Book.GetIdentHash (u.host_, ident))
|
|
|
|
{
|
2014-12-27 03:09:44 +03:00
|
|
|
std::condition_variable newDataReceived;
|
|
|
|
std::mutex newDataReceivedMutex;
|
2015-01-27 19:27:58 +03:00
|
|
|
auto leaseSet = i2p::client::context.GetSharedLocalDestination ()->FindLeaseSet (ident);
|
2014-12-19 22:40:02 +03:00
|
|
|
if (!leaseSet)
|
|
|
|
{
|
2014-12-27 03:09:44 +03:00
|
|
|
std::unique_lock<std::mutex> l(newDataReceivedMutex);
|
|
|
|
i2p::client::context.GetSharedLocalDestination ()->RequestDestination (ident,
|
2015-04-07 19:02:25 +03:00
|
|
|
[&newDataReceived, &leaseSet](std::shared_ptr<i2p::data::LeaseSet> ls)
|
2014-12-27 03:09:44 +03:00
|
|
|
{
|
2015-04-07 19:02:25 +03:00
|
|
|
leaseSet = ls;
|
2014-12-27 03:09:44 +03:00
|
|
|
newDataReceived.notify_all ();
|
|
|
|
});
|
2014-12-28 23:45:58 +03:00
|
|
|
if (newDataReceived.wait_for (l, std::chrono::seconds (SUBSCRIPTION_REQUEST_TIMEOUT)) == std::cv_status::timeout)
|
2015-12-13 22:40:43 +03:00
|
|
|
{
|
2016-02-11 03:00:00 +03:00
|
|
|
LogPrint (eLogError, "Addressbook: Subscription LeaseSet request timeout expired");
|
2015-12-13 22:40:43 +03:00
|
|
|
i2p::client::context.GetSharedLocalDestination ()->CancelDestinationRequest (ident);
|
|
|
|
}
|
2014-12-19 22:40:02 +03:00
|
|
|
}
|
|
|
|
if (leaseSet)
|
|
|
|
{
|
|
|
|
std::stringstream request, response;
|
2014-12-22 23:06:54 +03:00
|
|
|
// standard header
|
2016-01-13 02:34:59 +03:00
|
|
|
request << "GET " << u.path_ << " HTTP/1.1\r\n"
|
|
|
|
<< "Host: " << u.host_ << "\r\n"
|
|
|
|
<< "Accept: */*\r\n"
|
|
|
|
<< "User-Agent: Wget/1.11.4\r\n"
|
2016-02-18 21:19:31 +03:00
|
|
|
//<< "Accept-Encoding: gzip\r\n"
|
2016-02-19 04:35:14 +03:00
|
|
|
<< "X-Accept-Encoding: x-i2p-gzip;q=1.0, identity;q=0.5, deflate;q=0, gzip;q=0, *;q=0\r\n"
|
2016-01-13 02:34:59 +03:00
|
|
|
<< "Connection: close\r\n";
|
2014-12-22 23:06:54 +03:00
|
|
|
if (m_Etag.length () > 0) // etag
|
2014-12-24 17:45:25 +03:00
|
|
|
request << i2p::util::http::IF_NONE_MATCH << ": \"" << m_Etag << "\"\r\n";
|
2014-12-22 23:06:54 +03:00
|
|
|
if (m_LastModified.length () > 0) // if-modfief-since
|
2014-12-23 00:45:50 +03:00
|
|
|
request << i2p::util::http::IF_MODIFIED_SINCE << ": " << m_LastModified << "\r\n";
|
2014-12-22 23:06:54 +03:00
|
|
|
request << "\r\n"; // end of header
|
2015-01-27 19:27:58 +03:00
|
|
|
auto stream = i2p::client::context.GetSharedLocalDestination ()->CreateStream (leaseSet, u.port_);
|
2014-12-19 22:40:02 +03:00
|
|
|
stream->Send ((uint8_t *)request.str ().c_str (), request.str ().length ());
|
|
|
|
|
2015-03-30 04:26:05 +03:00
|
|
|
uint8_t buf[4096];
|
2014-12-19 22:40:02 +03:00
|
|
|
bool end = false;
|
|
|
|
while (!end)
|
|
|
|
{
|
|
|
|
stream->AsyncReceive (boost::asio::buffer (buf, 4096),
|
|
|
|
[&](const boost::system::error_code& ecode, std::size_t bytes_transferred)
|
|
|
|
{
|
2014-12-25 18:42:49 +03:00
|
|
|
if (bytes_transferred)
|
2014-12-19 22:40:02 +03:00
|
|
|
response.write ((char *)buf, bytes_transferred);
|
2014-12-26 16:55:24 +03:00
|
|
|
if (ecode == boost::asio::error::timed_out || !stream->IsOpen ())
|
|
|
|
end = true;
|
2014-12-25 18:42:49 +03:00
|
|
|
newDataReceived.notify_all ();
|
2014-12-19 22:40:02 +03:00
|
|
|
},
|
|
|
|
30); // wait for 30 seconds
|
2014-12-25 18:42:49 +03:00
|
|
|
std::unique_lock<std::mutex> l(newDataReceivedMutex);
|
2014-12-28 23:45:58 +03:00
|
|
|
if (newDataReceived.wait_for (l, std::chrono::seconds (SUBSCRIPTION_REQUEST_TIMEOUT)) == std::cv_status::timeout)
|
2016-03-11 03:46:52 +03:00
|
|
|
LogPrint (eLogError, "Addressbook: subscriptions request timeout expired");
|
2014-12-19 22:40:02 +03:00
|
|
|
}
|
2014-12-26 16:55:24 +03:00
|
|
|
// process remaining buffer
|
|
|
|
while (size_t len = stream->ReadSome (buf, 4096))
|
|
|
|
response.write ((char *)buf, len);
|
|
|
|
|
2014-12-20 06:03:34 +03:00
|
|
|
// parse response
|
|
|
|
std::string version;
|
|
|
|
response >> version; // HTTP version
|
|
|
|
int status = 0;
|
|
|
|
response >> status; // status
|
|
|
|
if (status == 200) // OK
|
|
|
|
{
|
2016-02-18 21:19:31 +03:00
|
|
|
bool isChunked = false, isGzip = false;
|
2014-12-20 06:03:34 +03:00
|
|
|
std::string header, statusMessage;
|
|
|
|
std::getline (response, statusMessage);
|
|
|
|
// read until new line meaning end of header
|
|
|
|
while (!response.eof () && header != "\r")
|
2014-12-23 00:45:50 +03:00
|
|
|
{
|
2014-12-20 06:03:34 +03:00
|
|
|
std::getline (response, header);
|
2014-12-23 00:45:50 +03:00
|
|
|
auto colon = header.find (':');
|
|
|
|
if (colon != std::string::npos)
|
|
|
|
{
|
|
|
|
std::string field = header.substr (0, colon);
|
2016-02-19 18:04:52 +03:00
|
|
|
boost::to_lower (field); // field are not case-sensitive
|
2016-02-19 04:35:14 +03:00
|
|
|
colon++;
|
2014-12-23 00:45:50 +03:00
|
|
|
header.resize (header.length () - 1); // delete \r
|
|
|
|
if (field == i2p::util::http::ETAG)
|
|
|
|
m_Etag = header.substr (colon + 1);
|
|
|
|
else if (field == i2p::util::http::LAST_MODIFIED)
|
|
|
|
m_LastModified = header.substr (colon + 1);
|
|
|
|
else if (field == i2p::util::http::TRANSFER_ENCODING)
|
|
|
|
isChunked = !header.compare (colon + 1, std::string::npos, "chunked");
|
2016-02-18 21:19:31 +03:00
|
|
|
else if (field == i2p::util::http::CONTENT_ENCODING)
|
2016-02-19 18:04:52 +03:00
|
|
|
isGzip = !header.compare (colon + 1, std::string::npos, "gzip") ||
|
|
|
|
!header.compare (colon + 1, std::string::npos, "x-i2p-gzip");
|
2014-12-23 00:45:50 +03:00
|
|
|
}
|
|
|
|
}
|
2016-01-13 02:32:32 +03:00
|
|
|
LogPrint (eLogInfo, "Addressbook: ", m_Link, " ETag: ", m_Etag, " Last-Modified: ", m_LastModified);
|
2014-12-21 17:33:02 +03:00
|
|
|
if (!response.eof ())
|
2014-12-23 05:20:39 +03:00
|
|
|
{
|
2014-12-23 21:57:09 +03:00
|
|
|
success = true;
|
2014-12-23 05:20:39 +03:00
|
|
|
if (!isChunked)
|
2016-02-18 21:19:31 +03:00
|
|
|
success = ProcessResponse (response, isGzip);
|
2014-12-23 05:20:39 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// merge chunks
|
|
|
|
std::stringstream merged;
|
|
|
|
i2p::util::http::MergeChunkedResponse (response, merged);
|
2016-02-18 21:19:31 +03:00
|
|
|
success = ProcessResponse (merged, isGzip);
|
2014-12-23 05:20:39 +03:00
|
|
|
}
|
|
|
|
}
|
2014-12-20 06:03:34 +03:00
|
|
|
}
|
2014-12-24 15:48:18 +03:00
|
|
|
else if (status == 304)
|
|
|
|
{
|
|
|
|
success = true;
|
2016-01-13 02:32:32 +03:00
|
|
|
LogPrint (eLogInfo, "Addressbook: no updates from ", m_Link);
|
2014-12-24 15:48:18 +03:00
|
|
|
}
|
2014-12-20 06:03:34 +03:00
|
|
|
else
|
2016-01-13 02:32:32 +03:00
|
|
|
LogPrint (eLogWarning, "Adressbook: HTTP response ", status);
|
2014-12-19 22:40:02 +03:00
|
|
|
}
|
|
|
|
else
|
2016-01-13 02:32:32 +03:00
|
|
|
LogPrint (eLogError, "Addressbook: address ", u.host_, " not found");
|
2014-12-19 22:40:02 +03:00
|
|
|
}
|
|
|
|
else
|
2016-01-13 02:32:32 +03:00
|
|
|
LogPrint (eLogError, "Addressbook: Can't resolve ", u.host_);
|
2015-12-18 07:05:52 +03:00
|
|
|
|
|
|
|
if (!success)
|
2016-02-11 03:00:00 +03:00
|
|
|
LogPrint (eLogError, "Addressbook: download hosts.txt from ", m_Link, " failed");
|
2015-12-18 07:05:52 +03:00
|
|
|
|
2014-12-23 21:57:09 +03:00
|
|
|
m_Book.DownloadComplete (success);
|
2014-12-19 22:40:02 +03:00
|
|
|
}
|
2016-02-18 21:19:31 +03:00
|
|
|
|
|
|
|
bool AddressBookSubscription::ProcessResponse (std::stringstream& s, bool isGzip)
|
|
|
|
{
|
|
|
|
if (isGzip)
|
|
|
|
{
|
|
|
|
std::stringstream uncompressed;
|
|
|
|
i2p::data::GzipInflator inflator;
|
|
|
|
inflator.Inflate (s, uncompressed);
|
|
|
|
if (!uncompressed.fail ())
|
|
|
|
m_Book.LoadHostsFromStream (uncompressed);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_Book.LoadHostsFromStream (s);
|
|
|
|
return true;
|
|
|
|
}
|
2014-04-01 23:18:14 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|