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;
var name = metadata[key] ? metadata[key].title : key;
encryptstring(value)
.then(encrypted =>
{
return queryremote({action: "push", name: key, data: encrypted});
})
queryremote({action: "push", name: key, data: value})
.then( () =>
{
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)
{
return new Promise( (apply, failed) => {
params.password = settings.password;
var paramlist = [];
for (var i in params)
return encryptdata(params)
.then(encparams =>
{
return new Promise ( (resolve, reject) =>
{
paramlist.push(i + "=" + encodeURIComponent(params[i]));
}
encparams.password = settings.password;
var xhr = new XMLHttpRequest();
xhr.open("POST", "handler.php");
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onerror = function()
{
failed("XMLHttpRequest error");
}
xhr.onload = function()
{
if (xhr.status !== 200)
var paramlist = [];
for (var i in encparams)
{
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)
.then(decrypted =>
return reject("XMLHttpRequest error");
}
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);
}
else
{
apply(decrypted);
}
});
if (decrypted.startsWith("error: "))
{
return reject(decrypted);
}
else
{
return resolve(decrypted);
}
});
}
}
}
var paramstring = paramlist.join("&");
console.log("http request length: " + formatsize(paramstring.length));
xhr.send(paramstring);
var paramstring = paramlist.join("&");
console.log("http request length: " + formatsize(paramstring.length));
xhr.send(paramstring);
});
});
}