Merge branch 'master' of https://github.com/myfingerhurt/RetroShare into myfingerhurt-webui-PR1014

This commit is contained in:
Gioacchino Mazzurco 2017-09-15 08:25:30 +02:00
commit 016cafdd23
2 changed files with 518 additions and 293 deletions

View File

@ -32,6 +32,11 @@
#include "Operators.h"
#include "ApiTypes.h"
//#include "util/rsdebug.h"
//static struct RsLog::logInfo peersHandlerInfo = {RsLog::Default, "peersHandler"};
//#define peerhandlerzone &peersHandlerInfo
namespace resource_api
{
@ -70,12 +75,13 @@ void peerDetailsToStream(StreamBase& stream, RsPeerDetails& details)
}
stream
<< makeKeyValueReference("pgp_id" , details.gpg_id)
<< makeKeyValueReference("peer_id" , details.id)
<< makeKeyValueReference("name" , details.name)
<< makeKeyValueReference("location" , details.location)
<< makeKeyValueReference("pgp_id", details.gpg_id)
<< makeKeyValueReference("isHiddenNode", details.isHiddenNode)
<< makeKeyValueReference("nodeType", nodeType_string );
<< makeKeyValueReference("is_hidden_node", details.isHiddenNode)
<< makeKeyValueReference("nodeType" , nodeType_string )
<< makeKeyValueReference("last_contact" , details.lastConnect );
if(details.state & RS_PEER_STATE_CONNECTED)
{
@ -231,12 +237,10 @@ std::string connectStateString(RsPeerDetails &details)
return stateString;
}
PeersHandler::PeersHandler(StateTokenServer* sts, RsNotify* notify, RsPeers *peers, RsMsgs* msgs):
mStateTokenServer(sts),
mNotify(notify),
mRsPeers(peers), mRsMsgs(msgs),
status(0),
mMtx("PeersHandler Mutex")
PeersHandler::PeersHandler( StateTokenServer* sts, RsNotify* notify,
RsPeers *peers, RsMsgs* msgs ) :
mStateTokenServer(sts), mNotify(notify), mRsPeers(peers), mRsMsgs(msgs),
status(0), mMtx("PeersHandler Mutex")
{
mNotify->registerNotifyClient(this);
mStateTokenServer->registerTickClient(this);
@ -253,6 +257,8 @@ PeersHandler::PeersHandler(StateTokenServer* sts, RsNotify* notify, RsPeers *pee
addResourceHandler("get_node_options", this, &PeersHandler::handleGetNodeOptions);
addResourceHandler("set_node_options", this, &PeersHandler::handleSetNodeOptions);
addResourceHandler("examine_cert", this, &PeersHandler::handleExamineCert);
addResourceHandler("remove_node", this, &PeersHandler::handleRemoveNode);
addResourceHandler("get_inactive_users", this, &PeersHandler::handleGetInactiveUsers);
}
@ -505,6 +511,7 @@ void PeersHandler::handleWildcard(Request &req, Response &resp)
locationStream.getStreamToMember();
int bestPeerState = 0;
uint32_t lastConnect = 0;
unsigned int bestRSState = 0;
std::string bestCustomStateString;
@ -519,6 +526,9 @@ void PeersHandler::handleWildcard(Request &req, Response &resp)
stream << makeKeyValueReference("unread_msgs", unread);
peerInfoToStream(stream,*vit, mRsPeers, grpInfo, have_avatar(mRsMsgs, vit->id));
// Get latest contact timestamp
if(vit->lastConnect > lastConnect)
lastConnect = vit->lastConnect;
/* Custom state string */
std::string customStateString;
@ -597,6 +607,7 @@ void PeersHandler::handleWildcard(Request &req, Response &resp)
state_string = "undefined";
itemStream << makeKeyValue("state_string", state_string);
itemStream << makeKeyValue("last_contact", lastConnect);
}
resp.mStateToken = getCurrentStateToken();
}
@ -625,32 +636,132 @@ void PeersHandler::handleWildcard(Request &req, Response &resp)
{
flags = RS_NODE_PERM_DEFAULT;
}
RsPeerId peer_id;
//RsPeerId peer_id;
//RsPgpId pgp_id;
//std::string cleanCert;
uint32_t error_code;
std::string error_string;
RsPeerDetails peerDetails;
//std::string debug_string = " ";
RsPgpId own_pgp = mRsPeers->getGPGOwnId();
RsPeerId ownpeer_id = mRsPeers->getOwnId();
do
{
if (!cert_string.empty())
{
RsPgpId pgp_id ;
std::string cleanCert;
int error_code;
RsPeerId ssl_id ;
std::string error_string ;
if (mRsPeers->cleanCertificate(cert_string, cleanCert, error_code))
if(!mRsPeers->loadCertificateFromString(cert_string,ssl_id,pgp_id,error_string))
{
if(mRsPeers->loadCertificateFromString(cert_string, peer_id, pgp_id, error_string)
&& mRsPeers->addFriend(peer_id, pgp_id, flags))
{
ok = true;
resp.mDataStream << makeKeyValueReference("pgp_id", pgp_id);
resp.mDataStream << makeKeyValueReference("peer_id", peer_id);
}
else
{
resp.mDebug << "Error: failed to add peer" << std::endl;
std::cerr << "handleWildcard::loadCertificateFromString(): cannot load that certificate. " ;
std::cerr << error_string << std::endl;
resp.mDebug << "cannot load that certificate " << std::endl;
resp.mDebug << error_string << std::endl;
break ;
}
}
std::cerr << "handleWildcard addPeer: loadDetailsFromStringCert" << std::endl;
if(!mRsPeers->loadDetailsFromStringCert(cert_string, peerDetails, error_code))
{
resp.mDebug << "Error: failed to add peer can not get peerDetails" << std::endl;
resp.mDebug << error_code << std::endl;
break;
}
std::cerr << "handleWildcard addPeer: peerDetails.gpg_id" << std::endl;
if(peerDetails.gpg_id.isNull())
{
resp.mDebug << "Error: failed to add peer gpg_id.isNull" << std::endl;
resp.mDebug << error_string << std::endl;
break;
}
if(peerDetails.gpg_id == own_pgp)
{
if(peerDetails.id == ownpeer_id)
{
std::cerr << "handleWildcard addPeer: is own peer_id, ignore" << std::endl;
resp.mDebug << "Error: failed to add peer_id, because its your own peer_id" << std::endl;
resp.mDebug << error_string << std::endl;
break;
}
std::cerr << "handleWildcard addPeer: is own pgp_id, update only" << std::endl;
}
else
{
resp.mDebug << "Error: failed to add peer" << std::endl;
resp.mDebug << error_code << std::endl;
if(!mRsPeers->addFriend(peerDetails.id, peerDetails.gpg_id, flags))
{
resp.mDebug << "Error: failed to addFriend" << std::endl;
resp.mDebug << error_string << std::endl;
break;
}
std::cerr << "handleWildcard addPeer: addFriend ok " << peerDetails.gpg_id << std::endl;
}
ok = true;
// Retrun node details to response, after succes of adding peers
peerDetailsToStream(resp.mDataStream, peerDetails);
std::cerr << "handleWildcard addPeer: peerDetailsToStream" << std::endl;
if (!peerDetails.location.empty())
{
std::cerr << "handleWildcard addPeer: setting peer node." << std::endl;
mRsPeers->setLocation(peerDetails.id, peerDetails.location);
}
// Update new address even the peer already existed.
if (peerDetails.isHiddenNode)
{
std::cerr << "handleWildcard addPeer: setting hidden node.";
std::cerr << peerDetails.hiddenNodeAddress;
std::cerr << " Port: " << peerDetails.hiddenNodePort << std::endl;
mRsPeers->setHiddenNode(peerDetails.id, peerDetails.hiddenNodeAddress, peerDetails.hiddenNodePort);
}
else
{
//let's check if there is ip adresses in the certificate.
if (!peerDetails.extAddr.empty() && peerDetails.extPort)
{
std::cerr << "handleWildcard addPeer: setting ip ext address." ;
std::cerr << peerDetails.extAddr;
std::cerr << " Port: " << peerDetails.extPort;
std::cerr << std::endl;
mRsPeers->setExtAddress(peerDetails.id, peerDetails.extAddr, peerDetails.extPort);
}
if (!peerDetails.localAddr.empty() && peerDetails.localPort)
{
std::cerr << "handleWildcard addPeer: setting ip local address." ;
std::cerr << peerDetails.localAddr;
std::cerr << " Port: " << peerDetails.localPort << std::endl;
mRsPeers->setLocalAddress(peerDetails.id, peerDetails.localAddr, peerDetails.localPort);
}
if (!peerDetails.dyndns.empty())
{
std::cerr << "handleWildcard addPeer: setting DynDNS." ;
std::cerr << peerDetails.dyndns << std::endl;
mRsPeers->setDynDNS(peerDetails.id, peerDetails.dyndns);
}
}
// std::cerr << debug_string << std::endl;
// resp.mDebug << debug_string << std::endl;
// rslog(RSL_WARNING, peerhandlerzone, debug_string );
}while(0);
}
}
if(ok)
@ -693,6 +804,118 @@ void PeersHandler::handleExamineCert(Request &req, Response &resp)
}
}
/**
* \brief Remove specific location or user
*
* \param [in] req request from user either peer_id or gpg_id is needed.
* \param [in] resp response to user
* \return
*
* \details
*/
void PeersHandler::handleRemoveNode(Request &req, Response &resp)
{
std::string ssl_peer_id;
std::string gpg_id;
req.mStream << makeKeyValueReference("peer_id", ssl_peer_id);
req.mStream << makeKeyValueReference("gpg_id", gpg_id);
RsPeerId peerId(ssl_peer_id);
RsPgpId gpgId(gpg_id);
if(!gpgId.isNull())
{
mRsPeers->removeFriend(gpgId);
resp.mDataStream << makeKeyValue("gpg_id", gpg_id);
resp.setOk();
}
else if(!peerId.isNull())
{
mRsPeers->removeFriendLocation(peerId);
resp.mDataStream << makeKeyValue("peer_id", ssl_peer_id);
resp.setOk();
}
else
{
resp.setFail("handleRemoveNode Invalid peer_id or gpg_id");
}
}
/**
* \brief Retrieve inactive user list before specific UNIX time
*
* \param [in] req request from user, datatime in UNIX timestamp.
* \param [in] resp response to request
* \return a pgp_id list.
*
* \details Combine this with handleRemoveNode,
* batch remove inactive users are possible and convenient.
*/
void PeersHandler::handleGetInactiveUsers(Request &req, Response &resp)
{
std::string datetime;
req.mStream << makeKeyValueReference("datetime", datetime);
uint32_t before = strtoul(datetime.c_str(), NULL, 10);
if(datetime.empty())
{
std::cerr << "PeersHandler:datetime is empty" << std::endl;
before = time(NULL);
}
// list all peers
std::list<RsPeerId> peers;
mRsPeers->getFriendList(peers);
// Make pair of gpg_id and last connect time
std::map<RsPgpId, uint32_t> mapRsPgpId;
// get all peers' infomations
for(std::list<RsPeerId>::iterator lt = peers.begin(); lt != peers.end(); ++lt)
{
RsPeerDetails details;
mRsPeers->getPeerDetails(*lt, details);
// Search for exist pgp_id, update the last connect time
if(mapRsPgpId.find(details.gpg_id) != mapRsPgpId.end())
{
if(mapRsPgpId[details.gpg_id] < details.lastConnect)
mapRsPgpId[details.gpg_id] = details.lastConnect;
}
else
{
mapRsPgpId.insert(std::make_pair(details.gpg_id, details.lastConnect));
}
}
// mark response as list, in case it is empty
resp.mDataStream.getStreamToMember();
std::map<RsPgpId, uint32_t>::iterator lit;
for( lit = mapRsPgpId.begin(); lit != mapRsPgpId.end(); ++lit)
{
if (lit->second < before)
{
std::cerr << "YES Adding OLD: " << mRsPeers->getGPGName(lit->first);
std::cerr << "last_contact: " << lit->second <<std::endl;
StreamBase& itemStream = resp.mDataStream.getStreamToMember();
itemStream << makeKeyValue("pgp_id", lit->first.toStdString());
itemStream << makeKeyValue("name", mRsPeers->getGPGName(lit->first));
itemStream << makeKeyValue("last_contact", lit->second);
}
}
resp.setOk();
}
void PeersHandler::handleGetNetworkOptions(Request& /*req*/, Response& resp)
{
RsPeerDetails detail;
@ -865,7 +1088,7 @@ void PeersHandler::handleSetNetworkOptions(Request& req, Response& resp)
bool checkIP;
req.mStream << makeKeyValueReference("check_ip", checkIP);
rsPeers->allowServerIPDetermination(checkIP) ;
mRsPeers->allowServerIPDetermination(checkIP) ;
// Tor
std::string toraddr;
@ -1110,7 +1333,7 @@ void PeersHandler::handleSetNodeOptions(Request& req, Response& resp)
else
{
if(detail.hiddenNodeAddress != local_address || detail.hiddenNodePort != local_port)
rsPeers->setHiddenNode(peerId, local_address, local_port);
mRsPeers->setHiddenNode(peerId, local_address, local_port);
}
resp.setOk();

View File

@ -73,6 +73,8 @@ private:
void handleGetNodeOptions(Request& req, Response& resp);
void handleSetNodeOptions(Request& req, Response& resp);
void handleRemoveNode(Request &req, Response &resp);
void handleGetInactiveUsers(Request &req, Response &resp);
// a helper which ensures proper mutex locking
StateToken getCurrentStateToken();