keeping Qt internally for a while and making RsTor Qt-free

This commit is contained in:
csoler 2021-06-25 21:46:00 +02:00
parent d7fb3d8bf4
commit f13b0cbe9f
20 changed files with 258 additions and 183 deletions

View File

@ -9,13 +9,18 @@ libretroshare_shared {
} else { } else {
CONFIG += staticlib CONFIG += staticlib
} }
CONFIG -= qt CONFIG += qt
QT += network
TARGET = retroshare TARGET = retroshare
TARGET_PRL = libretroshare TARGET_PRL = libretroshare
DESTDIR = lib DESTDIR = lib
!include("use_libretroshare.pri"):error("Including") !include("use_libretroshare.pri"):error("Including")
QMAKE_CXXFLAGS += -fPIC
# treat warnings as error for better removing # treat warnings as error for better removing
#QMAKE_CFLAGS += -Werror #QMAKE_CFLAGS += -Werror
#QMAKE_CXXFLAGS += -Werror #QMAKE_CXXFLAGS += -Werror
@ -720,6 +725,8 @@ SOURCES += rsitems/rsnxsitems.cc \
gxs/rsgxsrequesttypes.cc gxs/rsgxsrequesttypes.cc
# Tor # Tor
HEADERS += retroshare/rstor.h
HEADERS += tor/AddOnionCommand.h \ HEADERS += tor/AddOnionCommand.h \
tor/AuthenticateCommand.h \ tor/AuthenticateCommand.h \
tor/CryptoKey.h \ tor/CryptoKey.h \

View File

@ -103,7 +103,10 @@ enum class RsEventType : uint32_t
/// @see rspeers.h /// @see rspeers.h
NETWORK = 16, NETWORK = 16,
__MAX /// Used internally, keep last /// @see rspeers.h
TOR_MANAGER = 17,
__MAX /// Used internally, keep last
}; };
enum class RsEventsErrorNum : int32_t enum class RsEventsErrorNum : int32_t

View File

@ -34,7 +34,9 @@
#define ADDONIONCOMMAND_H #define ADDONIONCOMMAND_H
#include "TorControlCommand.h" #include "TorControlCommand.h"
#include <list> #include <QList>
#include <QPair>
#include <QVariant>
namespace Tor namespace Tor
{ {
@ -43,28 +45,27 @@ class HiddenService;
class AddOnionCommand : public TorControlCommand class AddOnionCommand : public TorControlCommand
{ {
#ifdef NO_TOR_CONTROL_PROPERTIES Q_OBJECT
Q_PROPERTY(std::string errorMessage READ errorMessage CONSTANT) Q_DISABLE_COPY(AddOnionCommand)
Q_PROPERTY(QString errorMessage READ errorMessage CONSTANT)
Q_PROPERTY(bool successful READ isSuccessful CONSTANT) Q_PROPERTY(bool successful READ isSuccessful CONSTANT)
#endif
public: public:
AddOnionCommand(HiddenService *service); AddOnionCommand(HiddenService *service);
QByteArray build(); QByteArray build();
std::string errorMessage() const { return m_errorMessage; } QString errorMessage() const { return m_errorMessage; }
bool isSuccessful() const; bool isSuccessful() const;
#ifdef NO_TOR_CONTROL_SIGNALS
signals: signals:
void succeeded(); void succeeded();
void failed(int code); void failed(int code);
#endif
protected: protected:
HiddenService *m_service; HiddenService *m_service;
std::string m_errorMessage; QString m_errorMessage;
virtual void onReply(int statusCode, const QByteArray &data); virtual void onReply(int statusCode, const QByteArray &data);
virtual void onFinished(int statusCode); virtual void onFinished(int statusCode);

View File

@ -31,13 +31,13 @@
*/ */
#include <iostream> #include <iostream>
#include <stdio.h>
#include "CryptoKey.h" #include "CryptoKey.h"
#include "SecureRNG.h" #include "SecureRNG.h"
#include "Useful.h" #include "Useful.h"
#include "TorTypes.h" #include <QtDebug>
#include <QFile>
#include <QByteArray>
#include <openssl/bn.h> #include <openssl/bn.h>
#include <openssl/bio.h> #include <openssl/bio.h>
#include <openssl/pem.h> #include <openssl/pem.h>
@ -120,23 +120,17 @@ bool CryptoKey::loadFromData(const QByteArray &data, KeyType type, KeyFormat for
} }
#endif #endif
bool CryptoKey::loadFromFile(const std::string &path) bool CryptoKey::loadFromFile(const QString& path)
{ {
FILE *f = fopen(path.c_str(),"r"); QFile file(path);
if (!file.open(QIODevice::ReadOnly))
if(!f)
{ {
std::cerr << "Failed to open Tor key file " << path << std::endl; qWarning() << "Failed to open Tor key file " << path << ": " << file.errorString();
return false; return false;
} }
Tor::TorByteArray data ; QByteArray data = file.readAll();
int c; file.close();
while( EOF != (c=fgetc(f)))
data += (unsigned char)c;
fclose(f);
if(data.contains("-----BEGIN RSA PRIVATE KEY-----")) if(data.contains("-----BEGIN RSA PRIVATE KEY-----"))
{ {
@ -152,14 +146,14 @@ bool CryptoKey::loadFromFile(const std::string &path)
} }
std::cerr << "Have read the following key: " << std::endl; std::cerr << "Have read the following key: " << std::endl;
std::cerr << data.toStdString() << std::endl; std::cerr << QString(data).toStdString() << std::endl;
key_data = data; key_data = data;
return true; return true;
} }
bool CryptoKey::loadFromTorMessage(const Tor::TorByteArray& b) bool CryptoKey::loadFromTorMessage(const QByteArray& b)
{ {
// note: We should probably check the structure a bit more, for security. // note: We should probably check the structure a bit more, for security.
@ -169,7 +163,7 @@ bool CryptoKey::loadFromTorMessage(const Tor::TorByteArray& b)
std::cerr << " type: RSA-1024 (Tor v2)" << std::endl; std::cerr << " type: RSA-1024 (Tor v2)" << std::endl;
else if(b.startsWith("ED25519-V3")) else if(b.startsWith("ED25519-V3"))
std::cerr << " type: ED25519-V3 (Tor v3)" << std::endl; std::cerr << " type: ED25519-V3 (Tor v3)" << std::endl;
else if(b.indexOf(':') >= 0) else if(b.indexOf(':'))
{ {
std::cerr << " unknown type, or bad syntax in key: \"" << b.left(b.indexOf(':')).toStdString() << "\". Not accepted." << std::endl; std::cerr << " unknown type, or bad syntax in key: \"" << b.left(b.indexOf(':')).toStdString() << "\". Not accepted." << std::endl;
return false; return false;
@ -180,22 +174,22 @@ bool CryptoKey::loadFromTorMessage(const Tor::TorByteArray& b)
} }
/* Cryptographic hash of a password as expected by Tor's HashedControlPassword */ /* Cryptographic hash of a password as expected by Tor's HashedControlPassword */
Tor::TorByteArray torControlHashedPassword(const Tor::TorByteArray& password) QByteArray torControlHashedPassword(const QByteArray &password)
{ {
Tor::TorByteArray salt = SecureRNG::random(8); QByteArray salt = SecureRNG::random(8);
if (salt.isNull()) if (salt.isNull())
return Tor::TorByteArray(); return QByteArray();
int count = ((quint32)16 + (96 & 15)) << ((96 >> 4) + 6); int count = ((quint32)16 + (96 & 15)) << ((96 >> 4) + 6);
SHA_CTX hash; SHA_CTX hash;
SHA1_Init(&hash); SHA1_Init(&hash);
Tor::TorByteArray tmp = salt + password; QByteArray tmp = salt + password;
while (count) while (count)
{ {
int c = std::min(count, tmp.size()); int c = qMin(count, tmp.size());
SHA1_Update(&hash, reinterpret_cast<const void*>(tmp.data()), c); SHA1_Update(&hash, reinterpret_cast<const void*>(tmp.constData()), c);
count -= c; count -= c;
} }
@ -203,8 +197,8 @@ Tor::TorByteArray torControlHashedPassword(const Tor::TorByteArray& password)
SHA1_Final(md, &hash); SHA1_Final(md, &hash);
/* 60 is the hex-encoded value of 96, which is a constant used by Tor's algorithm. */ /* 60 is the hex-encoded value of 96, which is a constant used by Tor's algorithm. */
return Tor::TorByteArray("16:") + salt.toHex().toUpper() + Tor::TorByteArray("60") + return QByteArray("16:") + salt.toHex().toUpper() + QByteArray("60") +
Tor::TorByteArray::fromRawData(reinterpret_cast<const char*>(md), 20).toHex().toUpper(); QByteArray::fromRawData(reinterpret_cast<const char*>(md), 20).toHex().toUpper();
} }

View File

@ -33,7 +33,9 @@
#ifndef CRYPTOKEY_H #ifndef CRYPTOKEY_H
#define CRYPTOKEY_H #define CRYPTOKEY_H
#include "tor/TorTypes.h" #include <QString>
#include <QSharedData>
#include <QExplicitlySharedDataPointer>
class CryptoKey class CryptoKey
{ {
@ -55,13 +57,12 @@ public:
bool loadFromData(const QByteArray &data, KeyType type, KeyFormat format = PEM); bool loadFromData(const QByteArray &data, KeyType type, KeyFormat format = PEM);
bool loadFromFile(const QString &path, KeyType type, KeyFormat format = PEM); bool loadFromFile(const QString &path, KeyType type, KeyFormat format = PEM);
#endif #endif
bool loadFromFile(const std::string& path); bool loadFromFile(const QString &path);
void clear(); void clear();
const Tor::TorByteArray& bytes() const { return key_data; } const QByteArray bytes() const { return key_data; }
bool loadFromTorMessage(const Tor::TorByteArray& b); bool loadFromTorMessage(const QByteArray& b);
bool isLoaded() const { return !key_data.empty(); } bool isLoaded() const { return !key_data.isNull(); }
#ifdef TO_REMOVE #ifdef TO_REMOVE
bool isPrivate() const; bool isPrivate() const;
@ -100,6 +101,6 @@ private:
#endif #endif
}; };
Tor::TorByteArray torControlHashedPassword(const Tor::TorByteArray& password); QByteArray torControlHashedPassword(const QByteArray &password);
#endif // CRYPTOKEY_H #endif // CRYPTOKEY_H

View File

@ -30,11 +30,9 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#include <stdexcept>
#include "TorTypes.h"
#include "GetConfCommand.h" #include "GetConfCommand.h"
#include "StrUtil.h" #include "StrUtil.h"
#include <QDebug>
using namespace Tor; using namespace Tor;
@ -45,28 +43,27 @@ GetConfCommand::GetConfCommand(Type t)
QByteArray GetConfCommand::build(const QByteArray &key) QByteArray GetConfCommand::build(const QByteArray &key)
{ {
return build(std::list<QByteArray>{key}); return build(QList<QByteArray>() << key);
} }
QByteArray GetConfCommand::build(const std::list<QByteArray> &keys) QByteArray GetConfCommand::build(const QList<QByteArray> &keys)
{ {
QByteArray out; QByteArray out;
if (type == GetConf) { if (type == GetConf) {
out = QByteArray("GETCONF"); out = "GETCONF";
} else if (type == GetInfo) { } else if (type == GetInfo) {
out = QByteArray("GETINFO"); out = "GETINFO";
} else { } else {
throw std::runtime_error("Unsupported build type in GetConfCommand"); Q_ASSERT(false);
return out; return out;
} }
for(const QByteArray& key: keys) foreach (const QByteArray &key, keys) {
{ out.append(' ');
out += (' '); out.append(key);
out += key;
} }
out += std::string("\r\n"); out.append("\r\n");
return out; return out;
} }
@ -77,7 +74,7 @@ void GetConfCommand::onReply(int statusCode, const QByteArray &data)
return; return;
int kep = data.indexOf('='); int kep = data.indexOf('=');
std::string key = QString::fromLatin1(data.mid(0, kep)); QString key = QString::fromLatin1(data.mid(0, kep));
QVariant value; QVariant value;
if (kep >= 0) if (kep >= 0)
value = QString::fromLatin1(unquotedString(data.mid(kep + 1))); value = QString::fromLatin1(unquotedString(data.mid(kep + 1)));

View File

@ -34,16 +34,18 @@
#define GETCONFCOMMAND_H #define GETCONFCOMMAND_H
#include "TorControlCommand.h" #include "TorControlCommand.h"
#include <list> #include <QList>
#include <QVariantMap>
namespace Tor namespace Tor
{ {
class GetConfCommand : public TorControlCommand class GetConfCommand : public TorControlCommand
{ {
#ifdef NO_TOR_CONTROL_PROPERTIES Q_OBJECT
Q_DISABLE_COPY(GetConfCommand)
Q_PROPERTY(QVariantMap results READ results CONSTANT) Q_PROPERTY(QVariantMap results READ results CONSTANT)
#endif
public: public:
enum Type { enum Type {
@ -55,10 +57,10 @@ public:
GetConfCommand(Type type); GetConfCommand(Type type);
QByteArray build(const QByteArray &key); QByteArray build(const QByteArray &key);
QByteArray build(const std::list<QByteArray> &keys); QByteArray build(const QList<QByteArray> &keys);
const QVariantMap& results() const { return m_results; } const QVariantMap &results() const { return m_results; }
QVariant get(const QByteArray& key) const; QVariant get(const QByteArray &key) const;
protected: protected:
virtual void onReply(int statusCode, const QByteArray &data); virtual void onReply(int statusCode, const QByteArray &data);
@ -67,7 +69,7 @@ protected:
private: private:
QVariantMap m_results; QVariantMap m_results;
std::string m_lastKey; QString m_lastKey;
}; };
} }

View File

@ -90,7 +90,7 @@ void HiddenService::addTarget(quint16 servicePort, QHostAddress targetAddress, q
m_targets.append(t); m_targets.append(t);
} }
void HiddenService::setServiceId(const TorByteArray &sid) void HiddenService::setServiceId(const QByteArray& sid)
{ {
m_service_id = sid; m_service_id = sid;
m_hostname = sid + ".onion"; m_hostname = sid + ".onion";

View File

@ -33,25 +33,28 @@
#ifndef HIDDENSERVICE_H #ifndef HIDDENSERVICE_H
#define HIDDENSERVICE_H #define HIDDENSERVICE_H
#include <string> #include <QObject>
#include <list> #include <QHostAddress>
#include <QList>
#include "tor/CryptoKey.h" #include "CryptoKey.h"
#include "tor/TorTypes.h"
namespace Tor namespace Tor
{ {
class TorSocket; class TorSocket;
class HiddenService : public NonCopiable class HiddenService : public QObject
{ {
Q_OBJECT
Q_DISABLE_COPY(HiddenService)
friend class TorControlPrivate; friend class TorControlPrivate;
public: public:
struct Target struct Target
{ {
TorHostAddress targetAddress; QHostAddress targetAddress;
unsigned short servicePort, targetPort; quint16 servicePort, targetPort;
}; };
enum Status enum Status
@ -61,25 +64,24 @@ public:
Online /* Published */ Online /* Published */
}; };
HiddenService(); HiddenService(QObject *parent = 0);
HiddenService(const std::string& dataPath); HiddenService(const QString &dataPath, QObject *parent = 0);
HiddenService(const CryptoKey& privateKey, const std::string& dataPath = std::string()); HiddenService(const CryptoKey &privateKey, const QString &dataPath = QString(), QObject *parent = 0);
Status status() const { return m_status; } Status status() const { return m_status; }
const std::string& hostname() const { return m_hostname; } const QString& hostname() const { return m_hostname; }
const std::string serviceId() const { return std::string(m_service_id.data()); } const QString serviceId() const { return QString(m_service_id); }
const std::string& dataPath() const { return m_dataPath; } const QString& dataPath() const { return m_dataPath; }
CryptoKey privateKey() { return m_privateKey; } CryptoKey privateKey() { return m_privateKey; }
void setPrivateKey(const CryptoKey &privateKey); void setPrivateKey(const CryptoKey &privateKey);
void setServiceId(const TorByteArray& sid); void setServiceId(const QByteArray& sid);
const std::list<Target>& targets() const { return m_targets; } const QList<Target> &targets() const { return m_targets; }
void addTarget(const Target &target); void addTarget(const Target &target);
void addTarget(unsigned short servicePort, TorHostAddress targetAddress, unsigned short targetPort); void addTarget(quint16 servicePort, QHostAddress targetAddress, quint16 targetPort);
#ifdef NO_TOR_CONTROL_SIGNALS
signals: signals:
void statusChanged(int newStatus, int oldStatus); void statusChanged(int newStatus, int oldStatus);
void serviceOnline(); void serviceOnline();
@ -88,15 +90,14 @@ signals:
private slots: private slots:
void servicePublished(); void servicePublished();
#endif
private: private:
std::string m_dataPath; QString m_dataPath;
std::list<Target> m_targets; QList<Target> m_targets;
std::string m_hostname; QString m_hostname;
Status m_status; Status m_status;
CryptoKey m_privateKey; CryptoKey m_privateKey;
TorByteArray m_service_id; QByteArray m_service_id;
void loadPrivateKey(); void loadPrivateKey();
void setStatus(Status newStatus); void setStatus(Status newStatus);

View File

@ -33,17 +33,8 @@
#ifndef PROTOCOLINFOCOMMAND_H #ifndef PROTOCOLINFOCOMMAND_H
#define PROTOCOLINFOCOMMAND_H #define PROTOCOLINFOCOMMAND_H
#include <retroshare/rsflags.h>
#include "TorControlCommand.h" #include "TorControlCommand.h"
#include <QFlags>
enum class AuthMethods: uint8_t
{
AuthUnknown = 0x0,
AuthNull = 0x1,
AuthHashedPassword = 0x2,
AuthCookie = 0x4
};
RS_REGISTER_ENUM_FLAGS_TYPE(AuthMethods)
namespace Tor namespace Tor
{ {
@ -52,13 +43,25 @@ class TorControl;
class ProtocolInfoCommand : public TorControlCommand class ProtocolInfoCommand : public TorControlCommand
{ {
Q_OBJECT
Q_DISABLE_COPY(ProtocolInfoCommand)
public: public:
enum AuthMethod
{
AuthUnknown = 0,
AuthNull = 0x1,
AuthHashedPassword = 0x2,
AuthCookie = 0x4
};
Q_DECLARE_FLAGS(AuthMethods, AuthMethod)
ProtocolInfoCommand(TorControl *manager); ProtocolInfoCommand(TorControl *manager);
QByteArray build(); QByteArray build();
AuthMethods authMethods() const { return m_authMethods; } AuthMethods authMethods() const { return m_authMethods; }
std::string torVersion() const { return m_torVersion; } QString torVersion() const { return m_torVersion; }
std::string cookieFile() const { return m_cookieFile; } QString cookieFile() const { return m_cookieFile; }
protected: protected:
virtual void onReply(int statusCode, const QByteArray &data); virtual void onReply(int statusCode, const QByteArray &data);
@ -66,8 +69,8 @@ protected:
private: private:
TorControl *manager; TorControl *manager;
AuthMethods m_authMethods; AuthMethods m_authMethods;
std::string m_torVersion; QString m_torVersion;
std::string m_cookieFile; QString m_cookieFile;
}; };
} }

View File

@ -31,12 +31,10 @@
*/ */
#include "SecureRNG.h" #include "SecureRNG.h"
#include <QtDebug>
#include <openssl/rand.h> #include <openssl/rand.h>
#include <openssl/err.h> #include <openssl/err.h>
#include <limits.h> #include <limits.h>
#include <iostream>
#include <sstream>
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <wtypes.h> #include <wtypes.h>
@ -84,7 +82,7 @@ bool SecureRNG::seed()
#else #else
if (!RAND_poll()) if (!RAND_poll())
{ {
std::cerr << "OpenSSL RNG seed failed:" << ERR_get_error(); qWarning() << "OpenSSL RNG seed failed:" << ERR_get_error();
return false; return false;
} }
#endif #endif
@ -96,16 +94,11 @@ bool SecureRNG::seed()
return true; return true;
} }
void SecureRNG::random(unsigned char *buf, int size) void SecureRNG::random(char *buf, int size)
{ {
int r = RAND_bytes(buf, size); int r = RAND_bytes(reinterpret_cast<unsigned char*>(buf), size);
if (r <= 0) if (r <= 0)
{ qFatal("RNG failed: %lu", ERR_get_error());
std::ostringstream s;
s << "RNG failed: " << ERR_get_error() ;
throw std::runtime_error(s.str());
}
} }
QByteArray SecureRNG::random(int size) QByteArray SecureRNG::random(int size)
@ -118,7 +111,7 @@ QByteArray SecureRNG::random(int size)
QByteArray SecureRNG::randomPrintable(int length) QByteArray SecureRNG::randomPrintable(int length)
{ {
QByteArray re(length, 0); QByteArray re(length, 0);
for (uint32_t i = 0; i < re.size(); i++) for (int i = 0; i < re.size(); i++)
re[i] = randomInt(95) + 32; re[i] = randomInt(95) + 32;
return re; return re;
} }
@ -130,24 +123,24 @@ unsigned SecureRNG::randomInt(unsigned max)
for (;;) for (;;)
{ {
random(reinterpret_cast<unsigned char*>(&value), sizeof(value)); random(reinterpret_cast<char*>(&value), sizeof(value));
if (value < cutoff) if (value < cutoff)
return value % max; return value % max;
} }
} }
#ifndef UINT64_MAX #ifndef UINT64_MAX
#define UINT64_MAX ((uint64_t)-1) #define UINT64_MAX ((quint64)-1)
#endif #endif
uint64_t SecureRNG::randomInt64(uint64_t max) quint64 SecureRNG::randomInt64(quint64 max)
{ {
uint64_t cutoff = UINT64_MAX - (UINT64_MAX % max); quint64 cutoff = UINT64_MAX - (UINT64_MAX % max);
uint64_t value = 0; quint64 value = 0;
for (;;) for (;;)
{ {
random(reinterpret_cast<unsigned char*>(value), sizeof(value)); random(reinterpret_cast<char*>(value), sizeof(value));
if (value < cutoff) if (value < cutoff)
return value % max; return value % max;
} }

View File

@ -33,19 +33,19 @@
#ifndef SECURERNG_H #ifndef SECURERNG_H
#define SECURERNG_H #define SECURERNG_H
#include "TorTypes.h" #include <QByteArray>
class SecureRNG class SecureRNG
{ {
public: public:
static bool seed(); static bool seed();
static void random(unsigned char *buf, int size); static void random(char *buf, int size);
static Tor::TorByteArray random(int size); static QByteArray random(int size);
static Tor::TorByteArray randomPrintable(int length); static QByteArray randomPrintable(int length);
static unsigned randomInt(unsigned max); static unsigned randomInt(unsigned max);
static uint64_t randomInt64(uint64_t max); static quint64 randomInt64(quint64 max);
}; };
#endif // SECURERNG_H #endif // SECURERNG_H

View File

@ -32,14 +32,14 @@
#include "StrUtil.h" #include "StrUtil.h"
QByteArray quotedString(const QByteArray& string) QByteArray quotedString(const QByteArray &string)
{ {
QByteArray out; QByteArray out;
out.reserve(string.size() * 2); out.reserve(string.size() * 2);
out += '"'; out.append('"');
for (uint32_t i = 0; i < string.size(); ++i) for (int i = 0; i < string.size(); ++i)
{ {
switch (string[i]) switch (string[i])
{ {
@ -67,7 +67,7 @@ QByteArray unquotedString(const QByteArray &string)
QByteArray out; QByteArray out;
out.reserve(string.size() - 2); out.reserve(string.size() - 2);
for (uint32_t i = 1; i < string.size(); ++i) for (int i = 1; i < string.size(); ++i)
{ {
switch (string[i]) switch (string[i])
{ {
@ -85,9 +85,9 @@ QByteArray unquotedString(const QByteArray &string)
return out; return out;
} }
std::list<QByteArray> splitQuotedStrings(const QByteArray &input, char separator) QList<QByteArray> splitQuotedStrings(const QByteArray &input, char separator)
{ {
std::list<QByteArray> out; QList<QByteArray> out;
bool inquote = false; bool inquote = false;
int start = 0; int start = 0;

View File

@ -33,14 +33,14 @@
#ifndef STRINGUTIL_H #ifndef STRINGUTIL_H
#define STRINGUTIL_H #define STRINGUTIL_H
#include "TorTypes.h" #include <QByteArray>
#include <list> #include <QList>
QByteArray quotedString(const QByteArray &string); QByteArray quotedString(const QByteArray &string);
/* Return the unquoted contents of a string, either until an end quote or an unescaped separator character. */ /* Return the unquoted contents of a string, either until an end quote or an unescaped separator character. */
QByteArray unquotedString(const QByteArray &string); QByteArray unquotedString(const QByteArray &string);
std::list<QByteArray> splitQuotedStrings(const QByteArray &input, char separator); QList<QByteArray> splitQuotedStrings(const QByteArray &input, char separator);
#endif // STRINGUTIL_H #endif // STRINGUTIL_H

View File

@ -33,31 +33,32 @@
#ifndef TORCONTROLCOMMAND_H #ifndef TORCONTROLCOMMAND_H
#define TORCONTROLCOMMAND_H #define TORCONTROLCOMMAND_H
#include <vector> #include <QObject>
#include "tor/TorTypes.h" #include <QByteArray>
namespace Tor namespace Tor
{ {
class TorControlCommand : public NonCopiable
class TorControlCommand : public QObject
{ {
Q_OBJECT
Q_DISABLE_COPY(TorControlCommand)
friend class TorControlSocket; friend class TorControlSocket;
public: public:
TorControlCommand(); TorControlCommand();
virtual ~TorControlCommand() {}
int statusCode() const { return m_finalStatus; } int statusCode() const { return m_finalStatus; }
#ifdef NO_TOR_CONTROL_SIGNALS
signals: signals:
void replyLine(int statusCode, const TorByteArray& data); void replyLine(int statusCode, const QByteArray &data);
void finished(); void finished();
#endif
protected: protected:
virtual void onReply(int statusCode, const TorByteArray& data); virtual void onReply(int statusCode, const QByteArray &data);
virtual void onFinished(int statusCode); virtual void onFinished(int statusCode);
virtual void onDataLine(const TorByteArray& data); virtual void onDataLine(const QByteArray &data);
virtual void onDataFinished(); virtual void onDataFinished();
private: private:

View File

@ -118,11 +118,6 @@ TorProcess *TorManager::process()
return d->process; return d->process;
} }
bool TorManager::isTorAvailable()
{
return !instance()->d->torExecutablePath().isNull();
}
QString TorManager::torDataDirectory() const QString TorManager::torDataDirectory() const
{ {
return d->dataDir; return d->dataDir;
@ -524,3 +519,47 @@ void TorManagerPrivate::setError(const QString &message)
#include "TorManager.moc" #include "TorManager.moc"
bool RsTor::isTorAvailable()
{
return !instance()->d->torExecutablePath().isNull();
}
bool RsTor::getHiddenServiceInfo(std::string& service_id,
std::string& service_onion_address,
uint16_t& service_port,
std::string& service_target_address,
uint16_t& target_port)
{
QString sid;
QString soa;
QHostAddress sta;
if(!instance()->getHiddenServiceInfo(sid,soa,service_port,sta,target_port))
return false;
service_id = sid.toStdString();
service_onion_address = soa.toStdString();
service_target_address = sta.toString().toStdString();
return true;
}
std::list<std::string> RsTor::logMessages()
{
QStringList qs = instance()->logMessages();
std::list<std::string> s;
for(auto& ss:qs)
s.push_back(ss.toStdString());
return s;
}
std::string RsTor::socksAddress()
{
return instance()->control()->socksAddress().toString().toStdString();
}
uint16_t RsTor::socksPort()
{
return instance()->control()->socksPort();
}

View File

@ -35,6 +35,8 @@
#ifndef TORMANAGER_H #ifndef TORMANAGER_H
#define TORMANAGER_H #define TORMANAGER_H
#include "retroshare/rstor.h"
#include <QObject> #include <QObject>
#include <QStringList> #include <QStringList>
#include <QHostAddress> #include <QHostAddress>
@ -48,7 +50,8 @@ 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 QObject
class TorManager : public QObject, public RsTor
{ {
Q_OBJECT Q_OBJECT
@ -61,7 +64,6 @@ class TorManager : public QObject
Q_PROPERTY(QString torDataDirectory READ torDataDirectory WRITE setTorDataDirectory) Q_PROPERTY(QString torDataDirectory READ torDataDirectory WRITE setTorDataDirectory)
public: public:
static bool isTorAvailable() ;
static TorManager *instance(); static TorManager *instance();
TorProcess *process(); TorProcess *process();
@ -103,6 +105,7 @@ signals:
private: private:
explicit TorManager(QObject *parent = 0); explicit TorManager(QObject *parent = 0);
TorManagerPrivate *d; TorManagerPrivate *d;
friend class RsTor;
}; };
} }

View File

@ -1,7 +1,9 @@
#pragma once #pragma once
#include <vector> #include <vector>
#include <sstream>
#include <string> #include <string>
#include <stdexcept>
namespace Tor namespace Tor
{ {
@ -18,7 +20,13 @@ private:
class TorByteArray: public std::vector<unsigned char> class TorByteArray: public std::vector<unsigned char>
{ {
public: public:
TorByteArray(const std::string& s = std::string()) TorByteArray(const unsigned char *data,uint32_t len)
{
clear();
for(uint32_t i=0;i<len;++i)
push_back(data[i]);
}
explicit TorByteArray(const std::string& s)
{ {
clear(); clear();
for(uint32_t i=0;i<s.length();++i) for(uint32_t i=0;i<s.length();++i)
@ -29,13 +37,14 @@ public:
clear(); clear();
resize(s,c); resize(s,c);
} }
TorByteArray() { clear() ; }
bool startsWith(const std::string& s) const bool startsWith(const TorByteArray& s) const
{ {
if(s.length() > size()) if(s.size() > size())
return false; return false;
for(uint32_t i=0;i<s.length();++i) for(uint32_t i=0;i<s.size();++i)
if(s[i] != data()[i]) if(s[i] != data()[i])
return false; return false;
@ -68,7 +77,7 @@ public:
push_back(t.data()[i]); push_back(t.data()[i]);
return *this; return *this;
} }
const TorByteArray& append(const std::string& s) { return operator+=(s); } const TorByteArray& append(const std::string& s) { return operator+=(TorByteArray(s)); }
const TorByteArray& append(char s) { return operator+=(s); } const TorByteArray& append(char s) { return operator+=(s); }
TorByteArray operator+(const TorByteArray& t) const TorByteArray operator+(const TorByteArray& t) const
@ -84,29 +93,50 @@ public:
return std::string((const char *)data(),size()); return std::string((const char *)data(),size());
} }
bool contains(const TorByteArray& b) const bool contains(const TorByteArray& b) const
{ {
if(b.size() > size()) if(b.size() > size())
return false;
for(uint32_t i=0;i<size()-b.size();++i)
{
bool c = true;
for(uint32_t j=0;j<b.size();++j)
if(b[j] != data()[i+j])
{
c = false;
break;
}
if(c)
return true;
}
return false; return false;
for(uint32_t i=0;i<size()-b.size();++i)
{
bool c = true;
for(uint32_t j=0;j<b.size();++j)
if(b[j] != data()[i+j])
{
c = false;
break;
}
if(c)
return true;
} }
return false;
}
TorByteArray mid(uint32_t start,int length=-1) const
{
if(length==-1)
return TorByteArray(data()+start,size()-start);
if(length < 0 || start + length > size())
throw std::runtime_error("Length out of range in TorByteArray::mid()");
TorByteArray b;
for(uint32_t i=0;i<(uint32_t)length;++i)
b.push_back(data()[i+start]);
return b;
}
static TorByteArray number(uint64_t n)
{
std::ostringstream o;
o << n ;
return TorByteArray(o.str());
}
}; };
typedef std::string TorHostAddress; typedef std::string TorHostAddress;
} }
typedef Tor::TorByteArray QByteArray; // to be removed

View File

@ -33,15 +33,15 @@
#include <rshare.h> #include <rshare.h>
#include "gui/settings/rsharesettings.h" #include "gui/settings/rsharesettings.h"
#include "TorControl/TorManager.h"
#include "util/misc.h" #include "util/misc.h"
#include "gui/common/FilesDefs.h" #include "gui/common/FilesDefs.h"
#include <retroshare/rsidentity.h> #include "retroshare/rstor.h"
#include <retroshare/rsinit.h> #include "retroshare/rsidentity.h"
#include <retroshare/rsnotify.h> #include "retroshare/rsinit.h"
#include <rsserver/rsaccounts.h> #include "retroshare/rsnotify.h"
#include <util/rsrandom.h> #include "rsserver/rsaccounts.h"
#include "util/rsrandom.h"
#include <time.h> #include <time.h>
#include <math.h> #include <math.h>

View File

@ -66,7 +66,7 @@ CrashStackTrace gCrashStackTrace;
# include "gui/settings/JsonApiPage.h" # include "gui/settings/JsonApiPage.h"
#endif // RS_JSONAPI #endif // RS_JSONAPI
#include "TorControl/TorManager.h" #include "retroshare/rstor.h"
#include "TorControl/TorControlWindow.h" #include "TorControl/TorControlWindow.h"
#include "retroshare/rsidentity.h" #include "retroshare/rsidentity.h"