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

View File

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

View File

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

View File

@ -85,8 +85,14 @@ bool TcpSocketServer::running() const
bool TcpSocketServer::startServer()
{
if (m_server) {
m_server->close();
delete m_server;
m_server = nullptr;
}
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()))) {
qCWarning(dcTcpSocketServer()) << "Tcp server error: can not listen on" << m_serverUrl.toString();
delete m_server;
@ -98,11 +104,13 @@ bool TcpSocketServer::startServer()
connect(m_server, &SslServer::socketDisconnected, this, &TcpSocketServer::onSocketDisconnected);
connect(m_server, &SslServer::dataAvailable, this, &TcpSocketServer::onDataAvailable);
qCDebug(dcTcpSocketServer()) << "Server started successfully.";
qCDebug(dcTcpSocketServer()) << m_server;
return true;
}
bool TcpSocketServer::stopServer()
{
qCDebug(dcTcpSocketServer()) << "Stopping server" << m_serverUrl.toString() << m_server;
if (!m_server)
return true;
@ -111,8 +119,6 @@ bool TcpSocketServer::stopServer()
killClientConnection(clientId, "Stop server");
}
qCDebug(dcTcpSocketServer()) << "Stop server" << m_serverUrl.toString();
m_server->close();
m_server->deleteLater();
m_server = nullptr;
@ -162,8 +168,11 @@ void SslServer::incomingConnection(qintptr socketDescriptor)
{
QSslSocket *sslSocket = new QSslSocket(this);
qCDebug(dcTcpSocketServer()) << "Incomming connection" << sslSocket;
connect(sslSocket, &QSslSocket::disconnected, this, &SslServer::onSocketDisconnected);
connect(sslSocket, &QSslSocket::readyRead, this, &SslServer::onSocketReadyRead);
typedef void (QAbstractSocket:: *errorSignal)(QAbstractSocket::SocketError);
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)) {
qCWarning(dcTcpSocketServer()) << "Failed to set SSL socket descriptor.";
@ -188,6 +196,8 @@ void SslServer::incomingConnection(qintptr socketDescriptor)
return;
}
addPendingConnection(sslSocket);
if (m_sslEnabled) {
qCDebug(dcTcpSocketServer()) << "Start SSL encryption for" << sslSocket;
sslSocket->setSslConfiguration(m_config);
@ -218,7 +228,7 @@ void SslServer::onSocketError(QAbstractSocket::SocketError error)
QSslSocket *sslSocket = static_cast<QSslSocket *>(sender());
qCWarning(dcTcpSocketServer()) << "Socket error occurred" << error << sslSocket->errorString();
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)
{
qCDebug(dcTunnelProxyServer()) << "Register transport interface" << interface->serverName();
//interface->setParent(this);
if (m_transportInterfaces.contains(interface)) {
qCWarning(dcTunnelProxyServer()) << "Transport interface already registerd.";
return;
}
connect(interface, &TransportInterface::clientConnected, this, &TunnelProxyServer::onClientConnected);
@ -109,9 +110,15 @@ TunnelProxyServer::TunnelProxyError TunnelProxyServer::registerServer(const QUui
// Enable SLIP from now on
tunnelProxyClient->enableSlipAfterResponse();
TunnelProxyServerConnection *serverConnection = new TunnelProxyServerConnection(tunnelProxyClient, serverUuid, serverName, this);
TunnelProxyServerConnection *serverConnection = new TunnelProxyServerConnection(tunnelProxyClient, serverUuid, serverName, tunnelProxyClient);
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;
}
@ -153,12 +160,17 @@ TunnelProxyServer::TunnelProxyError TunnelProxyServer::registerClient(const QUui
tunnelProxyClient->setUuid(clientUuid);
tunnelProxyClient->setName(clientName);
TunnelProxyClientConnection *clientConnection = new TunnelProxyClientConnection(tunnelProxyClient, clientUuid, clientName, this);
TunnelProxyClientConnection *clientConnection = new TunnelProxyClientConnection(tunnelProxyClient, clientUuid, clientName, tunnelProxyClient);
clientConnection->setServerConnection(serverConnection);
m_tunnelProxyClientConnections.insert(clientUuid, clientConnection);
qCDebug(dcTunnelProxyServer()) << "Register client" << clientConnection << "-->" << serverConnection;
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
QVariantMap params;
@ -286,6 +298,11 @@ void TunnelProxyServer::onClientConnected(const QUuid &clientId, const QHostAddr
TunnelProxyClient *tunnelProxyClient = new TunnelProxyClient(interface, clientId, address, this);
m_proxyClients.insert(clientId, 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)
@ -333,6 +350,11 @@ void TunnelProxyServer::onClientDisconnected(const QUuid &clientId)
// Unregister from json rpc server
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
tunnelProxyClient->deleteLater();
}
@ -395,10 +417,6 @@ void TunnelProxyServer::onClientDataAvailable(const QUuid &clientId, const QByte
TunnelProxyClientConnection *clientConnection = serverConnection->getClientConnection(frame.socketAddress);
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()) << "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;
}

View File

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