From bb09c5ac17c2e2a0188a32676030a33acfb09331 Mon Sep 17 00:00:00 2001 From: "Jonathan \"Duke\" Leto" Date: Sun, 17 Nov 2019 19:17:21 -0800 Subject: [PATCH] add lots of websocket debugging and listen for ssl error signals --- src/mainwindow.cpp | 6 +++++- src/websockets.cpp | 43 +++++++++++++++++++++++++++++++++++-------- src/websockets.h | 1 + 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index d75dec6..debd622 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -122,6 +122,7 @@ MainWindow::MainWindow(QWidget *parent) : setupZcashdTab(); rpc = new RPC(this); + qDebug() << "Created RPC"; restoreSavedStates(); @@ -132,6 +133,7 @@ MainWindow::MainWindow(QWidget *parent) : if (ads->getAllowInternetConnection()) wormholecode = ads->getWormholeCode(ads->getSecretHex()); + qDebug() << "MainWindow: createWebsocket with wormholcode=" << wormholecode; createWebsocket(wormholecode); } } @@ -142,10 +144,11 @@ void MainWindow::createWebsocket(QString wormholecode) { // TODO: env var bool msgDebug = true; wsserver = new WSServer(wsport, msgDebug, this); - qDebug() << "Listening for app connections on port " << wsport; + qDebug() << "createWebsocket: Listening for app connections on port " << wsport; if (!wormholecode.isEmpty()) { // Connect to the wormhole service + qDebug() << "Creating WormholeClient"; wormhole = new WormholeClient(this, wormholecode); } } @@ -165,6 +168,7 @@ bool MainWindow::isWebsocketListening() { } void MainWindow::replaceWormholeClient(WormholeClient* newClient) { + qDebug() << "replacing WormholeClient"; delete wormhole; wormhole = newClient; } diff --git a/src/websockets.cpp b/src/websockets.cpp index b4735d2..9a5ebc5 100644 --- a/src/websockets.cpp +++ b/src/websockets.cpp @@ -111,6 +111,13 @@ void ws_error() { qDebug() << "websocket error!"; } +void WormholeClient::sslerrors(const QList &) +{ + qDebug() << "SSL errors occurred!"; + m_webSocket->ignoreSslErrors(); + +} + void WormholeClient::connect() { delete m_webSocket; m_webSocket = new QWebSocket(); @@ -118,6 +125,7 @@ void WormholeClient::connect() { QObject::connect(m_webSocket, &QWebSocket::connected, this, &WormholeClient::onConnected); QObject::connect(m_webSocket, &QWebSocket::disconnected, this, &WormholeClient::closed); + QObject::connect(m_webSocket, QOverload&>::of(&QWebSocket::sslErrors), this, &WormholeClient::sslerrors); qDebug() << "Opening connection to the SilentDragonWormhole"; m_webSocket->open(wormhole); @@ -127,6 +135,20 @@ void WormholeClient::connect() { } +void WormholeClient::retryConnect() { + QTimer::singleShot(5 * 1000 * pow(2, retryCount), [=]() { + if (retryCount < 10) { + qDebug() << "Retrying websocket connection, count=" << this->retryCount; + this->retryCount++; + connect(); + } + else { + qDebug() << "Retry count exceeded, will not attempt retry any more"; + } + }); +} + +/* void WormholeClient::retryConnect() { int max_retries = 10; qDebug() << "Websocket retryConnect, retryCount=" << retryCount; @@ -145,6 +167,7 @@ void WormholeClient::retryConnect() { qDebug() << "Invalid retryCount=" << retryCount << " detected!"; } } +*/ // Called when the websocket is closed. If this was closed without our explicitly closing it, // then we need to try and reconnect @@ -193,11 +216,12 @@ void WormholeClient::onTextMessageReceived(QString message) // ============================== // AppDataServer // ============================== -AppDataServer* AppDataServer::instance = nullptr; +AppDataServer* AppDataServer::instance = nullptr; QString AppDataServer::getWormholeCode(QString secretHex) { + qDebug() << "AppDataServer::getWormholeCode"; unsigned char* secret = new unsigned char[crypto_secretbox_KEYBYTES]; - sodium_hex2bin(secret, crypto_secretbox_KEYBYTES, secretHex.toStdString().c_str(), crypto_secretbox_KEYBYTES*2, + sodium_hex2bin(secret, crypto_secretbox_KEYBYTES, secretHex.toStdString().c_str(), crypto_secretbox_KEYBYTES*2, NULL, NULL, NULL); unsigned char* out1 = new unsigned char[crypto_hash_sha256_BYTES]; @@ -207,7 +231,7 @@ QString AppDataServer::getWormholeCode(QString secretHex) { crypto_hash_sha256(out2, out1, crypto_hash_sha256_BYTES); char* wmcode = new char[crypto_hash_sha256_BYTES*2 + 1]; - sodium_bin2hex(wmcode, crypto_hash_sha256_BYTES*2 + 1, out2, crypto_hash_sha256_BYTES); + sodium_bin2hex(wmcode, crypto_hash_sha256_BYTES*2 + 1, out2, crypto_hash_sha256_BYTES); QString wmcodehex(wmcode); @@ -216,7 +240,7 @@ QString AppDataServer::getWormholeCode(QString secretHex) { delete[] out1; delete[] secret; - qDebug() << "Created wormhole secretHex"; + qDebug() << "Created wormhole secretHex=" << wmcodehex; return wmcodehex; } @@ -275,17 +299,18 @@ void AppDataServer::connectAppDialog(MainWindow* parent) { ui = new Ui_MobileAppConnector(); ui->setupUi(&d); Settings::saveRestore(&d); + qDebug() << "connectAppDialog"; updateUIWithNewQRCode(parent); updateConnectedUI(); QObject::connect(ui->btnDisconnect, &QPushButton::clicked, [=] () { + qDebug() << "Disconnecting"; QSettings().setValue("mobileapp/connectedname", ""); saveNewSecret(""); - updateConnectedUI(); }); - + QObject::connect(ui->txtConnStr, &QLineEdit::cursorPositionChanged, [=](int, int) { ui->txtConnStr->selectAll(); }); @@ -300,6 +325,7 @@ void AppDataServer::connectAppDialog(MainWindow* parent) { // If we're not listening for the app, then start the websockets if (!parent->isWebsocketListening()) { + qDebug() << "websocket not listening"; QString wormholecode = ""; if (getAllowInternetConnection()) { wormholecode = AppDataServer::getInstance()->getWormholeCode(AppDataServer::getInstance()->getSecretHex()); @@ -307,21 +333,22 @@ void AppDataServer::connectAppDialog(MainWindow* parent) { } parent->createWebsocket(wormholecode); + } else { + qDebug() << "no websocket not listening"; } d.exec(); // If there is nothing connected when the dialog exits, then shutdown the websockets if (!isAppConnected()) { + qDebug() << "no app connected, stopping websockets"; parent->stopWebsocket(); } // Cleanup tempSecret = ""; - delete tempWormholeClient; tempWormholeClient = nullptr; - delete ui; ui = nullptr; } diff --git a/src/websockets.h b/src/websockets.h index 9456730..1149a20 100644 --- a/src/websockets.h +++ b/src/websockets.h @@ -64,6 +64,7 @@ public: void connect(); void retryConnect(); + void sslerrors(const QList &); private: MainWindow* parent = nullptr;