From 03cf3473941c253ae9c0703b812b9f36117c9538 Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 20 Jan 2021 02:01:14 +0000 Subject: [PATCH] Try to clean up some websocket noise, and try harder to not duplicate connections. git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3647 ed5197a5-7fde-0310-b194-c3ffbd925b24 --- core/client.js | 15 +++++++++++---- src/ssb.c | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/core/client.js b/core/client.js index 3b6bb918..e358fffa 100644 --- a/core/client.js +++ b/core/client.js @@ -548,15 +548,22 @@ function message(event) { function reconnect(path) { let oldSocket = gSocket; gSocket = null - oldSocket.onclose = function() {} - oldSocket.onmessage = function() {} + oldSocket.onopen = null; + oldSocket.onclose = null; + oldSocket.onmessage = null; oldSocket.close(); connectSocket(path); } function connectSocket(path) { - if (!gSocket || gSocket.readyState == gSocket.CLOSED) { - setStatusMessage("Connecting...", kStatusColor, true); + if (!gSocket || gSocket.readyState != gSocket.OPEN) { + if (gSocket) { + gSocket.onopen = null; + gSocket.onclose = null; + gSocket.onmessage = null; + gSocket.close(); + } + setStatusMessage("Connecting...", kStatusColor, false); gSocket = new WebSocket( (window.location.protocol == "https:" ? "wss://" : "ws://") + window.location.hostname diff --git a/src/ssb.c b/src/ssb.c index 635c4664..32765593 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -794,6 +794,18 @@ bool tf_ssb_connection_get_id(tf_ssb_connection_t* connection, char* out_id, siz return tf_ssb_id_bin_to_str(out_id, out_id_size, connection->serverpub); } +static bool _tf_ssb_is_already_connected(tf_ssb_t* ssb, uint8_t* id) +{ + for (tf_ssb_connection_t* connection = ssb->connections; connection; connection = connection->next) { + if (memcmp(connection->serverpub, id, k_id_bin_len) == 0) { + return true; + } else if (memcmp(ssb->pub, id, k_id_bin_len) == 0) { + return true; + } + } + return false; +} + static void _tf_ssb_connection_verify_client_identity(tf_ssb_connection_t* connection, const uint8_t* message, size_t len) { uint8_t nonce[crypto_secretbox_NONCEBYTES] = { 0 }; @@ -859,6 +871,11 @@ static void _tf_ssb_connection_verify_client_identity(tf_ssb_connection_t* conne } uint8_t* detached_signature_A = m; + if (_tf_ssb_is_already_connected(connection->ssb, m + 64)) { + _tf_ssb_connection_close(connection, "already connected"); + return; + } + memcpy(connection->serverpub, m + 64, sizeof(connection->serverpub)); uint8_t hash3[crypto_hash_sha256_BYTES];