Fixed checkRedirect helper

This commit is contained in:
Ildar Kamalov 2019-02-21 18:28:23 +03:00
parent 37431735fd
commit 2814c393ad
2 changed files with 40 additions and 25 deletions

View File

@ -27,8 +27,8 @@ const clearFields = (change, setTlsConfig, t) => {
const fields = { const fields = {
private_key: '', private_key: '',
certificate_chain: '', certificate_chain: '',
port_https: '', port_https: 443,
port_dns_over_tls: '', port_dns_over_tls: 853,
server_name: '', server_name: '',
force_https: false, force_https: false,
enabled: false, enabled: false,

View File

@ -11,7 +11,6 @@ import {
STANDARD_WEB_PORT, STANDARD_WEB_PORT,
STANDARD_HTTPS_PORT, STANDARD_HTTPS_PORT,
CHECK_TIMEOUT, CHECK_TIMEOUT,
STOP_TIMEOUT,
} from './constants'; } from './constants';
export const formatTime = (time) => { export const formatTime = (time) => {
@ -149,26 +148,42 @@ export const getWebAddress = (ip, port = '') => {
return address; return address;
}; };
export const redirectCheck = (url) => { export const checkRedirect = (url, attempts) => {
const redirectCheck = setInterval(() => { let count = attempts || 1;
axios.get(url)
.then((response) => { if (count > 10) {
if (response) { window.location.replace(url);
clearInterval(redirectCheck); return false;
window.location.replace(url); }
}
}) const rmTimeout = t => t && clearTimeout(t);
.catch((error) => { const setRecursiveTimeout = (time, ...args) => setTimeout(
if (error.response) { checkRedirect,
clearInterval(redirectCheck); time,
window.location.replace(url); ...args,
} );
});
}, CHECK_TIMEOUT); let timeout;
setTimeout(() => {
clearInterval(redirectCheck); axios.get(url)
console.error('Redirect check stopped'); .then((response) => {
}, STOP_TIMEOUT); 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) => { 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}` : ''; const httpsPort = port_https !== STANDARD_HTTPS_PORT ? `:${port_https}` : '';
if (protocol !== 'https:' && enabled && 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) { } 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)) { } else if (protocol === 'https:' && (!enabled || !port_https)) {
window.location.replace(`http://${hostname}:${httpPort}/${hash}`); window.location.replace(`http://${hostname}:${httpPort}/${hash}`);
} }