Add signed key rotation scheduler
Rotate signed prekey every 48hrs, waiting for online access if necessary. After a rotation attempt is made, schedule the next run for 48hrs in the future. We use a timeout to "wake up" and handle the rotation. This timeout gets set on startup and whenever the next rotation time is changed. For paranoia's sake, always clear the current timeout before setting the next one. Since new registrations necessarily upload new signed keys, we reset the scheduled time to T+48hrs on `registration_done` events. // FREEBIEpull/749/head
parent
b92dd45a22
commit
536dd7b951
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* vim: ts=4:sw=4:expandtab
|
||||
*/
|
||||
|
||||
;(function () {
|
||||
'use strict';
|
||||
var ROTATION_INTERVAL = 48 * 60 * 60 * 1000;
|
||||
var timeout;
|
||||
|
||||
function scheduleNextRotation() {
|
||||
var now = Date.now();
|
||||
var nextTime = now + ROTATION_INTERVAL;
|
||||
storage.put('nextSignedKeyRotationTime', nextTime);
|
||||
}
|
||||
|
||||
function run() {
|
||||
console.log('Rotating signed prekey...');
|
||||
getAccountManager().rotateSignedPreKey();
|
||||
scheduleNextRotation();
|
||||
setTimeoutForNextRun();
|
||||
}
|
||||
|
||||
function runWhenOnline() {
|
||||
if (navigator.onLine) {
|
||||
run();
|
||||
} else {
|
||||
var listener = function() {
|
||||
window.removeEventListener('online', listener);
|
||||
run();
|
||||
};
|
||||
window.addEventListener('online', listener);
|
||||
}
|
||||
}
|
||||
|
||||
function setTimeoutForNextRun() {
|
||||
var now = Date.now();
|
||||
var scheduledTime = storage.get('nextSignedKeyRotationTime', now);
|
||||
console.log('Next signed key rotation scheduled for', new Date(scheduledTime));
|
||||
var waitTime = scheduledTime - now;
|
||||
if (waitTime < 0) {
|
||||
waitTime = 0;
|
||||
}
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(runWhenOnline, waitTime);
|
||||
}
|
||||
|
||||
window.RotateSignedPreKeyListener = {
|
||||
init: function() {
|
||||
if (Whisper.Registration.isDone()) {
|
||||
setTimeoutForNextRun();
|
||||
}
|
||||
extension.on('registration_done', function() {
|
||||
scheduleNextRotation();
|
||||
setTimeoutForNextRun();
|
||||
});
|
||||
}
|
||||
};
|
||||
}());
|
Loading…
Reference in New Issue