mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
196 lines
5.5 KiB
196 lines
5.5 KiB
![]()
9 years ago
|
#include <sys/stat.h>
|
||
![]()
10 years ago
|
#include <boost/property_tree/ptree.hpp>
|
||
|
#include <boost/property_tree/ini_parser.hpp>
|
||
![]()
9 years ago
|
#include "Base.h"
|
||
![]()
9 years ago
|
#include "FS.h"
|
||
![]()
9 years ago
|
#include "Log.h"
|
||
![]()
10 years ago
|
#include "Profiling.h"
|
||
|
|
||
|
namespace i2p
|
||
|
{
|
||
|
namespace data
|
||
|
{
|
||
![]()
9 years ago
|
i2p::fs::HashedStorage m_ProfilesStorage("peerProfiles", "p", "profile-", "txt");
|
||
|
|
||
![]()
8 years ago
|
RouterProfile::RouterProfile ():
|
||
|
m_LastUpdateTime (boost::posix_time::second_clock::local_time()),
|
||
![]()
9 years ago
|
m_NumTunnelsAgreed (0), m_NumTunnelsDeclined (0), m_NumTunnelsNonReplied (0),
|
||
|
m_NumTimesTaken (0), m_NumTimesRejected (0)
|
||
![]()
10 years ago
|
{
|
||
|
}
|
||
![]()
10 years ago
|
|
||
![]()
10 years ago
|
boost::posix_time::ptime RouterProfile::GetTime () const
|
||
|
{
|
||
|
return boost::posix_time::second_clock::local_time();
|
||
|
}
|
||
|
|
||
![]()
10 years ago
|
void RouterProfile::UpdateTime ()
|
||
|
{
|
||
![]()
10 years ago
|
m_LastUpdateTime = GetTime ();
|
||
![]()
10 years ago
|
}
|
||
![]()
10 years ago
|
|
||
![]()
8 years ago
|
void RouterProfile::Save (const IdentHash& identHash)
|
||
![]()
10 years ago
|
{
|
||
|
// fill sections
|
||
|
boost::property_tree::ptree participation;
|
||
|
participation.put (PEER_PROFILE_PARTICIPATION_AGREED, m_NumTunnelsAgreed);
|
||
|
participation.put (PEER_PROFILE_PARTICIPATION_DECLINED, m_NumTunnelsDeclined);
|
||
![]()
10 years ago
|
participation.put (PEER_PROFILE_PARTICIPATION_NON_REPLIED, m_NumTunnelsNonReplied);
|
||
![]()
9 years ago
|
boost::property_tree::ptree usage;
|
||
|
usage.put (PEER_PROFILE_USAGE_TAKEN, m_NumTimesTaken);
|
||
|
usage.put (PEER_PROFILE_USAGE_REJECTED, m_NumTimesRejected);
|
||
![]()
10 years ago
|
// fill property tree
|
||
|
boost::property_tree::ptree pt;
|
||
![]()
10 years ago
|
pt.put (PEER_PROFILE_LAST_UPDATE_TIME, boost::posix_time::to_simple_string (m_LastUpdateTime));
|
||
![]()
10 years ago
|
pt.put_child (PEER_PROFILE_SECTION_PARTICIPATION, participation);
|
||
![]()
9 years ago
|
pt.put_child (PEER_PROFILE_SECTION_USAGE, usage);
|
||
![]()
9 years ago
|
|
||
![]()
10 years ago
|
// save to file
|
||
![]()
8 years ago
|
std::string ident = identHash.ToBase64 ();
|
||
![]()
9 years ago
|
std::string path = m_ProfilesStorage.Path(ident);
|
||
![]()
9 years ago
|
|
||
|
try {
|
||
|
boost::property_tree::write_ini (path, pt);
|
||
|
} catch (std::exception& ex) {
|
||
|
/* boost exception verbose enough */
|
||
|
LogPrint (eLogError, "Profiling: ", ex.what ());
|
||
![]()
10 years ago
|
}
|
||
![]()
9 years ago
|
}
|
||
![]()
10 years ago
|
|
||
![]()
8 years ago
|
void RouterProfile::Load (const IdentHash& identHash)
|
||
![]()
10 years ago
|
{
|
||
![]()
8 years ago
|
std::string ident = identHash.ToBase64 ();
|
||
![]()
9 years ago
|
std::string path = m_ProfilesStorage.Path(ident);
|
||
![]()
9 years ago
|
boost::property_tree::ptree pt;
|
||
|
|
||
![]()
8 years ago
|
if (!i2p::fs::Exists(path))
|
||
|
{
|
||
![]()
9 years ago
|
LogPrint(eLogWarning, "Profiling: no profile yet for ", ident);
|
||
|
return;
|
||
|
}
|
||
|
|
||
![]()
8 years ago
|
try
|
||
|
{
|
||
![]()
9 years ago
|
boost::property_tree::read_ini (path, pt);
|
||
![]()
8 years ago
|
} catch (std::exception& ex)
|
||
|
{
|
||
![]()
9 years ago
|
/* boost exception verbose enough */
|
||
|
LogPrint (eLogError, "Profiling: ", ex.what ());
|
||
|
return;
|
||
|
}
|
||
|
|
||
![]()
8 years ago
|
try
|
||
|
{
|
||
![]()
9 years ago
|
auto t = pt.get (PEER_PROFILE_LAST_UPDATE_TIME, "");
|
||
|
if (t.length () > 0)
|
||
|
m_LastUpdateTime = boost::posix_time::time_from_string (t);
|
||
![]()
8 years ago
|
if ((GetTime () - m_LastUpdateTime).hours () < PEER_PROFILE_EXPIRATION_TIMEOUT)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
![]()
9 years ago
|
// 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);
|
||
![]()
8 years ago
|
}
|
||
|
catch (boost::property_tree::ptree_bad_path& ex)
|
||
|
{
|
||
![]()
9 years ago
|
LogPrint (eLogWarning, "Profiling: Missing section ", PEER_PROFILE_SECTION_PARTICIPATION, " in profile for ", ident);
|
||
![]()
10 years ago
|
}
|
||
![]()
8 years ago
|
try
|
||
|
{
|
||
![]()
9 years ago
|
// read usage
|
||
|
auto usage = pt.get_child (PEER_PROFILE_SECTION_USAGE);
|
||
|
m_NumTimesTaken = usage.get (PEER_PROFILE_USAGE_TAKEN, 0);
|
||
|
m_NumTimesRejected = usage.get (PEER_PROFILE_USAGE_REJECTED, 0);
|
||
![]()
8 years ago
|
}
|
||
|
catch (boost::property_tree::ptree_bad_path& ex)
|
||
|
{
|
||
![]()
9 years ago
|
LogPrint (eLogWarning, "Missing section ", PEER_PROFILE_SECTION_USAGE, " in profile for ", ident);
|
||
|
}
|
||
![]()
8 years ago
|
}
|
||
|
else
|
||
|
*this = RouterProfile ();
|
||
|
}
|
||
|
catch (std::exception& ex)
|
||
|
{
|
||
![]()
9 years ago
|
LogPrint (eLogError, "Profiling: Can't read profile ", ident, " :", ex.what ());
|
||
|
}
|
||
|
}
|
||
|
|
||
![]()
10 years ago
|
void RouterProfile::TunnelBuildResponse (uint8_t ret)
|
||
|
{
|
||
![]()
9 years ago
|
UpdateTime ();
|
||
![]()
10 years ago
|
if (ret > 0)
|
||
|
m_NumTunnelsDeclined++;
|
||
|
else
|
||
|
m_NumTunnelsAgreed++;
|
||
|
}
|
||
![]()
10 years ago
|
|
||
|
void RouterProfile::TunnelNonReplied ()
|
||
|
{
|
||
|
m_NumTunnelsNonReplied++;
|
||
![]()
10 years ago
|
UpdateTime ();
|
||
![]()
10 years ago
|
}
|
||
![]()
10 years ago
|
|
||
![]()
9 years ago
|
bool RouterProfile::IsLowPartcipationRate () const
|
||
![]()
10 years ago
|
{
|
||
![]()
9 years ago
|
return 4*m_NumTunnelsAgreed < m_NumTunnelsDeclined; // < 20% rate
|
||
![]()
10 years ago
|
}
|
||
![]()
10 years ago
|
|
||
![]()
9 years ago
|
bool RouterProfile::IsLowReplyRate () const
|
||
![]()
10 years ago
|
{
|
||
|
auto total = m_NumTunnelsAgreed + m_NumTunnelsDeclined;
|
||
![]()
9 years ago
|
return m_NumTunnelsNonReplied > 10*(total + 1);
|
||
![]()
10 years ago
|
}
|
||
![]()
10 years ago
|
|
||
![]()
9 years ago
|
bool RouterProfile::IsBad ()
|
||
![]()
10 years ago
|
{
|
||
![]()
9 years ago
|
auto isBad = IsAlwaysDeclining () || IsLowPartcipationRate () /*|| IsLowReplyRate ()*/;
|
||
|
if (isBad && m_NumTimesRejected > 10*(m_NumTimesTaken + 1))
|
||
![]()
9 years ago
|
{
|
||
![]()
9 years ago
|
// reset profile
|
||
|
m_NumTunnelsAgreed = 0;
|
||
|
m_NumTunnelsDeclined = 0;
|
||
|
m_NumTunnelsNonReplied = 0;
|
||
|
isBad = false;
|
||
|
}
|
||
![]()
9 years ago
|
if (isBad) m_NumTimesRejected++; else m_NumTimesTaken++;
|
||
|
return isBad;
|
||
![]()
10 years ago
|
}
|
||
![]()
10 years ago
|
|
||
|
std::shared_ptr<RouterProfile> GetRouterProfile (const IdentHash& identHash)
|
||
![]()
10 years ago
|
{
|
||
![]()
8 years ago
|
auto profile = std::make_shared<RouterProfile> ();
|
||
|
profile->Load (identHash); // if possible
|
||
![]()
10 years ago
|
return profile;
|
||
![]()
10 years ago
|
}
|
||
![]()
10 years ago
|
|
||
![]()
9 years ago
|
void InitProfilesStorage ()
|
||
|
{
|
||
|
m_ProfilesStorage.SetPlace(i2p::fs::GetDataDir());
|
||
|
m_ProfilesStorage.Init(i2p::data::GetBase64SubstitutionTable(), 64);
|
||
|
}
|
||
|
|
||
![]()
10 years ago
|
void DeleteObsoleteProfiles ()
|
||
|
{
|
||
![]()
9 years ago
|
struct stat st;
|
||
|
std::time_t now = std::time(nullptr);
|
||
|
|
||
|
std::vector<std::string> files;
|
||
![]()
9 years ago
|
m_ProfilesStorage.Traverse(files);
|
||
![]()
8 years ago
|
for (const auto& path: files) {
|
||
![]()
9 years ago
|
if (stat(path.c_str(), &st) != 0) {
|
||
|
LogPrint(eLogWarning, "Profiling: Can't stat(): ", path);
|
||
|
continue;
|
||
|
}
|
||
|
if (((now - st.st_mtime) / 3600) >= PEER_PROFILE_EXPIRATION_TIMEOUT) {
|
||
|
LogPrint(eLogDebug, "Profiling: removing expired peer profile: ", path);
|
||
|
i2p::fs::Remove(path);
|
||
|
}
|
||
![]()
10 years ago
|
}
|
||
![]()
9 years ago
|
}
|
||
![]()
10 years ago
|
}
|
||
![]()
10 years ago
|
}
|