From 896762b948d1bc9b616194562f858c8244513777 Mon Sep 17 00:00:00 2001 From: csoler Date: Sun, 2 Jan 2022 20:41:47 +0100 Subject: [PATCH] made friend server to use tor hidden service to listen to connections --- libretroshare/src/rsserver/rsinit.cc | 6 +++ libretroshare/src/tor/TorManager.cpp | 2 + retroshare-friendserver/src/friendserver.cc | 8 ++-- retroshare-friendserver/src/friendserver.h | 4 +- retroshare-friendserver/src/network.cc | 21 ++++++++--- retroshare-friendserver/src/network.h | 5 ++- .../src/retroshare-friendserver.cc | 37 ++++++++++++++++++- 7 files changed, 71 insertions(+), 12 deletions(-) diff --git a/libretroshare/src/rsserver/rsinit.cc b/libretroshare/src/rsserver/rsinit.cc index 6e448fd73..2417cd69e 100644 --- a/libretroshare/src/rsserver/rsinit.cc +++ b/libretroshare/src/rsserver/rsinit.cc @@ -1940,6 +1940,12 @@ int RsServer::StartupRetroShare() std::string RsInit::executablePath() { + if(rsInitConfig->mainExecutablePath.empty()) + { + RsErr() << "Main executable path not set! Plz call RsInit::InitRetroShare(conf) with conf.main_executable_path = argv[0]"; + assert(false); + } + return rsInitConfig->mainExecutablePath; } bool RsInit::startAutoTor() diff --git a/libretroshare/src/tor/TorManager.cpp b/libretroshare/src/tor/TorManager.cpp index 0b0f59cef..7fa275c5a 100644 --- a/libretroshare/src/tor/TorManager.cpp +++ b/libretroshare/src/tor/TorManager.cpp @@ -142,6 +142,8 @@ std::string TorManager::torDataDirectory() const void TorManager::setTorDataDirectory(const std::string &path) { + assert(RsDirUtil::checkCreateDirectory(std::string(path))); + d->dataDir = path; if (!d->dataDir.empty() && !ByteArray(d->dataDir).endsWith('/')) diff --git a/retroshare-friendserver/src/friendserver.cc b/retroshare-friendserver/src/friendserver.cc index cbafc7ade..b3c5d55ec 100644 --- a/retroshare-friendserver/src/friendserver.cc +++ b/retroshare-friendserver/src/friendserver.cc @@ -6,6 +6,7 @@ #include "pgp/pgpkeyutil.h" #include "pgp/rscertificate.h" +#include "pgp/openpgpsdkhandler.h" #include "friendserver.h" #include "friend_server/fsitem.h" @@ -314,7 +315,8 @@ PeerInfo::PeerDistance FriendServer::computePeerDistance(const RsPgpFingerprint& std::cerr << "Computing peer distance: p1=" << p1 << " p2=" << p2 << " p1^p2=" << (p1^p2) << " distance=" << ((p1^p2)^mRandomPeerBias) << std::endl; return (p1 ^ p2)^mRandomPeerBias; } -FriendServer::FriendServer(const std::string& base_dir) +FriendServer::FriendServer(const std::string& base_dir,const std::string& listening_address,uint16_t listening_port) + : mListeningAddress(listening_address),mListeningPort(listening_port) { RsDbg() << "Creating friend server." ; mBaseDirectory = base_dir; @@ -327,7 +329,7 @@ FriendServer::FriendServer(const std::string& base_dir) 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); + mPgpHandler = new OpenPGPSDKHandler(pgp_public_keyring_path,pgp_private_keyring_path,pgp_trustdb_path,pgp_lock_path); // Random bias. Should be cryptographically safe. @@ -338,7 +340,7 @@ void FriendServer::run() { // 1 - create network interface. - mni = new FsNetworkInterface; + mni = new FsNetworkInterface(mListeningAddress,mListeningPort); mni->start(); while(!shouldStop()) { threadTick() ; } diff --git a/retroshare-friendserver/src/friendserver.h b/retroshare-friendserver/src/friendserver.h index edf2a38cb..202f6a1e0 100644 --- a/retroshare-friendserver/src/friendserver.h +++ b/retroshare-friendserver/src/friendserver.h @@ -46,7 +46,7 @@ struct PeerInfo class FriendServer : public RsTickingThread { public: - FriendServer(const std::string& base_directory); + FriendServer(const std::string& base_directory,const std::string& listening_address,uint16_t listening_port); private: // overloads RsTickingThread @@ -86,4 +86,6 @@ private: RsPgpFingerprint mRandomPeerBias; std::map mCurrentClientPeers; + std::string mListeningAddress; + uint16_t mListeningPort; }; diff --git a/retroshare-friendserver/src/network.cc b/retroshare-friendserver/src/network.cc index dca6e71a5..3f1f59964 100644 --- a/retroshare-friendserver/src/network.cc +++ b/retroshare-friendserver/src/network.cc @@ -40,8 +40,8 @@ #include "network.h" #include "friend_server/fsitem.h" -FsNetworkInterface::FsNetworkInterface() - : PQInterface(RsPeerId()),mFsNiMtx(std::string("FsNetworkInterface")) +FsNetworkInterface::FsNetworkInterface(const std::string& listening_address,uint16_t listening_port) + : PQInterface(RsPeerId()),mFsNiMtx(std::string("FsNetworkInterface")),mListeningAddress(listening_address),mListeningPort(listening_port) { RS_STACK_MUTEX(mFsNiMtx); @@ -54,9 +54,20 @@ FsNetworkInterface::FsNetworkInterface() struct sockaddr_in ipOfServer; memset(&ipOfServer, '0', sizeof(ipOfServer)); + assert(mListeningPort > 1024); + ipOfServer.sin_family = AF_INET; - ipOfServer.sin_port = htons(2017); // this is the port number of running server - ipOfServer.sin_addr.s_addr = htonl(INADDR_ANY); + ipOfServer.sin_port = htons(mListeningPort); // this is the port number of running server + + int addr[4]; + if(sscanf(listening_address.c_str(),"%d.%d.%d.%d",&addr[0],&addr[1],&addr[2],&addr[3]) != 4) + throw std::runtime_error("Cannot parse a proper IPv4 address in \""+listening_address+"\""); + + for(int i=0;i<4;++i) + if(addr[i] < 0 || addr[i] > 255) + throw std::runtime_error("Cannot parse a proper IPv4 address in \""+listening_address+"\""); + + ipOfServer.sin_addr.s_addr = htonl( (addr[0] << 24) + (addr[1] << 16) + (addr[2] << 8) + addr[3] ); if(bind(mClintListn, (struct sockaddr*)&ipOfServer , sizeof(ipOfServer)) < 0) { @@ -154,7 +165,7 @@ bool FsNetworkInterface::checkForNewConnections() RsSerialiser *rss = new RsSerialiser ; rss->addSerialType(new FsSerializer) ; - RsFdBinInterface *bio = new RsFdBinInterface(clintConnt); + RsFdBinInterface *bio = new RsFdBinInterface(clintConnt,true); auto pqi = new pqithreadstreamer(this,rss, pid, bio,BIN_FLAGS_READABLE | BIN_FLAGS_WRITEABLE); diff --git a/retroshare-friendserver/src/network.h b/retroshare-friendserver/src/network.h index 50e792613..fcec0648b 100644 --- a/retroshare-friendserver/src/network.h +++ b/retroshare-friendserver/src/network.h @@ -43,7 +43,7 @@ struct ConnectionData class FsNetworkInterface: public RsTickingThread, public PQInterface { public: - FsNetworkInterface() ; + FsNetworkInterface(const std::string& listening_address,uint16_t listening_port) ; virtual ~FsNetworkInterface() ; // basic functionality @@ -74,6 +74,9 @@ private: int mClintListn ; // listening socket std::map mConnections; + + std::string mListeningAddress; + uint16_t mListeningPort; }; diff --git a/retroshare-friendserver/src/retroshare-friendserver.cc b/retroshare-friendserver/src/retroshare-friendserver.cc index d4327c668..0d7463b5c 100644 --- a/retroshare-friendserver/src/retroshare-friendserver.cc +++ b/retroshare-friendserver/src/retroshare-friendserver.cc @@ -25,6 +25,9 @@ #include "util/rstime.h" #include "util/rsdebug.h" +#include "retroshare/rstor.h" +#include "retroshare/rsinit.h" + #include "friendserver.h" // debug @@ -41,7 +44,6 @@ int main(int argc, char* argv[]) "+================================================================+" << std::endl << std::endl; - //RsInit::InitRsConfig(); //RsControl::earlyInitNotificationSystem(); std::string base_directory = "FSData"; @@ -53,6 +55,12 @@ int main(int argc, char* argv[]) as.defaultErrorHandling(true, true); + RsConfigOptions conf; + conf.main_executable_path = argv[0]; + + RsInit::InitRsConfig(); + RsInit::InitRetroShare(conf); + // Create the base directory if needed if(!RsDirUtil::checkCreateDirectory(base_directory)) @@ -60,9 +68,34 @@ int main(int argc, char* argv[]) RsErr() << "Cannot create base directory \"" << base_directory << "\". Check permissions, paths, etc." ; return 1; } + // Create/start TorManager + + RsTor::setTorDataDirectory(RsDirUtil::makePath(base_directory,"tor")); + RsTor::setHiddenServiceDirectory(RsDirUtil::makePath(base_directory,"hidden_service")); + + if(! RsTor::start() || RsTor::hasError()) + { + RsErr() << "Tor cannot be started on your system: " << RsTor::errorMessage() ; + return 1 ; + } + + std::string service_id; + + while(RsTor::torStatus() != RsTorStatus::READY || RsTor::getHiddenServiceStatus(service_id) != RsTorHiddenServiceStatus::ONLINE) + std::this_thread::sleep_for(std::chrono::seconds(1)); + + std::string onion_address,service_target_address; + uint16_t service_port,target_port; + + RsTor::getHiddenServiceInfo(service_id,onion_address,service_port,service_target_address,target_port) ; + + RsDbg() << "Tor properly started: " ; + RsDbg() << " Hidden service address: " << onion_address << ":" << service_port; + RsDbg() << " Target address : " << service_target_address << ":" << target_port; + // Now start the real thing. - FriendServer fs(base_directory); + FriendServer fs(base_directory,service_target_address,target_port); fs.start(); while(fs.isRunning())