fix encryption

This commit is contained in:
quenousimporte 2024-02-16 10:51:18 +01:00
parent 717b2a1bce
commit 77ddc4907f
1 changed files with 58 additions and 41 deletions

99
main.js
View File

@ -358,11 +358,7 @@ function pushitem(key, value)
pending[key] = true; pending[key] = true;
var name = metadata[key] ? metadata[key].title : key; var name = metadata[key] ? metadata[key].title : key;
encryptstring(value) queryremote({action: "push", name: key, data: value})
.then(encrypted =>
{
return queryremote({action: "push", name: key, data: encrypted});
})
.then( () => .then( () =>
{ {
console.log("'" + name + "' pushed to server"); console.log("'" + name + "' pushed to server");
@ -1448,53 +1444,74 @@ function init()
}); });
} }
function encryptdata(params)
{
if (params.data)
{
return encryptstring(params.data)
.then(encrypted =>
{
params.data = encrypted;
return Promise.resolve(params);
});
}
else
{
return Promise.resolve(params);
}
}
function queryremote(params) function queryremote(params)
{ {
return new Promise( (apply, failed) => { return encryptdata(params)
.then(encparams =>
params.password = settings.password; {
return new Promise ( (resolve, reject) =>
var paramlist = [];
for (var i in params)
{ {
paramlist.push(i + "=" + encodeURIComponent(params[i])); encparams.password = settings.password;
}
var xhr = new XMLHttpRequest(); var paramlist = [];
xhr.open("POST", "handler.php"); for (var i in encparams)
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onerror = function()
{
failed("XMLHttpRequest error");
}
xhr.onload = function()
{
if (xhr.status !== 200)
{ {
failed("Http status " + xhr.status); paramlist.push(i + "=" + encodeURIComponent(encparams[i]));
} }
else
var xhr = new XMLHttpRequest();
xhr.open("POST", "handler.php");
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onerror = function()
{ {
decryptstring(xhr.responseText) return reject("XMLHttpRequest error");
.then(decrypted => }
xhr.onload = function()
{
if (xhr.status !== 200)
{ {
if (decrypted.startsWith("error: ")) return reject("Http status " + xhr.status);
}
else
{
decryptstring(xhr.responseText)
.then(decrypted =>
{ {
failed(decrypted); if (decrypted.startsWith("error: "))
} {
else return reject(decrypted);
{ }
apply(decrypted); else
} {
}); return resolve(decrypted);
}
});
}
} }
}
var paramstring = paramlist.join("&"); var paramstring = paramlist.join("&");
console.log("http request length: " + formatsize(paramstring.length)); console.log("http request length: " + formatsize(paramstring.length));
xhr.send(paramstring); xhr.send(paramstring);
});
}); });
} }