Fix client test applications and implement tunnel client utils

This commit is contained in:
Simon Stürz 2021-08-12 10:27:28 +02:00
parent 6d4ff26481
commit 3eb247e652
13 changed files with 154 additions and 60 deletions

View File

@ -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();

View File

@ -158,7 +158,6 @@ void SslServer::incomingConnection(qintptr socketDescriptor)
emit clientConnected(sslSocket);
});
typedef void (QSslSocket:: *sslErrorsSignal)(const QList<QSslError> &);
connect(sslSocket, static_cast<sslErrorsSignal>(&QSslSocket::sslErrors), this, [](const QList<QSslError> &errors) {
qCWarning(dcTcpSocketServer()) << "SSL Errors happened in the client connections:";

View File

@ -53,11 +53,6 @@ void ProxyConnection::setConnected(bool connected)
emit connectedChanged(m_connected);
}
void ProxyConnection::setServerUrl(const QUrl &serverUrl)
{
m_serverUrl = serverUrl;
}
ProxyConnection::~ProxyConnection()
{

View File

@ -31,6 +31,7 @@
#include <QUrl>
#include <QObject>
#include <QSslError>
#include <QSslConfiguration>
#include <QHostAddress>
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);

View File

@ -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<errorSignal>(&QSslSocket::error), this, &TcpSocketConnection::onError);
connect(m_tcpSocket, &QSslSocket::stateChanged, this, &TcpSocketConnection::onStateChanged);
QObject::connect(m_tcpSocket, static_cast<errorSignal>(&QSslSocket::error), this, &TcpSocketConnection::onError);
typedef void (QSslSocket:: *sslErrorsSignal)(const QList<QSslError> &);
QObject::connect(m_tcpSocket, static_cast<sslErrorsSignal>(&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<QSslError>)), this, SIGNAL(sslErrors(QList<QSslError>)));
}
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<quint16>(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<quint16>(m_serverUrl.port()));
} else {
m_ssl = true;
qCDebug(dcRemoteProxyClientTcpSocket()) << "Connecting encrypted to" << this->serverUrl().toString();
m_tcpSocket->connectToHostEncrypted(this->serverUrl().host(), static_cast<quint16>(this->serverUrl().port()));
qCDebug(dcRemoteProxyClientTcpSocket()) << "Connecting encrypted to" << m_serverUrl.host() + ":" + QString::number(m_serverUrl.port());
m_tcpSocket->connectToHostEncrypted(m_serverUrl.host(), static_cast<quint16>(m_serverUrl.port()));
}
}
void TcpSocketConnection::disconnectServer()
{
qCDebug(dcRemoteProxyClientTcpSocket()) << "Disconnecting from" << serverUrl().toString();
m_tcpSocket->disconnectFromHost();
m_tcpSocket->close();
m_tcpSocket->abort();
}
}

View File

@ -35,7 +35,7 @@
#include "proxyconnection.h"
Q_DECLARE_LOGGING_CATEGORY(dcRemoteProxyClienTcpSocket)
Q_DECLARE_LOGGING_CATEGORY(dcRemoteProxyClientTcpSocket)
namespace remoteproxyclient {

View File

@ -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;

View File

@ -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();
}

View File

@ -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<QSslError> &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);
}

View File

@ -1,15 +1,30 @@
#ifndef CLIENTCONNECTION_H
#define CLIENTCONNECTION_H
#include <QUrl>
#include <QObject>
#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:
};

View File

@ -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();

View File

@ -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<QSslError> &errors){
if (m_insecure) {
qDebug() << "SSL errors occured. Ignoring because explicit specified.";
m_socketServer->ignoreSslErrors(errors);
} else {
qWarning() << "SSL errors occured:";

View File

@ -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