Skip to content

Commit

Permalink
libtego, libtego_ui, ricochet-refresh: purged unused events and enums…
Browse files Browse the repository at this point in the history
…, updated onion service state notifications
  • Loading branch information
Richard Pospesel committed May 14, 2022
1 parent af17cf0 commit 399634c
Show file tree
Hide file tree
Showing 18 changed files with 110 additions and 130 deletions.
27 changes: 12 additions & 15 deletions src/libtego/include/tego/tego.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,27 +331,24 @@ void tego_context_get_host_user_id(
tego_user_id_t** out_hostUser,
tego_error_t** error);

// state of the host user, encapsulates all of the tor daemon launch,
// network connection, and onion service creation into 'connecting'
// TODO: squish these into the tego_user_status_t ?
// state of the host user's onion service
typedef enum
{
tego_host_user_state_unknown,
tego_host_user_state_offline,
tego_host_user_state_connecting,
tego_host_user_state_online,
} tego_host_user_state_t;
tego_host_onion_service_state_none,
tego_host_onion_service_state_service_added,
tego_host_onion_service_state_service_published,
} tego_host_onion_service_state_t;

/*
* Get the current state of the host user
* Get the current state of the host's onion service
*
* @param context : the current tego context
* @param out_state : destination to save state
* @param error : filled on error
*/
void tego_context_get_host_user_state(
void tego_context_get_host_onion_service_state(
const tego_context_t* context,
tego_host_user_state_t* out_state,
tego_host_onion_service_state_t* out_state,
tego_error_t** error);

// TODO: figure out which statuses we need later
Expand Down Expand Up @@ -1113,9 +1110,9 @@ typedef void (*tego_tor_log_received_callback_t)(
* @param context : the current tego context
* @param state : the current host user state
*/
typedef void (*tego_host_user_state_changed_callback_t)(
typedef void (*tego_host_onion_service_state_changed_callback_t)(
tego_context_t* context,
tego_host_user_state_t state);
tego_host_onion_service_state_t state);

/*
* Callback fired when the host receives a chat request from another user
Expand Down Expand Up @@ -1345,9 +1342,9 @@ void tego_context_set_tor_log_received_callback(
tego_tor_log_received_callback_t,
tego_error_t** error);

void tego_context_set_host_user_state_changed_callback(
void tego_context_set_host_onion_service_state_changed_callback(
tego_context_t* context,
tego_host_user_state_changed_callback_t,
tego_host_onion_service_state_changed_callback_t,
tego_error_t** error);

void tego_context_set_chat_request_received_callback(
Expand Down
12 changes: 6 additions & 6 deletions src/libtego/source/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,15 +365,15 @@ void tego_context::update_disable_network_flag(bool disableNetwork)
this->torControl->setConfiguration(vm);
}

void tego_context::set_host_user_state(tego_host_user_state_t state)
void tego_context::set_host_onion_service_state(tego_host_onion_service_state_t state)
{
if (state == hostUserState)
{
return;
}

this->hostUserState = state;
this->callback_registry_.emit_host_user_state_changed(state);
this->callback_registry_.emit_host_onion_service_state_changed(state);
}

std::unique_ptr<tego_user_id_t> tego_context::get_host_user_id() const
Expand All @@ -387,7 +387,7 @@ std::unique_ptr<tego_user_id_t> tego_context::get_host_user_id() const
return std::make_unique<tego_user_id_t>(serviceId);
}

tego_host_user_state_t tego_context::get_host_user_state() const
tego_host_onion_service_state_t tego_context::get_host_onion_service_state() const
{
return this->hostUserState;
}
Expand Down Expand Up @@ -938,9 +938,9 @@ extern "C"
}, error);
}

void tego_context_get_host_user_state(
void tego_context_get_host_onion_service_state(
const tego_context_t* context,
tego_host_user_state_t* out_state,
tego_host_onion_service_state_t* out_state,
tego_error_t** error)
{
return tego::translateExceptions([=]() -> void
Expand All @@ -949,7 +949,7 @@ extern "C"
TEGO_THROW_IF_FALSE(context->threadId == std::this_thread::get_id());
TEGO_THROW_IF_NULL(out_state);

auto state = context->get_host_user_state();
auto state = context->get_host_onion_service_state();
*out_state = state;
}, error);
}
Expand Down
6 changes: 3 additions & 3 deletions src/libtego/source/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ struct tego_context
void update_tor_daemon_config(const tego_tor_daemon_config_t* config);
void update_disable_network_flag(bool disableNetwork);
void save_tor_daemon_config();
void set_host_user_state(tego_host_user_state_t state);
void set_host_onion_service_state(tego_host_onion_service_state_t state);
std::unique_ptr<tego_user_id_t> get_host_user_id() const;
tego_host_user_state_t get_host_user_state() const;
tego_host_onion_service_state_t get_host_onion_service_state() const;
void send_chat_request(
const tego_user_id_t* user,
const char* message,
Expand Down Expand Up @@ -87,5 +87,5 @@ struct tego_context

mutable std::string torVersion;
mutable std::vector<std::string> torLogs;
tego_host_user_state_t hostUserState = tego_host_user_state_unknown;
tego_host_onion_service_state_t hostUserState = tego_host_onion_service_state_none;
};
2 changes: 0 additions & 2 deletions src/libtego/source/core/IdentityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ void IdentityManager::addIdentity(UserIdentity *identity)
SLOT(onIncomingRequest(IncomingContactRequest*)));
connect(&identity->contacts.incomingRequests, SIGNAL(requestRemoved(IncomingContactRequest*)),
SLOT(onIncomingRequestRemoved(IncomingContactRequest*)));

emit identityAdded(identity);
}

UserIdentity *IdentityManager::createIdentity()
Expand Down
1 change: 0 additions & 1 deletion src/libtego/source/core/IdentityManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class IdentityManager : public QObject
class UserIdentity *createIdentity();

signals:
void identityAdded(class UserIdentity *identity);
void contactDeleted(class ContactUser *user, class UserIdentity *identity);
void outgoingRequestAdded(class OutgoingContactRequest *request, class UserIdentity *identity);
void incomingRequestAdded(class IncomingContactRequest *request, class UserIdentity *identity);
Expand Down
19 changes: 0 additions & 19 deletions src/libtego/source/core/UserIdentity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ UserIdentity *UserIdentity::createIdentity(int uniqueID)
// TODO: Handle the error cases of this function in a useful way
void UserIdentity::setupService(const QString& serviceID)
{
g_globals.context->set_host_user_state(tego_host_user_state_offline);

QString keyData = serviceID;

if (!keyData.isEmpty()) {
Expand Down Expand Up @@ -101,10 +99,7 @@ void UserIdentity::setupService(const QString& serviceID)
);
}

g_globals.context->set_host_user_state(tego_host_user_state_connecting);

Q_ASSERT(m_hiddenService);
connect(m_hiddenService, SIGNAL(statusChanged(int,int)), SLOT(onStatusChanged(int,int)));

m_incomingServer = new QTcpServer(this);
if (!m_incomingServer->listen(QHostAddress::LocalHost, 0)) {
Expand All @@ -131,20 +126,6 @@ QString UserIdentity::contactID() const
return ContactIDValidator::idFromHostname(hostname());
}

void UserIdentity::onStatusChanged(int newStatus, int oldStatus)
{
if (oldStatus == Tor::HiddenService::NotCreated && newStatus > oldStatus)
{
emit contactIDChanged();
}
emit statusChanged();
}

bool UserIdentity::isServiceOnline() const
{
return m_hiddenService && m_hiddenService->status() == Tor::HiddenService::Online;
}

/* Handle an incoming connection to this service
*
* A Protocol::Connection is created to handle this socket. The
Expand Down
4 changes: 0 additions & 4 deletions src/libtego/source/core/UserIdentity.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,16 @@ class UserIdentity : public QObject
ContactsManager *getContacts() { return &contacts; }

/* State */
bool isServiceOnline() const;
Tor::HiddenService *hiddenService() const { return m_hiddenService; }

/* Take ownership of an inbound connection. Returns the shared pointer to
* the connection, and releases the reference held by UserIdentity. */
QSharedPointer<Protocol::Connection> takeIncomingConnection(Protocol::Connection *connection);

signals:
void statusChanged();
void contactIDChanged(); // only possible during creation
void incomingConnection(Protocol::Connection *connection);

private slots:
void onStatusChanged(int newStatus, int oldStatus);
void onIncomingConnection();

private:
Expand Down
2 changes: 1 addition & 1 deletion src/libtego/source/signals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ extern "C"
TEGO_DEFINE_CALLBACK_SETTER(tor_network_status_changed)
TEGO_DEFINE_CALLBACK_SETTER(tor_bootstrap_status_changed)
TEGO_DEFINE_CALLBACK_SETTER(tor_log_received)
TEGO_DEFINE_CALLBACK_SETTER(host_user_state_changed)
TEGO_DEFINE_CALLBACK_SETTER(host_onion_service_state_changed)
TEGO_DEFINE_CALLBACK_SETTER(chat_request_received)
TEGO_DEFINE_CALLBACK_SETTER(chat_request_response_received)
TEGO_DEFINE_CALLBACK_SETTER(message_received)
Expand Down
2 changes: 1 addition & 1 deletion src/libtego/source/signals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ namespace tego
TEGO_IMPLEMENT_CALLBACK_FUNCTIONS(tor_network_status_changed, tego_tor_network_status_t)
TEGO_IMPLEMENT_CALLBACK_FUNCTIONS(tor_bootstrap_status_changed, int32_t, tego_tor_bootstrap_tag_t)
TEGO_IMPLEMENT_CALLBACK_FUNCTIONS(tor_log_received, char*, size_t)
TEGO_IMPLEMENT_CALLBACK_FUNCTIONS(host_user_state_changed, tego_host_user_state_t)
TEGO_IMPLEMENT_CALLBACK_FUNCTIONS(host_onion_service_state_changed, tego_host_onion_service_state_t)
TEGO_IMPLEMENT_CALLBACK_FUNCTIONS(chat_request_received, tego_user_id_t*, char*, size_t)
TEGO_IMPLEMENT_CALLBACK_FUNCTIONS(chat_request_response_received, tego_user_id_t*, tego_bool_t)
TEGO_IMPLEMENT_CALLBACK_FUNCTIONS(message_received, tego_user_id_t*, tego_time_t, tego_message_id_t, char*, size_t)
Expand Down
29 changes: 9 additions & 20 deletions src/libtego/source/tor/HiddenService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,20 @@
#include "utils/CryptoKey.h"
#include "utils/Useful.h"

#include "globals.hpp"
using tego::g_globals;

using namespace Tor;

HiddenService::HiddenService(QObject *parent)
: QObject(parent), m_status(NotCreated)
: QObject(parent)
{
}

HiddenService::HiddenService(const CryptoKey &privateKey, QObject *parent)
: QObject(parent), m_status(NotCreated)
: QObject(parent)
{
setPrivateKey(privateKey);
m_status = Offline;
}

void HiddenService::setStatus(Status newStatus)
{
if (m_status == newStatus)
return;

Status old = m_status;
m_status = newStatus;

emit statusChanged(m_status, old);

if (m_status == Online)
emit serviceOnline();
}

void HiddenService::addTarget(const Target &target)
Expand Down Expand Up @@ -92,14 +80,15 @@ void HiddenService::setPrivateKey(const CryptoKey &key)
emit privateKeyChanged();
}

void HiddenService::servicePublished()
void HiddenService::serviceAdded()
{
if (m_hostname.isEmpty()) {
qDebug() << "Failed to read hidden service hostname";
return;
}

qDebug() << "Hidden service published successfully";
setStatus(Online);
g_globals.context->set_host_onion_service_state(tego_host_onion_service_state_service_added);

qDebug() << "Hidden service added successfully";
}

17 changes: 2 additions & 15 deletions src/libtego/source/tor/HiddenService.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,11 @@ class HiddenService : public QObject
quint16 servicePort, targetPort;
};

enum Status
{
NotCreated = -1, /* Service has not been created yet */
Offline = 0, /* Data exists, but service is not published */
Online /* Published */
};

HiddenService(QObject *parent = 0);
HiddenService(const CryptoKey &privateKey, QObject *parent = 0);

Status status() const { return m_status; }

const QString &hostname() const { return m_hostname; }
const QString serviceId() const { return m_hostname.left(TEGO_V3_ONION_SERVICE_ID_LENGTH); }

CryptoKey privateKey() { return m_privateKey; }
void setPrivateKey(const CryptoKey &privateKey);
Expand All @@ -76,20 +68,15 @@ class HiddenService : public QObject
void addTarget(quint16 servicePort, QHostAddress targetAddress, quint16 targetPort);

signals:
void statusChanged(int newStatus, int oldStatus);
void serviceOnline();
void privateKeyChanged();

private slots:
void servicePublished();
void serviceAdded();

private:
QList<Target> m_targets;
QString m_hostname;
Status m_status;
CryptoKey m_privateKey;

void setStatus(Status newStatus);
};

}
Expand Down
32 changes: 26 additions & 6 deletions src/libtego/source/tor/TorControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public slots:
void setError(const QString &message);

void statusEvent(int code, const QByteArray &data);
void hsDescEvent(int code, const QByteArray &data);
void updateBootstrap(const QList<QByteArray> &data);
};

Expand Down Expand Up @@ -171,10 +172,6 @@ void TorControlPrivate::setTorStatus(TorControl::TorStatus n)
// Request info again to read the SOCKS port
getTorInfo();
}
else
{
g_globals.context->set_host_user_state(tego_host_user_state_online);
}
}
}

Expand Down Expand Up @@ -297,6 +294,9 @@ void TorControlPrivate::authenticateReply()
TorControlCommand *clientEvents = new TorControlCommand;
connect(clientEvents, &TorControlCommand::replyLine, this, &TorControlPrivate::statusEvent);
socket->registerEvent("STATUS_CLIENT", clientEvents);
TorControlCommand *hsDescEvents = new TorControlCommand;
connect(hsDescEvents, &TorControlCommand::replyLine, this, &TorControlPrivate::hsDescEvent);
socket->registerEvent("HS_DESC", hsDescEvents);

getTorInfo();
}
Expand Down Expand Up @@ -380,7 +380,6 @@ void TorControlPrivate::getTorInfoReply()

if (command->get(QByteArray("status/circuit-established")).toInt() == 1) {
qDebug() << "torctrl: Tor indicates that circuits have been established; state is TorReady";
g_globals.context->set_host_user_state(tego_host_user_state_online);
setTorStatus(TorControl::TorReady);
} else {
setTorStatus(TorControl::TorOffline);
Expand Down Expand Up @@ -420,7 +419,7 @@ void TorControlPrivate::publishService()
else
qDebug() << "torctrl: Publishing hidden service" << service->hostname();
AddOnionCommand *onionCommand = new AddOnionCommand(service);
QObject::connect(onionCommand, &AddOnionCommand::succeeded, service, &HiddenService::servicePublished);
QObject::connect(onionCommand, &AddOnionCommand::succeeded, service, &HiddenService::serviceAdded);
socket->sendCommand(onionCommand, onionCommand->build());
}

Expand Down Expand Up @@ -469,6 +468,27 @@ void TorControlPrivate::statusEvent(int code, const QByteArray &data)
}
}

void TorControlPrivate::hsDescEvent(int code, const QByteArray &data)
{
Q_UNUSED(code);

QList<QByteArray> tokens = splitQuotedStrings(data.trimmed(), ' ');
if (tokens.size() < 3)
return;

if (service != nullptr) {
qDebug() << "hostname:" << service->serviceId();
}

if (service != nullptr &&
tokens[1] == "UPLOADED" && tokens[2] == service->serviceId()) {
qDebug() << "SERVICE PUBLISHED";
g_globals.context->set_host_onion_service_state(tego_host_onion_service_state_service_published);
}

qDebug() << "torctrl: hs_desc event:" << data.trimmed();
}

void TorControlPrivate::updateBootstrap(const QList<QByteArray> &data)
{
bootstrapStatus.clear();
Expand Down
Loading

0 comments on commit 399634c

Please sign in to comment.