removed more QString, QHostAddress, QFile, QDir,...

This commit is contained in:
csoler 2021-12-05 00:02:12 +01:00
parent b03802fa1b
commit bb37e2692b
17 changed files with 151 additions and 84 deletions

View file

@ -48,6 +48,7 @@ public:
int netstatus() override; int netstatus() override;
int isactive() override; int isactive() override;
bool moretoread(uint32_t usec) override; bool moretoread(uint32_t usec) override;
bool moretowrite(uint32_t usec) { return mTotalOutBufferBytes > 0 ; }
bool cansend(uint32_t usec) override; bool cansend(uint32_t usec) override;
int close() override; int close() override;

View file

@ -220,6 +220,7 @@ public:
static void setAutoLogin(bool autoLogin); static void setAutoLogin(bool autoLogin);
static bool RsClearAutoLogin() ; static bool RsClearAutoLogin() ;
static std::string executablePath() ;
private: private:
/** @brief Lock profile directory /** @brief Lock profile directory
* param[in] accountDir account directory to lock * param[in] accountDir account directory to lock

View file

@ -146,6 +146,7 @@ struct RsInitConfig
{} {}
RsFileHash main_executable_hash; RsFileHash main_executable_hash;
std::string mainExecutablePath;
#ifdef WINDOWS_SYS #ifdef WINDOWS_SYS
bool portable; bool portable;
@ -307,6 +308,7 @@ int RsInit::InitRetroShare(const RsConfigOptions& conf)
rsInitConfig->optBaseDir = conf.optBaseDir; rsInitConfig->optBaseDir = conf.optBaseDir;
rsInitConfig->jsonApiPort = conf.jsonApiPort; rsInitConfig->jsonApiPort = conf.jsonApiPort;
rsInitConfig->jsonApiBindAddress = conf.jsonApiBindAddress; rsInitConfig->jsonApiBindAddress = conf.jsonApiBindAddress;
rsInitConfig->mainExecutablePath = conf.main_executable_path;
#ifdef PTW32_STATIC_LIB #ifdef PTW32_STATIC_LIB
// for static PThreads under windows... we need to init the library... // for static PThreads under windows... we need to init the library...
@ -1932,6 +1934,10 @@ int RsServer::StartupRetroShare()
return 1; return 1;
} }
std::string RsInit::executablePath()
{
return rsInitConfig->mainExecutablePath;
}
bool RsInit::startAutoTor() bool RsInit::startAutoTor()
{ {
std::cerr << "(II) node is an automated Tor node => launching Tor auto-configuration." << std::endl; std::cerr << "(II) node is an automated Tor node => launching Tor auto-configuration." << std::endl;

View file

@ -65,7 +65,7 @@ ByteArray AddOnionCommand::build()
out += " Port="; out += " Port=";
out += QByteArray::number(target.servicePort); out += QByteArray::number(target.servicePort);
out += ","; out += ",";
out += target.targetAddress.toString().toLatin1(); out += target.targetAddress;
out += ":"; out += ":";
out += QByteArray::number(target.targetPort); out += QByteArray::number(target.targetPort);
} }

View file

@ -83,7 +83,7 @@ void HiddenService::addTarget(const Target &target)
m_targets.push_back(target); m_targets.push_back(target);
} }
void HiddenService::addTarget(quint16 servicePort, QHostAddress targetAddress, quint16 targetPort) void HiddenService::addTarget(quint16 servicePort, std::string targetAddress, quint16 targetPort)
{ {
Target t = { targetAddress, servicePort, targetPort }; Target t = { targetAddress, servicePort, targetPort };
m_targets.push_back(t); m_targets.push_back(t);

View file

@ -34,7 +34,6 @@
#define HIDDENSERVICE_H #define HIDDENSERVICE_H
#include <QObject> #include <QObject>
#include <QHostAddress>
#include "CryptoKey.h" #include "CryptoKey.h"
#include "bytearray.h" #include "bytearray.h"
@ -63,7 +62,7 @@ class HiddenService : public QObject
public: public:
struct Target struct Target
{ {
QHostAddress targetAddress; std::string targetAddress;
quint16 servicePort, targetPort; quint16 servicePort, targetPort;
}; };
@ -90,7 +89,7 @@ public:
const std::list<Target> &targets() const { return m_targets; } const std::list<Target> &targets() const { return m_targets; }
void addTarget(const Target &target); void addTarget(const Target &target);
void addTarget(quint16 servicePort, QHostAddress targetAddress, quint16 targetPort); void addTarget(quint16 servicePort, std::string targetAddress, quint16 targetPort);
private slots: private slots:
void servicePublished(); void servicePublished();

View file

@ -83,11 +83,11 @@ public:
TorControl *q; TorControl *q;
TorControlSocket *socket; TorControlSocket *socket;
QHostAddress torAddress; std::string torAddress;
std::string errorMessage; std::string errorMessage;
std::string torVersion; std::string torVersion;
ByteArray authPassword; ByteArray authPassword;
QHostAddress socksAddress; std::string socksAddress;
QList<HiddenService*> services; QList<HiddenService*> services;
quint16 controlPort, socksPort; quint16 controlPort, socksPort;
TorControl::Status status; TorControl::Status status;
@ -282,7 +282,7 @@ void TorControl::setAuthPassword(const ByteArray &password)
d->authPassword = password; d->authPassword = password;
} }
void TorControl::connect(const QHostAddress &address, quint16 port) void TorControl::connect(const std::string &address, quint16 port)
{ {
if (status() > Connecting) if (status() > Connecting)
{ {
@ -294,9 +294,9 @@ void TorControl::connect(const QHostAddress &address, quint16 port)
d->controlPort = port; d->controlPort = port;
d->setTorStatus(TorUnknown); d->setTorStatus(TorUnknown);
bool b = d->socket->blockSignals(true); //bool b = d->socket->blockSignals(true);
d->socket->abort(); d->socket->fullstop();
d->socket->blockSignals(b); //d->socket->blockSignals(b);
d->setStatus(Connecting); d->setStatus(Connecting);
d->socket->connectToHost(address, port); d->socket->connectToHost(address, port);
@ -304,8 +304,9 @@ void TorControl::connect(const QHostAddress &address, quint16 port)
void TorControl::reconnect() void TorControl::reconnect()
{ {
Q_ASSERT(!d->torAddress.isNull() && d->controlPort); assert(!d->torAddress.empty() && d->controlPort);
if (d->torAddress.isNull() || !d->controlPort || status() >= Connecting)
if (d->torAddress.empty() || !d->controlPort || status() >= Connecting)
return; return;
d->setStatus(Connecting); d->setStatus(Connecting);
@ -332,7 +333,7 @@ void TorControlPrivate::authenticateReply()
TorControlCommand *clientEvents = new TorControlCommand; TorControlCommand *clientEvents = new TorControlCommand;
connect(clientEvents, &TorControlCommand::replyLine, this, &TorControlPrivate::statusEvent); connect(clientEvents, &TorControlCommand::replyLine, this, &TorControlPrivate::statusEvent);
socket->registerEvent("STATUS_CLIENT", clientEvents); socket->registerEvent(ByteArray("STATUS_CLIENT"), clientEvents);
getTorInfo(); getTorInfo();
publishServices(); publishServices();
@ -400,11 +401,15 @@ void TorControlPrivate::protocolInfoReply()
std::string cookieError; std::string cookieError;
torCtrlDebug() << "torctrl: Using cookie authentication with file" << cookieFile << std::endl; torCtrlDebug() << "torctrl: Using cookie authentication with file" << cookieFile << std::endl;
QFile file(cookieFile); FILE *f = fopen(cookieFile.c_str(),"r");
if (file.open(QIODevice::ReadOnly))
if(f)
{ {
ByteArray cookie = file.readAll(); std::string cookie;
file.close(); char c;
while((c=getc(f))!=EOF)
cookie += c;
fclose(f);
/* Simple test to avoid a vulnerability where any process listening on what we think is /* Simple test to avoid a vulnerability where any process listening on what we think is
* the control port could trick us into sending the contents of an arbitrary file */ * the control port could trick us into sending the contents of an arbitrary file */
@ -414,7 +419,7 @@ void TorControlPrivate::protocolInfoReply()
cookieError = "Unexpected file size"; cookieError = "Unexpected file size";
} }
else else
cookieError = file.errorString(); cookieError = "Cannot open file " + cookieFile + ". errno=" + RsUtil::NumberToString(errno);
if (!cookieError.empty() || data.isNull()) if (!cookieError.empty() || data.isNull())
{ {
@ -498,13 +503,13 @@ void TorControlPrivate::getTorInfoReply()
for (const auto& add:listenAddresses) { for (const auto& add:listenAddresses) {
ByteArray value = unquotedString(add); ByteArray value = unquotedString(add);
int sepp = value.indexOf(':'); int sepp = value.indexOf(':');
QHostAddress address(value.mid(0, sepp)); std::string address(value.mid(0, sepp).toString());
quint16 port = (quint16)value.mid(sepp+1).toUInt(); quint16 port = (quint16)value.mid(sepp+1).toInt();
/* Use the first address that matches the one used for this control connection. If none do, /* Use the first address that matches the one used for this control connection. If none do,
* just use the first address and rely on the user to reconfigure if necessary (not a problem; * just use the first address and rely on the user to reconfigure if necessary (not a problem;
* their setup is already very customized) */ * their setup is already very customized) */
if (socksAddress.isNull() || address == socket->peerAddress()) { if (socksAddress.empty() || address == socket->peerAddress()) {
socksAddress = address; socksAddress = address;
socksPort = port; socksPort = port;
if (address == socket->peerAddress()) if (address == socket->peerAddress())
@ -515,8 +520,8 @@ void TorControlPrivate::getTorInfoReply()
/* It is not immediately an error to have no SOCKS address; when DisableNetwork is set there won't be a /* It is not immediately an error to have no SOCKS address; when DisableNetwork is set there won't be a
* listener yet. To handle that situation, we'll try to read the socks address again when TorReady state * listener yet. To handle that situation, we'll try to read the socks address again when TorReady state
* is reached. */ * is reached. */
if (!socksAddress.isNull()) { if (!socksAddress.empty()) {
torCtrlDebug() << "torctrl: SOCKS address is " << socksAddress.toString().toStdString() << ":" << socksPort << std::endl; torCtrlDebug() << "torctrl: SOCKS address is " << socksAddress << ":" << socksPort << std::endl;
if(rsEvents) if(rsEvents)
{ {
@ -601,8 +606,7 @@ void TorControlPrivate::publishServices()
torCtrlDebug() << "torctrl: Configuring hidden service at" << service->dataPath() << std::endl; torCtrlDebug() << "torctrl: Configuring hidden service at" << service->dataPath() << std::endl;
QDir dir(service->dataPath()); torConfig.push_back(std::make_pair("HiddenServiceDir", service->dataPath()));
torConfig.append(qMakePair(ByteArray("HiddenServiceDir"), dir.absolutePath().toLocal8Bit()));
const std::list<HiddenService::Target> &targets = service->targets(); const std::list<HiddenService::Target> &targets = service->targets();
for (auto tit:targets) for (auto tit:targets)
@ -610,7 +614,7 @@ void TorControlPrivate::publishServices()
std::string target = RsUtil::NumberToString(tit.servicePort) + " " std::string target = RsUtil::NumberToString(tit.servicePort) + " "
+tit.targetAddress + ":" +tit.targetAddress + ":"
+RsUtil::NumberToString(tit.targetPort); +RsUtil::NumberToString(tit.targetPort);
torConfig.append(qMakePair(ByteArray("HiddenServicePort"), target)); torConfig.push_back(std::make_pair("HiddenServicePort", target));
} }
QObject::connect(command, &SetConfCommand::setConfSucceeded, service, &HiddenService::servicePublished); QObject::connect(command, &SetConfCommand::setConfSucceeded, service, &HiddenService::servicePublished);
@ -628,7 +632,7 @@ void TorControl::shutdown()
return; return;
} }
d->socket->sendCommand("SIGNAL SHUTDOWN\r\n"); d->socket->sendCommand(ByteArray("SIGNAL SHUTDOWN\r\n"));
} }
void TorControl::shutdownSync() void TorControl::shutdownSync()
@ -639,11 +643,10 @@ void TorControl::shutdownSync()
} }
shutdown(); shutdown();
while (d->socket->bytesToWrite()) while (d->socket->moretowrite())
{ std::this_thread::sleep_for(std::chrono::milliseconds(100));
if (!d->socket->waitForBytesWritten(5000))
return; d->socket->close();
}
} }
void TorControlPrivate::statusEvent(int code, const ByteArray &data) void TorControlPrivate::statusEvent(int code, const ByteArray &data)
@ -835,7 +838,7 @@ bool TorControl::hasOwnership() const
void TorControl::takeOwnership() void TorControl::takeOwnership()
{ {
d->hasOwnership = true; d->hasOwnership = true;
d->socket->sendCommand("TAKEOWNERSHIP\r\n"); d->socket->sendCommand(ByteArray("TAKEOWNERSHIP\r\n"));
// Reset PID-based polling // Reset PID-based polling
std::list<std::pair<std::string,std::string> > options; std::list<std::pair<std::string,std::string> > options;

View file

@ -101,7 +101,7 @@ public:
/* Connection */ /* Connection */
bool isConnected() const { return status() == Connected; } bool isConnected() const { return status() == Connected; }
void connect(const QHostAddress &address, quint16 port); void connect(const std::string &address, quint16 port);
/* Ownership means that tor is managed by this socket, and we /* Ownership means that tor is managed by this socket, and we
* can shut it down, own its configuration, etc. */ * can shut it down, own its configuration, etc. */

View file

@ -49,6 +49,13 @@ TorControlSocket::~TorControlSocket()
clear(); clear();
} }
std::string TorControlSocket::peerAddress() const
{
if(connectionState() == State::CONNECTED)
return connectAddress();
else
return std::string();
}
void TorControlSocket::sendCommand(TorControlCommand *command, const ByteArray& data) void TorControlSocket::sendCommand(TorControlCommand *command, const ByteArray& data)
{ {
assert(data.endsWith(ByteArray("\r\n"))); assert(data.endsWith(ByteArray("\r\n")));
@ -78,10 +85,12 @@ void TorControlSocket::registerEvent(const ByteArray &event, TorControlCommand *
void TorControlSocket::clear() void TorControlSocket::clear()
{ {
qDeleteAll(commandQueue); for(auto cmd:commandQueue) delete cmd;
commandQueue.clear(); commandQueue.clear();
qDeleteAll(eventCommands);
for(auto cmd:eventCommands) delete cmd.second;
eventCommands.clear(); eventCommands.clear();
inDataReply = false; inDataReply = false;
currentCommand = 0; currentCommand = 0;
} }

View file

@ -48,6 +48,7 @@ public:
std::string errorMessage() const { return m_errorMessage; } std::string errorMessage() const { return m_errorMessage; }
void connectToHost(const std::string& tcp_address,uint16_t tcp_port);
void registerEvent(const ByteArray &event, TorControlCommand *handler); void registerEvent(const ByteArray &event, TorControlCommand *handler);
void sendCommand(const ByteArray& data) { sendCommand(0, data); } void sendCommand(const ByteArray& data) { sendCommand(0, data); }
@ -59,6 +60,7 @@ public:
virtual int tick() override; virtual int tick() override;
std::string peerAddress() const;
//signals: //signals:
void error(const std::string& message); void error(const std::string& message);

View file

@ -41,6 +41,9 @@
#include <sys/syscall.h> #include <sys/syscall.h>
#endif #endif
#include "util/rsdir.h"
#include "retroshare/rsinit.h"
#include <QObject> #include <QObject>
#include "TorManager.h" #include "TorManager.h"
@ -136,10 +139,10 @@ std::string TorManager::torDataDirectory() const
void TorManager::setTorDataDirectory(const std::string &path) void TorManager::setTorDataDirectory(const std::string &path)
{ {
d->dataDir = QDir::fromNativeSeparators(path); d->dataDir = path;
if (!d->dataDir.isEmpty() && !d->dataDir.endsWith(QLatin1Char('/'))) if (!d->dataDir.empty() && !ByteArray(d->dataDir).endsWith('/'))
d->dataDir.append(QLatin1Char('/')); d->dataDir += '/';
} }
std::string TorManager::hiddenServiceDirectory() const std::string TorManager::hiddenServiceDirectory() const
@ -148,7 +151,7 @@ std::string TorManager::hiddenServiceDirectory() const
} }
void TorManager::setHiddenServiceDirectory(const std::string &path) void TorManager::setHiddenServiceDirectory(const std::string &path)
{ {
d->hiddenServiceDir = QDir::fromNativeSeparators(path); d->hiddenServiceDir = path;
if (!d->hiddenServiceDir.empty() && !(d->hiddenServiceDir.back() == '/')) if (!d->hiddenServiceDir.empty() && !(d->hiddenServiceDir.back() == '/'))
d->hiddenServiceDir += '/'; d->hiddenServiceDir += '/';
@ -265,16 +268,15 @@ void TorManager::hiddenServiceHostnameChanged()
if(!d->hiddenService) if(!d->hiddenService)
return ; return ;
QFile outfile2(d->hiddenServiceDir + QLatin1String("/hostname")) ; std::string outfile2_name = RsDirUtil::makePath(d->hiddenServiceDir,"/hostname") ;
outfile2.open( QIODevice::WriteOnly | QIODevice::Text ); std::ofstream of(outfile2_name);
QTextStream t(&outfile2);
std::string hostname(d->hiddenService->hostname()); std::string hostname(d->hiddenService->hostname());
t << hostname << endl; of << hostname << std::endl;
outfile2.close(); of.close();
std::cerr << "Hidden service hostname changed: " << hostname.toStdString() << std::endl; std::cerr << "Hidden service hostname changed: " << hostname << std::endl;
} }
bool TorManager::configurationNeeded() const bool TorManager::configurationNeeded() const
@ -282,14 +284,14 @@ bool TorManager::configurationNeeded() const
return d->configNeeded; return d->configNeeded;
} }
std::string TorManager::logMessages() const const std::list<std::string>& TorManager::logMessages() const
{ {
return d->logMessages; return d->logMessages;
} }
bool TorManager::hasError() const bool TorManager::hasError() const
{ {
return !d->errorMessage.isEmpty(); return !d->errorMessage.empty();
} }
std::string TorManager::errorMessage() const std::string TorManager::errorMessage() const
@ -299,7 +301,7 @@ std::string TorManager::errorMessage() const
bool TorManager::start() bool TorManager::start()
{ {
if (!d->errorMessage.isEmpty()) { if (!d->errorMessage.empty()) {
d->errorMessage.clear(); d->errorMessage.clear();
//emit errorChanged(); // not needed because there's no error to handle //emit errorChanged(); // not needed because there's no error to handle
@ -346,9 +348,9 @@ bool TorManager::start()
// Launch a bundled Tor instance // Launch a bundled Tor instance
std::string executable = d->torExecutablePath(); std::string executable = d->torExecutablePath();
std::cerr << "Executable path: " << executable.toStdString() << std::endl; std::cerr << "Executable path: " << executable << std::endl;
if (executable.isEmpty()) { if (executable.empty()) {
d->setError("Cannot find tor executable"); d->setError("Cannot find tor executable");
return false; return false;
} }
@ -361,20 +363,27 @@ bool TorManager::start()
// QObject::connect(d->process, SIGNAL(logMessage(std::string)), d, SLOT(processLogMessage(std::string))); // QObject::connect(d->process, SIGNAL(logMessage(std::string)), d, SLOT(processLogMessage(std::string)));
} }
if (!QFile::exists(d->dataDir) && !d->createDataDir(d->dataDir)) { if (!RsDirUtil::checkCreateDirectory(d->dataDir))
{
d->setError(std::string("Cannot write data location: ") + d->dataDir); d->setError(std::string("Cannot write data location: ") + d->dataDir);
return false; return false;
} }
std::string defaultTorrc = d->dataDir + "default_torrc"; std::string defaultTorrc = RsDirUtil::makePath(d->dataDir,"default_torrc");
if (!QFile::exists(defaultTorrc) && !d->createDefaultTorrc(defaultTorrc))
if (!RsDirUtil::fileExists(defaultTorrc) && !d->createDefaultTorrc(defaultTorrc))
{ {
d->setError("Cannot write data files: ")+defaultTorrc); d->setError("Cannot write data files: "+defaultTorrc);
return false; return false;
} }
QFile torrc(d->dataDir + "torrc"); std::string torrc = RsDirUtil::makePath(d->dataDir,"torrc");
if (!torrc.exists() || torrc.size() == 0) { uint64_t file_size;
bool torrc_exists = RsDirUtil::checkFile(torrc,file_size);
if(!torrc_exists || torrc.size() == 0)
{
d->configNeeded = true; d->configNeeded = true;
if(rsEvents) if(rsEvents)
@ -387,9 +396,9 @@ bool TorManager::start()
} }
std::cerr << "Starting Tor process:" << std::endl; std::cerr << "Starting Tor process:" << std::endl;
std::cerr << " Tor executable path: " << executable.toStdString() << std::endl; std::cerr << " Tor executable path: " << executable << std::endl;
std::cerr << " Tor data directory : " << d->dataDir.toStdString() << std::endl; std::cerr << " Tor data directory : " << d->dataDir << std::endl;
std::cerr << " Tor default torrc : " << defaultTorrc.toStdString() << std::endl; std::cerr << " Tor default torrc : " << defaultTorrc << std::endl;
d->process->setExecutable(executable); d->process->setExecutable(executable);
d->process->setDataDir(d->dataDir); d->process->setDataDir(d->dataDir);
@ -435,8 +444,9 @@ bool TorManager::getHiddenServiceInfo(std::string& service_id,std::string& servi
void TorManagerPrivate::processStateChanged(int state) void TorManagerPrivate::processStateChanged(int state)
{ {
std::cerr << Q_FUNC_INFO << "state: " << state << " passwd=\"" << std::string(process->controlPassword()).toStdString() << "\" " << process->controlHost().toString().toStdString() RsInfo() << "state: " << state << " passwd=\"" << process->controlPassword().toString() << "\" " << process->controlHost().toString().toStdString()
<< ":" << process->controlPort() << std::endl; << ":" << process->controlPort() << std::endl;
if (state == TorProcess::Ready) { if (state == TorProcess::Ready) {
control->setAuthPassword(process->controlPassword()); control->setAuthPassword(process->controlPassword());
control->connect(process->controlHost(), process->controlPort()); control->connect(process->controlHost(), process->controlPort());
@ -479,7 +489,11 @@ void TorManagerPrivate::getConfFinished()
if (!command) if (!command)
return; return;
if (command->get("DisableNetwork").toInt() == 1 && !configNeeded) { int n;
for(auto str:command->get("DisableNetwork"))
if(RsUtil::StringToInt(str,n) && n==1 && !configNeeded)
{
configNeeded = true; configNeeded = true;
//emit q->configurationNeededChanged(); //emit q->configurationNeededChanged();
@ -509,34 +523,37 @@ std::string TorManagerPrivate::torExecutablePath() const
std::string filename("/tor"); std::string filename("/tor");
#endif #endif
path = qApp->applicationDirPath(); path = RsDirUtil::getDirectory(RsInit::executablePath());
std::string tor_exe_path = RsDirUtil::makePath(path,filename);
if (QFile::exists(path + filename)) if (RsDirUtil::fileExists(tor_exe_path))
return path + filename; return tor_exe_path;
#ifdef BUNDLED_TOR_PATH #ifdef BUNDLED_TOR_PATH
path = BUNDLED_TOR_PATH; path = BUNDLED_TOR_PATH;
if (QFile::exists(path + filename)) tor_exe_path = RsDirUtil::makePath(path,filename);
return path + filename;
if (RsDirUtil::fileExists(tor_exe_path))
return tor_exe_path;
#endif #endif
#ifdef __APPLE__ #ifdef __APPLE__
// on MacOS, try traditional brew installation path // on MacOS, try traditional brew installation path
path = "/usr/local/opt/tor/bin" ; path = "/usr/local/opt/tor/bin" ;
tor_exe_path = RsDirUtil::makePath(path,filename);
if (QFile::exists(path + filename)) if (RsDirUtil::fileExists(tor_exe_path))
return path + filename; return tor_exe_path;
#endif #endif
// Try $PATH // Try $PATH
return filename.mid(1); return filename.substr(1);
} }
bool TorManagerPrivate::createDataDir(const std::string &path) bool TorManagerPrivate::createDataDir(const std::string &path)
{ {
QDir dir(path); return RsDirUtil::checkCreateDirectory(path);
return dir.mkpath(".");
} }
bool TorManagerPrivate::createDefaultTorrc(const std::string &path) bool TorManagerPrivate::createDefaultTorrc(const std::string &path)
@ -547,7 +564,7 @@ bool TorManagerPrivate::createDefaultTorrc(const std::string &path)
// "DisableNetwork 1\n" // (cyril) I removed this because it prevents Tor to bootstrap. // "DisableNetwork 1\n" // (cyril) I removed this because it prevents Tor to bootstrap.
"__ReloadTorrcOnSIGHUP 0\n"; "__ReloadTorrcOnSIGHUP 0\n";
FILE *f = fopen(path,"w"); FILE *f = fopen(path.c_str(),"w");
if (!f) if (!f)
return false; return false;

View file

@ -81,7 +81,7 @@ public:
// True on first run or when the Tor configuration wizard needs to be shown // True on first run or when the Tor configuration wizard needs to be shown
bool configurationNeeded() const; bool configurationNeeded() const;
std::list<std::string> logMessages() const; const std::list<std::string>& logMessages() const;
bool hasError() const; bool hasError() const;
std::string errorMessage() const; std::string errorMessage() const;

View file

@ -38,7 +38,7 @@
#include "util/rsdir.h" #include "util/rsdir.h"
#include "pqi/pqifdbin.h" #include "pqi/pqifdbin.h"
#include "TorProcess_p.h" #include "TorProcess.h"
#include "CryptoKey.h" #include "CryptoKey.h"
#include "SecureRNG.h" #include "SecureRNG.h"
@ -105,7 +105,7 @@ std::string TorProcess::errorMessage() const
return mErrorMessage; return mErrorMessage;
} }
// Does a fopen, but dup all file descriptors (STDIN STDOUT and STDERR) to the // Does a popen, but dup all file descriptors (STDIN STDOUT and STDERR) to the
// FDs supplied by the parent process // FDs supplied by the parent process
int popen3(int fd[3],const char **const cmd,pid_t& pid) int popen3(int fd[3],const char **const cmd,pid_t& pid)

View file

@ -93,6 +93,16 @@ std::string RsDirUtil::getFileName(const std::string& full_file_path)
else else
return full_file_path.substr(n+1); return full_file_path.substr(n+1);
} }
std::string RsDirUtil::getDirectory(const std::string& full_file_path)
{
size_t n = full_file_path.find_last_of('/');
if(n == std::string::npos)
return std::string();
else
return full_file_path.substr(0,n);
}
std::string RsDirUtil::getTopDir(const std::string& dir) std::string RsDirUtil::getTopDir(const std::string& dir)
{ {
std::string top; std::string top;

View file

@ -58,13 +58,20 @@ class RsStackFileLock
namespace RsDirUtil { namespace RsDirUtil {
// Returns the name of the directory on top of the given path (as opposed to the full path to that directory)
std::string getTopDir(const std::string&); std::string getTopDir(const std::string&);
std::string getRootDir(const std::string&); std::string getRootDir(const std::string&);
std::string removeRootDir(const std::string& path); std::string removeRootDir(const std::string& path);
void removeTopDir(const std::string& dir, std::string &path); void removeTopDir(const std::string& dir, std::string &path);
std::string removeRootDirs(const std::string& path, const std::string& root); std::string removeRootDirs(const std::string& path, const std::string& root);
// Returns the filename at the end of the path. An empty string is returned if the path is a directory path.
std::string getFileName(const std::string& full_file_path); std::string getFileName(const std::string& full_file_path);
// Returns the directory (full path) that contains the given path (filename or directory).
// If a directory is supplied, the same path is returned.
std::string getDirectory(const std::string& full_file_path);
// Renames file from to file to. Files should be on the same file system. // Renames file from to file to. Files should be on the same file system.
// returns true if succeed, false otherwise. // returns true if succeed, false otherwise.
bool renameFile(const std::string& from,const std::string& to) ; bool renameFile(const std::string& from,const std::string& to) ;

View file

@ -22,6 +22,7 @@
#include "util/rsprint.h" #include "util/rsprint.h"
#include "util/rsstring.h" #include "util/rsstring.h"
#include <stdio.h>
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
#include <openssl/sha.h> #include <openssl/sha.h>
@ -45,6 +46,13 @@ std::string RsUtil::NumberToString(uint64_t n,bool hex)
return os.str(); return os.str();
} }
bool RsUtil::StringToInt(const std::string& s,int& n)
{
if(sscanf(s.c_str(),"%d",&n) == 1)
return true;
else
return false;
}
std::string RsUtil::BinToHex(const std::string &bin) std::string RsUtil::BinToHex(const std::string &bin)
{ {
return BinToHex(bin.c_str(), bin.length()); return BinToHex(bin.c_str(), bin.length());

View file

@ -36,6 +36,10 @@ std::string BinToHex(const char *arr, const uint32_t len);
std::string BinToHex(const unsigned char *arr, const uint32_t len, uint32_t max_len=0); std::string BinToHex(const unsigned char *arr, const uint32_t len, uint32_t max_len=0);
bool HexToBin(const std::string& input,unsigned char *data, const uint32_t len); bool HexToBin(const std::string& input,unsigned char *data, const uint32_t len);
std::string NumberToString(uint64_t n, bool hex=false); std::string NumberToString(uint64_t n, bool hex=false);
// Returns in n the int that can be read in the string. Returns false when no int is fond.
bool StringToInt(const std::string& s,int& n);
std::string HashId(const std::string &id, bool reverse = false); std::string HashId(const std::string &id, bool reverse = false);
std::vector<uint8_t> BinToSha256(const std::vector<uint8_t> &in); std::vector<uint8_t> BinToSha256(const std::vector<uint8_t> &in);