2018-08-30 14:25:33 +00:00
|
|
|
import dateParse from 'date-fns/parse';
|
|
|
|
import dateFormat from 'date-fns/format';
|
2018-09-11 09:40:01 +00:00
|
|
|
import subHours from 'date-fns/sub_hours';
|
2018-08-30 14:25:33 +00:00
|
|
|
import addHours from 'date-fns/add_hours';
|
|
|
|
import round from 'lodash/round';
|
2019-02-20 08:36:24 +00:00
|
|
|
import axios from 'axios';
|
2018-08-30 14:25:33 +00:00
|
|
|
|
2019-02-19 16:19:40 +00:00
|
|
|
import {
|
|
|
|
STATS_NAMES,
|
|
|
|
STANDARD_DNS_PORT,
|
|
|
|
STANDARD_WEB_PORT,
|
2019-02-20 08:36:24 +00:00
|
|
|
STANDARD_HTTPS_PORT,
|
2019-02-19 16:19:40 +00:00
|
|
|
CHECK_TIMEOUT,
|
|
|
|
} from './constants';
|
2018-10-12 12:23:21 +00:00
|
|
|
|
2018-10-12 13:58:48 +00:00
|
|
|
export const formatTime = (time) => {
|
2018-08-30 14:25:33 +00:00
|
|
|
const parsedTime = dateParse(time);
|
|
|
|
return dateFormat(parsedTime, 'HH:mm:ss');
|
|
|
|
};
|
|
|
|
|
|
|
|
export const normalizeLogs = logs => logs.map((log) => {
|
|
|
|
const {
|
|
|
|
time,
|
|
|
|
question,
|
|
|
|
answer: response,
|
2018-09-03 12:55:20 +00:00
|
|
|
reason,
|
|
|
|
client,
|
2018-10-30 14:27:47 +00:00
|
|
|
filterId,
|
2018-09-03 12:55:20 +00:00
|
|
|
rule,
|
2018-08-30 14:25:33 +00:00
|
|
|
} = log;
|
|
|
|
const { host: domain, type } = question;
|
|
|
|
const responsesArray = response ? response.map((response) => {
|
|
|
|
const { value, type, ttl } = response;
|
|
|
|
return `${type}: ${value} (ttl=${ttl})`;
|
|
|
|
}) : [];
|
|
|
|
return {
|
2018-10-12 13:58:48 +00:00
|
|
|
time,
|
2018-08-30 14:25:33 +00:00
|
|
|
domain,
|
|
|
|
type,
|
|
|
|
response: responsesArray,
|
2018-09-03 12:55:20 +00:00
|
|
|
reason,
|
|
|
|
client,
|
2018-10-30 14:27:47 +00:00
|
|
|
filterId,
|
2018-09-03 12:55:20 +00:00
|
|
|
rule,
|
2018-08-30 14:25:33 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
export const normalizeHistory = history => Object.keys(history).map((key) => {
|
2018-10-10 14:54:21 +00:00
|
|
|
let id = STATS_NAMES[key];
|
|
|
|
if (!id) {
|
|
|
|
id = key.replace(/_/g, ' ').replace(/^\w/, c => c.toUpperCase());
|
|
|
|
}
|
2018-08-30 14:25:33 +00:00
|
|
|
|
2018-09-11 09:40:01 +00:00
|
|
|
const dayAgo = subHours(Date.now(), 24);
|
2018-08-30 14:25:33 +00:00
|
|
|
|
|
|
|
const data = history[key].map((item, index) => {
|
2018-09-11 09:40:01 +00:00
|
|
|
const formatHour = dateFormat(addHours(dayAgo, index), 'ddd HH:00');
|
2018-08-30 14:25:33 +00:00
|
|
|
const roundValue = round(item, 2);
|
|
|
|
|
|
|
|
return {
|
|
|
|
x: formatHour,
|
|
|
|
y: roundValue,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
data,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
export const normalizeFilteringStatus = (filteringStatus) => {
|
|
|
|
const { enabled, filters, user_rules: userRules } = filteringStatus;
|
|
|
|
const newFilters = filters ? filters.map((filter) => {
|
|
|
|
const {
|
2018-10-30 14:27:47 +00:00
|
|
|
id, url, enabled, lastUpdated: lastUpdated = Date.now(), name = 'Default name', rulesCount: rulesCount = 0,
|
2018-08-30 14:25:33 +00:00
|
|
|
} = filter;
|
|
|
|
|
|
|
|
return {
|
2018-10-30 14:27:47 +00:00
|
|
|
id, url, enabled, lastUpdated: formatTime(lastUpdated), name, rulesCount,
|
2018-08-30 14:25:33 +00:00
|
|
|
};
|
|
|
|
}) : [];
|
|
|
|
const newUserRules = Array.isArray(userRules) ? userRules.join('\n') : '';
|
|
|
|
return { enabled, userRules: newUserRules, filters: newFilters };
|
|
|
|
};
|
2018-10-12 12:23:21 +00:00
|
|
|
|
2018-10-12 13:02:01 +00:00
|
|
|
export const getPercent = (amount, number) => {
|
|
|
|
if (amount > 0 && number > 0) {
|
|
|
|
return round(100 / (amount / number), 2);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
};
|
2018-10-14 20:42:25 +00:00
|
|
|
|
|
|
|
export const captitalizeWords = text => text.split(/[ -_]/g).map(str => str.charAt(0).toUpperCase() + str.substr(1)).join(' ');
|
2019-02-04 14:13:59 +00:00
|
|
|
|
|
|
|
export const getInterfaceIp = (option) => {
|
|
|
|
const onlyIPv6 = option.ip_addresses.every(ip => ip.includes(':'));
|
|
|
|
let interfaceIP = option.ip_addresses[0];
|
|
|
|
|
|
|
|
if (!onlyIPv6) {
|
|
|
|
option.ip_addresses.forEach((ip) => {
|
|
|
|
if (!ip.includes(':')) {
|
|
|
|
interfaceIP = ip;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return interfaceIP;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getIpList = (interfaces) => {
|
|
|
|
let list = [];
|
|
|
|
|
|
|
|
Object.keys(interfaces).forEach((item) => {
|
|
|
|
list = [...list, ...interfaces[item].ip_addresses];
|
|
|
|
});
|
|
|
|
|
|
|
|
return list.sort();
|
|
|
|
};
|
|
|
|
|
2019-02-06 14:32:32 +00:00
|
|
|
export const getDnsAddress = (ip, port = '') => {
|
|
|
|
const isStandardDnsPort = port === STANDARD_DNS_PORT;
|
|
|
|
let address = ip;
|
2019-02-04 14:13:59 +00:00
|
|
|
|
2019-02-06 14:32:32 +00:00
|
|
|
if (port) {
|
|
|
|
if (ip.includes(':') && !isStandardDnsPort) {
|
|
|
|
address = `[${ip}]:${port}`;
|
|
|
|
} else if (!isStandardDnsPort) {
|
|
|
|
address = `${ip}:${port}`;
|
2019-02-04 14:13:59 +00:00
|
|
|
}
|
2019-02-06 14:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return address;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getWebAddress = (ip, port = '') => {
|
|
|
|
const isStandardWebPort = port === STANDARD_WEB_PORT;
|
|
|
|
let address = `http://${ip}`;
|
2019-02-04 14:13:59 +00:00
|
|
|
|
2019-02-06 14:32:32 +00:00
|
|
|
if (port) {
|
|
|
|
if (ip.includes(':') && !isStandardWebPort) {
|
|
|
|
address = `http://[${ip}]:${port}`;
|
|
|
|
} else if (!isStandardWebPort) {
|
|
|
|
address = `http://${ip}:${port}`;
|
|
|
|
}
|
2019-02-04 14:13:59 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 14:32:32 +00:00
|
|
|
return address;
|
2019-02-04 14:13:59 +00:00
|
|
|
};
|
2019-02-19 12:43:36 +00:00
|
|
|
|
2019-02-21 15:28:23 +00:00
|
|
|
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;
|
2019-02-20 08:36:24 +00:00
|
|
|
};
|
|
|
|
|
2019-02-20 09:46:34 +00:00
|
|
|
export const redirectToCurrentProtocol = (values, httpPort = 80) => {
|
2019-02-19 15:04:23 +00:00
|
|
|
const {
|
|
|
|
protocol, hostname, hash, port,
|
|
|
|
} = window.location;
|
2019-02-19 12:43:36 +00:00
|
|
|
const { enabled, port_https } = values;
|
2019-02-20 08:36:24 +00:00
|
|
|
const httpsPort = port_https !== STANDARD_HTTPS_PORT ? `:${port_https}` : '';
|
2019-02-19 12:43:36 +00:00
|
|
|
|
|
|
|
if (protocol !== 'https:' && enabled && port_https) {
|
2019-02-21 15:28:23 +00:00
|
|
|
checkRedirect(`https://${hostname}${httpsPort}/${hash}`);
|
2019-02-21 16:16:09 +00:00
|
|
|
} else if (protocol === 'https:' && enabled && port_https && port_https !== parseInt(port, 10)) {
|
2019-02-21 15:28:23 +00:00
|
|
|
checkRedirect(`https://${hostname}${httpsPort}/${hash}`);
|
2019-02-19 12:43:36 +00:00
|
|
|
} else if (protocol === 'https:' && (!enabled || !port_https)) {
|
2019-02-20 09:46:34 +00:00
|
|
|
window.location.replace(`http://${hostname}:${httpPort}/${hash}`);
|
2019-02-19 12:43:36 +00:00
|
|
|
}
|
|
|
|
};
|
2019-03-06 11:45:21 +00:00
|
|
|
|
|
|
|
export const normalizeTextarea = text => text && text.replace(/[;, ]/g, '\n').split('\n');
|