diff --git a/web/share/js/index/main.js b/web/share/js/index/main.js index 9ebb1f60..aa425c00 100644 --- a/web/share/js/index/main.js +++ b/web/share/js/index/main.js @@ -39,84 +39,100 @@ export function main() { } function __setAppText() { + let e_href = tools.escape(window.location.href); $("app-text").innerHTML = ` # On Linux using Chromium/Chrome via any terminal:
- $
\`which chromium 2>/dev/null || which chrome 2>/dev/null || which google-chrome\` --app="${window.location.href}"
+ $ \`which chromium 2>/dev/null || which chrome 2>/dev/null || which google-chrome\` --app="${e_href}"

# On MacOS using Terminal application:
- $
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --app="${window.location.href}"
+ $ /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --app="${e_href}"

# On Windows via cmd.exe:
- C:\>
start chrome --app="${window.location.href}" + C:\> start chrome --app="${e_href}" `; } function __loadKvmdInfo() { tools.httpGet("api/info", {"fields": "auth,meta,extras"}, function(http) { - if (http.status === 200) { - let info = JSON.parse(http.responseText).result; + switch (http.status) { + case 200: + __showKvmdInfo(JSON.parse(http.responseText).result); + break; - let apps = []; - if (info.extras === null) { - wm.error("Not all applications in the menu can be displayed due an error.
See KVMD logs for details."); - } else { - apps = Object.values(info.extras).sort(function(a, b) { - if (a.place < b.place) { - return -1; - } else if (a.place > b.place) { - return 1; - } else { - return 0; - } - }); - } + case 401: + case 403: + tools.currentOpen("login"); + break; - $("apps-box").innerHTML = ""; - - // Don't use this option, it may be removed in any time - let hide_kvm_button = ( - (info.meta !== null && info.meta.web && info.meta.web.hide_kvm_button) - || tools.config.getBool("index--hide-kvm-button", false) - ); - if (!hide_kvm_button) { - $("apps").innerHTML += __makeApp(null, "kvm", "share/svg/kvm.svg", "KVM"); - } - - for (let app of apps) { - if (app.place >= 0 && (app.enabled || app.started)) { - $("apps").innerHTML += __makeApp(null, app.path, app.icon, app.name); - } - } - - if (info.auth.enabled) { - $("apps").innerHTML += __makeApp("logout-button", "#", "share/svg/logout.svg", "Logout"); - tools.el.setOnClick($("logout-button"), __logout); - } - - if (info.meta !== null && info.meta.server && info.meta.server.host) { - $("kvmd-meta-server-host").innerHTML = info.meta.server.host; - document.title = `PiKVM Index: ${info.meta.server.host}`; - } else { - $("kvmd-meta-server-host").innerHTML = ""; - document.title = "PiKVM Index"; - } - } else if (http.status === 401 || http.status === 403) { - tools.currentOpen("login"); - } else { - setTimeout(__loadKvmdInfo, 1000); + default: + setTimeout(__loadKvmdInfo, 1000); + break; } }); } +function __showKvmdInfo(info) { + let apps = []; + if (info.extras === null) { + wm.error("Not all applications in the menu can be displayed due an error.
See KVMD logs for details."); + } else { + apps = Object.values(info.extras).sort(function(a, b) { + if (a.place < b.place) { + return -1; + } else if (a.place > b.place) { + return 1; + } else { + return 0; + } + }); + } + + let html = ""; + + // Don't use this option, it may be removed in any time + let hide_kvm_button = ( + (info.meta !== null && info.meta.web && info.meta.web.hide_kvm_button) + || tools.config.getBool("index--hide-kvm-button", false) + ); + if (!hide_kvm_button) { + html += __makeApp(null, "kvm", "share/svg/kvm.svg", "KVM"); + } + + for (let app of apps) { + if (app.place >= 0 && (app.enabled || app.started)) { + html += __makeApp(null, app.path, app.icon, app.name); + } + } + + if (info.auth.enabled) { + html += __makeApp("logout-button", "#", "share/svg/logout.svg", "Logout"); + } + + $("apps-box").innerHTML = ``; + + if (info.auth.enabled) { + tools.el.setOnClick($("logout-button"), __logout); + } + + if (info.meta !== null && info.meta.server && info.meta.server.host) { + $("kvmd-meta-server-host").innerHTML = info.meta.server.host; + document.title = `PiKVM Index: ${info.meta.server.host}`; + } else { + $("kvmd-meta-server-host").innerHTML = ""; + document.title = "PiKVM Index"; + } +} + function __makeApp(id, path, icon, name) { // Tailing slash in href is added to avoid Nginx 301 redirect // when the location doesn't have tailing slash: "foo -> foo/". // Reverse proxy over PiKVM can be misconfigured to handle this. + let e_add_id = (id ? `id="${tools.escape(id)}"` : ""); return `
  • -
    - +
    +
    - + ${tools.escape(name)}
    @@ -126,10 +142,16 @@ function __makeApp(id, path, icon, name) { function __logout() { tools.httpPost("api/auth/logout", null, function(http) { - if (http.status === 200 || http.status === 401 || http.status === 403) { - tools.currentOpen("login"); - } else { - wm.error("Logout error", http.responseText); + switch (http.status) { + case 200: + case 401: + case 403: + tools.currentOpen("login"); + break; + + default: + wm.error("Logout error", http.responseText); + break; } }); } diff --git a/web/share/js/ipmi/main.js b/web/share/js/ipmi/main.js index 3f08e315..570cb839 100644 --- a/web/share/js/ipmi/main.js +++ b/web/share/js/ipmi/main.js @@ -32,29 +32,43 @@ export function main() { function __loadKvmdInfo() { tools.httpGet("api/info", null, function(http) { - if (http.status === 200) { - let ipmi_port = JSON.parse(http.responseText).result.extras.ipmi.port; - let make_item = (comment, ipmi, api) => ` - # ${comment}:
    $
    - ipmitool -I lanplus -U admin -P admin -H ${window.location.hostname} -p ${ipmi_port} ${ipmi}
    - $ curl -XPOST -HX-KVMD-User:admin -HX-KVMD-Passwd:admin -k \\
    -     ${window.location.protocol}//${window.location.host}/api/atx${api}
    - `; - $("ipmi-text").innerHTML = ` - ${make_item("Power on the server if it's off", "power on", "/power?action=on")} -
    - ${make_item("Soft power off the server if it's on", "power soft", "/power?action=off")} -
    - ${make_item("Hard power off the server if it's on", "power off", "/power?action=off_hard")} -
    - ${make_item("Hard reset the server if it's on", "power reset", "/power?action=reset_hard")} -
    - ${make_item("Check the power status", "power status", "")} - `; - } else if (http.status === 401 || http.status === 403) { - tools.currentOpen("login"); - } else { - setTimeout(__loadKvmdInfo, 1000); + switch (http.status) { + case 200: + __showKvmdInfo(JSON.parse(http.responseText).result); + break; + + case 401: + case 403: + tools.currentOpen("login"); + break; + + default: + setTimeout(__loadKvmdInfo, 1000); + break; } }); } + +function __showKvmdInfo(info) { + let make_item = function (comment, cmd, api) { + return ` + + # ${tools.escape(comment)}:
    $ +
    + ipmitool -I lanplus -U admin -P admin + -H ${tools.escape(window.location.hostname)} + -p ${tools.escape(info.extras.ipmi.port)} ${tools.escape(cmd)} +
    + $ + curl -XPOST -HX-KVMD-User:admin -HX-KVMD-Passwd:admin -k \\
         + ${tools.escape(window.location.protocol + "//" + window.location.host + "/api/atx" + api)} + `; + }; + $("ipmi-text").innerHTML = [ + make_item("Power on the server if it's off", "power on", "/power?action=on"), + make_item("Soft power off the server if it's on", "power soft", "/power?action=off"), + make_item("Hard power off the server if it's on", "power off", "/power?action=off_hard"), + make_item("Hard reset the server if it's on", "power reset", "/power?action=reset_hard"), + make_item("Check the power status", "power status", ""), + ].join("

    "); +} diff --git a/web/share/js/kvm/atx.js b/web/share/js/kvm/atx.js index 6bdcf571..69a0d2c5 100644 --- a/web/share/js/kvm/atx.js +++ b/web/share/js/kvm/atx.js @@ -106,7 +106,7 @@ export function Atx(__recorder) { if ($("atx-ask-switch").checked) { wm.confirm(` - Are you sure you want to press the ${button} button?
    + Are you sure you want to press the ${tools.escape(button)} button?
    Warning! This could cause data loss on the server. `).then(function(ok) { if (ok) { diff --git a/web/share/js/kvm/gpio.js b/web/share/js/kvm/gpio.js index d4221b3d..fd602ce8 100644 --- a/web/share/js/kvm/gpio.js +++ b/web/share/js/kvm/gpio.js @@ -135,30 +135,36 @@ export function Gpio(__recorder) { var __createItem = function(item) { if (item.type === "label") { return item.text; + } else if (item.type === "input") { + let e_ch_class = tools.escape(`__gpio-led-${item.channel}`); + let e_icon = tools.escape(`${ROOT_PREFIX}share/svg/led-circle.svg`); return ` `; + } else if (item.type === "output") { let controls = []; - let confirm = (item.confirm ? "Are you sure you want to perform this action?" : ""); + let e_ch = tools.escape(item.channel); + let e_confirm = (item.confirm ? tools.escape("Are you sure you want to perform this action?") : ""); if (item.scheme["switch"]) { - let id = tools.makeId(); + let e_id = tools.escape(`__gpio-switch-${tools.makeRandomId()}`); + let e_ch_class = tools.escape(`__gpio-switch-${item.channel}`); controls.push(`
    -