mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2025-05-20 07:26:58 -04:00
Initial commit
This commit is contained in:
85
GPClient/cdpcommandmanager.cpp
Normal file
85
GPClient/cdpcommandmanager.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
#include "cdpcommandmanager.h"
|
||||
#include <QVariantMap>
|
||||
|
||||
CDPCommandManager::CDPCommandManager(QObject *parent)
|
||||
: QObject(parent)
|
||||
, networkManager(new QNetworkAccessManager)
|
||||
, socket(new QWebSocket)
|
||||
{
|
||||
// WebSocket setup
|
||||
QObject::connect(socket, &QWebSocket::connected, this, &CDPCommandManager::ready);
|
||||
QObject::connect(socket, &QWebSocket::textMessageReceived, this, &CDPCommandManager::onTextMessageReceived);
|
||||
QObject::connect(socket, &QWebSocket::disconnected, this, &CDPCommandManager::onSocketDisconnected);
|
||||
QObject::connect(socket, QOverload<QAbstractSocket::SocketError>::of(&QWebSocket::error), this, &CDPCommandManager::onSocketError);
|
||||
}
|
||||
|
||||
CDPCommandManager::~CDPCommandManager()
|
||||
{
|
||||
delete networkManager;
|
||||
delete socket;
|
||||
}
|
||||
|
||||
void CDPCommandManager::initialize(QString endpoint)
|
||||
{
|
||||
QNetworkReply *reply = networkManager->get(QNetworkRequest(endpoint));
|
||||
|
||||
QObject::connect(
|
||||
reply, &QNetworkReply::finished,
|
||||
[reply, this]() {
|
||||
if (reply->error()) {
|
||||
qDebug() << "CDP request error";
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
|
||||
QJsonArray pages = doc.array();
|
||||
QJsonObject page = pages.first().toObject();
|
||||
QString wsUrl = page.value("webSocketDebuggerUrl").toString();
|
||||
|
||||
socket->open(wsUrl);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
CDPCommand *CDPCommandManager::sendCommand(QString cmd)
|
||||
{
|
||||
QVariantMap emptyParams;
|
||||
return sendCommend(cmd, emptyParams);
|
||||
}
|
||||
|
||||
CDPCommand *CDPCommandManager::sendCommend(QString cmd, QVariantMap ¶ms)
|
||||
{
|
||||
int id = ++commandId;
|
||||
CDPCommand *command = new CDPCommand(id, cmd, params);
|
||||
socket->sendTextMessage(command->toJson());
|
||||
commandPool.insert(id, command);
|
||||
|
||||
return command;
|
||||
}
|
||||
|
||||
void CDPCommandManager::onTextMessageReceived(QString message)
|
||||
{
|
||||
QJsonDocument responseDoc = QJsonDocument::fromJson(message.toUtf8());
|
||||
QJsonObject response = responseDoc.object();
|
||||
|
||||
// Response for method
|
||||
if (response.contains("id")) {
|
||||
int id = response.value("id").toInt();
|
||||
if (commandPool.contains(id)) {
|
||||
CDPCommand *command = commandPool.take(id);
|
||||
command->finished();
|
||||
}
|
||||
} else { // Response for event
|
||||
emit eventReceived(response.value("method").toString(), response.value("params").toObject());
|
||||
}
|
||||
}
|
||||
|
||||
void CDPCommandManager::onSocketDisconnected()
|
||||
{
|
||||
qDebug() << "WebSocket disconnected";
|
||||
}
|
||||
|
||||
void CDPCommandManager::onSocketError(QAbstractSocket::SocketError error)
|
||||
{
|
||||
qDebug() << "WebSocket error" << error;
|
||||
}
|
Reference in New Issue
Block a user