Check if redirect is available before enable

This commit is contained in:
Ildar Kamalov 2019-02-20 11:36:24 +03:00
parent 9d4b829fb6
commit a60eeb55f1
2 changed files with 28 additions and 17 deletions

View File

@ -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';

View File

@ -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}`);
}