2015-03-25 01:48:16 +03:00
|
|
|
#include <boost/filesystem.hpp>
|
2015-03-24 19:47:57 +03:00
|
|
|
#include <boost/property_tree/ptree.hpp>
|
|
|
|
#include <boost/property_tree/ini_parser.hpp>
|
2015-03-25 01:48:16 +03:00
|
|
|
#include "base64.h"
|
|
|
|
#include "util.h"
|
2015-03-24 19:47:57 +03:00
|
|
|
#include "Profiling.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace data
|
|
|
|
{
|
|
|
|
RouterProfile::RouterProfile (const IdentHash& identHash):
|
2015-03-31 04:05:04 +03:00
|
|
|
m_IdentHash (identHash), m_LastUpdateTime (boost::posix_time::second_clock::local_time()),
|
|
|
|
m_NumTunnelsAgreed (0), m_NumTunnelsDeclined (0),
|
2015-03-28 23:09:34 +03:00
|
|
|
m_NumTunnelsNonReplied (0)
|
2015-03-24 19:47:57 +03:00
|
|
|
{
|
|
|
|
}
|
2015-03-31 04:05:04 +03:00
|
|
|
|
2015-04-01 00:13:01 +03:00
|
|
|
boost::posix_time::ptime RouterProfile::GetTime () const
|
|
|
|
{
|
|
|
|
return boost::posix_time::second_clock::local_time();
|
|
|
|
}
|
|
|
|
|
2015-03-31 04:05:04 +03:00
|
|
|
void RouterProfile::UpdateTime ()
|
|
|
|
{
|
2015-04-01 00:13:01 +03:00
|
|
|
m_LastUpdateTime = GetTime ();
|
2015-03-31 04:05:04 +03:00
|
|
|
}
|
2015-03-25 01:48:16 +03:00
|
|
|
|
|
|
|
void RouterProfile::Save ()
|
|
|
|
{
|
|
|
|
// fill sections
|
|
|
|
boost::property_tree::ptree participation;
|
|
|
|
participation.put (PEER_PROFILE_PARTICIPATION_AGREED, m_NumTunnelsAgreed);
|
|
|
|
participation.put (PEER_PROFILE_PARTICIPATION_DECLINED, m_NumTunnelsDeclined);
|
2015-03-28 23:09:34 +03:00
|
|
|
participation.put (PEER_PROFILE_PARTICIPATION_NON_REPLIED, m_NumTunnelsNonReplied);
|
2015-03-25 01:48:16 +03:00
|
|
|
// fill property tree
|
|
|
|
boost::property_tree::ptree pt;
|
2015-03-31 04:05:04 +03:00
|
|
|
pt.put (PEER_PROFILE_LAST_UPDATE_TIME, boost::posix_time::to_simple_string (m_LastUpdateTime));
|
2015-03-25 01:48:16 +03:00
|
|
|
pt.put_child (PEER_PROFILE_SECTION_PARTICIPATION, participation);
|
|
|
|
|
|
|
|
// save to file
|
|
|
|
auto path = i2p::util::filesystem::GetDefaultDataDir() / PEER_PROFILES_DIRECTORY;
|
|
|
|
if (!boost::filesystem::exists (path))
|
|
|
|
{
|
|
|
|
// Create directory is necessary
|
|
|
|
if (!boost::filesystem::create_directory (path))
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Failed to create directory ", path);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const char * chars = GetBase64SubstitutionTable (); // 64 bytes
|
|
|
|
for (int i = 0; i < 64; i++)
|
|
|
|
{
|
|
|
|
auto path1 = path / (std::string ("p") + chars[i]);
|
|
|
|
if (!boost::filesystem::create_directory (path1))
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Failed to create directory ", path1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::string base64 = m_IdentHash.ToBase64 ();
|
|
|
|
path = path / (std::string ("p") + base64[0]);
|
|
|
|
auto filename = path / (std::string (PEER_PROFILE_PREFIX) + base64 + ".txt");
|
|
|
|
try
|
|
|
|
{
|
|
|
|
boost::property_tree::write_ini (filename.string (), pt);
|
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Can't write ", filename, ": ", ex.what ());
|
|
|
|
}
|
|
|
|
}
|
2015-03-25 15:45:50 +03:00
|
|
|
|
|
|
|
void RouterProfile::Load ()
|
|
|
|
{
|
|
|
|
std::string base64 = m_IdentHash.ToBase64 ();
|
|
|
|
auto path = i2p::util::filesystem::GetDefaultDataDir() / PEER_PROFILES_DIRECTORY;
|
|
|
|
path /= std::string ("p") + base64[0];
|
|
|
|
auto filename = path / (std::string (PEER_PROFILE_PREFIX) + base64 + ".txt");
|
|
|
|
if (boost::filesystem::exists (filename))
|
|
|
|
{
|
|
|
|
boost::property_tree::ptree pt;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
boost::property_tree::read_ini (filename.string (), pt);
|
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Can't read ", filename, ": ", ex.what ());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
2015-03-31 04:05:04 +03:00
|
|
|
auto t = pt.get (PEER_PROFILE_LAST_UPDATE_TIME, "");
|
|
|
|
if (t.length () > 0)
|
|
|
|
m_LastUpdateTime = boost::posix_time::time_from_string (t);
|
2015-04-11 22:39:23 +03:00
|
|
|
if ((GetTime () - m_LastUpdateTime).hours () < PEER_PROFILE_EXPIRATION_TIMEOUT)
|
2015-04-01 00:13:01 +03:00
|
|
|
{
|
|
|
|
// read participations
|
|
|
|
auto participations = pt.get_child (PEER_PROFILE_SECTION_PARTICIPATION);
|
|
|
|
m_NumTunnelsAgreed = participations.get (PEER_PROFILE_PARTICIPATION_AGREED, 0);
|
|
|
|
m_NumTunnelsDeclined = participations.get (PEER_PROFILE_PARTICIPATION_DECLINED, 0);
|
|
|
|
m_NumTunnelsNonReplied = participations.get (PEER_PROFILE_PARTICIPATION_NON_REPLIED, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*this = RouterProfile (m_IdentHash);
|
2015-03-25 15:45:50 +03:00
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
|
|
|
LogPrint (eLogError, "Can't read profile ", base64, " :", ex.what ());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-25 01:48:16 +03:00
|
|
|
|
|
|
|
void RouterProfile::TunnelBuildResponse (uint8_t ret)
|
|
|
|
{
|
|
|
|
if (ret > 0)
|
|
|
|
m_NumTunnelsDeclined++;
|
|
|
|
else
|
|
|
|
m_NumTunnelsAgreed++;
|
2015-03-31 04:05:04 +03:00
|
|
|
UpdateTime ();
|
2015-03-25 01:48:16 +03:00
|
|
|
}
|
2015-03-28 23:09:34 +03:00
|
|
|
|
|
|
|
void RouterProfile::TunnelNonReplied ()
|
|
|
|
{
|
|
|
|
m_NumTunnelsNonReplied++;
|
2015-03-31 04:05:04 +03:00
|
|
|
UpdateTime ();
|
2015-03-28 23:09:34 +03:00
|
|
|
}
|
2015-04-01 00:13:01 +03:00
|
|
|
|
|
|
|
bool RouterProfile::IsLowPartcipationRate () const
|
|
|
|
{
|
|
|
|
if ((GetTime () - m_LastUpdateTime).total_seconds () < 900) // if less than 15 minutes
|
|
|
|
return m_NumTunnelsAgreed < m_NumTunnelsDeclined; // 50% rate
|
|
|
|
else
|
2015-04-11 22:39:23 +03:00
|
|
|
return 3*m_NumTunnelsAgreed < m_NumTunnelsDeclined; // 25% rate
|
2015-04-01 00:13:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RouterProfile::IsBad () const
|
|
|
|
{
|
|
|
|
return IsAlwaysDeclining () || IsNonResponding () || IsLowPartcipationRate ();
|
|
|
|
}
|
2015-03-25 01:48:16 +03:00
|
|
|
|
|
|
|
std::shared_ptr<RouterProfile> GetRouterProfile (const IdentHash& identHash)
|
2015-03-24 19:47:57 +03:00
|
|
|
{
|
2015-03-25 15:45:50 +03:00
|
|
|
auto profile = std::make_shared<RouterProfile> (identHash);
|
|
|
|
profile->Load (); // if possible
|
|
|
|
return profile;
|
2015-03-24 19:47:57 +03:00
|
|
|
}
|
2015-04-11 22:39:23 +03:00
|
|
|
|
|
|
|
void DeleteObsoleteProfiles ()
|
|
|
|
{
|
|
|
|
int num = 0;
|
|
|
|
auto ts = boost::posix_time::second_clock::local_time();
|
|
|
|
boost::filesystem::path p (i2p::util::filesystem::GetDataDir()/PEER_PROFILES_DIRECTORY);
|
|
|
|
if (boost::filesystem::exists (p))
|
|
|
|
{
|
|
|
|
boost::filesystem::directory_iterator end;
|
|
|
|
for (boost::filesystem::directory_iterator it (p); it != end; ++it)
|
|
|
|
{
|
|
|
|
if (boost::filesystem::is_directory (it->status()))
|
|
|
|
{
|
|
|
|
for (boost::filesystem::directory_iterator it1 (it->path ()); it1 != end; ++it1)
|
|
|
|
{
|
|
|
|
auto lastModified = boost::posix_time::from_time_t (boost::filesystem::last_write_time (it1->path ()));
|
|
|
|
if ((ts - lastModified).hours () >= PEER_PROFILE_EXPIRATION_TIMEOUT)
|
|
|
|
{
|
|
|
|
boost::filesystem::remove (it1->path ());
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LogPrint (eLogInfo, num, " obsolete profiles deleted");
|
|
|
|
}
|
2015-03-24 19:47:57 +03:00
|
|
|
}
|
|
|
|
}
|