From a9c368e98098f7791c47e7bbccf14e9a67b20036 Mon Sep 17 00:00:00 2001 From: Tim Bazuin Date: Sat, 3 Aug 2019 20:07:42 +0200 Subject: [PATCH] I've got file downloading working for the zip file, basically as before - now on to the meat and potatoes --- public/js/pages/configBuilder.js | 74 ++++++++++++++++++++-- src/Handler/Panel/ConfigBuilder/Action.php | 15 +++-- views/panel/config_builder.html.twig | 39 ++++++------ 3 files changed, 96 insertions(+), 32 deletions(-) diff --git a/public/js/pages/configBuilder.js b/public/js/pages/configBuilder.js index 07c69df..d87a000 100644 --- a/public/js/pages/configBuilder.js +++ b/public/js/pages/configBuilder.js @@ -7,8 +7,11 @@ $(function () { 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() { + decryptKeyCheckbox.change(function () { if (this.checked) { passwordContainer.show(); return; @@ -18,27 +21,86 @@ $(function () { }); function handleKeyFile(element) { - var keyFileContent = element.target.result; + keyFileContent = element.target.result; console.log(keyFileContent); // TODO: Add keyfile to embedded server config using forge } - keyLocationContainer.on('show', function() { + // Retrieve zip file with post + getCertificateForm.submit(function (event) { + event.preventDefault(); + }); + + function handleZipResult(data) { + //var blob = new Blob(data, {type: 'application/zip'}); + var blobUrl = URL.createObjectURL(data); + saveBlobUrl(blobUrl, 'config.zip'); + } + + function handleEmbeddedResult(data) { + console.log("I haven't implemented this yet") + } + + 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) { + console.log('submitCert'); + var url = getCertificateForm.attr('action'), + method = getCertificateForm.attr('method'), + data = getCertificateForm.serialize(), + dataType = 'application/zip', + handler = handleZipResult; + + if (embedConfiguration.checked) { + dataType = 'text/plain'; + 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); + + // Load content of embedded server config + // Put keyfile content in embedded server config if it is not null + // If the decrypt key checkbox is checked, attempt to decrypt the private key in the embedded server config + // Offer the decrypted server config up as a file + + keyLocationContainer.on('show', function () { decryptKeyCheckboxContainer.show(); }); - keyLocationContainer.on('hide', function() { + keyLocationContainer.on('hide', function () { decryptKeyCheckboxContainer.hide(); }); - decryptKeyCheckboxContainer.on('show', function() { + decryptKeyCheckboxContainer.on('show', function () { if (decryptKeyCheckbox.prop('checked') === true) { passwordContainer.show(); } }); - decryptKeyCheckboxContainer.on('hide', function() { + decryptKeyCheckboxContainer.on('hide', function () { passwordContainer.hide(); passwordContainer.val(''); }); diff --git a/src/Handler/Panel/ConfigBuilder/Action.php b/src/Handler/Panel/ConfigBuilder/Action.php index 7bbb724..42bcfa7 100644 --- a/src/Handler/Panel/ConfigBuilder/Action.php +++ b/src/Handler/Panel/ConfigBuilder/Action.php @@ -24,8 +24,13 @@ class Action extends Session if ($cert !== null && $wantEmbedded !== null) { $certModel = CertificateQuery::create()->findOneByUserAndName($this->getUser(), $cert); - $this->fillZipWithEmbeddedConfig($zip, $server, $certModel); + $config = $this->getEmbeddedConfig($zip, $server, $certModel); $name .= '-' . $certModel->getName() . '.' . $certModel->getSerial(); + + return $this->getResponse() + ->withHeader('Content-Type', 'text/plain') + ->withHeader('Content-Disposition', 'attachment; filename="' . $name . '-embedded.conf"') + ->write($config); } if ($cert !== null && $wantEmbedded === null) { @@ -62,14 +67,14 @@ class Action extends Session } /** - * @param \ZipArchive $zip * @param Server $server * @param Certificate $cert * @throws \Twig_Error_Loader * @throws \Twig_Error_Runtime * @throws \Twig_Error_Syntax + * @return string */ - public function fillZipWithEmbeddedConfig($zip, $server, $cert) + public function getEmbeddedConfig($server, $cert) { /** @var \Twig_Environment $twig */ $twig = $this->get('twig'); @@ -85,9 +90,7 @@ class Action extends Session $parameters['key'] = $cert->getPrivateKey(); } - $config = $twig->render('etc/openvpn-client-embedded.conf.twig', $parameters); - - $zip->addFromString('server-embedded.conf', $config); + return $twig->render('etc/openvpn-client-embedded.conf.twig', $parameters); } /** diff --git a/views/panel/config_builder.html.twig b/views/panel/config_builder.html.twig index d8a76a3..2846f3d 100644 --- a/views/panel/config_builder.html.twig +++ b/views/panel/config_builder.html.twig @@ -13,7 +13,7 @@ {% block panel_contents %}

Config builder

-
+
- - - - - - - -
- -
+ + + + + + +
+ +
{% endblock %}