made friend server to use tor hidden service to listen to connections

This commit is contained in:
csoler 2022-01-02 20:41:47 +01:00
parent 7367fb3e46
commit 896762b948
7 changed files with 71 additions and 12 deletions

View file

@ -1940,6 +1940,12 @@ int RsServer::StartupRetroShare()
std::string RsInit::executablePath() 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; return rsInitConfig->mainExecutablePath;
} }
bool RsInit::startAutoTor() bool RsInit::startAutoTor()

View file

@ -142,6 +142,8 @@ std::string TorManager::torDataDirectory() const
void TorManager::setTorDataDirectory(const std::string &path) void TorManager::setTorDataDirectory(const std::string &path)
{ {
assert(RsDirUtil::checkCreateDirectory(std::string(path)));
d->dataDir = path; d->dataDir = path;
if (!d->dataDir.empty() && !ByteArray(d->dataDir).endsWith('/')) if (!d->dataDir.empty() && !ByteArray(d->dataDir).endsWith('/'))

View file

@ -6,6 +6,7 @@
#include "pgp/pgpkeyutil.h" #include "pgp/pgpkeyutil.h"
#include "pgp/rscertificate.h" #include "pgp/rscertificate.h"
#include "pgp/openpgpsdkhandler.h"
#include "friendserver.h" #include "friendserver.h"
#include "friend_server/fsitem.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; std::cerr << "Computing peer distance: p1=" << p1 << " p2=" << p2 << " p1^p2=" << (p1^p2) << " distance=" << ((p1^p2)^mRandomPeerBias) << std::endl;
return (p1 ^ p2)^mRandomPeerBias; 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." ; RsDbg() << "Creating friend server." ;
mBaseDirectory = base_dir; 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_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. 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. // Random bias. Should be cryptographically safe.
@ -338,7 +340,7 @@ void FriendServer::run()
{ {
// 1 - create network interface. // 1 - create network interface.
mni = new FsNetworkInterface; mni = new FsNetworkInterface(mListeningAddress,mListeningPort);
mni->start(); mni->start();
while(!shouldStop()) { threadTick() ; } while(!shouldStop()) { threadTick() ; }

View file

@ -46,7 +46,7 @@ struct PeerInfo
class FriendServer : public RsTickingThread class FriendServer : public RsTickingThread
{ {
public: public:
FriendServer(const std::string& base_directory); FriendServer(const std::string& base_directory,const std::string& listening_address,uint16_t listening_port);
private: private:
// overloads RsTickingThread // overloads RsTickingThread
@ -86,4 +86,6 @@ private:
RsPgpFingerprint mRandomPeerBias; RsPgpFingerprint mRandomPeerBias;
std::map<RsPeerId, PeerInfo> mCurrentClientPeers; std::map<RsPeerId, PeerInfo> mCurrentClientPeers;
std::string mListeningAddress;
uint16_t mListeningPort;
}; };

View file

@ -40,8 +40,8 @@
#include "network.h" #include "network.h"
#include "friend_server/fsitem.h" #include "friend_server/fsitem.h"
FsNetworkInterface::FsNetworkInterface() FsNetworkInterface::FsNetworkInterface(const std::string& listening_address,uint16_t listening_port)
: PQInterface(RsPeerId()),mFsNiMtx(std::string("FsNetworkInterface")) : PQInterface(RsPeerId()),mFsNiMtx(std::string("FsNetworkInterface")),mListeningAddress(listening_address),mListeningPort(listening_port)
{ {
RS_STACK_MUTEX(mFsNiMtx); RS_STACK_MUTEX(mFsNiMtx);
@ -54,9 +54,20 @@ FsNetworkInterface::FsNetworkInterface()
struct sockaddr_in ipOfServer; struct sockaddr_in ipOfServer;
memset(&ipOfServer, '0', sizeof(ipOfServer)); memset(&ipOfServer, '0', sizeof(ipOfServer));
assert(mListeningPort > 1024);
ipOfServer.sin_family = AF_INET; ipOfServer.sin_family = AF_INET;
ipOfServer.sin_port = htons(2017); // this is the port number of running server ipOfServer.sin_port = htons(mListeningPort); // this is the port number of running server
ipOfServer.sin_addr.s_addr = htonl(INADDR_ANY);
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) if(bind(mClintListn, (struct sockaddr*)&ipOfServer , sizeof(ipOfServer)) < 0)
{ {
@ -154,7 +165,7 @@ bool FsNetworkInterface::checkForNewConnections()
RsSerialiser *rss = new RsSerialiser ; RsSerialiser *rss = new RsSerialiser ;
rss->addSerialType(new FsSerializer) ; 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); auto pqi = new pqithreadstreamer(this,rss, pid, bio,BIN_FLAGS_READABLE | BIN_FLAGS_WRITEABLE);

View file

@ -43,7 +43,7 @@ struct ConnectionData
class FsNetworkInterface: public RsTickingThread, public PQInterface class FsNetworkInterface: public RsTickingThread, public PQInterface
{ {
public: public:
FsNetworkInterface() ; FsNetworkInterface(const std::string& listening_address,uint16_t listening_port) ;
virtual ~FsNetworkInterface() ; virtual ~FsNetworkInterface() ;
// basic functionality // basic functionality
@ -74,6 +74,9 @@ private:
int mClintListn ; // listening socket int mClintListn ; // listening socket
std::map<RsPeerId,ConnectionData> mConnections; std::map<RsPeerId,ConnectionData> mConnections;
std::string mListeningAddress;
uint16_t mListeningPort;
}; };

View file

@ -25,6 +25,9 @@
#include "util/rstime.h" #include "util/rstime.h"
#include "util/rsdebug.h" #include "util/rsdebug.h"
#include "retroshare/rstor.h"
#include "retroshare/rsinit.h"
#include "friendserver.h" #include "friendserver.h"
// debug // debug
@ -41,7 +44,6 @@ int main(int argc, char* argv[])
"+================================================================+" "+================================================================+"
<< std::endl << std::endl; << std::endl << std::endl;
//RsInit::InitRsConfig();
//RsControl::earlyInitNotificationSystem(); //RsControl::earlyInitNotificationSystem();
std::string base_directory = "FSData"; std::string base_directory = "FSData";
@ -53,6 +55,12 @@ int main(int argc, char* argv[])
as.defaultErrorHandling(true, true); as.defaultErrorHandling(true, true);
RsConfigOptions conf;
conf.main_executable_path = argv[0];
RsInit::InitRsConfig();
RsInit::InitRetroShare(conf);
// Create the base directory if needed // Create the base directory if needed
if(!RsDirUtil::checkCreateDirectory(base_directory)) 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." ; RsErr() << "Cannot create base directory \"" << base_directory << "\". Check permissions, paths, etc." ;
return 1; 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. // Now start the real thing.
FriendServer fs(base_directory); FriendServer fs(base_directory,service_target_address,target_port);
fs.start(); fs.start();
while(fs.isRunning()) while(fs.isRunning())