Add pending connection to tcp server

cleanup-proxy-code
Simon Stürz 2023-02-22 00:55:16 +01:00
parent 3b3c117f49
commit 756513f668
6 changed files with 57 additions and 15 deletions

View File

@ -135,7 +135,8 @@ void Engine::start(ProxyConfiguration *configuration)
qCDebug(dcEngine()) << "Starting the tunnel proxy manager..."; qCDebug(dcEngine()) << "Starting the tunnel proxy manager...";
m_tunnelProxyServer->startServer(); m_tunnelProxyServer->startServer();
// Start the monitor server // Monitor server
// -------------------------------------
m_monitorServer = new MonitorServer(configuration->monitorSocketFileName(), this); m_monitorServer = new MonitorServer(configuration->monitorSocketFileName(), this);
m_monitorServer->startServer(); m_monitorServer->startServer();
@ -266,7 +267,6 @@ Engine::Engine(QObject *parent) :
m_timer->setInterval(50); m_timer->setInterval(50);
connect(m_timer, &QTimer::timeout, this, &Engine::onTimerTick); connect(m_timer, &QTimer::timeout, this, &Engine::onTimerTick);
m_logEngine = new LogEngine(this); m_logEngine = new LogEngine(this);
} }
@ -357,6 +357,9 @@ void Engine::setRunning(bool running)
if (m_proxyServer) if (m_proxyServer)
m_proxyServer->setRunning(running); m_proxyServer->setRunning(running);
if (m_tunnelProxyServer)
m_tunnelProxyServer->setRunning(running);
qCDebug(dcEngine()) << "Engine is" << (running ? "now running." : "not running any more."); qCDebug(dcEngine()) << "Engine is" << (running ? "now running." : "not running any more.");
if (running) { if (running) {

View File

@ -189,6 +189,11 @@ void JsonRpcServer::unregisterHandler(JsonHandler *handler)
m_handlers.remove(handler->name()); m_handlers.remove(handler->name());
} }
uint JsonRpcServer::registeredClientCount() const
{
return m_clients.count();
}
void JsonRpcServer::processDataPacket(TransportClient *transportClient, const QByteArray &data) void JsonRpcServer::processDataPacket(TransportClient *transportClient, const QByteArray &data)
{ {
QJsonParseError error; QJsonParseError error;

View File

@ -54,6 +54,8 @@ public:
void registerHandler(JsonHandler *handler); void registerHandler(JsonHandler *handler);
void unregisterHandler(JsonHandler *handler); void unregisterHandler(JsonHandler *handler);
uint registeredClientCount() const;
signals: signals:
void TunnelEstablished(const QVariantMap &params); void TunnelEstablished(const QVariantMap &params);

View File

@ -85,8 +85,14 @@ bool TcpSocketServer::running() const
bool TcpSocketServer::startServer() bool TcpSocketServer::startServer()
{ {
if (m_server) {
m_server->close();
delete m_server;
m_server = nullptr;
}
qCDebug(dcTcpSocketServer()) << "Starting TCP server" << m_serverUrl.toString(); qCDebug(dcTcpSocketServer()) << "Starting TCP server" << m_serverUrl.toString();
m_server = new SslServer(m_sslEnabled, m_sslConfiguration); m_server = new SslServer(m_sslEnabled, m_sslConfiguration, this);
if(!m_server->listen(QHostAddress(m_serverUrl.host()), static_cast<quint16>(m_serverUrl.port()))) { if(!m_server->listen(QHostAddress(m_serverUrl.host()), static_cast<quint16>(m_serverUrl.port()))) {
qCWarning(dcTcpSocketServer()) << "Tcp server error: can not listen on" << m_serverUrl.toString(); qCWarning(dcTcpSocketServer()) << "Tcp server error: can not listen on" << m_serverUrl.toString();
delete m_server; delete m_server;
@ -98,11 +104,13 @@ bool TcpSocketServer::startServer()
connect(m_server, &SslServer::socketDisconnected, this, &TcpSocketServer::onSocketDisconnected); connect(m_server, &SslServer::socketDisconnected, this, &TcpSocketServer::onSocketDisconnected);
connect(m_server, &SslServer::dataAvailable, this, &TcpSocketServer::onDataAvailable); connect(m_server, &SslServer::dataAvailable, this, &TcpSocketServer::onDataAvailable);
qCDebug(dcTcpSocketServer()) << "Server started successfully."; qCDebug(dcTcpSocketServer()) << "Server started successfully.";
qCDebug(dcTcpSocketServer()) << m_server;
return true; return true;
} }
bool TcpSocketServer::stopServer() bool TcpSocketServer::stopServer()
{ {
qCDebug(dcTcpSocketServer()) << "Stopping server" << m_serverUrl.toString() << m_server;
if (!m_server) if (!m_server)
return true; return true;
@ -111,8 +119,6 @@ bool TcpSocketServer::stopServer()
killClientConnection(clientId, "Stop server"); killClientConnection(clientId, "Stop server");
} }
qCDebug(dcTcpSocketServer()) << "Stop server" << m_serverUrl.toString();
m_server->close(); m_server->close();
m_server->deleteLater(); m_server->deleteLater();
m_server = nullptr; m_server = nullptr;
@ -162,8 +168,11 @@ void SslServer::incomingConnection(qintptr socketDescriptor)
{ {
QSslSocket *sslSocket = new QSslSocket(this); QSslSocket *sslSocket = new QSslSocket(this);
qCDebug(dcTcpSocketServer()) << "Incomming connection" << sslSocket; qCDebug(dcTcpSocketServer()) << "Incomming connection" << sslSocket;
connect(sslSocket, &QSslSocket::disconnected, this, &SslServer::onSocketDisconnected); connect(sslSocket, &QSslSocket::disconnected, this, &SslServer::onSocketDisconnected);
connect(sslSocket, &QSslSocket::readyRead, this, &SslServer::onSocketReadyRead);
typedef void (QAbstractSocket:: *errorSignal)(QAbstractSocket::SocketError); typedef void (QAbstractSocket:: *errorSignal)(QAbstractSocket::SocketError);
connect(sslSocket, static_cast<errorSignal>(&QAbstractSocket::error), this, &SslServer::onSocketError); connect(sslSocket, static_cast<errorSignal>(&QAbstractSocket::error), this, &SslServer::onSocketError);
@ -180,7 +189,6 @@ void SslServer::incomingConnection(qintptr socketDescriptor)
} }
}); });
connect(sslSocket, &QSslSocket::readyRead, this, &SslServer::onSocketReadyRead);
if (!sslSocket->setSocketDescriptor(socketDescriptor)) { if (!sslSocket->setSocketDescriptor(socketDescriptor)) {
qCWarning(dcTcpSocketServer()) << "Failed to set SSL socket descriptor."; qCWarning(dcTcpSocketServer()) << "Failed to set SSL socket descriptor.";
@ -188,6 +196,8 @@ void SslServer::incomingConnection(qintptr socketDescriptor)
return; return;
} }
addPendingConnection(sslSocket);
if (m_sslEnabled) { if (m_sslEnabled) {
qCDebug(dcTcpSocketServer()) << "Start SSL encryption for" << sslSocket; qCDebug(dcTcpSocketServer()) << "Start SSL encryption for" << sslSocket;
sslSocket->setSslConfiguration(m_config); sslSocket->setSslConfiguration(m_config);
@ -218,7 +228,7 @@ void SslServer::onSocketError(QAbstractSocket::SocketError error)
QSslSocket *sslSocket = static_cast<QSslSocket *>(sender()); QSslSocket *sslSocket = static_cast<QSslSocket *>(sender());
qCWarning(dcTcpSocketServer()) << "Socket error occurred" << error << sslSocket->errorString(); qCWarning(dcTcpSocketServer()) << "Socket error occurred" << error << sslSocket->errorString();
qCWarning(dcTcpSocketServer()) << "Explicitly closing the socket connection."; qCWarning(dcTcpSocketServer()) << "Explicitly closing the socket connection.";
sslSocket->close(); sslSocket->abort();
} }
} }

View File

@ -67,10 +67,11 @@ void TunnelProxyServer::setRunning(bool running)
void TunnelProxyServer::registerTransportInterface(TransportInterface *interface) void TunnelProxyServer::registerTransportInterface(TransportInterface *interface)
{ {
qCDebug(dcTunnelProxyServer()) << "Register transport interface" << interface->serverName(); qCDebug(dcTunnelProxyServer()) << "Register transport interface" << interface->serverName();
//interface->setParent(this);
if (m_transportInterfaces.contains(interface)) { if (m_transportInterfaces.contains(interface)) {
qCWarning(dcTunnelProxyServer()) << "Transport interface already registerd."; qCWarning(dcTunnelProxyServer()) << "Transport interface already registerd.";
return; return;
} }
connect(interface, &TransportInterface::clientConnected, this, &TunnelProxyServer::onClientConnected); connect(interface, &TransportInterface::clientConnected, this, &TunnelProxyServer::onClientConnected);
@ -109,9 +110,15 @@ TunnelProxyServer::TunnelProxyError TunnelProxyServer::registerServer(const QUui
// Enable SLIP from now on // Enable SLIP from now on
tunnelProxyClient->enableSlipAfterResponse(); tunnelProxyClient->enableSlipAfterResponse();
TunnelProxyServerConnection *serverConnection = new TunnelProxyServerConnection(tunnelProxyClient, serverUuid, serverName, this); TunnelProxyServerConnection *serverConnection = new TunnelProxyServerConnection(tunnelProxyClient, serverUuid, serverName, tunnelProxyClient);
m_tunnelProxyServerConnections.insert(serverUuid, serverConnection); m_tunnelProxyServerConnections.insert(serverUuid, serverConnection);
qCDebug(dcTunnelProxyServer()) << "New server connection registered successfully" << serverConnection;
// For debugging
qCDebug(dcTunnelProxyServer()) << "####" << "Total clients" << m_proxyClients.count() << "JSON RPC clients" << m_jsonRpcServer->registeredClientCount() << "interface connections" << tunnelProxyClient->interface()->connectionsCount()
<< "Servers" << m_tunnelProxyServerConnections.count() << "Clients" << m_tunnelProxyClientConnections.count();
return TunnelProxyServer::TunnelProxyErrorNoError; return TunnelProxyServer::TunnelProxyErrorNoError;
} }
@ -153,12 +160,17 @@ TunnelProxyServer::TunnelProxyError TunnelProxyServer::registerClient(const QUui
tunnelProxyClient->setUuid(clientUuid); tunnelProxyClient->setUuid(clientUuid);
tunnelProxyClient->setName(clientName); tunnelProxyClient->setName(clientName);
TunnelProxyClientConnection *clientConnection = new TunnelProxyClientConnection(tunnelProxyClient, clientUuid, clientName, this); TunnelProxyClientConnection *clientConnection = new TunnelProxyClientConnection(tunnelProxyClient, clientUuid, clientName, tunnelProxyClient);
clientConnection->setServerConnection(serverConnection); clientConnection->setServerConnection(serverConnection);
m_tunnelProxyClientConnections.insert(clientUuid, clientConnection); m_tunnelProxyClientConnections.insert(clientUuid, clientConnection);
qCDebug(dcTunnelProxyServer()) << "Register client" << clientConnection << "-->" << serverConnection;
serverConnection->registerClientConnection(clientConnection); serverConnection->registerClientConnection(clientConnection);
qCDebug(dcTunnelProxyServer()) << "New client connection registered successfully" << clientConnection << "-->" << serverConnection;;
// For debugging
qCDebug(dcTunnelProxyServer()) << "#### Total clients:" << m_proxyClients.count() << "JSON RPC clients:" << m_jsonRpcServer->registeredClientCount()
<< "Interface connections:" << tunnelProxyClient->interface()->connectionsCount()
<< "Servers:" << m_tunnelProxyServerConnections.count() << "Clients:" << m_tunnelProxyClientConnections.count();
// Tell the server a new client want's to connect // Tell the server a new client want's to connect
QVariantMap params; QVariantMap params;
@ -286,6 +298,11 @@ void TunnelProxyServer::onClientConnected(const QUuid &clientId, const QHostAddr
TunnelProxyClient *tunnelProxyClient = new TunnelProxyClient(interface, clientId, address, this); TunnelProxyClient *tunnelProxyClient = new TunnelProxyClient(interface, clientId, address, this);
m_proxyClients.insert(clientId, tunnelProxyClient); m_proxyClients.insert(clientId, tunnelProxyClient);
m_jsonRpcServer->registerClient(tunnelProxyClient); m_jsonRpcServer->registerClient(tunnelProxyClient);
// For debugging
qCDebug(dcTunnelProxyServer()) << "#### Total clients:" << m_proxyClients.count() << "JSON RPC clients:" << m_jsonRpcServer->registeredClientCount()
<< "Interface connections:" << tunnelProxyClient->interface()->connectionsCount()
<< "Servers:" << m_tunnelProxyServerConnections.count() << "Clients:" << m_tunnelProxyClientConnections.count();
} }
void TunnelProxyServer::onClientDisconnected(const QUuid &clientId) void TunnelProxyServer::onClientDisconnected(const QUuid &clientId)
@ -333,6 +350,11 @@ void TunnelProxyServer::onClientDisconnected(const QUuid &clientId)
// Unregister from json rpc server // Unregister from json rpc server
m_jsonRpcServer->unregisterClient(tunnelProxyClient); m_jsonRpcServer->unregisterClient(tunnelProxyClient);
// For debugging
qCDebug(dcTunnelProxyServer()) << "#### Total clients:" << m_proxyClients.count() << "JSON RPC clients:" << m_jsonRpcServer->registeredClientCount()
<< "Interface connections:" << tunnelProxyClient->interface()->connectionsCount()
<< "Servers:" << m_tunnelProxyServerConnections.count() << "Clients:" << m_tunnelProxyClientConnections.count();
// Delete the proxy client // Delete the proxy client
tunnelProxyClient->deleteLater(); tunnelProxyClient->deleteLater();
} }
@ -395,10 +417,6 @@ void TunnelProxyServer::onClientDataAvailable(const QUuid &clientId, const QByte
TunnelProxyClientConnection *clientConnection = serverConnection->getClientConnection(frame.socketAddress); TunnelProxyClientConnection *clientConnection = serverConnection->getClientConnection(frame.socketAddress);
if (!clientConnection) { if (!clientConnection) {
qCWarning(dcTunnelProxyServer()) << "The server connection wants to send data to a client connection which has not been registered to the server."; qCWarning(dcTunnelProxyServer()) << "The server connection wants to send data to a client connection which has not been registered to the server.";
qCWarning(dcTunnelProxyServer()) << "Notifying the server that there is no longer any socket connected with address" << frame.socketAddress;
QVariantMap params;
params.insert("socketAddress", frame.socketAddress);
m_jsonRpcServer->sendNotification("TunnelProxy", "ClientDisconnected", params, serverConnection->transportClient());
continue; continue;
} }

View File

@ -179,6 +179,10 @@ void RemoteProxyTestsTunnelProxy::apiBasicCalls()
QFETCH(int, responseId); QFETCH(int, responseId);
QFETCH(QString, responseStatus); QFETCH(QString, responseStatus);
resetDebugCategories();
addDebugCategory("TunnelProxyServer.debug=true");
addDebugCategory("*.debug=true");
// Start the server // Start the server
startServer(); startServer();