2021-10-08 19:08:23 -04:00
|
|
|
#include "util/rsdebug.h"
|
2021-11-01 04:16:41 -04:00
|
|
|
#include "util/rsprint.h"
|
|
|
|
#include "util/rsdir.h"
|
|
|
|
#include "util/rsbase64.h"
|
|
|
|
#include "util/radix64.h"
|
2021-10-08 19:08:23 -04:00
|
|
|
|
2021-11-10 17:36:23 -05:00
|
|
|
#include "pgp/pgpkeyutil.h"
|
2021-11-01 05:47:07 -04:00
|
|
|
#include "pgp/rscertificate.h"
|
|
|
|
|
2021-10-08 19:08:23 -04:00
|
|
|
#include "friendserver.h"
|
2021-10-24 14:01:19 -04:00
|
|
|
#include "friend_server/fsitem.h"
|
2021-10-08 19:08:23 -04:00
|
|
|
|
2021-10-31 13:00:43 -04:00
|
|
|
static const rstime_t MAXIMUM_PEER_INACTIVE_DELAY = 600;
|
|
|
|
static const rstime_t DELAY_BETWEEN_TWO_AUTOWASH = 60;
|
|
|
|
static const rstime_t DELAY_BETWEEN_TWO_DEBUG_PRINT = 10;
|
2021-11-05 11:52:00 -04:00
|
|
|
static const uint32_t MAXIMUM_PEERS_TO_REQUEST = 10;
|
2021-10-31 13:00:43 -04:00
|
|
|
|
2021-10-08 19:08:23 -04:00
|
|
|
void FriendServer::threadTick()
|
|
|
|
{
|
|
|
|
// Listen to the network interface, capture incoming data etc.
|
|
|
|
|
2021-10-11 17:40:22 -04:00
|
|
|
RsItem *item;
|
|
|
|
|
2021-10-19 17:24:50 -04:00
|
|
|
while(nullptr != (item = mni->GetItem()))
|
2021-10-11 17:40:22 -04:00
|
|
|
{
|
|
|
|
RsFriendServerItem *fsitem = dynamic_cast<RsFriendServerItem*>(item);
|
|
|
|
|
|
|
|
if(!fsitem)
|
|
|
|
{
|
|
|
|
RsErr() << "Received an item of the wrong type!" ;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2021-10-19 17:24:50 -04:00
|
|
|
std::cerr << "Received item: " << std::endl << *fsitem << std::endl;
|
2021-10-11 17:40:22 -04:00
|
|
|
|
|
|
|
switch(fsitem->PacketSubType())
|
|
|
|
{
|
|
|
|
case RS_PKT_SUBTYPE_FS_CLIENT_PUBLISH: handleClientPublish(dynamic_cast<RsFriendServerClientPublishItem*>(fsitem));
|
|
|
|
break;
|
2021-11-10 17:36:23 -05:00
|
|
|
case RS_PKT_SUBTYPE_FS_CLIENT_REMOVE: handleClientRemove(dynamic_cast<RsFriendServerClientRemoveItem*>(fsitem));
|
|
|
|
break;
|
2021-10-11 17:40:22 -04:00
|
|
|
default: ;
|
|
|
|
}
|
|
|
|
delete item;
|
|
|
|
}
|
2021-11-04 09:24:19 -04:00
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
2021-10-31 13:00:43 -04:00
|
|
|
|
|
|
|
static rstime_t last_autowash_TS = time(nullptr);
|
|
|
|
rstime_t now = time(nullptr);
|
|
|
|
|
|
|
|
if(last_autowash_TS + DELAY_BETWEEN_TWO_AUTOWASH < now)
|
|
|
|
{
|
|
|
|
last_autowash_TS = now;
|
|
|
|
autoWash();
|
|
|
|
}
|
|
|
|
|
|
|
|
static rstime_t last_debugprint_TS = time(nullptr);
|
|
|
|
|
|
|
|
if(last_debugprint_TS + DELAY_BETWEEN_TWO_DEBUG_PRINT < now)
|
|
|
|
{
|
|
|
|
last_debugprint_TS = now;
|
|
|
|
debugPrint();
|
|
|
|
}
|
2021-10-08 19:08:23 -04:00
|
|
|
}
|
|
|
|
|
2021-10-11 17:40:22 -04:00
|
|
|
void FriendServer::handleClientPublish(const RsFriendServerClientPublishItem *item)
|
|
|
|
{
|
2021-11-07 09:16:24 -05:00
|
|
|
// We always respond with exactly one item, be it an error item or a list of friends to connect to.
|
|
|
|
|
2021-11-01 04:16:41 -04:00
|
|
|
try
|
|
|
|
{
|
2021-11-01 11:14:13 -04:00
|
|
|
RsDbg() << "Received a client publish item from " << item->PeerId() << ":";
|
2021-11-01 04:16:41 -04:00
|
|
|
RsDbg() << *item ;
|
|
|
|
|
|
|
|
// First of all, read PGP key and short invites, parse them, and check that they contain the same information
|
|
|
|
|
2021-11-01 17:01:59 -04:00
|
|
|
std::map<RsPeerId,PeerInfo>::iterator pi = handleIncomingClientData(item->pgp_public_key_b64,item->short_invite);
|
2021-11-01 11:14:13 -04:00
|
|
|
|
2021-11-01 17:01:59 -04:00
|
|
|
// No need to test for it==mCurrentClients.end() because it will be directly caught by the exception handling below even before.
|
2021-11-01 11:14:13 -04:00
|
|
|
// Respond with a list of potential friends
|
|
|
|
|
2021-11-04 09:24:19 -04:00
|
|
|
RsDbg() << "Sending response item to " << item->PeerId() ;
|
|
|
|
|
2021-11-01 17:01:59 -04:00
|
|
|
RsFriendServerServerResponseItem *sr_item = new RsFriendServerServerResponseItem;
|
2021-11-01 11:14:13 -04:00
|
|
|
|
2021-11-07 16:39:24 -05:00
|
|
|
std::map<RsPeerId,RsPgpFingerprint> friends;
|
2021-11-01 17:01:59 -04:00
|
|
|
sr_item->nonce = pi->second.last_nonce;
|
2021-11-07 16:39:24 -05:00
|
|
|
sr_item->friend_invites = computeListOfFriendInvites(item->n_requested_friends,pi->first,friends);
|
2021-11-01 17:01:59 -04:00
|
|
|
sr_item->PeerId(item->PeerId());
|
|
|
|
|
2021-11-06 12:49:31 -04:00
|
|
|
// Update the have_added_as_friend for the list of each peer. We do that before sending because sending destroys
|
|
|
|
// the item.
|
|
|
|
|
2021-11-07 16:39:24 -05:00
|
|
|
for(const auto& pid:friends)
|
|
|
|
{
|
|
|
|
auto& p(mCurrentClientPeers[pid.first]);
|
|
|
|
p.have_added_this_peer[computePeerDistance(p.pgp_fingerprint, pi->second.pgp_fingerprint)] = pi->first;
|
|
|
|
}
|
2021-11-06 12:49:31 -04:00
|
|
|
|
|
|
|
// Send the item.
|
2021-11-01 17:01:59 -04:00
|
|
|
mni->SendItem(sr_item);
|
2021-11-04 09:24:19 -04:00
|
|
|
|
2021-11-05 11:52:00 -04:00
|
|
|
// Update the list of closest peers for all peers currently in the database.
|
|
|
|
|
|
|
|
updateClosestPeers(pi->first,pi->second.pgp_fingerprint);
|
2021-11-01 11:14:13 -04:00
|
|
|
}
|
|
|
|
catch(std::exception& e)
|
|
|
|
{
|
|
|
|
RsErr() << "ERROR: " << e.what() ;
|
2021-11-04 09:24:19 -04:00
|
|
|
|
|
|
|
RsFriendServerStatusItem *status_item = new RsFriendServerStatusItem;
|
|
|
|
status_item->status = RsFriendServerStatusItem::END_OF_TRANSMISSION;
|
|
|
|
status_item->PeerId(item->PeerId());
|
|
|
|
mni->SendItem(status_item);
|
|
|
|
return;
|
2021-11-01 11:14:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:39:24 -05:00
|
|
|
std::map<std::string, bool> FriendServer::computeListOfFriendInvites(uint32_t nb_reqs_invites, const RsPeerId &pid, std::map<RsPeerId,RsPgpFingerprint>& friends)
|
2021-11-01 17:01:59 -04:00
|
|
|
{
|
2021-11-05 11:52:00 -04:00
|
|
|
// Strategy: we want to return the same set of friends for a given PGP profile key.
|
|
|
|
// Still, using some closest distance strategy, the n-closest peers for profile A is not the
|
|
|
|
// same set than the n-closest peers for profile B. We have multiple options:
|
|
|
|
//
|
|
|
|
// Option 1:
|
|
|
|
//
|
|
|
|
// (1) for each profile, keep the list of n-closest peers updated (when a new peer if added/removed all lists are updated)
|
|
|
|
//
|
|
|
|
// When a peer asks for k friends, read from (1), until the number of collected peers
|
|
|
|
// reaches the requested value. Then when a peer receives a connection request, ask the friend server if the
|
|
|
|
// peer has been sent your own cert.
|
|
|
|
//
|
|
|
|
// Option 2:
|
|
|
|
//
|
|
|
|
// (1) for each profile, keep the list of n-closest peers updated (when a new peer if added/removed all lists are updated)
|
|
|
|
// (2) for each profile, keep the list of which peers have been sent this profile already
|
|
|
|
//
|
|
|
|
// When a peer asks for k friends, read from (2) first, then (1), until the number of collected peers
|
|
|
|
// reaches the requested value.
|
|
|
|
//
|
2021-11-07 16:39:24 -05:00
|
|
|
// So we choose Option 2.
|
|
|
|
|
|
|
|
std::map<std::string,bool> res;
|
|
|
|
|
|
|
|
auto add_from = [&res,&friends,nb_reqs_invites,this](bool added,const std::map<PeerInfo::PeerDistance,RsPeerId>& lst) -> bool
|
|
|
|
{
|
|
|
|
for(const auto& pid:lst)
|
|
|
|
{
|
|
|
|
const auto p = mCurrentClientPeers.find(pid.second);
|
|
|
|
res.insert(std::make_pair(p->second.short_certificate,added));
|
|
|
|
friends.insert(std::make_pair(p->first,p->second.pgp_fingerprint));
|
|
|
|
|
|
|
|
if(res.size() >= nb_reqs_invites)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto& pinfo(mCurrentClientPeers[pid]);
|
|
|
|
|
|
|
|
// First add from peers who already added the current peer as friend, and leave if we already have enough
|
|
|
|
|
|
|
|
if(add_from(true,pinfo.have_added_this_peer))
|
|
|
|
return res;
|
|
|
|
|
|
|
|
add_from(false,pinfo.closest_peers);
|
2021-11-05 11:52:00 -04:00
|
|
|
|
2021-11-01 17:01:59 -04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::map<RsPeerId,PeerInfo>::iterator FriendServer::handleIncomingClientData(const std::string& pgp_public_key_b64,const std::string& short_invite_b64)
|
2021-11-01 11:14:13 -04:00
|
|
|
{
|
2021-11-10 17:36:23 -05:00
|
|
|
// 1 - Check that the incoming data is sound.
|
|
|
|
|
2021-11-01 04:16:41 -04:00
|
|
|
RsDbg() << " Checking item data...";
|
|
|
|
|
|
|
|
std::string error_string;
|
|
|
|
std::vector<uint8_t> key_binary_data ;
|
|
|
|
|
2021-11-04 09:24:19 -04:00
|
|
|
if(RsBase64::decode(pgp_public_key_b64,key_binary_data))
|
2021-11-01 11:14:13 -04:00
|
|
|
throw std::runtime_error(" Cannot decode client pgp public key: \"" + pgp_public_key_b64 + "\". Wrong format??");
|
|
|
|
|
2021-11-10 17:36:23 -05:00
|
|
|
RsDbg() << " Parsing public key:" ;
|
|
|
|
|
|
|
|
PGPKeyInfo received_key_info;
|
|
|
|
|
|
|
|
if(!PGPKeyManagement::parsePGPPublicKey(key_binary_data.data(),key_binary_data.size(),received_key_info))
|
|
|
|
throw std::runtime_error("Cannot parse received pgp public key.") ;
|
2021-11-01 04:16:41 -04:00
|
|
|
|
2021-11-10 17:36:23 -05:00
|
|
|
RsDbg() << " Issuer : \"" << received_key_info.user_id << "\"" ;
|
|
|
|
RsDbg() << " Fingerprint: " << RsPgpFingerprint::fromBufferUnsafe(received_key_info.fingerprint) ;
|
2021-11-01 04:16:41 -04:00
|
|
|
|
2021-11-10 17:36:23 -05:00
|
|
|
RsDbg() << " Parsing short invite:" ;
|
2021-11-01 04:16:41 -04:00
|
|
|
|
|
|
|
RsPeerDetails shortInviteDetails;
|
|
|
|
uint32_t errorCode = 0;
|
|
|
|
|
2021-11-01 11:14:13 -04:00
|
|
|
if(short_invite_b64.empty() || !RsCertificate::decodeRadix64ShortInvite(short_invite_b64, shortInviteDetails,errorCode ))
|
2021-11-01 04:16:41 -04:00
|
|
|
throw std::runtime_error("Could not parse short certificate. Error = " + RsUtil::NumberToString(errorCode));
|
|
|
|
|
2021-11-10 17:36:23 -05:00
|
|
|
RsDbg() << " Fingerprint: " << shortInviteDetails.fpr ;
|
|
|
|
RsDbg() << " Peer ID: " << shortInviteDetails.id ;
|
2021-11-01 04:16:41 -04:00
|
|
|
|
2021-11-10 17:36:23 -05:00
|
|
|
if(shortInviteDetails.fpr != RsPgpFingerprint::fromBufferUnsafe(received_key_info.fingerprint))
|
|
|
|
throw std::runtime_error("Fingerpring from short invite and public key are different! Very unexpected! Message will be ignored.");
|
2021-11-01 04:16:41 -04:00
|
|
|
|
2021-11-10 17:36:23 -05:00
|
|
|
// 3 - if the key is not already here, add it to keyring.
|
2021-10-31 07:02:09 -04:00
|
|
|
|
2021-11-10 17:36:23 -05:00
|
|
|
{
|
|
|
|
RsPgpFingerprint fpr_test;
|
|
|
|
if(mPgpHandler->isPgpPubKeyAvailable(RsPgpId::fromBufferUnsafe(received_key_info.fingerprint+12)))
|
|
|
|
RsDbg() << " PGP Key is already into keyring.";
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RsPgpId pgp_id;
|
|
|
|
if(!mPgpHandler->LoadCertificateFromBinaryData(key_binary_data.data(),key_binary_data.size(), pgp_id, error_string))
|
|
|
|
throw std::runtime_error("Cannot load client's pgp public key into keyring: " + error_string) ;
|
2021-11-01 04:16:41 -04:00
|
|
|
|
2021-11-10 17:36:23 -05:00
|
|
|
RsDbg() << " Public key added to keyring.";
|
|
|
|
RsDbg() << " Sync-ing the PGP keyring on disk";
|
|
|
|
|
|
|
|
mPgpHandler->syncDatabase();
|
|
|
|
}
|
|
|
|
}
|
2021-11-01 04:16:41 -04:00
|
|
|
|
2021-11-01 11:14:13 -04:00
|
|
|
// Check the item's data signature. Is that needed? Not sure, since the data is sent PGP-encrypted, so only the owner
|
|
|
|
// of the secret PGP key can actually use it.
|
2021-11-01 04:16:41 -04:00
|
|
|
#warning TODO
|
|
|
|
|
2021-11-01 11:14:13 -04:00
|
|
|
// All good.
|
|
|
|
|
2021-11-01 04:16:41 -04:00
|
|
|
// Store/update the peer info
|
|
|
|
|
|
|
|
auto& pi(mCurrentClientPeers[shortInviteDetails.id]);
|
|
|
|
|
2021-11-01 11:14:13 -04:00
|
|
|
pi.short_certificate = short_invite_b64;
|
2021-11-01 04:16:41 -04:00
|
|
|
pi.last_connection_TS = time(nullptr);
|
2021-11-10 17:36:23 -05:00
|
|
|
pi.pgp_fingerprint = shortInviteDetails.fpr;
|
2021-11-01 04:16:41 -04:00
|
|
|
|
2021-11-01 17:01:59 -04:00
|
|
|
while(pi.last_nonce == 0) // reuse the same identifier (so it's not really a nonce, but it's kept secret whatsoever).
|
|
|
|
pi.last_nonce = RsRandom::random_u64();
|
|
|
|
|
|
|
|
return mCurrentClientPeers.find(shortInviteDetails.id);
|
2021-11-01 11:14:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void FriendServer::handleClientRemove(const RsFriendServerClientRemoveItem *item)
|
|
|
|
{
|
|
|
|
RsDbg() << "Received a client remove item:" << *item ;
|
|
|
|
|
|
|
|
auto it = mCurrentClientPeers.find(item->peer_id);
|
|
|
|
|
|
|
|
if(it == mCurrentClientPeers.end())
|
2021-11-01 04:16:41 -04:00
|
|
|
{
|
2021-11-01 11:14:13 -04:00
|
|
|
RsErr() << " ERROR: Client " << item->peer_id << " is not known to the server." ;
|
|
|
|
return;
|
2021-11-01 04:16:41 -04:00
|
|
|
}
|
2021-10-31 07:02:09 -04:00
|
|
|
|
2021-11-01 11:14:13 -04:00
|
|
|
if(it->second.last_nonce != item->nonce)
|
|
|
|
{
|
|
|
|
RsErr() << " ERROR: Client supplied a nonce " << std::hex << item->nonce << std::dec << " that is not correct (expected "
|
|
|
|
<< std::hex << it->second.last_nonce << std::dec << ")";
|
|
|
|
return;
|
|
|
|
}
|
2021-10-31 07:02:09 -04:00
|
|
|
|
2021-11-01 11:14:13 -04:00
|
|
|
RsDbg() << " Nonce is correct: " << std::hex << item->nonce << std::dec << ". Removing peer " << item->peer_id ;
|
2021-10-31 07:02:09 -04:00
|
|
|
|
2021-11-06 12:49:31 -04:00
|
|
|
removePeer(item->peer_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FriendServer::removePeer(const RsPeerId& peer_id)
|
|
|
|
{
|
|
|
|
auto it = mCurrentClientPeers.find(peer_id);
|
2021-11-05 11:52:00 -04:00
|
|
|
|
2021-11-06 12:49:31 -04:00
|
|
|
if(it != mCurrentClientPeers.end())
|
|
|
|
mCurrentClientPeers.erase(it);
|
2021-11-05 11:52:00 -04:00
|
|
|
|
|
|
|
for(auto& it:mCurrentClientPeers)
|
2021-11-06 12:49:31 -04:00
|
|
|
{
|
|
|
|
// Also remove that peer from all n-closest lists
|
|
|
|
|
2021-11-05 11:52:00 -04:00
|
|
|
for(auto pit(it.second.closest_peers.begin());pit!=it.second.closest_peers.end();)
|
2021-11-06 12:49:31 -04:00
|
|
|
if(pit->second == peer_id)
|
2021-11-05 11:52:00 -04:00
|
|
|
{
|
2021-11-06 12:49:31 -04:00
|
|
|
RsDbg() << " Removing from n-closest peers of peer " << pit->first ;
|
|
|
|
|
2021-11-05 11:52:00 -04:00
|
|
|
auto tmp(pit);
|
|
|
|
++tmp;
|
|
|
|
it.second.closest_peers.erase(pit);
|
|
|
|
pit=tmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++pit;
|
2021-11-06 12:49:31 -04:00
|
|
|
|
|
|
|
// Also remove that peer from peers that have accepted each peer
|
|
|
|
|
|
|
|
for(auto fit(it.second.have_added_this_peer.begin());fit!=it.second.have_added_this_peer.end();)
|
|
|
|
if(fit->second == peer_id)
|
|
|
|
{
|
|
|
|
RsDbg() << " Removing from have_added_as_friend peers of peer " << fit->first ;
|
|
|
|
|
|
|
|
auto tmp(fit);
|
|
|
|
++tmp;
|
|
|
|
it.second.closest_peers.erase(fit);
|
|
|
|
fit=tmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++fit;
|
|
|
|
}
|
2021-10-11 17:40:22 -04:00
|
|
|
}
|
2021-11-01 04:16:41 -04:00
|
|
|
|
2021-11-05 11:52:00 -04:00
|
|
|
PeerInfo::PeerDistance FriendServer::computePeerDistance(const RsPgpFingerprint& p1,const RsPgpFingerprint& p2)
|
|
|
|
{
|
2021-11-07 16:39:24 -05:00
|
|
|
std::cerr << "Computing peer distance: p1=" << p1 << " p2=" << p2 << " p1^p2=" << (p1^p2) << " distance=" << ((p1^p2)^mRandomPeerBias) << std::endl;
|
2021-11-05 11:52:00 -04:00
|
|
|
return (p1 ^ p2)^mRandomPeerBias;
|
|
|
|
}
|
2021-10-08 19:08:23 -04:00
|
|
|
FriendServer::FriendServer(const std::string& base_dir)
|
|
|
|
{
|
2021-10-09 09:06:06 -04:00
|
|
|
RsDbg() << "Creating friend server." ;
|
2021-10-08 19:08:23 -04:00
|
|
|
mBaseDirectory = base_dir;
|
2021-11-01 04:16:41 -04:00
|
|
|
|
|
|
|
// Create a PGP Handler
|
|
|
|
|
|
|
|
std::string pgp_public_keyring_path = RsDirUtil::makePath(base_dir,"pgp_public_keyring") ;
|
|
|
|
std::string pgp_lock_path = RsDirUtil::makePath(base_dir,"pgp_lock") ;
|
|
|
|
|
|
|
|
std::string pgp_private_keyring_path = RsDirUtil::makePath(base_dir,"pgp_private_keyring") ; // not used.
|
|
|
|
std::string pgp_trustdb_path = RsDirUtil::makePath(base_dir,"pgp_trustdb") ; // not used.
|
|
|
|
|
|
|
|
mPgpHandler = new PGPHandler(pgp_public_keyring_path,pgp_private_keyring_path,pgp_trustdb_path,pgp_lock_path);
|
2021-11-05 11:52:00 -04:00
|
|
|
|
|
|
|
// Random bias. Should be cryptographically safe.
|
|
|
|
|
|
|
|
mRandomPeerBias = RsPgpFingerprint::random();
|
2021-10-08 19:08:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void FriendServer::run()
|
|
|
|
{
|
|
|
|
// 1 - create network interface.
|
|
|
|
|
|
|
|
mni = new FsNetworkInterface;
|
2021-10-09 09:06:06 -04:00
|
|
|
mni->start();
|
2021-10-08 19:08:23 -04:00
|
|
|
|
|
|
|
while(!shouldStop()) { threadTick() ; }
|
|
|
|
}
|
|
|
|
|
2021-10-31 13:00:43 -04:00
|
|
|
void FriendServer::autoWash()
|
|
|
|
{
|
|
|
|
rstime_t now = time(nullptr);
|
2021-11-04 09:24:19 -04:00
|
|
|
RsDbg() << "autoWash..." ;
|
2021-10-31 13:00:43 -04:00
|
|
|
|
2021-11-06 12:49:31 -04:00
|
|
|
std::list<RsPeerId> to_remove;
|
|
|
|
|
2021-11-07 09:16:24 -05:00
|
|
|
for(std::map<RsPeerId,PeerInfo>::iterator it(mCurrentClientPeers.begin());it!=mCurrentClientPeers.end();++it)
|
2021-10-31 13:00:43 -04:00
|
|
|
if(it->second.last_connection_TS + MAXIMUM_PEER_INACTIVE_DELAY < now)
|
|
|
|
{
|
|
|
|
RsDbg() << "Removing client peer " << it->first << " because it's inactive for more than " << MAXIMUM_PEER_INACTIVE_DELAY << " seconds." ;
|
2021-11-06 12:49:31 -04:00
|
|
|
to_remove.push_back(it->first);
|
2021-10-31 13:00:43 -04:00
|
|
|
}
|
2021-11-06 12:49:31 -04:00
|
|
|
|
|
|
|
for(auto peer_id:to_remove)
|
|
|
|
removePeer(peer_id);
|
2021-11-04 09:24:19 -04:00
|
|
|
|
|
|
|
RsDbg() << "done." ;
|
2021-10-31 13:00:43 -04:00
|
|
|
}
|
|
|
|
|
2021-11-05 11:52:00 -04:00
|
|
|
void FriendServer::updateClosestPeers(const RsPeerId& pid,const RsPgpFingerprint& fpr)
|
|
|
|
{
|
|
|
|
for(auto& it:mCurrentClientPeers)
|
2021-11-07 16:39:24 -05:00
|
|
|
if(it.first != pid)
|
|
|
|
{
|
|
|
|
PeerInfo::PeerDistance d = computePeerDistance(fpr,it.second.pgp_fingerprint);
|
2021-11-05 11:52:00 -04:00
|
|
|
|
2021-11-07 16:39:24 -05:00
|
|
|
it.second.closest_peers.insert(std::make_pair(d,pid));
|
2021-11-05 11:52:00 -04:00
|
|
|
|
2021-11-07 16:39:24 -05:00
|
|
|
if(it.second.closest_peers.size() > MAXIMUM_PEERS_TO_REQUEST)
|
|
|
|
it.second.closest_peers.erase(std::prev(it.second.closest_peers.end()));
|
|
|
|
}
|
2021-11-05 11:52:00 -04:00
|
|
|
}
|
|
|
|
|
2021-10-31 13:00:43 -04:00
|
|
|
void FriendServer::debugPrint()
|
|
|
|
{
|
|
|
|
RsDbg() << "========== FriendServer statistics ============";
|
|
|
|
RsDbg() << " Base directory: "<< mBaseDirectory;
|
2021-11-05 11:52:00 -04:00
|
|
|
RsDbg() << " Random peer bias: "<< mRandomPeerBias;
|
2021-10-31 13:00:43 -04:00
|
|
|
RsDbg() << " Network interface: ";
|
2021-11-05 11:52:00 -04:00
|
|
|
RsDbg() << " Max peers in n-closest list: " << MAXIMUM_PEERS_TO_REQUEST;
|
|
|
|
RsDbg() << " Current active peers: " << mCurrentClientPeers.size() ;
|
2021-10-31 13:00:43 -04:00
|
|
|
|
|
|
|
rstime_t now = time(nullptr);
|
|
|
|
|
2021-11-05 11:52:00 -04:00
|
|
|
for(const auto& it:mCurrentClientPeers)
|
|
|
|
{
|
2021-11-07 16:39:24 -05:00
|
|
|
RsDbg() << " " << it.first << ": nonce=" << std::hex << it.second.last_nonce << std::dec << " fpr: " << it.second.pgp_fingerprint << ", last contact: " << now - it.second.last_connection_TS << " secs ago.";
|
|
|
|
RsDbg() << " Closest peers:" ;
|
2021-10-31 13:00:43 -04:00
|
|
|
|
2021-11-05 11:52:00 -04:00
|
|
|
for(const auto& pit:it.second.closest_peers)
|
2021-11-07 16:39:24 -05:00
|
|
|
RsDbg() << " " << pit.second << " distance=" << pit.first ;
|
|
|
|
|
|
|
|
RsDbg() << " Have added this peer:" ;
|
|
|
|
|
|
|
|
for(const auto& pit:it.second.have_added_this_peer)
|
|
|
|
RsDbg() << " " << pit.second << " distance=" << pit.first ;
|
2021-11-05 11:52:00 -04:00
|
|
|
}
|
|
|
|
|
2021-10-31 13:00:43 -04:00
|
|
|
RsDbg() << "===============================================";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|