Browse Source

add lots of websocket debugging and listen for ssl error signals

pull/102/head
Jonathan "Duke" Leto 5 years ago
parent
commit
bb09c5ac17
  1. 6
      src/mainwindow.cpp
  2. 43
      src/websockets.cpp
  3. 1
      src/websockets.h

6
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;
}

43
src/websockets.cpp

@ -111,6 +111,13 @@ void ws_error() {
qDebug() << "websocket error!";
}
void WormholeClient::sslerrors(const QList<QSslError> &)
{
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<const QList<QSslError>&>::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;
}

1
src/websockets.h

@ -64,6 +64,7 @@ public:
void connect();
void retryConnect();
void sslerrors(const QList<QSslError> &);
private:
MainWindow* parent = nullptr;

Loading…
Cancel
Save