diff --git a/core/client.js b/core/client.js index 3b6bb918f..e358fffad 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 635c46646..32765593c 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];