From a60eeb55f1b8b9f5c0991bf0922498ba6d4f2953 Mon Sep 17 00:00:00 2001 From: Ildar Kamalov Date: Wed, 20 Feb 2019 11:36:24 +0300 Subject: [PATCH] Check if redirect is available before enable --- client/src/helpers/constants.js | 1 + client/src/helpers/helpers.js | 44 ++++++++++++++++++++------------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/client/src/helpers/constants.js b/client/src/helpers/constants.js index 1964f38e..295bae58 100644 --- a/client/src/helpers/constants.js +++ b/client/src/helpers/constants.js @@ -73,6 +73,7 @@ export const SETTINGS_NAMES = { export const STANDARD_DNS_PORT = 53; export const STANDARD_WEB_PORT = 80; +export const STANDARD_HTTPS_PORT = 443; export const EMPTY_DATE = '0001-01-01T00:00:00Z'; diff --git a/client/src/helpers/helpers.js b/client/src/helpers/helpers.js index c335c04e..81e6cbd6 100644 --- a/client/src/helpers/helpers.js +++ b/client/src/helpers/helpers.js @@ -3,11 +3,13 @@ import dateFormat from 'date-fns/format'; import subHours from 'date-fns/sub_hours'; import addHours from 'date-fns/add_hours'; import round from 'lodash/round'; +import axios from 'axios'; import { STATS_NAMES, STANDARD_DNS_PORT, STANDARD_WEB_PORT, + STANDARD_HTTPS_PORT, CHECK_TIMEOUT, STOP_TIMEOUT, } from './constants'; @@ -147,31 +149,39 @@ 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 redirectToCurrentProtocol = (values) => { const { protocol, hostname, hash, port, } = window.location; const { enabled, port_https } = values; - const httpsPort = port_https !== 443 ? `:${port_https}` : ''; + const httpsPort = port_https !== STANDARD_HTTPS_PORT ? `:${port_https}` : ''; if (protocol !== 'https:' && enabled && port_https) { - window.location.replace(`https://${hostname}${httpsPort}/${hash}`); + redirectCheck(`https://${hostname}${httpsPort}/${hash}`); } else if (protocol === 'https:' && enabled && port_https && port_https !== port) { - // TODO - const redirectCheck = setInterval(() => { - fetch(`https://${hostname}${httpsPort}/${hash}`, { mode: 'no-cors' }) - .then((response) => { - if (response) { - clearInterval(redirectCheck); - window.location.replace(`https://${hostname}${httpsPort}/${hash}`); - } - }) - .catch(() => false); - }, CHECK_TIMEOUT); - setTimeout(() => { - clearInterval(redirectCheck); - console.error('Redirect check stopped'); - }, STOP_TIMEOUT); + redirectCheck(`https://${hostname}${httpsPort}/${hash}`); } else if (protocol === 'https:' && (!enabled || !port_https)) { window.location.replace(`http://${hostname}/${hash}`); }