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_DNS_PORT = 53;
export const STANDARD_WEB_PORT = 80; export const STANDARD_WEB_PORT = 80;
export const STANDARD_HTTPS_PORT = 443;
export const EMPTY_DATE = '0001-01-01T00:00:00Z'; 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 subHours from 'date-fns/sub_hours';
import addHours from 'date-fns/add_hours'; import addHours from 'date-fns/add_hours';
import round from 'lodash/round'; import round from 'lodash/round';
import axios from 'axios';
import { import {
STATS_NAMES, STATS_NAMES,
STANDARD_DNS_PORT, STANDARD_DNS_PORT,
STANDARD_WEB_PORT, STANDARD_WEB_PORT,
STANDARD_HTTPS_PORT,
CHECK_TIMEOUT, CHECK_TIMEOUT,
STOP_TIMEOUT, STOP_TIMEOUT,
} from './constants'; } from './constants';
@ -147,31 +149,39 @@ export const getWebAddress = (ip, port = '') => {
return address; return address;
}; };
export const redirectToCurrentProtocol = (values) => { export const redirectCheck = (url) => {
const {
protocol, hostname, hash, port,
} = window.location;
const { enabled, port_https } = values;
const httpsPort = port_https !== 443 ? `:${port_https}` : '';
if (protocol !== 'https:' && enabled && port_https) {
window.location.replace(`https://${hostname}${httpsPort}/${hash}`);
} else if (protocol === 'https:' && enabled && port_https && port_https !== port) {
// TODO
const redirectCheck = setInterval(() => { const redirectCheck = setInterval(() => {
fetch(`https://${hostname}${httpsPort}/${hash}`, { mode: 'no-cors' }) axios.get(url)
.then((response) => { .then((response) => {
if (response) { if (response) {
clearInterval(redirectCheck); clearInterval(redirectCheck);
window.location.replace(`https://${hostname}${httpsPort}/${hash}`); window.location.replace(url);
} }
}) })
.catch(() => false); .catch((error) => {
if (error.response) {
clearInterval(redirectCheck);
window.location.replace(url);
}
});
}, CHECK_TIMEOUT); }, CHECK_TIMEOUT);
setTimeout(() => { setTimeout(() => {
clearInterval(redirectCheck); clearInterval(redirectCheck);
console.error('Redirect check stopped'); console.error('Redirect check stopped');
}, STOP_TIMEOUT); }, STOP_TIMEOUT);
};
export const redirectToCurrentProtocol = (values) => {
const {
protocol, hostname, hash, port,
} = window.location;
const { enabled, port_https } = values;
const httpsPort = port_https !== STANDARD_HTTPS_PORT ? `:${port_https}` : '';
if (protocol !== 'https:' && enabled && port_https) {
redirectCheck(`https://${hostname}${httpsPort}/${hash}`);
} else if (protocol === 'https:' && enabled && port_https && port_https !== port) {
redirectCheck(`https://${hostname}${httpsPort}/${hash}`);
} else if (protocol === 'https:' && (!enabled || !port_https)) { } else if (protocol === 'https:' && (!enabled || !port_https)) {
window.location.replace(`http://${hostname}/${hash}`); window.location.replace(`http://${hostname}/${hash}`);
} }