templates/main/mirror_checker.html.twig line 1

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <title>loading...</title>
  5.     <script>
  6.         function pingPage(url) {
  7.             return fetch("https://"+url+"/ping")
  8.                 .then(response => {
  9.                     if (response.status === 200) {
  10.                         return response.json();
  11.                     } else {
  12.                         throw new Error('Response status is not 200');
  13.                     }
  14.                 })
  15.                 .then(data => {
  16.                     const currentTime = Math.floor(Date.now() / 1000);
  17.                     const responseTime = Math.floor(data / 1000);
  18.                     if (currentTime - responseTime <= 60) {
  19.                         return url;
  20.                     } else {
  21.                         throw new Error('The timestamp is older than 1 minute.');
  22.                     }
  23.                 })
  24.             ;
  25.         }
  26.         function pingPage3Times(url) {
  27.             return waitForTwoOfThree([pingPage(url), pingPage(url), pingPage(url)]);
  28.         }
  29.         function checkLinks(urls, priorityMirror) {
  30.             Promise.any(urls.map(url => pingPage3Times(url)))
  31.                 .then(result => {
  32.                     const params = {'successMirror': result}
  33.                     if (priorityMirror !== "") {
  34.                         params['failedMirror'] = priorityMirror
  35.                     }
  36.                     redirectWithExtraParams(params);
  37.                 })
  38.                 .catch(error => {
  39.                     redirectWithExtraParams({'failedMirror': urls.join(',')});
  40.                 });
  41.         }
  42.         function redirectWithExtraParams(params) {
  43.             const url = new URL(window.location.href);
  44.             for (const param in params) {
  45.                 if (param === "failedMirror") {
  46.                     let oldParam = url.searchParams.get(param);
  47.                     if (oldParam) {
  48.                         params[param] = uniqueArray((oldParam + "," + params[param]).split(',')).join(',')
  49.                     }
  50.                 }
  51.                 url.searchParams.set(param, params[param]);
  52.             }
  53.             url.searchParams.set("stt", {{ startTimeMs }});
  54.             url.searchParams.set("rlc", {{ reloadCount }});
  55.             url.searchParams.set("cookieEnabled", checkCookiesEnabled() ? "1" : "0");
  56.             window.location.href = url.toString();
  57.         }
  58.         function uniqueArray(arr) {
  59.             var a = [];
  60.             for (var i=0, l=arr.length; i<l; i++)
  61.                 if (a.indexOf(arr[i]) === -1 && arr[i] !== '')
  62.                     a.push(arr[i]);
  63.             return a;
  64.         }
  65.         function checkCookiesEnabled() {
  66.             return navigator.cookieEnabled;
  67.             // reserve method
  68.             document.cookie = "testCookie=1";
  69.             const cookiesEnabled = document.cookie.indexOf("testCookie") !== -1;
  70.             document.cookie = "testCookie=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
  71.             return cookiesEnabled;
  72.         }
  73.         function waitForTwoOfThree(promises) {
  74.             return new Promise((resolve, reject) => {
  75.                 let resolvedCount = 0;
  76.                 let results = [];
  77.                 promises.forEach((promise, index) => {
  78.                     promise
  79.                         .then(result => {
  80.                             resolvedCount++;
  81.                             results[index] = result;
  82.                             if (resolvedCount >= 2) {
  83.                                 resolve(result);
  84.                             }
  85.                         })
  86.                         .catch(error => {
  87.                             results[index] = error;
  88.                         });
  89.                 });
  90.                 // Если все промисы завершились с ошибкой
  91.                 Promise.all(promises.map(p => p.catch(e => e)))
  92.                     .then(errors => {
  93.                         if (resolvedCount < 2) {
  94.                             reject(errors);
  95.                         }
  96.                     });
  97.             });
  98.         }
  99.         async function main() {
  100.             const priorityMirror = "{{ priorityMirrorHost | default }}";
  101.             if (priorityMirror !== "") {
  102.                 let working = false;
  103.                 await pingPage3Times(priorityMirror)
  104.                     .then(() => {
  105.                         working = true;
  106.                     })
  107.                     .catch(() => {
  108.                         working = false;
  109.                     })
  110.                 ;
  111.                 if (working) {
  112.                     redirectWithExtraParams({"successMirror": priorityMirror});
  113.                     return;
  114.                 }
  115.             }
  116.             const pageUrls = [
  117.                 {% for mirrorHost in mirrorHostList %}
  118.                 "{{ mirrorHost }}",
  119.                 {% endfor %}
  120.             ];
  121.             checkLinks(pageUrls, priorityMirror);
  122.         }
  123.         {% if needToSlowDown %}
  124.             setTimeout(()=>{main();}, 5000); // sleep 5 sec if needToSlowDown
  125.         {% else %}
  126.             main();
  127.         {% endif %}
  128.     </script>
  129. </head>
  130. <body>
  131. loading..
  132. </body>
  133. </html>