From 991ffbecb6d1e516c3d54d35de5f858bf9f68a65 Mon Sep 17 00:00:00 2001 From: varjolintu Date: Tue, 8 Oct 2019 13:13:09 +0300 Subject: [PATCH] Fix connection issues --- keepassxc-browser/background/init.js | 2 +- keepassxc-browser/background/keepass.js | 62 ++++++++++++------------ keepassxc-browser/popups/popup.css | 14 ++++++ keepassxc-browser/popups/popup.html | 2 +- keepassxc-browser/popups/throbber.gif | Bin 673 -> 0 bytes 5 files changed, 48 insertions(+), 32 deletions(-) delete mode 100644 keepassxc-browser/popups/throbber.gif diff --git a/keepassxc-browser/background/init.js b/keepassxc-browser/background/init.js index 0eee148..14202f6 100644 --- a/keepassxc-browser/background/init.js +++ b/keepassxc-browser/background/init.js @@ -6,7 +6,7 @@ await page.initSettings(); await page.initOpenedTabs(); await httpAuth.init(); - await keepass.reconnect(); + await keepass.reconnect(null, 5000); // 5 second timeout for the first connect await keepass.enableAutomaticReconnect(); } catch (e) { console.log('init.js failed'); diff --git a/keepassxc-browser/background/keepass.js b/keepassxc-browser/background/keepass.js index 030a71a..625ca7f 100755 --- a/keepassxc-browser/background/keepass.js +++ b/keepassxc-browser/background/keepass.js @@ -86,7 +86,7 @@ browser.storage.local.get({ 'latestKeePassXC': { 'version': '', 'lastChecked': n keepass.keyRing = item.keyRing; }); -keepass.sendNativeMessage = function(request, enableTimeout = false) { +keepass.sendNativeMessage = function(request, enableTimeout = false, timeoutValue) { return new Promise((resolve, reject) => { let timeout; const requestAction = request.action; @@ -106,6 +106,7 @@ keepass.sendNativeMessage = function(request, enableTimeout = false) { })(ev, requestAction); ev.addListener(listener); + let messageTimeout = timeoutValue || keepass.messageTimeout; // Handle timeouts if (enableTimeout) { @@ -118,7 +119,7 @@ keepass.sendNativeMessage = function(request, enableTimeout = false) { keepass.isKeePassXCAvailable = false; ev.removeListener(listener.handler); resolve(errorMessage); - }, keepass.messageTimeout); + }, messageTimeout); } // Send the request @@ -571,7 +572,7 @@ keepass.getDatabaseHash = async function(tab, args = []) { } }; -keepass.changePublicKeys = async function(tab, enableTimeout = false) { +keepass.changePublicKeys = async function(tab, enableTimeout = false, connectionTimeout) { if (!keepass.isConnected) { keepass.handleError(tab, kpErrors.TIMEOUT_OR_NOT_CONNECTED); return false; @@ -590,19 +591,19 @@ keepass.changePublicKeys = async function(tab, enableTimeout = false) { }; try { - const response = await keepass.sendNativeMessage(request, enableTimeout); + const response = await keepass.sendNativeMessage(request, enableTimeout, connectionTimeout); keepass.setcurrentKeePassXCVersion(response.version); if (!keepass.verifyKeyResponse(response, key, incrementedNonce)) { if (tab && page.tabs[tab.id]) { keepass.handleError(tab, kpErrors.KEY_CHANGE_FAILED); - return false; } - } else { - keepass.isKeePassXCAvailable = true; - console.log('Server public key: ' + nacl.util.encodeBase64(keepass.serverPublicKey)); + + return false; } + keepass.isKeePassXCAvailable = true; + console.log('Server public key: ' + nacl.util.encodeBase64(keepass.serverPublicKey)); return true; } catch (err) { console.log('changePublicKeys failed: ', err); @@ -987,17 +988,15 @@ keepass.verifyKeyResponse = function(response, key, nonce) { return false; } - let reply = false; if (!keepass.checkNonceLength(response.nonce)) { console.log('Error: Invalid nonce length'); return false; } - reply = (response.nonce === nonce); - - if (response.publicKey) { + const reply = (response.nonce === nonce); + if (response.publicKey && reply) { keepass.serverPublicKey = nacl.util.decodeBase64(response.publicKey); - reply = true; + return true; } return reply; @@ -1126,24 +1125,27 @@ keepass.disableAutomaticReconnect = function() { keepass.reconnectLoop = null; }; -keepass.reconnect = function(tab) { - return new Promise(async (resolve) => { - keepass.connectToNative(); - keepass.generateNewKeyPair(); - await keepass.changePublicKeys(tab, true).catch((e) => { - resolve(false); - }); - - const hash = await keepass.getDatabaseHash(tab); - if (hash !== '' && tab && page.tabs[tab.id]) { - delete page.tabs[tab.id].errorMessage; - } - - await keepass.testAssociation(); - await keepass.isConfigured(); - keepass.updateDatabaseHashToContent(); - resolve(true); +keepass.reconnect = async function(tab, connectionTimeout) { + keepass.connectToNative(); + keepass.generateNewKeyPair(); + const keyChangeResult = await keepass.changePublicKeys(tab, true, connectionTimeout).catch((e) => { + return false; }); + + // Change public keys timeout + if (!keyChangeResult) { + return false; + } + + const hash = await keepass.getDatabaseHash(tab); + if (hash !== '' && tab && page.tabs[tab.id]) { + delete page.tabs[tab.id].errorMessage; + } + + await keepass.testAssociation(); + await keepass.isConfigured(); + keepass.updateDatabaseHashToContent(); + return true; }; keepass.updatePopup = function(iconType) { diff --git a/keepassxc-browser/popups/popup.css b/keepassxc-browser/popups/popup.css index 33ae6f0..a1a640c 100644 --- a/keepassxc-browser/popups/popup.css +++ b/keepassxc-browser/popups/popup.css @@ -13,6 +13,20 @@ body { max-width: 460px; width: auto; } +.loader { + animation: spin 2s linear infinite; + border: 4px solid #f3f3f3; + border-radius: 50%; + border-top: 4px solid #28a745; + float: left; + height: 1.4em; + margin-right: 1em; + width: 1.4em; +} +@keyframes spin { + 0% { transform: rotate(0deg); } + 100% { transform: rotate(360deg); } +} .list-group { font-size: .9em !important; } diff --git a/keepassxc-browser/popups/popup.html b/keepassxc-browser/popups/popup.html index 20d804e..8da2eca 100644 --- a/keepassxc-browser/popups/popup.html +++ b/keepassxc-browser/popups/popup.html @@ -28,7 +28,7 @@
-

+