templates/main/mirror_checker.html.twig line 1
<!DOCTYPE html>
<html>
<head>
<title>loading...</title>
<script>
function pingPage(url) {
return fetch("https://"+url+"/ping")
.then(response => {
if (response.status === 200) {
return response.json();
} else {
throw new Error('Response status is not 200');
}
})
.then(data => {
const currentTime = Math.floor(Date.now() / 1000);
const responseTime = Math.floor(data / 1000);
if (currentTime - responseTime <= 60) {
return url;
} else {
throw new Error('The timestamp is older than 1 minute.');
}
})
;
}
function pingPage3Times(url) {
return waitForTwoOfThree([pingPage(url), pingPage(url), pingPage(url)]);
}
function checkLinks(urls, priorityMirror) {
Promise.any(urls.map(url => pingPage3Times(url)))
.then(result => {
const params = {'successMirror': result}
if (priorityMirror !== "") {
params['failedMirror'] = priorityMirror
}
redirectWithExtraParams(params);
})
.catch(error => {
redirectWithExtraParams({'failedMirror': urls.join(',')});
});
}
function redirectWithExtraParams(params) {
const url = new URL(window.location.href);
for (const param in params) {
if (param === "failedMirror") {
let oldParam = url.searchParams.get(param);
if (oldParam) {
params[param] = uniqueArray((oldParam + "," + params[param]).split(',')).join(',')
}
}
url.searchParams.set(param, params[param]);
}
url.searchParams.set("stt", {{ startTimeMs }});
url.searchParams.set("rlc", {{ reloadCount }});
url.searchParams.set("cookieEnabled", checkCookiesEnabled() ? "1" : "0");
window.location.href = url.toString();
}
function uniqueArray(arr) {
var a = [];
for (var i=0, l=arr.length; i<l; i++)
if (a.indexOf(arr[i]) === -1 && arr[i] !== '')
a.push(arr[i]);
return a;
}
function checkCookiesEnabled() {
return navigator.cookieEnabled;
// reserve method
document.cookie = "testCookie=1";
const cookiesEnabled = document.cookie.indexOf("testCookie") !== -1;
document.cookie = "testCookie=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
return cookiesEnabled;
}
function waitForTwoOfThree(promises) {
return new Promise((resolve, reject) => {
let resolvedCount = 0;
let results = [];
promises.forEach((promise, index) => {
promise
.then(result => {
resolvedCount++;
results[index] = result;
if (resolvedCount >= 2) {
resolve(result);
}
})
.catch(error => {
results[index] = error;
});
});
// Если все промисы завершились с ошибкой
Promise.all(promises.map(p => p.catch(e => e)))
.then(errors => {
if (resolvedCount < 2) {
reject(errors);
}
});
});
}
async function main() {
const priorityMirror = "{{ priorityMirrorHost | default }}";
if (priorityMirror !== "") {
let working = false;
await pingPage3Times(priorityMirror)
.then(() => {
working = true;
})
.catch(() => {
working = false;
})
;
if (working) {
redirectWithExtraParams({"successMirror": priorityMirror});
return;
}
}
const pageUrls = [
{% for mirrorHost in mirrorHostList %}
"{{ mirrorHost }}",
{% endfor %}
];
checkLinks(pageUrls, priorityMirror);
}
{% if needToSlowDown %}
setTimeout(()=>{main();}, 5000); // sleep 5 sec if needToSlowDown
{% else %}
main();
{% endif %}
</script>
</head>
<body>
loading..
</body>
</html>