From 2814c393ad7128f6db90a8d0fdfa63e7a11722a6 Mon Sep 17 00:00:00 2001 From: Ildar Kamalov Date: Thu, 21 Feb 2019 18:28:23 +0300 Subject: [PATCH] Fixed checkRedirect helper --- .../components/Settings/Encryption/Form.js | 4 +- client/src/helpers/helpers.js | 61 ++++++++++++------- 2 files changed, 40 insertions(+), 25 deletions(-) diff --git a/client/src/components/Settings/Encryption/Form.js b/client/src/components/Settings/Encryption/Form.js index b85bcfeb..f4b01560 100644 --- a/client/src/components/Settings/Encryption/Form.js +++ b/client/src/components/Settings/Encryption/Form.js @@ -27,8 +27,8 @@ const clearFields = (change, setTlsConfig, t) => { const fields = { private_key: '', certificate_chain: '', - port_https: '', - port_dns_over_tls: '', + port_https: 443, + port_dns_over_tls: 853, server_name: '', force_https: false, enabled: false, diff --git a/client/src/helpers/helpers.js b/client/src/helpers/helpers.js index 3c0544e6..2ac0afe9 100644 --- a/client/src/helpers/helpers.js +++ b/client/src/helpers/helpers.js @@ -11,7 +11,6 @@ import { STANDARD_WEB_PORT, STANDARD_HTTPS_PORT, CHECK_TIMEOUT, - STOP_TIMEOUT, } from './constants'; export const formatTime = (time) => { @@ -149,26 +148,42 @@ export const getWebAddress = (ip, port = '') => { return address; }; -export const redirectCheck = (url) => { - const redirectCheck = setInterval(() => { - axios.get(url) - .then((response) => { - if (response) { - clearInterval(redirectCheck); - window.location.replace(url); - } - }) - .catch((error) => { - if (error.response) { - clearInterval(redirectCheck); - window.location.replace(url); - } - }); - }, CHECK_TIMEOUT); - setTimeout(() => { - clearInterval(redirectCheck); - console.error('Redirect check stopped'); - }, STOP_TIMEOUT); +export const checkRedirect = (url, attempts) => { + let count = attempts || 1; + + if (count > 10) { + window.location.replace(url); + return false; + } + + const rmTimeout = t => t && clearTimeout(t); + const setRecursiveTimeout = (time, ...args) => setTimeout( + checkRedirect, + time, + ...args, + ); + + let timeout; + + axios.get(url) + .then((response) => { + rmTimeout(timeout); + if (response) { + window.location.replace(url); + return; + } + timeout = setRecursiveTimeout(CHECK_TIMEOUT, url, count += 1); + }) + .catch((error) => { + rmTimeout(timeout); + if (error.response) { + window.location.replace(url); + return; + } + timeout = setRecursiveTimeout(CHECK_TIMEOUT, url, count += 1); + }); + + return false; }; export const redirectToCurrentProtocol = (values, httpPort = 80) => { @@ -179,9 +194,9 @@ export const redirectToCurrentProtocol = (values, httpPort = 80) => { const httpsPort = port_https !== STANDARD_HTTPS_PORT ? `:${port_https}` : ''; if (protocol !== 'https:' && enabled && port_https) { - redirectCheck(`https://${hostname}${httpsPort}/${hash}`); + checkRedirect(`https://${hostname}${httpsPort}/${hash}`); } else if (protocol === 'https:' && enabled && port_https && port_https !== port) { - redirectCheck(`https://${hostname}${httpsPort}/${hash}`); + checkRedirect(`https://${hostname}${httpsPort}/${hash}`); } else if (protocol === 'https:' && (!enabled || !port_https)) { window.location.replace(`http://${hostname}:${httpPort}/${hash}`); }