mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-07-22 14:10:54 -04:00
moved event loop in TorManager to the TorManager class
This commit is contained in:
parent
1c576411fb
commit
24e862ae25
5 changed files with 78 additions and 66 deletions
|
@ -38,6 +38,7 @@ enum class RsTorManagerEventCode: uint8_t
|
||||||
TOR_CONNECTIVITY_CHANGED = 0x03,
|
TOR_CONNECTIVITY_CHANGED = 0x03,
|
||||||
TOR_MANAGER_ERROR = 0x04,
|
TOR_MANAGER_ERROR = 0x04,
|
||||||
CONFIGURATION_NEEDED = 0x05,
|
CONFIGURATION_NEEDED = 0x05,
|
||||||
|
TOR_MANAGER_STOPPED = 0x06,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Status of the Tor hidden service setup/loaded by RS
|
// Status of the Tor hidden service setup/loaded by RS
|
||||||
|
|
|
@ -328,7 +328,7 @@ std::string TorManager::errorMessage() const
|
||||||
return d->errorMessage;
|
return d->errorMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TorManager::start()
|
bool TorManager::startTorManager()
|
||||||
{
|
{
|
||||||
if (!d->errorMessage.empty()) {
|
if (!d->errorMessage.empty()) {
|
||||||
d->errorMessage.clear();
|
d->errorMessage.clear();
|
||||||
|
@ -432,11 +432,39 @@ bool TorManager::start()
|
||||||
d->process->setExecutable(executable);
|
d->process->setExecutable(executable);
|
||||||
d->process->setDataDir(d->dataDir);
|
d->process->setDataDir(d->dataDir);
|
||||||
d->process->setDefaultTorrc(defaultTorrc);
|
d->process->setDefaultTorrc(defaultTorrc);
|
||||||
d->process->start();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::cerr << "Starting Tor manager thread:" << std::endl;
|
||||||
|
RsThread::start("TorManager");
|
||||||
return true ;
|
return true ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TorManager::run()
|
||||||
|
{
|
||||||
|
d->process->start();
|
||||||
|
|
||||||
|
while(!shouldStop())
|
||||||
|
{
|
||||||
|
threadTick();
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
||||||
|
}
|
||||||
|
|
||||||
|
d->control->shutdown();
|
||||||
|
d->process->stop();
|
||||||
|
|
||||||
|
if(rsEvents)
|
||||||
|
{
|
||||||
|
auto ev = std::make_shared<RsTorManagerEvent>();
|
||||||
|
ev->mTorManagerEventType = RsTorManagerEventCode::TOR_MANAGER_STOPPED;
|
||||||
|
rsEvents->sendEvent(ev);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void TorManager::threadTick()
|
||||||
|
{
|
||||||
|
d->process->tick();
|
||||||
|
}
|
||||||
|
|
||||||
bool TorManager::getProxyServerInfo(std::string& proxy_server_adress,uint16_t& proxy_server_port)
|
bool TorManager::getProxyServerInfo(std::string& proxy_server_adress,uint16_t& proxy_server_port)
|
||||||
{
|
{
|
||||||
proxy_server_adress = control()->socksAddress();
|
proxy_server_adress = control()->socksAddress();
|
||||||
|
@ -743,7 +771,7 @@ void RsTor::getProxyServerInfo(std::string& server_address, uint16_t& server_po
|
||||||
|
|
||||||
bool RsTor::start()
|
bool RsTor::start()
|
||||||
{
|
{
|
||||||
return instance()->start();
|
return instance()->startTorManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
void RsTor::setTorDataDirectory(const std::string& dir)
|
void RsTor::setTorDataDirectory(const std::string& dir)
|
||||||
|
|
|
@ -47,7 +47,7 @@ class TorManagerPrivate;
|
||||||
/* Run/connect to an instance of Tor according to configuration, and manage
|
/* Run/connect to an instance of Tor according to configuration, and manage
|
||||||
* UI interaction, first time configuration, etc. */
|
* UI interaction, first time configuration, etc. */
|
||||||
|
|
||||||
class TorManager : public HiddenServiceClient, public RsTor
|
class TorManager : public HiddenServiceClient, public RsThread, public RsTor
|
||||||
{
|
{
|
||||||
// Q_OBJECT
|
// Q_OBJECT
|
||||||
|
|
||||||
|
@ -65,7 +65,6 @@ public:
|
||||||
TorProcess *process();
|
TorProcess *process();
|
||||||
TorControl *control();
|
TorControl *control();
|
||||||
|
|
||||||
|
|
||||||
std::string torDataDirectory() const;
|
std::string torDataDirectory() const;
|
||||||
void setTorDataDirectory(const std::string &path);
|
void setTorDataDirectory(const std::string &path);
|
||||||
|
|
||||||
|
@ -87,7 +86,7 @@ public:
|
||||||
bool getProxyServerInfo(std::string &proxy_server_adress, uint16_t& proxy_server_port);
|
bool getProxyServerInfo(std::string &proxy_server_adress, uint16_t& proxy_server_port);
|
||||||
|
|
||||||
//public slots:
|
//public slots:
|
||||||
bool start();
|
bool startTorManager();
|
||||||
|
|
||||||
//private slots:
|
//private slots:
|
||||||
virtual void hiddenServiceOnline() override {} // do nothing here.
|
virtual void hiddenServiceOnline() override {} // do nothing here.
|
||||||
|
@ -95,9 +94,10 @@ public:
|
||||||
virtual void hiddenServiceHostnameChanged() override;
|
virtual void hiddenServiceHostnameChanged() override;
|
||||||
virtual void hiddenServiceStatusChanged(int old_status,int new_status) override;
|
virtual void hiddenServiceStatusChanged(int old_status,int new_status) override;
|
||||||
|
|
||||||
//signals:
|
// Thread stuff
|
||||||
// void configurationNeededChanged();
|
|
||||||
// void errorChanged();
|
virtual void run() override;
|
||||||
|
void threadTick() ;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit TorManager();
|
explicit TorManager();
|
||||||
|
|
|
@ -216,12 +216,7 @@ void TorProcess::start()
|
||||||
mControlPort = 0;
|
mControlPort = 0;
|
||||||
mControlHost.clear();
|
mControlHost.clear();
|
||||||
|
|
||||||
RsThread::start("TorControl");
|
// Launch the process
|
||||||
}
|
|
||||||
|
|
||||||
void TorProcess::run()
|
|
||||||
{
|
|
||||||
// We're inside the process control thread: launch the process,
|
|
||||||
|
|
||||||
std::vector<std::string> args;
|
std::vector<std::string> args;
|
||||||
|
|
||||||
|
@ -278,23 +273,25 @@ void TorProcess::run()
|
||||||
flags = fcntl(fd[STDOUT_FILENO], F_GETFL); fcntl(fd[STDOUT_FILENO], F_SETFL, flags | O_NONBLOCK);
|
flags = fcntl(fd[STDOUT_FILENO], F_GETFL); fcntl(fd[STDOUT_FILENO], F_SETFL, flags | O_NONBLOCK);
|
||||||
flags = fcntl(fd[STDERR_FILENO], F_GETFL); fcntl(fd[STDERR_FILENO], F_SETFL, flags | O_NONBLOCK);
|
flags = fcntl(fd[STDERR_FILENO], F_GETFL); fcntl(fd[STDERR_FILENO], F_SETFL, flags | O_NONBLOCK);
|
||||||
|
|
||||||
RsFdBinInterface stdout_FD(fd[STDOUT_FILENO]);
|
mStdOutFD = new RsFdBinInterface(fd[STDOUT_FILENO]);
|
||||||
RsFdBinInterface stderr_FD(fd[STDERR_FILENO]);
|
mStdErrFD = new RsFdBinInterface(fd[STDERR_FILENO]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TorProcess::tick()
|
||||||
|
{
|
||||||
|
mStdOutFD->tick();
|
||||||
|
mStdErrFD->tick();
|
||||||
|
|
||||||
unsigned char buff[1024];
|
unsigned char buff[1024];
|
||||||
|
|
||||||
while(!shouldStop())
|
|
||||||
{
|
|
||||||
stdout_FD.tick();
|
|
||||||
stderr_FD.tick();
|
|
||||||
int s;
|
int s;
|
||||||
|
|
||||||
if((s=stdout_FD.readline(buff,1024))) logMessage(std::string((char*)buff,s));
|
if((s=mStdOutFD->readline(buff,1024))) logMessage(std::string((char*)buff,s));
|
||||||
if((s=stderr_FD.readline(buff,1024))) logMessage(std::string((char*)buff,s));
|
if((s=mStdErrFD->readline(buff,1024))) logMessage(std::string((char*)buff,s));
|
||||||
|
|
||||||
if(!stdout_FD.isactive() && !stderr_FD.isactive())
|
if(!mStdOutFD->isactive() && !mStdErrFD->isactive())
|
||||||
{
|
{
|
||||||
RsErr() << "Tor process died. Exiting TorControl process." ;
|
RsErr() << "Tor process died. Exiting TorControl process." ;
|
||||||
|
stop();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
time_t now = time(nullptr);
|
time_t now = time(nullptr);
|
||||||
|
@ -316,15 +313,6 @@ void TorProcess::run()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Kill the Tor process since we've been asked to stop.
|
|
||||||
|
|
||||||
kill(mTorProcessId,SIGTERM);
|
|
||||||
int status=0;
|
|
||||||
wait(&status);
|
|
||||||
|
|
||||||
RsInfo() << "Tor process has been normally terminated. Exiting.";
|
|
||||||
}
|
|
||||||
|
|
||||||
void TorProcess::stop()
|
void TorProcess::stop()
|
||||||
{
|
{
|
||||||
if (state() < Starting)
|
if (state() < Starting)
|
||||||
|
@ -333,7 +321,9 @@ void TorProcess::stop()
|
||||||
while(mState == Starting)
|
while(mState == Starting)
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
|
|
||||||
fullstop();
|
kill(mTorProcessId,SIGTERM);
|
||||||
|
|
||||||
|
RsInfo() << "Tor process has been normally terminated. Exiting.";
|
||||||
|
|
||||||
mState = NotStarted;
|
mState = NotStarted;
|
||||||
|
|
||||||
|
@ -431,7 +421,7 @@ bool TorProcess::tryReadControlPort()
|
||||||
|
|
||||||
if (!mControlHost.empty() && mControlPort > 0)
|
if (!mControlHost.empty() && mControlPort > 0)
|
||||||
{
|
{
|
||||||
std::cerr << "Read control port = " << mControlPort << std::endl;
|
std::cerr << "Got control host/port = " << mControlHost << ":" << mControlPort << std::endl;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,6 +36,8 @@
|
||||||
#include "bytearray.h"
|
#include "bytearray.h"
|
||||||
#include "util/rsthreads.h"
|
#include "util/rsthreads.h"
|
||||||
|
|
||||||
|
class RsFdBinInterface ;
|
||||||
|
|
||||||
namespace Tor
|
namespace Tor
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -53,14 +55,8 @@ public:
|
||||||
|
|
||||||
/* Launches and controls a Tor instance with behavior suitable for bundling
|
/* Launches and controls a Tor instance with behavior suitable for bundling
|
||||||
* an instance with the application. */
|
* an instance with the application. */
|
||||||
class TorProcess: public RsThread
|
class TorProcess
|
||||||
{
|
{
|
||||||
//Q_OBJECT
|
|
||||||
//Q_ENUMS(State)
|
|
||||||
|
|
||||||
//Q_PROPERTY(State state READ state NOTIFY stateChanged)
|
|
||||||
//Q_PROPERTY(std::string errorMessage READ errorMessage NOTIFY errorMessageChanged)
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
enum State {
|
enum State {
|
||||||
Failed = -1,
|
Failed = -1,
|
||||||
|
@ -91,20 +87,14 @@ public:
|
||||||
unsigned short controlPort();
|
unsigned short controlPort();
|
||||||
ByteArray controlPassword();
|
ByteArray controlPassword();
|
||||||
|
|
||||||
//signals:
|
|
||||||
void stateChanged(int newState);
|
void stateChanged(int newState);
|
||||||
void errorMessageChanged(const std::string &errorMessage);
|
void errorMessageChanged(const std::string &errorMessage);
|
||||||
void logMessage(const std::string &message);
|
void logMessage(const std::string &message);
|
||||||
|
|
||||||
//public slots:
|
|
||||||
void start();
|
void start();
|
||||||
void stop();
|
void stop();
|
||||||
|
void tick();
|
||||||
|
|
||||||
// Implements RsThread
|
|
||||||
void run() override ;
|
|
||||||
|
|
||||||
// Keeps reading the output of the tor process and so on.
|
|
||||||
void threadTick();
|
|
||||||
private:
|
private:
|
||||||
TorProcessClient *m_client;
|
TorProcessClient *m_client;
|
||||||
|
|
||||||
|
@ -127,12 +117,15 @@ private:
|
||||||
pid_t mTorProcessId;
|
pid_t mTorProcessId;
|
||||||
time_t mLastTryReadControlPort ;
|
time_t mLastTryReadControlPort ;
|
||||||
int mControlPortReadNbTries ;
|
int mControlPortReadNbTries ;
|
||||||
//public slots:
|
|
||||||
void processStarted();
|
void processStarted();
|
||||||
void processFinished();
|
void processFinished();
|
||||||
void processError(std::string error);
|
void processError(std::string error);
|
||||||
void processReadable();
|
void processReadable();
|
||||||
bool tryReadControlPort();
|
bool tryReadControlPort();
|
||||||
|
|
||||||
|
RsFdBinInterface *mStdOutFD;
|
||||||
|
RsFdBinInterface *mStdErrFD;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue