From 850554429432964ed0bcab275be2c590b7b07bcb Mon Sep 17 00:00:00 2001 From: csoler Date: Sat, 3 Jul 2021 21:54:44 +0200 Subject: [PATCH] removed signals in HiddenService class --- libretroshare/src/tor/HiddenService.cpp | 27 +++++++++++++++---------- libretroshare/src/tor/HiddenService.h | 26 +++++++++++++++--------- libretroshare/src/tor/TorManager.cpp | 19 +++++++++++------ libretroshare/src/tor/TorManager.h | 14 +++++++------ 4 files changed, 54 insertions(+), 32 deletions(-) diff --git a/libretroshare/src/tor/HiddenService.cpp b/libretroshare/src/tor/HiddenService.cpp index b2b58626a..22b7200fe 100644 --- a/libretroshare/src/tor/HiddenService.cpp +++ b/libretroshare/src/tor/HiddenService.cpp @@ -42,13 +42,13 @@ using namespace Tor; -HiddenService::HiddenService(QObject *parent) - : QObject(parent), m_status(NotCreated) +HiddenService::HiddenService(HiddenServiceClient *client) + : m_status(NotCreated), m_client(client) { } -HiddenService::HiddenService(const QString &path, QObject *parent) - : QObject(parent), m_dataPath(path), m_status(NotCreated) +HiddenService::HiddenService(HiddenServiceClient *client,const QString &path) + : m_dataPath(path), m_status(NotCreated), m_client(client) { /* Set the initial status and, if possible, load the hostname */ if (QDir(m_dataPath).exists(QLatin1String("private_key"))) { @@ -58,8 +58,8 @@ HiddenService::HiddenService(const QString &path, QObject *parent) } } -HiddenService::HiddenService(const CryptoKey &privateKey, const QString &path, QObject *parent) - : QObject(parent), m_dataPath(path), m_status(NotCreated) +HiddenService::HiddenService(HiddenServiceClient *client,const CryptoKey &privateKey, const QString &path) + : m_dataPath(path), m_status(NotCreated), m_client(client) { setPrivateKey(privateKey); m_status = Offline; @@ -73,10 +73,12 @@ void HiddenService::setStatus(Status newStatus) Status old = m_status; m_status = newStatus; - emit statusChanged(m_status, old); + if(m_client) + m_client->hiddenServiceStatusChanged(m_status,old); //emit statusChanged(m_status, old); if (m_status == Online) - emit serviceOnline(); + if(m_client) + m_client->hiddenServiceOnline(); //emit serviceOnline(); } void HiddenService::addTarget(const Target &target) @@ -95,7 +97,8 @@ void HiddenService::setServiceId(const QByteArray& sid) m_service_id = sid; m_hostname = sid + ".onion"; - emit hostnameChanged(); + if(m_client) + m_client->hiddenServiceHostnameChanged(); // emit hostnameChanged(); } void HiddenService::setPrivateKey(const CryptoKey &key) { @@ -113,7 +116,8 @@ void HiddenService::setPrivateKey(const CryptoKey &key) m_privateKey = key; - emit privateKeyChanged(); + if(m_client) + m_client->hiddenServicePrivateKeyChanged(); //emit privateKeyChanged(); } void HiddenService::loadPrivateKey() @@ -128,7 +132,8 @@ void HiddenService::loadPrivateKey() return; } - emit privateKeyChanged(); + if(m_client) + m_client->hiddenServicePrivateKeyChanged(); // emit privateKeyChanged(); } void HiddenService::servicePublished() diff --git a/libretroshare/src/tor/HiddenService.h b/libretroshare/src/tor/HiddenService.h index 20fa1d851..4efa55582 100644 --- a/libretroshare/src/tor/HiddenService.h +++ b/libretroshare/src/tor/HiddenService.h @@ -43,6 +43,18 @@ namespace Tor class TorSocket; +// This class is used to receive synchroneous notifications from the hidden service. +// Each client should implement its own notification handling. + +class HiddenServiceClient +{ +public: + virtual void hiddenServiceStatusChanged(int /* newStatus */, int /* oldStatus */) =0; + virtual void hiddenServiceOnline() =0; + virtual void hiddenServicePrivateKeyChanged() =0; + virtual void hiddenServiceHostnameChanged() =0; +}; + class HiddenService : public QObject { Q_OBJECT @@ -64,9 +76,9 @@ public: Online /* Published */ }; - HiddenService(QObject *parent = 0); - HiddenService(const QString &dataPath, QObject *parent = 0); - HiddenService(const CryptoKey &privateKey, const QString &dataPath = QString(), QObject *parent = 0); + HiddenService(HiddenServiceClient *client); + HiddenService(HiddenServiceClient *client,const QString &dataPath); + HiddenService(HiddenServiceClient *client,const CryptoKey &privateKey, const QString &dataPath = QString()); Status status() const { return m_status; } @@ -82,12 +94,6 @@ public: void addTarget(const Target &target); void addTarget(quint16 servicePort, QHostAddress targetAddress, quint16 targetPort); -signals: - void statusChanged(int newStatus, int oldStatus); - void serviceOnline(); - void privateKeyChanged(); - void hostnameChanged(); - private slots: void servicePublished(); @@ -101,6 +107,8 @@ private: void loadPrivateKey(); void setStatus(Status newStatus); + + HiddenServiceClient *m_client; }; } diff --git a/libretroshare/src/tor/TorManager.cpp b/libretroshare/src/tor/TorManager.cpp index fc81d943e..7fa645beb 100644 --- a/libretroshare/src/tor/TorManager.cpp +++ b/libretroshare/src/tor/TorManager.cpp @@ -175,23 +175,24 @@ bool TorManager::setupHiddenService() return false; } - d->hiddenService = new Tor::HiddenService(key, legacyDir, this); + d->hiddenService = new Tor::HiddenService(this,key, legacyDir); std::cerr << "Got key from legacy dir: " << std::endl; std::cerr << key.bytes().toStdString() << std::endl; } else { - d->hiddenService = new Tor::HiddenService(legacyDir, this); + d->hiddenService = new Tor::HiddenService(this,legacyDir); std::cerr << "Creating new hidden service." << std::endl; - connect(d->hiddenService, SIGNAL(privateKeyChanged()), this, SLOT(hiddenServicePrivateKeyChanged())) ; - connect(d->hiddenService, SIGNAL(hostnameChanged()), this, SLOT(hiddenServiceHostnameChanged())) ; + // connect(d->hiddenService, SIGNAL(privateKeyChanged()), this, SLOT(hiddenServicePrivateKeyChanged())) ; + // connect(d->hiddenService, SIGNAL(hostnameChanged()), this, SLOT(hiddenServiceHostnameChanged())) ; } - Q_ASSERT(d->hiddenService); - connect(d->hiddenService, SIGNAL(statusChanged(int,int)), this, SLOT(hiddenServiceStatusChanged(int,int))); + assert(d->hiddenService); + + // connect(d->hiddenService, SIGNAL(statusChanged(int,int)), this, SLOT(hiddenServiceStatusChanged(int,int))); // Generally, these are not used, and we bind to localhost and port 0 // for an automatic (and portable) selection. @@ -225,6 +226,9 @@ void TorManager::hiddenServiceStatusChanged(int old_status,int new_status) void TorManager::hiddenServicePrivateKeyChanged() { + if(!d->hiddenService) + return ; + QString key = QString::fromLatin1(d->hiddenService->privateKey().bytes()); QFile outfile(d->hiddenServiceDir + QLatin1String("/private_key")) ; @@ -249,6 +253,9 @@ void TorManager::hiddenServicePrivateKeyChanged() void TorManager::hiddenServiceHostnameChanged() { + if(!d->hiddenService) + return ; + QFile outfile2(d->hiddenServiceDir + QLatin1String("/hostname")) ; outfile2.open( QIODevice::WriteOnly | QIODevice::Text ); QTextStream t(&outfile2); diff --git a/libretroshare/src/tor/TorManager.h b/libretroshare/src/tor/TorManager.h index 9d3dd388d..3c9733519 100644 --- a/libretroshare/src/tor/TorManager.h +++ b/libretroshare/src/tor/TorManager.h @@ -36,6 +36,7 @@ #define TORMANAGER_H #include "retroshare/rstor.h" +#include "HiddenService.h" #include #include @@ -51,7 +52,7 @@ class TorManagerPrivate; /* Run/connect to an instance of Tor according to configuration, and manage * UI interaction, first time configuration, etc. */ -class TorManager : public QObject, public RsTor +class TorManager : public QObject, public HiddenServiceClient, public RsTor { Q_OBJECT @@ -90,13 +91,14 @@ public: bool getHiddenServiceInfo(QString& service_id,QString& service_onion_address,uint16_t& service_port, QHostAddress& service_target_address,uint16_t& target_port); bool getProxyServerInfo(QHostAddress& proxy_server_adress,uint16_t& proxy_server_port); -public slots: +//public slots: bool start(); -private slots: - void hiddenServicePrivateKeyChanged(); - void hiddenServiceHostnameChanged(); - void hiddenServiceStatusChanged(int old_status,int new_status); +//private slots: + virtual void hiddenServiceOnline() override {} // do nothing here. + virtual void hiddenServicePrivateKeyChanged() override; + virtual void hiddenServiceHostnameChanged() override; + virtual void hiddenServiceStatusChanged(int old_status,int new_status) override; signals: void configurationNeededChanged();