From ddd7310e384349331ce82260e13e3b7a79f31f0b Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Wed, 28 Mar 2018 00:33:41 -0700 Subject: [PATCH] Avoid racing reads from storage that could cause pairing data to be wiped --- static/js/index.js | 80 +++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 43 deletions(-) diff --git a/static/js/index.js b/static/js/index.js index 6240fa5..2ba3e1b 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -185,35 +185,45 @@ function updateBitrateField() { } function moduleDidLoad() { - if(!myUniqueid) { - console.warn('%c[index.js, moduleDidLoad]', 'color: green;', 'Failed to get uniqueId. We should have already generated one. Regenerating...'); - myUniqueid = uniqueid(); - storeData('uniqueid', myUniqueid, null); - } + // load the HTTP cert and unique ID if we have one. + chrome.storage.sync.get('cert', function(savedCert) { + if (savedCert.cert != null) { // we have a saved cert + pairingCert = savedCert.cert; + } - if(!pairingCert) { // we couldn't load a cert. Make one. - console.warn('%c[index.js, moduleDidLoad]', 'color: green;', 'Failed to load local cert. Generating new one'); - sendMessage('makeCert', []).then(function (cert) { - storeData('cert', cert, null); - pairingCert = cert; - console.info('%c[index.js, moduleDidLoad]', 'color: green;', 'Generated new cert:', cert); - }, function (failedCert) { - console.error('%c[index.js, moduleDidLoad]', 'color: green;', 'Failed to generate new cert! Returned error was: \n', failedCert); - }).then(function (ret) { - sendMessage('httpInit', [pairingCert.cert, pairingCert.privateKey, myUniqueid]).then(function (ret) { - restoreUiAfterNaClLoad(); - }, function (failedInit) { - console.error('%c[index.js, moduleDidLoad]', 'color: green;', 'Failed httpInit! Returned error was: ', failedInit); - }); + chrome.storage.sync.get('uniqueid', function(savedUniqueid) { + if (savedUniqueid.uniqueid != null) { // we have a saved uniqueid + myUniqueid = savedUniqueid.uniqueid; + } else { + myUniqueid = uniqueid(); + storeData('uniqueid', myUniqueid, null); + } + + if (!pairingCert) { // we couldn't load a cert. Make one. + console.warn('%c[index.js, moduleDidLoad]', 'color: green;', 'Failed to load local cert. Generating new one'); + sendMessage('makeCert', []).then(function (cert) { + storeData('cert', cert, null); + pairingCert = cert; + console.info('%c[index.js, moduleDidLoad]', 'color: green;', 'Generated new cert:', cert); + }, function (failedCert) { + console.error('%c[index.js, moduleDidLoad]', 'color: green;', 'Failed to generate new cert! Returned error was: \n', failedCert); + }).then(function (ret) { + sendMessage('httpInit', [pairingCert.cert, pairingCert.privateKey, myUniqueid]).then(function (ret) { + restoreUiAfterNaClLoad(); + }, function (failedInit) { + console.error('%c[index.js, moduleDidLoad]', 'color: green;', 'Failed httpInit! Returned error was: ', failedInit); + }); + }); + } + else { + sendMessage('httpInit', [pairingCert.cert, pairingCert.privateKey, myUniqueid]).then(function (ret) { + restoreUiAfterNaClLoad(); + }, function (failedInit) { + console.error('%c[index.js, moduleDidLoad]', 'color: green;', 'Failed httpInit! Returned error was: ', failedInit); + }); + } }); - } - else { - sendMessage('httpInit', [pairingCert.cert, pairingCert.privateKey, myUniqueid]).then(function (ret) { - restoreUiAfterNaClLoad(); - }, function (failedInit) { - console.error('%c[index.js, moduleDidLoad]', 'color: green;', 'Failed httpInit! Returned error was: ', failedInit); - }); - } + }); } // pair to the given NvHTTP host object. Returns whether pairing was successful. @@ -870,22 +880,6 @@ function onWindowLoad(){ updateBitrateField(); }); - // load the HTTP cert if we have one. - chrome.storage.sync.get('cert', function(savedCert) { - if (savedCert.cert != null) { // we have a saved cert - pairingCert = savedCert.cert; - } - }); - - chrome.storage.sync.get('uniqueid', function(savedUniqueid) { - if (savedUniqueid.uniqueid != null) { // we have a saved uniqueid - myUniqueid = savedUniqueid.uniqueid; - } else { - myUniqueid = uniqueid(); - storeData('uniqueid', myUniqueid, null); - } - }); - // load previously connected hosts, which have been killed into an object, and revive them back into a class chrome.storage.sync.get('hosts', function(previousValue) { hosts = previousValue.hosts != null ? previousValue.hosts : {};