From 3eb247e652af64fb3c018846f034eda14e6f54c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Thu, 12 Aug 2021 10:27:28 +0200 Subject: [PATCH] Fix client test applications and implement tunnel client utils --- client/main.cpp | 19 +++--- .../server/tcpsocketserver.cpp | 1 - .../proxyconnection.cpp | 5 -- libnymea-remoteproxyclient/proxyconnection.h | 5 +- .../tcpsocketconnection.cpp | 33 ++++++---- .../tcpsocketconnection.h | 2 +- .../tunnelproxy/tunnelproxysocketserver.cpp | 5 +- .../websocketconnection.cpp | 6 +- tunnelclient/clientconnection.cpp | 47 ++++++++++++++- tunnelclient/clientconnection.h | 19 +++++- tunnelclient/main.cpp | 60 +++++++++++++------ tunnelclient/serverconnection.cpp | 5 +- tunnelclient/tunnelclient.pro | 7 ++- 13 files changed, 154 insertions(+), 60 deletions(-) diff --git a/client/main.cpp b/client/main.cpp index 801655d..5ac3d24 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -162,22 +162,25 @@ int main(int argc, char *argv[]) qCCritical(dcProxyClient()) << "Invalid proxy server url passed." << parser.value(urlOption); exit(-1); } + qCDebug(dcProxyClient()) << "Using URL" << serverUrl; QUuid uuid(parser.value(uuidOption)); if (uuid.isNull()) { uuid = QUuid::createUuid(); } + ProxyClient *client = nullptr; if (parser.isSet(tcpOption)) { - ProxyClient client(parser.value(nameOption), uuid, RemoteProxyConnection::ConnectionTypeTcpSocket); - client.setInsecure(parser.isSet(insecureOption)); - client.setPingpong(parser.isSet(pingPongOption)); - client.start(serverUrl, parser.value(tokenOption), parser.value(nonceOption)); + qCDebug(dcProxyClient()) << "Using TCP as transport layer"; + client = new ProxyClient(parser.value(nameOption), uuid, RemoteProxyConnection::ConnectionTypeTcpSocket); + client->setInsecure(parser.isSet(insecureOption)); + client->setPingpong(parser.isSet(pingPongOption)); + client->start(serverUrl, parser.value(tokenOption), parser.value(nonceOption)); } else { - ProxyClient client(parser.value(nameOption), uuid, RemoteProxyConnection::ConnectionTypeWebSocket); - client.setInsecure(parser.isSet(insecureOption)); - client.setPingpong(parser.isSet(pingPongOption)); - client.start(serverUrl, parser.value(tokenOption), parser.value(nonceOption)); + client = new ProxyClient(parser.value(nameOption), uuid, RemoteProxyConnection::ConnectionTypeWebSocket); + client->setInsecure(parser.isSet(insecureOption)); + client->setPingpong(parser.isSet(pingPongOption)); + client->start(serverUrl, parser.value(tokenOption), parser.value(nonceOption)); } return application.exec(); diff --git a/libnymea-remoteproxy/server/tcpsocketserver.cpp b/libnymea-remoteproxy/server/tcpsocketserver.cpp index f2d1079..3e627fd 100644 --- a/libnymea-remoteproxy/server/tcpsocketserver.cpp +++ b/libnymea-remoteproxy/server/tcpsocketserver.cpp @@ -158,7 +158,6 @@ void SslServer::incomingConnection(qintptr socketDescriptor) emit clientConnected(sslSocket); }); - typedef void (QSslSocket:: *sslErrorsSignal)(const QList &); connect(sslSocket, static_cast(&QSslSocket::sslErrors), this, [](const QList &errors) { qCWarning(dcTcpSocketServer()) << "SSL Errors happened in the client connections:"; diff --git a/libnymea-remoteproxyclient/proxyconnection.cpp b/libnymea-remoteproxyclient/proxyconnection.cpp index 2dcffd7..6fe5377 100644 --- a/libnymea-remoteproxyclient/proxyconnection.cpp +++ b/libnymea-remoteproxyclient/proxyconnection.cpp @@ -53,11 +53,6 @@ void ProxyConnection::setConnected(bool connected) emit connectedChanged(m_connected); } -void ProxyConnection::setServerUrl(const QUrl &serverUrl) -{ - m_serverUrl = serverUrl; -} - ProxyConnection::~ProxyConnection() { diff --git a/libnymea-remoteproxyclient/proxyconnection.h b/libnymea-remoteproxyclient/proxyconnection.h index 178fab7..f67ebdc 100644 --- a/libnymea-remoteproxyclient/proxyconnection.h +++ b/libnymea-remoteproxyclient/proxyconnection.h @@ -31,6 +31,7 @@ #include #include #include +#include #include namespace remoteproxyclient { @@ -53,11 +54,11 @@ public: private: bool m_connected = false; - QUrl m_serverUrl; protected: + QUrl m_serverUrl; + void setConnected(bool connected); - void setServerUrl(const QUrl &serverUrl); signals: void connectedChanged(bool connected); diff --git a/libnymea-remoteproxyclient/tcpsocketconnection.cpp b/libnymea-remoteproxyclient/tcpsocketconnection.cpp index cca948d..0f69742 100644 --- a/libnymea-remoteproxyclient/tcpsocketconnection.cpp +++ b/libnymea-remoteproxyclient/tcpsocketconnection.cpp @@ -36,18 +36,26 @@ TcpSocketConnection::TcpSocketConnection(QObject *parent) : { m_tcpSocket = new QSslSocket(this); - connect(m_tcpSocket, &QSslSocket::disconnected, this, &TcpSocketConnection::onDisconnected); - connect(m_tcpSocket, &QSslSocket::encrypted, this, &TcpSocketConnection::onEncrypted); - connect(m_tcpSocket, &QSslSocket::readyRead, this, &TcpSocketConnection::onReadyRead); + QObject::connect(m_tcpSocket, &QSslSocket::connected, this, [=](){ qCDebug(dcRemoteProxyClientTcpSocket()) << "Connected!!!!"; }); + QObject::connect(m_tcpSocket, &QSslSocket::disconnected, this, &TcpSocketConnection::onDisconnected); + QObject::connect(m_tcpSocket, &QSslSocket::encrypted, this, &TcpSocketConnection::onEncrypted); + QObject::connect(m_tcpSocket, &QSslSocket::readyRead, this, &TcpSocketConnection::onReadyRead); + QObject::connect(m_tcpSocket, &QSslSocket::stateChanged, this, &TcpSocketConnection::onStateChanged); + typedef void (QSslSocket:: *errorSignal)(QAbstractSocket::SocketError); - connect(m_tcpSocket, static_cast(&QSslSocket::error), this, &TcpSocketConnection::onError); - connect(m_tcpSocket, &QSslSocket::stateChanged, this, &TcpSocketConnection::onStateChanged); + QObject::connect(m_tcpSocket, static_cast(&QSslSocket::error), this, &TcpSocketConnection::onError); + typedef void (QSslSocket:: *sslErrorsSignal)(const QList &); QObject::connect(m_tcpSocket, static_cast(&QSslSocket::sslErrors), this, &TcpSocketConnection::sslErrors); + + // QObject::connect(m_tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(onError(QAbstractSocket::SocketError))); + // QObject::connect(m_tcpSocket, SIGNAL(sslErrors(QList)), this, SIGNAL(sslErrors(QList))); + } TcpSocketConnection::~TcpSocketConnection() { + qCDebug(dcRemoteProxyClientTcpSocket()) << "Delete socket connection"; m_tcpSocket->close(); } @@ -110,21 +118,24 @@ void TcpSocketConnection::onReadyRead() void TcpSocketConnection::connectServer(const QUrl &serverUrl) { - setServerUrl(serverUrl); - if (serverUrl.scheme() == "tcp") { - qCDebug(dcRemoteProxyClientTcpSocket()) << "Connecting to" << this->serverUrl().toString(); - m_tcpSocket->connectToHost(QHostAddress(this->serverUrl().host()), static_cast(this->serverUrl().port())); + m_serverUrl = serverUrl; + + if (m_serverUrl.scheme() == "tcp") { + qCDebug(dcRemoteProxyClientTcpSocket()) << "Connecting to" << m_serverUrl.toString(); + m_tcpSocket->connectToHost(QHostAddress(m_serverUrl.host()), static_cast(m_serverUrl.port())); } else { m_ssl = true; - qCDebug(dcRemoteProxyClientTcpSocket()) << "Connecting encrypted to" << this->serverUrl().toString(); - m_tcpSocket->connectToHostEncrypted(this->serverUrl().host(), static_cast(this->serverUrl().port())); + qCDebug(dcRemoteProxyClientTcpSocket()) << "Connecting encrypted to" << m_serverUrl.host() + ":" + QString::number(m_serverUrl.port()); + m_tcpSocket->connectToHostEncrypted(m_serverUrl.host(), static_cast(m_serverUrl.port())); } } void TcpSocketConnection::disconnectServer() { qCDebug(dcRemoteProxyClientTcpSocket()) << "Disconnecting from" << serverUrl().toString(); + m_tcpSocket->disconnectFromHost(); m_tcpSocket->close(); + m_tcpSocket->abort(); } } diff --git a/libnymea-remoteproxyclient/tcpsocketconnection.h b/libnymea-remoteproxyclient/tcpsocketconnection.h index 384a8dc..232d45c 100644 --- a/libnymea-remoteproxyclient/tcpsocketconnection.h +++ b/libnymea-remoteproxyclient/tcpsocketconnection.h @@ -35,7 +35,7 @@ #include "proxyconnection.h" -Q_DECLARE_LOGGING_CATEGORY(dcRemoteProxyClienTcpSocket) +Q_DECLARE_LOGGING_CATEGORY(dcRemoteProxyClientTcpSocket) namespace remoteproxyclient { diff --git a/libnymea-remoteproxyclient/tunnelproxy/tunnelproxysocketserver.cpp b/libnymea-remoteproxyclient/tunnelproxy/tunnelproxysocketserver.cpp index a1ec6f8..b63d44c 100644 --- a/libnymea-remoteproxyclient/tunnelproxy/tunnelproxysocketserver.cpp +++ b/libnymea-remoteproxyclient/tunnelproxy/tunnelproxysocketserver.cpp @@ -32,8 +32,8 @@ #include "proxyjsonrpcclient.h" #include "../../common/slipdataprocessor.h" -Q_LOGGING_CATEGORY(dcTunnelProxySocketServer, "dcTunnelProxySocketServer") -Q_LOGGING_CATEGORY(dcTunnelProxySocketServerTraffic, "dcTunnelProxySocketServerTraffic") +Q_LOGGING_CATEGORY(dcTunnelProxySocketServer, "TunnelProxySocketServer") +Q_LOGGING_CATEGORY(dcTunnelProxySocketServerTraffic, "TunnelProxySocketServerTraffic") namespace remoteproxyclient { @@ -109,7 +109,6 @@ QString TunnelProxySocketServer::remoteProxyApiVersion() const return m_remoteProxyApiVersion; } - void TunnelProxySocketServer::startServer(const QUrl &serverUrl) { m_serverUrl = serverUrl; diff --git a/libnymea-remoteproxyclient/websocketconnection.cpp b/libnymea-remoteproxyclient/websocketconnection.cpp index 703ebdf..0c2cdc3 100644 --- a/libnymea-remoteproxyclient/websocketconnection.cpp +++ b/libnymea-remoteproxyclient/websocketconnection.cpp @@ -103,15 +103,15 @@ void WebSocketConnection::connectServer(const QUrl &serverUrl) if (connected()) { m_webSocket->close(); } - setServerUrl(serverUrl); + m_serverUrl = serverUrl; - qCDebug(dcRemoteProxyClientWebSocket()) << "Connecting to" << this->serverUrl().toString(); + qCDebug(dcRemoteProxyClientWebSocket()) << "Connecting to" << m_serverUrl.toString(); m_webSocket->open(this->serverUrl()); } void WebSocketConnection::disconnectServer() { - qCDebug(dcRemoteProxyClientWebSocket()) << "Disconnecting from" << serverUrl().toString(); + qCDebug(dcRemoteProxyClientWebSocket()) << "Disconnecting from" << m_serverUrl.toString(); m_webSocket->close(); } diff --git a/tunnelclient/clientconnection.cpp b/tunnelclient/clientconnection.cpp index 1a81032..862e9fd 100644 --- a/tunnelclient/clientconnection.cpp +++ b/tunnelclient/clientconnection.cpp @@ -1,6 +1,51 @@ #include "clientconnection.h" -ClientConnection::ClientConnection(QObject *parent) : QObject(parent) +ClientConnection::ClientConnection(const QUrl &serverUrl, const QString &name, const QUuid &uuid, const QUuid &serverUuid, bool insecure, QObject *parent) : + QObject(parent), + m_serverUrl(serverUrl), + m_name(name), + m_uuid(uuid), + m_serverUuid(serverUuid), + m_insecure(insecure) { + m_remoteConnection = new TunnelProxyRemoteConnection(m_uuid, m_name); + + connect(m_remoteConnection, &TunnelProxyRemoteConnection::stateChanged, this, [this](TunnelProxyRemoteConnection::State state){ + switch (state) { + case TunnelProxyRemoteConnection::StateRegister: + qDebug() << "Connected with" << m_remoteConnection->remoteProxyServer() << m_remoteConnection->remoteProxyServerName() << m_remoteConnection->remoteProxyServerVersion() << m_remoteConnection->remoteProxyApiVersion(); + break; + default: + break; + } + }); + + connect(m_remoteConnection, &TunnelProxyRemoteConnection::remoteConnectedChanged, this, [](bool remoteConnected){ + qDebug() << "Remote connection " << (remoteConnected ? "established" : "disconnected"); + }); + + connect(m_remoteConnection, &TunnelProxyRemoteConnection::dataReady, this, [](const QByteArray &data){ + qDebug() << "Data received" << data; + }); + + connect(m_remoteConnection, &TunnelProxyRemoteConnection::errorOccured, this, [](QAbstractSocket::SocketError error){ + qWarning() << "Socket error occured" << error; + }); + + connect(m_remoteConnection, &TunnelProxyRemoteConnection::sslErrors, this, [=](const QList &errors){ + if (m_insecure) { + m_remoteConnection->ignoreSslErrors(errors); + } else { + qWarning() << "SSL errors occured:"; + foreach (const QSslError &sslError, errors) { + qWarning() << " --> " << sslError.errorString(); + } + } + }); +} + +void ClientConnection::connectToServer() +{ + m_remoteConnection->connectServer(m_serverUrl, m_serverUuid); } diff --git a/tunnelclient/clientconnection.h b/tunnelclient/clientconnection.h index 908f40e..4e2c1d2 100644 --- a/tunnelclient/clientconnection.h +++ b/tunnelclient/clientconnection.h @@ -1,15 +1,30 @@ #ifndef CLIENTCONNECTION_H #define CLIENTCONNECTION_H +#include #include +#include "tunnelproxy/tunnelproxyremoteconnection.h" + +using namespace remoteproxyclient; + class ClientConnection : public QObject { Q_OBJECT public: - explicit ClientConnection(QObject *parent = nullptr); + explicit ClientConnection(const QUrl &serverUrl, const QString &name, const QUuid &uuid, const QUuid &serverUuid, bool insecure, QObject *parent = nullptr); + + void connectToServer(); + +private: + QUrl m_serverUrl; + QString m_name; + QUuid m_uuid; + QUuid m_serverUuid; + bool m_insecure = false; + + TunnelProxyRemoteConnection *m_remoteConnection = nullptr; -signals: }; diff --git a/tunnelclient/main.cpp b/tunnelclient/main.cpp index 4e9a61d..eee8e1d 100644 --- a/tunnelclient/main.cpp +++ b/tunnelclient/main.cpp @@ -58,10 +58,18 @@ static void consoleLogHandler(QtMsgType type, const QMessageLogContext& context, { switch (type) { case QtInfoMsg: - fprintf(stdout, "%s: %s\n", context.category, message.toUtf8().data()); + if (context.category == QStringLiteral("default")) { + fprintf(stdout, "%s\n", message.toUtf8().data()); + } else { + fprintf(stdout, "%s: %s\n", context.category, message.toUtf8().data()); + } break; case QtDebugMsg: - fprintf(stdout, "%s: %s\n", context.category, message.toUtf8().data()); + if (context.category == QStringLiteral("default")) { + fprintf(stdout, "%s\n", message.toUtf8().data()); + } else { + fprintf(stdout, "%s: %s\n", context.category, message.toUtf8().data()); + } break; case QtWarningMsg: fprintf(stdout, "%s%s: %s%s\n", warning, context.category, message.toUtf8().data(), normal); @@ -86,12 +94,15 @@ int main(int argc, char *argv[]) application.setApplicationVersion(SERVER_VERSION_STRING); // Default debug categories - s_loggingFilters.insert("ProxyClient", true); s_loggingFilters.insert("RemoteProxyClientJsonRpc", false); - s_loggingFilters.insert("RemoteProxyClientWebSocket", false); - s_loggingFilters.insert("RemoteProxyClientConnection", false); s_loggingFilters.insert("RemoteProxyClientJsonRpcTraffic", false); + s_loggingFilters.insert("RemoteProxyClientWebSocket", false); + s_loggingFilters.insert("RemoteProxyClientTcpSocket", false); + s_loggingFilters.insert("RemoteProxyClientConnection", false); s_loggingFilters.insert("RemoteProxyClientConnectionTraffic", false); + s_loggingFilters.insert("TunnelProxySocketServer", false); + s_loggingFilters.insert("TunnelProxySocketServerTraffic", false); + s_loggingFilters.insert("TunnelProxyRemoteConnection", false); QCommandLineParser parser; parser.addHelpOption(); @@ -118,13 +129,13 @@ int main(int argc, char *argv[]) QCommandLineOption clientOption(QStringList() << "c" << "client", "Connect as tunnel proxy client connection. The server uuid is required."); parser.addOption(clientOption); - QCommandLineOption nameOption(QStringList() << "n" << "name", "The name of the connecting client.", "name"); + QCommandLineOption nameOption(QStringList() << "n" << "name", "The name of the connecting client. If not specified a default name will be selected.", "name"); parser.addOption(nameOption); - QCommandLineOption uuidOption(QStringList() << "u" << "uuid", "The uuid of the connecting client. If not specified, a new one will be created", "uuid"); + QCommandLineOption uuidOption(QStringList() << "uuid", "The uuid of the connecting client. If not specified, a new one will be created.", "uuid"); parser.addOption(uuidOption); - QCommandLineOption serverUuidOption(QStringList() << "server-uuid", "The uuid of the server you want to connect to as client connection."); + QCommandLineOption serverUuidOption(QStringList() << "server-uuid", "The uuid of the server you want to connect to as client connection.", "uuid"); parser.addOption(serverUuidOption); QCommandLineOption verboseOption(QStringList() << "verbose", "Print more information about the connection."); @@ -166,23 +177,25 @@ int main(int argc, char *argv[]) if (parser.isSet(verboseOption) || parser.isSet(veryVerboseOption)) { s_loggingFilters["default"] = true; s_loggingFilters["TunnelProxySocketServer"] = true; - s_loggingFilters["RemoteProxyClientJsonRpc"] = true; - s_loggingFilters["RemoteProxyClientWebSocket"] = true; - s_loggingFilters["RemoteProxyClientConnection"] = true; + s_loggingFilters["TunnelProxyRemoteConnection"] = true; } // Enable very verbose if (parser.isSet(veryVerboseOption)) { + s_loggingFilters["RemoteProxyClientJsonRpc"] = true; s_loggingFilters["RemoteProxyClientJsonRpcTraffic"] = true; + s_loggingFilters["RemoteProxyClientWebSocket"] = true; + s_loggingFilters["RemoteProxyClientTcpSocket"] = true; + s_loggingFilters["RemoteProxyClientConnection"] = true; + s_loggingFilters["RemoteProxyClientConnectionTraffic"] = true; s_loggingFilters["TunnelProxySocketServerTraffic"] = true; } QLoggingCategory::installFilter(loggingCategoryFilter); // Create the server connection - ServerConnection server(serverUrl, name, uuid, parser.isSet(insecureOption)); - - server.startServer(); + ServerConnection *server = new ServerConnection(serverUrl, name, uuid, parser.isSet(insecureOption)); + server->startServer(); } else { // Client @@ -197,21 +210,34 @@ int main(int argc, char *argv[]) exit(EXIT_FAILURE); } + QUuid serverUuid(parser.value(serverUuidOption)); + if (serverUuid.isNull()) { + qCritical() << "The given server uuid is not valid." << parser.value(serverUuidOption); + exit(EXIT_FAILURE); + } + // Enable verbose if (parser.isSet(verboseOption) || parser.isSet(veryVerboseOption)) { s_loggingFilters["default"] = true; + s_loggingFilters["TunnelProxySocketServer"] = true; s_loggingFilters["TunnelProxyRemoteConnection"] = true; - s_loggingFilters["RemoteProxyClientJsonRpc"] = true; - s_loggingFilters["RemoteProxyClientWebSocket"] = true; - s_loggingFilters["RemoteProxyClientConnection"] = true; } // Enable very verbose if (parser.isSet(veryVerboseOption)) { + s_loggingFilters["RemoteProxyClientJsonRpc"] = true; s_loggingFilters["RemoteProxyClientJsonRpcTraffic"] = true; + s_loggingFilters["RemoteProxyClientWebSocket"] = true; + s_loggingFilters["RemoteProxyClientTcpSocket"] = true; + s_loggingFilters["RemoteProxyClientConnection"] = true; + s_loggingFilters["RemoteProxyClientConnectionTraffic"] = true; s_loggingFilters["TunnelProxySocketServerTraffic"] = true; } QLoggingCategory::installFilter(loggingCategoryFilter); + + // Create the server connection + ClientConnection *client = new ClientConnection(serverUrl, name, uuid, serverUuid, parser.isSet(insecureOption)); + client->connectToServer(); } return application.exec(); diff --git a/tunnelclient/serverconnection.cpp b/tunnelclient/serverconnection.cpp index be48d3c..8d91ce0 100644 --- a/tunnelclient/serverconnection.cpp +++ b/tunnelclient/serverconnection.cpp @@ -19,15 +19,14 @@ ServerConnection::ServerConnection(const QUrl &serverUrl, const QString &name, c }); connect(m_socketServer, &TunnelProxySocketServer::runningChanged, this, [=](bool running){ - qDebug() << "--> Server is" << (running ? "running" : "not running any more"); if (running) { - qDebug() << "--> Connected with" << m_socketServer->remoteProxyServer() << m_socketServer->remoteProxyServerName() << m_socketServer->remoteProxyServerVersion() << m_socketServer->remoteProxyApiVersion(); + qDebug() << "Connected with" << m_socketServer->remoteProxyServer() << m_socketServer->remoteProxyServerName() << m_socketServer->remoteProxyServerVersion() << m_socketServer->remoteProxyApiVersion(); } + qDebug() << "--> The tunnel proxy server is" << (running ? "running and listening for incoming connections" : "not running any more"); }); connect(m_socketServer, &TunnelProxySocketServer::sslErrors, this, [=](const QList &errors){ if (m_insecure) { - qDebug() << "SSL errors occured. Ignoring because explicit specified."; m_socketServer->ignoreSslErrors(errors); } else { qWarning() << "SSL errors occured:"; diff --git a/tunnelclient/tunnelclient.pro b/tunnelclient/tunnelclient.pro index 8916884..a86e2d7 100644 --- a/tunnelclient/tunnelclient.pro +++ b/tunnelclient/tunnelclient.pro @@ -8,6 +8,10 @@ INCLUDEPATH += ../libnymea-remoteproxy LIBS += -L$$top_builddir/libnymea-remoteproxyclient/ -lnymea-remoteproxyclient +HEADERS += \ + clientconnection.h \ + serverconnection.h + SOURCES += main.cpp \ clientconnection.cpp \ serverconnection.cpp @@ -15,6 +19,3 @@ SOURCES += main.cpp \ target.path = $$[QT_INSTALL_PREFIX]/bin INSTALLS += target -HEADERS += \ - clientconnection.h \ - serverconnection.h