Avoid racing reads from storage that could cause pairing data to be wiped

This commit is contained in:
Cameron Gutman 2018-03-28 00:33:41 -07:00
parent 6c457fe56d
commit ddd7310e38

View File

@ -185,35 +185,45 @@ function updateBitrateField() {
} }
function moduleDidLoad() { function moduleDidLoad() {
if(!myUniqueid) { // load the HTTP cert and unique ID if we have one.
console.warn('%c[index.js, moduleDidLoad]', 'color: green;', 'Failed to get uniqueId. We should have already generated one. Regenerating...'); chrome.storage.sync.get('cert', function(savedCert) {
myUniqueid = uniqueid(); if (savedCert.cert != null) { // we have a saved cert
storeData('uniqueid', myUniqueid, null); pairingCert = savedCert.cert;
} }
if(!pairingCert) { // we couldn't load a cert. Make one. chrome.storage.sync.get('uniqueid', function(savedUniqueid) {
console.warn('%c[index.js, moduleDidLoad]', 'color: green;', 'Failed to load local cert. Generating new one'); if (savedUniqueid.uniqueid != null) { // we have a saved uniqueid
sendMessage('makeCert', []).then(function (cert) { myUniqueid = savedUniqueid.uniqueid;
storeData('cert', cert, null); } else {
pairingCert = cert; myUniqueid = uniqueid();
console.info('%c[index.js, moduleDidLoad]', 'color: green;', 'Generated new cert:', cert); storeData('uniqueid', myUniqueid, null);
}, function (failedCert) { }
console.error('%c[index.js, moduleDidLoad]', 'color: green;', 'Failed to generate new cert! Returned error was: \n', failedCert);
}).then(function (ret) { if (!pairingCert) { // we couldn't load a cert. Make one.
sendMessage('httpInit', [pairingCert.cert, pairingCert.privateKey, myUniqueid]).then(function (ret) { console.warn('%c[index.js, moduleDidLoad]', 'color: green;', 'Failed to load local cert. Generating new one');
restoreUiAfterNaClLoad(); sendMessage('makeCert', []).then(function (cert) {
}, function (failedInit) { storeData('cert', cert, null);
console.error('%c[index.js, moduleDidLoad]', 'color: green;', 'Failed httpInit! Returned error was: ', failedInit); 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. // pair to the given NvHTTP host object. Returns whether pairing was successful.
@ -870,22 +880,6 @@ function onWindowLoad(){
updateBitrateField(); 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 // 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) { chrome.storage.sync.get('hosts', function(previousValue) {
hosts = previousValue.hosts != null ? previousValue.hosts : {}; hosts = previousValue.hosts != null ? previousValue.hosts : {};