From da1fb92915448e715b2c17f0036143936071738c Mon Sep 17 00:00:00 2001 From: DL6ER Date: Wed, 11 Oct 2023 22:37:23 +0200 Subject: [PATCH 1/3] Restore Teleporter functionality. Concerning compatibility, https://caniuse.com/url tells us it can be used iny any sufficiently recent browser except IE and Opera Mini Signed-off-by: DL6ER --- scripts/pi-hole/js/settings-teleporter.js | 25 +++++++++++++++++++++++ settings-teleporter.lp | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/scripts/pi-hole/js/settings-teleporter.js b/scripts/pi-hole/js/settings-teleporter.js index a1dbacee5..5df3dbc29 100644 --- a/scripts/pi-hole/js/settings-teleporter.js +++ b/scripts/pi-hole/js/settings-teleporter.js @@ -33,6 +33,7 @@ function importZIP() { fetch("/api/teleporter", { method: "POST", body: formData, + headers: { "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content") }, }) .then(response => response.json()) .then(data => { @@ -70,3 +71,27 @@ function importZIP() { console.error(error); // eslint-disable-line no-console }); } + +// Inspired by https://stackoverflow.com/a/59576416/2087442 +$("#GETTeleporter").on("click", function () { + $.ajax({ + url: "/api/teleporter", + headers: { "X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content") }, + method: "GET", + xhrFields: { + responseType: "blob", + }, + success: function (data) { + var a = document.createElement("a"); + // eslint-disable-next-line compat/compat + var url = window.URL.createObjectURL(data); + a.href = url; + a.download = "teleporter.zip"; + document.body.append(a); + a.click(); + a.remove(); + // eslint-disable-next-line compat/compat + window.URL.revokeObjectURL(url); + }, + }); +}); diff --git a/settings-teleporter.lp b/settings-teleporter.lp index 761f54cdc..1edf02ee6 100644 --- a/settings-teleporter.lp +++ b/settings-teleporter.lp @@ -23,7 +23,7 @@ mg.include('scripts/pi-hole/lua/settings_header.lp','r')

Warning: This archive contains sensitive information about your Pi-hole installation, e.g. the API token and the 2FA-TOTP secret (if enabled). Please be careful with this file and do not share it with anyone even if they claim to help you.

Warning: You are currently not using an end-to-end encryption. This means that your API token and 2FA-TOTP secret will be transmitted in plain text. We recommend to use HTTPS when exporting your configuration.

From 2563213b6aa9b88acd02b074bdef750f21c2ac02 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Thu, 12 Oct 2023 18:40:06 +0200 Subject: [PATCH 2/3] Use filename as generated by FTL by analyzing the Content-Disposition response header Signed-off-by: DL6ER --- scripts/pi-hole/js/settings-teleporter.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/pi-hole/js/settings-teleporter.js b/scripts/pi-hole/js/settings-teleporter.js index 5df3dbc29..0dd8be3e3 100644 --- a/scripts/pi-hole/js/settings-teleporter.js +++ b/scripts/pi-hole/js/settings-teleporter.js @@ -81,12 +81,12 @@ $("#GETTeleporter").on("click", function () { xhrFields: { responseType: "blob", }, - success: function (data) { + success: function (data, status, xhr) { var a = document.createElement("a"); // eslint-disable-next-line compat/compat var url = window.URL.createObjectURL(data); a.href = url; - a.download = "teleporter.zip"; + a.download = xhr.getResponseHeader("Content-Disposition").split("filename=")[1]; document.body.append(a); a.click(); a.remove(); From 7e7c6d181575a6a1a3a6c05642ae01ef8bd3a625 Mon Sep 17 00:00:00 2001 From: DL6ER Date: Tue, 17 Oct 2023 22:44:25 +0200 Subject: [PATCH 3/3] Match only the filename that is in double-quotes Signed-off-by: DL6ER --- scripts/pi-hole/js/settings-teleporter.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/pi-hole/js/settings-teleporter.js b/scripts/pi-hole/js/settings-teleporter.js index 0dd8be3e3..5c0f08716 100644 --- a/scripts/pi-hole/js/settings-teleporter.js +++ b/scripts/pi-hole/js/settings-teleporter.js @@ -86,7 +86,7 @@ $("#GETTeleporter").on("click", function () { // eslint-disable-next-line compat/compat var url = window.URL.createObjectURL(data); a.href = url; - a.download = xhr.getResponseHeader("Content-Disposition").split("filename=")[1]; + a.download = xhr.getResponseHeader("Content-Disposition").match(/filename="([^"]*)"/)[1]; document.body.append(a); a.click(); a.remove();