You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
169 lines
5.0 KiB
JavaScript
169 lines
5.0 KiB
JavaScript
$(function () {
|
|
var keyLocation = $('#key-location');
|
|
var keyLocationContainer = keyLocation.parent();
|
|
var decryptKeyCheckbox = $('#decrypt-key');
|
|
var decryptKeyCheckboxContainer = decryptKeyCheckbox.parent();
|
|
var password = $('#password');
|
|
var passwordContainer = password.parent();
|
|
var embedConfiguration = $('#want-embedded');
|
|
var embedConfigurationContainer = embedConfiguration.parent();
|
|
var keyFileContent = null;
|
|
var buildConfigZip = $('#build-config-zip');
|
|
var getCertificateForm = $('#get-certificate-form');
|
|
|
|
decryptKeyCheckbox.change(function () {
|
|
if (this.checked) {
|
|
passwordContainer.show();
|
|
return;
|
|
}
|
|
|
|
passwordContainer.hide();
|
|
});
|
|
|
|
function handleKeyFile(element) {
|
|
keyFileContent = element.target.result;
|
|
}
|
|
|
|
// Retrieve zip file with post
|
|
getCertificateForm.submit(function (event) {
|
|
event.preventDefault();
|
|
});
|
|
|
|
function handleZipResult(data) {
|
|
var blobUrl = URL.createObjectURL(data);
|
|
saveBlobUrl(blobUrl, 'config.zip');
|
|
}
|
|
|
|
function handleEmbeddedResult(data) {
|
|
var fileReader = new FileReader();
|
|
|
|
fileReader.onload = function() {
|
|
var text = this.result;
|
|
if (keyFileContent === null) {
|
|
saveText(text, 'server-embedded.conf');
|
|
}
|
|
|
|
var match = /<key>/.exec(text);
|
|
matchOffset = match.index + 6;
|
|
text = text.substring(0, matchOffset) + keyFileContent + text.substring(matchOffset);
|
|
|
|
if (decryptKeyCheckbox.prop('checked') === true) {
|
|
var keyPassword = password.val();
|
|
text = decryptKey(text, keyPassword);
|
|
}
|
|
|
|
saveText(text, 'server-embedded.conf');
|
|
};
|
|
|
|
fileReader.readAsText(data);
|
|
}
|
|
|
|
function decryptKey(text, keyPassword) {
|
|
var match = /<key>/.exec(text);
|
|
|
|
var keyStartOffset = match.index + 6;
|
|
|
|
match = /<\/key>/.exec(text);
|
|
|
|
var keyEndOffset = match.index;
|
|
|
|
var keyContent = text.substring(keyStartOffset, keyEndOffset);
|
|
|
|
var decryptedPrivateKey = forge.pki.decryptRsaPrivateKey(keyContent.trim(), keyPassword);
|
|
var decryptedPem = forge.pki.privateKeyToPem(decryptedPrivateKey);
|
|
|
|
return text.substring(0, keyStartOffset) + decryptedPem + text.substring(keyEndOffset);
|
|
}
|
|
|
|
function saveText(text, fileName) {
|
|
var blob = new Blob([text], {type: 'text/plain'});
|
|
var blobUrl = URL.createObjectURL(blob);
|
|
saveBlobUrl(blobUrl, fileName);
|
|
}
|
|
|
|
var downloadElement = document.createElement("a");
|
|
document.body.appendChild(downloadElement);
|
|
downloadElement.style = "display: none";
|
|
|
|
function saveBlobUrl(url, fileName) {
|
|
downloadElement.href = url;
|
|
downloadElement.download = fileName;
|
|
downloadElement.click();
|
|
window.URL.revokeObjectURL(url);
|
|
}
|
|
|
|
function submitCertificateForm(event) {
|
|
var url = getCertificateForm.attr('action'),
|
|
method = getCertificateForm.attr('method'),
|
|
data = getCertificateForm.serialize(),
|
|
handler = handleZipResult;
|
|
|
|
if (embedConfiguration.prop('checked') === true) {
|
|
handler = handleEmbeddedResult;
|
|
}
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
xhr.onreadystatechange = function () {
|
|
if (this.readyState == 4 && this.status == 200) {
|
|
//this.response is what you're looking for
|
|
handler(this.response);
|
|
}
|
|
};
|
|
xhr.open(method, url);
|
|
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
|
|
xhr.responseType = 'blob';
|
|
xhr.send(data);
|
|
}
|
|
|
|
buildConfigZip.on('click', submitCertificateForm);
|
|
|
|
keyLocationContainer.on('show', function () {
|
|
decryptKeyCheckboxContainer.show();
|
|
});
|
|
|
|
keyLocationContainer.on('hide', function () {
|
|
decryptKeyCheckboxContainer.hide();
|
|
});
|
|
|
|
decryptKeyCheckboxContainer.on('show', function () {
|
|
if (decryptKeyCheckbox.prop('checked') === true) {
|
|
passwordContainer.show();
|
|
}
|
|
});
|
|
|
|
decryptKeyCheckboxContainer.on('hide', function () {
|
|
passwordContainer.hide();
|
|
passwordContainer.val('');
|
|
});
|
|
|
|
function setFileSelectVisibility() {
|
|
var selectedOption = $('#certificate option:selected');
|
|
|
|
if (selectedOption.data('hasPrivateKey') != '1') {
|
|
keyLocationContainer.show();
|
|
return;
|
|
}
|
|
|
|
keyLocationContainer.hide();
|
|
}
|
|
|
|
keyLocation.change(function () {
|
|
var file = this.files[0];
|
|
var reader = new FileReader();
|
|
|
|
reader.onload = handleKeyFile;
|
|
|
|
reader.readAsText(file);
|
|
});
|
|
|
|
embedConfiguration.change(function () {
|
|
if (this.checked) {
|
|
setFileSelectVisibility();
|
|
decryptKeyCheckboxContainer.show();
|
|
return;
|
|
}
|
|
|
|
decryptKeyCheckboxContainer.hide();
|
|
keyLocationContainer.hide();
|
|
});
|
|
}); |