diff --git a/src/assets/javascripts/utils.js b/src/assets/javascripts/utils.js index c8a771d..2b9a919 100644 --- a/src/assets/javascripts/utils.js +++ b/src/assets/javascripts/utils.js @@ -58,7 +58,48 @@ function getList() { } http.send(null) }) +} +function pingOnce(href) { + return new Promise(async resolve => { + let started + let http = new XMLHttpRequest() + http.timeout = 5000 + http.ontimeout = () => resolve(5000) + http.onerror = () => resolve() + http.onreadystatechange = () => { + if (http.readyState == 2) { + if (http.status == 200) { + let ended = new Date().getTime() + http.abort() + resolve(ended - started) + } else { + resolve(5000 + http.status) + } + } + } + http.open("GET", `${href}?_=${new Date().getTime()}`, true) + started = new Date().getTime() + http.send(null) + }) +} + +function ping(href) { + return new Promise(async resolve => { + let average = 0 + let time + for (let i = 0; i < 3; i++) { + time = await pingOnce(href) + if (i == 0) continue + if (time >= 5000) { + resolve(time) + return + } + average += time + } + average = parseInt(average / 3) + resolve(average) + }) } export default { @@ -68,5 +109,6 @@ export default { getBlacklist, camelCase, getConfig, - getOptions + getOptions, + ping, } diff --git a/src/pages/options/index.js b/src/pages/options/index.js index a68fa71..18fb120 100644 --- a/src/pages/options/index.js +++ b/src/pages/options/index.js @@ -90,6 +90,11 @@ async function loadPage(path) { for (const frontend in config.services[service].frontends) { if (config.services[service].frontends[frontend].instanceList) { processCustomInstances(frontend, document) + document.getElementById(`ping-${frontend}`).addEventListener("click", async () => { + document.getElementById(`ping-${frontend}`).getElementsByTagName('x')[0].innerHTML = "Pinging..." + await ping(frontend) + document.getElementById(`ping-${frontend}`).getElementsByTagName('x')[0].innerHTML = "Ping instances" + }) } } @@ -228,4 +233,44 @@ function createList(frontend, networks, document, redirects, blacklist) { const r = window.location.href.match(/#(.*)/) if (r) loadPage(r[1]) -else loadPage("general") \ No newline at end of file +else loadPage("general") + +async function ping(frontend) { + let instanceElements = document.getElementById(frontend) + .getElementsByClassName('clearnet')[0] + .getElementsByTagName('x') + for (const element of instanceElements) { + let span = element.getElementsByTagName('span')[0] + if (!span) span = document.createElement('span') + span.innerHTML = 'pinging...' + element.appendChild(span) + + const href = element.getElementsByTagName('a')[0].href + let time = await utils.ping(href) + + let color + let text + + if (time < 5000) { + text = `${time}ms` + if (time <= 1000) { color = "green" } + else if (time <= 2000) color = "orange" + } + else if (time >= 5000) { + color = "red" + if (time == 5000) { + text = "5000ms+" + } + if (time > 5000) { + text = `Error: ${time - 5000}` + } + } + else { + color = "red" + text = 'Server not found' + } + + span.innerHTML = `${text}` + + } +} \ No newline at end of file diff --git a/src/pages/options/widgets/services.pug b/src/pages/options/widgets/services.pug index c72a2b8..345e16a 100644 --- a/src/pages/options/widgets/services.pug +++ b/src/pages/options/widgets/services.pug @@ -51,6 +51,13 @@ each val, service in services div(class="checklist custom-checklist") + div(class="some-block") + a(class="button button-inline" id=`ping-${frontend}`) + svg(xmlns="http://www.w3.org/2000/svg" height="20px" viewBox="0 0 24 24" width="20px" fill="currentColor") + path(d="M10.45 15.5q.6.6 1.55.587.95-.012 1.4-.687L19 7l-8.4 5.6q-.675.45-.712 1.375-.038.925.562 1.525ZM12 4q1.475 0 2.838.412Q16.2 4.825 17.4 5.65l-1.9 1.2q-.825-.425-1.712-.637Q12.9 6 12 6 8.675 6 6.338 8.337 4 10.675 4 14q0 1.05.287 2.075Q4.575 17.1 5.1 18h13.8q.575-.95.838-1.975Q20 15 20 13.9q0-.9-.212-1.75-.213-.85-.638-1.65l1.2-1.9q.75 1.175 1.188 2.5.437 1.325.462 2.75.025 1.425-.325 2.725-.35 1.3-1.025 2.475-.275.45-.75.7-.475.25-1 .25H5.1q-.525 0-1-.25t-.75-.7q-.65-1.125-1-2.387Q2 15.4 2 14q0-2.075.788-3.888.787-1.812 2.15-3.175Q6.3 5.575 8.125 4.787 9.95 4 12 4Zm.175 7.825Z") + |  + x() Ping instances + each val, network in networks div(class=network) div(class="checklist")