Improve websocket bugs and debugging so we are less likely to coredump
This commit is contained in:
@@ -36,13 +36,15 @@ WSServer::WSServer(quint16 port, bool debug, QObject *parent) :
|
|||||||
|
|
||||||
WSServer::~WSServer()
|
WSServer::~WSServer()
|
||||||
{
|
{
|
||||||
qDebug() << "Closing websocket";
|
qDebug() << "Closing websocket server";
|
||||||
m_pWebSocketServer->close();
|
m_pWebSocketServer->close();
|
||||||
qDeleteAll(m_clients.begin(), m_clients.end());
|
qDeleteAll(m_clients.begin(), m_clients.end());
|
||||||
|
qDebug() << "Deleted all websocket clients";
|
||||||
}
|
}
|
||||||
|
|
||||||
void WSServer::onNewConnection()
|
void WSServer::onNewConnection()
|
||||||
{
|
{
|
||||||
|
qDebug() << "Websocket server: new connection";
|
||||||
QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection();
|
QWebSocket *pSocket = m_pWebSocketServer->nextPendingConnection();
|
||||||
|
|
||||||
connect(pSocket, &QWebSocket::textMessageReceived, this, &WSServer::processTextMessage);
|
connect(pSocket, &QWebSocket::textMessageReceived, this, &WSServer::processTextMessage);
|
||||||
@@ -90,27 +92,39 @@ WormholeClient::WormholeClient(MainWindow* p, QString wormholeCode) {
|
|||||||
this->parent = p;
|
this->parent = p;
|
||||||
this->code = wormholeCode;
|
this->code = wormholeCode;
|
||||||
connect();
|
connect();
|
||||||
|
qDebug() << "New wormhole client after connect()";
|
||||||
}
|
}
|
||||||
|
|
||||||
WormholeClient::~WormholeClient() {
|
WormholeClient::~WormholeClient() {
|
||||||
|
qDebug() << "WormholeClient destructor";
|
||||||
shuttingDown = true;
|
shuttingDown = true;
|
||||||
|
|
||||||
if (m_webSocket->isValid()) {
|
if (m_webSocket && m_webSocket->isValid()) {
|
||||||
|
qDebug() << "Wormhole closing!";
|
||||||
m_webSocket->close();
|
m_webSocket->close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (timer)
|
if (timer) {
|
||||||
|
qDebug() << "Wormhole timer stopping";
|
||||||
timer->stop();
|
timer->stop();
|
||||||
|
}
|
||||||
|
|
||||||
delete timer;
|
delete timer;
|
||||||
|
qDebug() << "Wormhole timer deleted";
|
||||||
}
|
}
|
||||||
|
|
||||||
void WormholeClient::connect() {
|
void WormholeClient::connect() {
|
||||||
|
qDebug() << "Wormhole::connect";
|
||||||
delete m_webSocket;
|
delete m_webSocket;
|
||||||
m_webSocket = new QWebSocket();
|
m_webSocket = new QWebSocket();
|
||||||
|
|
||||||
QObject::connect(m_webSocket, &QWebSocket::connected, this, &WormholeClient::onConnected);
|
if (m_webSocket) {
|
||||||
QObject::connect(m_webSocket, &QWebSocket::disconnected, this, &WormholeClient::closed);
|
QObject::connect(m_webSocket, &QWebSocket::connected, this, &WormholeClient::onConnected);
|
||||||
|
QObject::connect(m_webSocket, &QWebSocket::disconnected, this, &WormholeClient::closed);
|
||||||
|
} else {
|
||||||
|
qDebug() << "Invalid websocket object!";
|
||||||
|
}
|
||||||
|
|
||||||
m_webSocket->open(QUrl("wss://wormhole.myhush.org:443"));
|
m_webSocket->open(QUrl("wss://wormhole.myhush.org:443"));
|
||||||
//m_webSocket->open(QUrl("ws://127.0.0.1:7070"));
|
//m_webSocket->open(QUrl("ws://127.0.0.1:7070"));
|
||||||
@@ -122,8 +136,7 @@ void WormholeClient::retryConnect() {
|
|||||||
qDebug() << "Retrying websocket connection";
|
qDebug() << "Retrying websocket connection";
|
||||||
this->retryCount++;
|
this->retryCount++;
|
||||||
connect();
|
connect();
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
qDebug() << "Retry count exceeded, will not attempt retry any more";
|
qDebug() << "Retry count exceeded, will not attempt retry any more";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -141,7 +154,7 @@ void WormholeClient::onConnected()
|
|||||||
{
|
{
|
||||||
qDebug() << "WebSocket connected";
|
qDebug() << "WebSocket connected";
|
||||||
retryCount = 0;
|
retryCount = 0;
|
||||||
|
qDebug() << "WebSocket connected, retryCount=" << retryCount;
|
||||||
QObject::connect(m_webSocket, &QWebSocket::textMessageReceived,
|
QObject::connect(m_webSocket, &QWebSocket::textMessageReceived,
|
||||||
this, &WormholeClient::onTextMessageReceived);
|
this, &WormholeClient::onTextMessageReceived);
|
||||||
|
|
||||||
@@ -153,20 +166,34 @@ void WormholeClient::onConnected()
|
|||||||
|
|
||||||
// On connected, we'll also create a timer to ping it every 4 minutes, since the websocket
|
// On connected, we'll also create a timer to ping it every 4 minutes, since the websocket
|
||||||
// will timeout after 5 minutes
|
// will timeout after 5 minutes
|
||||||
timer = new QTimer(parent);
|
if (m_webSocket && m_webSocket->isValid()) {
|
||||||
QObject::connect(timer, &QTimer::timeout, [=]() {
|
m_webSocket->sendTextMessage(payload);
|
||||||
if (!shuttingDown && m_webSocket->isValid()) {
|
qDebug() << "Sent registration message with code=" << code;
|
||||||
auto payload = QJsonDocument(QJsonObject { {"ping", "ping"} }).toJson();
|
|
||||||
qint64 bytes = m_webSocket->sendTextMessage(payload);
|
// On connected, we'll also create a timer to ping it every 4 minutes, since the websocket
|
||||||
qDebug() << "Sent ping, " << bytes << " bytes";
|
// will timeout after 5 minutes
|
||||||
}
|
timer = new QTimer(parent);
|
||||||
});
|
qDebug() << "Created QTimer";
|
||||||
timer->start(4 * 60 * 1000); // 4 minutes
|
QObject::connect(timer, &QTimer::timeout, [=]() {
|
||||||
|
qDebug() << "Timer timeout!";
|
||||||
|
if (!shuttingDown && m_webSocket && m_webSocket->isValid()) {
|
||||||
|
auto payload = QJsonDocument(QJsonObject { {"ping", "ping"} }).toJson();
|
||||||
|
qint64 bytes = m_webSocket->sendTextMessage(payload);
|
||||||
|
qDebug() << "Sent ping, " << bytes << " bytes";
|
||||||
|
}
|
||||||
|
});
|
||||||
|
unsigned int interval = 4*60*1000;
|
||||||
|
timer->start(interval); // 4 minutes
|
||||||
|
qDebug() << "Started timer with interval=" << interval;
|
||||||
|
} else {
|
||||||
|
qDebug() << "Invalid websocket object onConnected!";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WormholeClient::onTextMessageReceived(QString message)
|
void WormholeClient::onTextMessageReceived(QString message)
|
||||||
{
|
{
|
||||||
AppDataServer::getInstance()->processMessage(message, parent, std::make_shared<ClientWebSocket>(m_webSocket), AppConnectionType::INTERNET);
|
AppDataServer::getInstance()->processMessage(message, parent, std::make_shared<ClientWebSocket>(m_webSocket), AppConnectionType::INTERNET);
|
||||||
|
qDebug() << "Destroyed tempWormholeClient and ui";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -320,6 +347,7 @@ void AppDataServer::updateUIWithNewQRCode(MainWindow* mainwindow) {
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
QString uri = "ws://" + ipv4Addr + ":8777";
|
QString uri = "ws://" + ipv4Addr + ":8777";
|
||||||
|
qDebug() << "Websocket URI: " << uri;
|
||||||
|
|
||||||
// Get a new secret
|
// Get a new secret
|
||||||
unsigned char* secretBin = new unsigned char[crypto_secretbox_KEYBYTES];
|
unsigned char* secretBin = new unsigned char[crypto_secretbox_KEYBYTES];
|
||||||
@@ -338,9 +366,11 @@ void AppDataServer::updateUIWithNewQRCode(MainWindow* mainwindow) {
|
|||||||
|
|
||||||
ui->qrcode->setQrcodeString(codeStr);
|
ui->qrcode->setQrcodeString(codeStr);
|
||||||
ui->txtConnStr->setText(codeStr);
|
ui->txtConnStr->setText(codeStr);
|
||||||
|
qDebug() << "New QR="<<codeStr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AppDataServer::registerNewTempSecret(QString tmpSecretHex, bool allowInternet, MainWindow* main) {
|
void AppDataServer::registerNewTempSecret(QString tmpSecretHex, bool allowInternet, MainWindow* main) {
|
||||||
|
qDebug() << "Registering new tempSecret, allowInternet=" << allowInternet;
|
||||||
tempSecret = tmpSecretHex;
|
tempSecret = tmpSecretHex;
|
||||||
|
|
||||||
delete tempWormholeClient;
|
delete tempWormholeClient;
|
||||||
@@ -348,7 +378,9 @@ void AppDataServer::registerNewTempSecret(QString tmpSecretHex, bool allowIntern
|
|||||||
|
|
||||||
if (allowInternet)
|
if (allowInternet)
|
||||||
tempWormholeClient = new WormholeClient(main, getWormholeCode(tempSecret));
|
tempWormholeClient = new WormholeClient(main, getWormholeCode(tempSecret));
|
||||||
}
|
qDebug() << "Created new wormhole client";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
QString AppDataServer::connDesc(AppConnectionType t) {
|
QString AppDataServer::connDesc(AppConnectionType t) {
|
||||||
if (t == AppConnectionType::DIRECT) {
|
if (t == AppConnectionType::DIRECT) {
|
||||||
@@ -516,12 +548,15 @@ QString AppDataServer::decryptMessage(QJsonDocument msg, QString secretHex, QStr
|
|||||||
delete[] noncebin;
|
delete[] noncebin;
|
||||||
delete[] encrypted;
|
delete[] encrypted;
|
||||||
delete[] decrypted;
|
delete[] decrypted;
|
||||||
|
|
||||||
|
qDebug() << "Returning decrypted payload="<<payload;
|
||||||
return payload;
|
return payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process an incoming text message. The message has to be encrypted with the secret key (or the temporary secret key)
|
// Process an incoming text message. The message has to be encrypted with the secret key (or the temporary secret key)
|
||||||
void AppDataServer::processMessage(QString message, MainWindow* mainWindow, std::shared_ptr<ClientWebSocket> pClient, AppConnectionType connType) {
|
void AppDataServer::processMessage(QString message, MainWindow* mainWindow, std::shared_ptr<ClientWebSocket> pClient, AppConnectionType connType) {
|
||||||
|
qDebug() << "processMessage message";
|
||||||
|
//qDebug() << "processMessage message=" << message; // this can log sensitive info
|
||||||
auto replyWithError = [=]() {
|
auto replyWithError = [=]() {
|
||||||
auto r = QJsonDocument(QJsonObject{
|
auto r = QJsonDocument(QJsonObject{
|
||||||
{"error", "Encryption error"},
|
{"error", "Encryption error"},
|
||||||
@@ -605,6 +640,7 @@ void AppDataServer::processMessage(QString message, MainWindow* mainWindow, std:
|
|||||||
|
|
||||||
// Decrypted method will be executed here.
|
// Decrypted method will be executed here.
|
||||||
void AppDataServer::processDecryptedMessage(QString message, MainWindow* mainWindow, std::shared_ptr<ClientWebSocket> pClient) {
|
void AppDataServer::processDecryptedMessage(QString message, MainWindow* mainWindow, std::shared_ptr<ClientWebSocket> pClient) {
|
||||||
|
//qDebug() << "processDecryptedMessage message=" << message;
|
||||||
// First, extract the command from the message
|
// First, extract the command from the message
|
||||||
auto msg = QJsonDocument::fromJson(message.toUtf8());
|
auto msg = QJsonDocument::fromJson(message.toUtf8());
|
||||||
|
|
||||||
@@ -637,6 +673,7 @@ void AppDataServer::processDecryptedMessage(QString message, MainWindow* mainWin
|
|||||||
|
|
||||||
// "sendTx" command. This method will actually send money, so be careful with everything
|
// "sendTx" command. This method will actually send money, so be careful with everything
|
||||||
void AppDataServer::processSendTx(QJsonObject sendTx, MainWindow* mainwindow, std::shared_ptr<ClientWebSocket> pClient) {
|
void AppDataServer::processSendTx(QJsonObject sendTx, MainWindow* mainwindow, std::shared_ptr<ClientWebSocket> pClient) {
|
||||||
|
qDebug() << "processSendTx with to=" << sendTx["to"].toString();
|
||||||
auto error = [=](QString reason) {
|
auto error = [=](QString reason) {
|
||||||
auto r = QJsonDocument(QJsonObject{
|
auto r = QJsonDocument(QJsonObject{
|
||||||
{"errorCode", -1},
|
{"errorCode", -1},
|
||||||
|
|||||||
Reference in New Issue
Block a user