From aa81cc16d0ba49d7aa151fa766ae179cd1076f2a Mon Sep 17 00:00:00 2001 From: Konrad Date: Wed, 12 Apr 2017 20:24:18 +0200 Subject: [PATCH] Added: Functions for handling PGP account details requests --- libresapi/src/api/PeersHandler.cpp | 116 +++++++++++++++++++++++++++++ libresapi/src/api/PeersHandler.h | 3 + 2 files changed, 119 insertions(+) diff --git a/libresapi/src/api/PeersHandler.cpp b/libresapi/src/api/PeersHandler.cpp index f48beb2f6..14cd1b7c6 100644 --- a/libresapi/src/api/PeersHandler.cpp +++ b/libresapi/src/api/PeersHandler.cpp @@ -119,6 +119,8 @@ PeersHandler::PeersHandler(StateTokenServer* sts, RsNotify* notify, RsPeers *pee addResourceHandler("set_state_string", this, &PeersHandler::handleSetStateString); addResourceHandler("get_custom_state_string", this, &PeersHandler::handleGetCustomStateString); addResourceHandler("set_custom_state_string", this, &PeersHandler::handleSetCustomStateString); + addResourceHandler("get_pgp_options", this, &PeersHandler::handleGetPGPOptions); + addResourceHandler("set_pgp_options", this, &PeersHandler::handleSetPGPOptions); addResourceHandler("examine_cert", this, &PeersHandler::handleExamineCert); } @@ -529,6 +531,120 @@ void PeersHandler::handleExamineCert(Request &req, Response &resp) } } +void PeersHandler::handleGetPGPOptions(Request& req, Response& resp) +{ + std::string pgp_id; + req.mStream << makeKeyValueReference("pgp_id", pgp_id); + + RsPgpId pgp(pgp_id); + RsPeerDetails detail; + + if(!rsPeers->getGPGDetails(pgp, detail)) + { + resp.setFail(); + return; + } + + std::string pgp_key = rsPeers->getPGPKey(detail.gpg_id, false); + + resp.mDataStream << makeKeyValue("pgp_fingerprint", detail.fpr.toStdString()); + resp.mDataStream << makeKeyValueReference("pgp_key", pgp_key); + + resp.mDataStream << makeKeyValue("direct_transfer", detail.service_perm_flags & RS_NODE_PERM_DIRECT_DL); + resp.mDataStream << makeKeyValue("allow_push", detail.service_perm_flags & RS_NODE_PERM_ALLOW_PUSH); + resp.mDataStream << makeKeyValue("require_WL", detail.service_perm_flags & RS_NODE_PERM_REQUIRE_WL); + + resp.mDataStream << makeKeyValue("own_sign", detail.ownsign); + resp.mDataStream << makeKeyValue("trustLvl", detail.trustLvl); + + uint32_t max_upload_speed = 0; + uint32_t max_download_speed = 0; + + rsPeers->getPeerMaximumRates(pgp, max_upload_speed, max_download_speed); + + resp.mDataStream << makeKeyValueReference("maxUploadSpeed", max_upload_speed); + resp.mDataStream << makeKeyValueReference("maxDownloadSpeed", max_download_speed); + + StreamBase& signersStream = resp.mDataStream.getStreamToMember("gpg_signers"); + + // mark as list (in case list is empty) + signersStream.getStreamToMember(); + + for(std::list::const_iterator it(detail.gpgSigners.begin()); it != detail.gpgSigners.end(); ++it) + { + RsPeerDetails detail; + if(!rsPeers->getGPGDetails(*it, detail)) + continue; + + std::string pgp_id = (*it).toStdString(); + std::string name = detail.name; + + signersStream.getStreamToMember() + << makeKeyValueReference("pgp_id", pgp_id) + << makeKeyValueReference("name", name); + } + + resp.setOk(); +} + +void PeersHandler::handleSetPGPOptions(Request& req, Response& resp) +{ + std::string pgp_id; + req.mStream << makeKeyValueReference("pgp_id", pgp_id); + + RsPgpId pgp(pgp_id); + RsPeerDetails detail; + + if(!rsPeers->getGPGDetails(pgp, detail)) + { + resp.setFail(); + return; + } + + int trustLvl; + req.mStream << makeKeyValueReference("trustLvl", trustLvl); + + if(trustLvl != (int)detail.trustLvl) + rsPeers->trustGPGCertificate(pgp, trustLvl); + + int max_upload_speed; + int max_download_speed; + + req.mStream << makeKeyValueReference("max_upload_speed", max_upload_speed); + req.mStream << makeKeyValueReference("max_download_speed", max_download_speed); + + rsPeers->setPeerMaximumRates(pgp, (uint32_t)max_upload_speed, (uint32_t)max_download_speed); + + bool direct_transfer; + bool allow_push; + bool require_WL; + + req.mStream << makeKeyValueReference("direct_transfer", direct_transfer); + req.mStream << makeKeyValueReference("allow_push", allow_push); + req.mStream << makeKeyValueReference("require_WL", require_WL); + + ServicePermissionFlags flags(0); + + if(direct_transfer) + flags = flags | RS_NODE_PERM_DIRECT_DL; + if(allow_push) + flags = flags | RS_NODE_PERM_ALLOW_PUSH; + if(require_WL) + flags = flags | RS_NODE_PERM_REQUIRE_WL; + + rsPeers->setServicePermissionFlags(pgp, flags); + + bool own_sign; + req.mStream << makeKeyValueReference("own_sign", own_sign); + + if(own_sign) + rsPeers->signGPGCertificate(pgp); + + resp.mStateToken = getCurrentStateToken(); + + resp.setOk(); +} + StateToken PeersHandler::getCurrentStateToken() { RsStackMutex stack(mMtx); /********** STACK LOCKED MTX ******/ diff --git a/libresapi/src/api/PeersHandler.h b/libresapi/src/api/PeersHandler.h index 15d231c10..5ae393b2f 100644 --- a/libresapi/src/api/PeersHandler.h +++ b/libresapi/src/api/PeersHandler.h @@ -41,6 +41,9 @@ private: void handleGetCustomStateString(Request& req, Response& resp); void handleSetCustomStateString(Request& req, Response& resp); + void handleGetPGPOptions(Request& req, Response& resp); + void handleSetPGPOptions(Request& req, Response& resp); + // a helper which ensures proper mutex locking StateToken getCurrentStateToken();