2019-10-15 09:28:49 +00:00
|
|
|
|
import 'url-polyfill';
|
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';
|
2019-08-22 13:10:47 +00:00
|
|
|
|
import addDays from 'date-fns/add_days';
|
|
|
|
|
import subDays from 'date-fns/sub_days';
|
2018-08-30 14:25:33 +00:00
|
|
|
|
import round from 'lodash/round';
|
2019-02-20 08:36:24 +00:00
|
|
|
|
import axios from 'axios';
|
2019-09-12 13:19:35 +00:00
|
|
|
|
import i18n from 'i18next';
|
2019-11-28 14:59:55 +00:00
|
|
|
|
import uniqBy from 'lodash/uniqBy';
|
2019-09-17 14:37:26 +00:00
|
|
|
|
import versionCompare from './versionCompare';
|
2018-08-30 14:25:33 +00:00
|
|
|
|
|
2019-02-19 16:19:40 +00:00
|
|
|
|
import {
|
|
|
|
|
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,
|
2019-09-20 12:05:10 +00:00
|
|
|
|
DNS_RECORD_TYPES,
|
2019-02-19 16:19:40 +00:00
|
|
|
|
} 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');
|
|
|
|
|
};
|
|
|
|
|
|
2019-09-12 13:19:35 +00:00
|
|
|
|
export const formatDateTime = (dateTime) => {
|
|
|
|
|
const currentLanguage = i18n.languages[0] || 'en';
|
|
|
|
|
const parsedTime = dateParse(dateTime);
|
|
|
|
|
const options = {
|
|
|
|
|
year: 'numeric',
|
|
|
|
|
month: 'numeric',
|
|
|
|
|
day: 'numeric',
|
|
|
|
|
hour: 'numeric',
|
|
|
|
|
minute: 'numeric',
|
|
|
|
|
hour12: false,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return parsedTime.toLocaleString(currentLanguage, options);
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-30 14:25:33 +00:00
|
|
|
|
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,
|
2019-07-18 11:52:47 +00:00
|
|
|
|
service_name,
|
2019-08-30 13:03:36 +00:00
|
|
|
|
status,
|
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,
|
2019-07-18 11:52:47 +00:00
|
|
|
|
serviceName: service_name,
|
2019-08-30 13:03:36 +00:00
|
|
|
|
status,
|
2018-08-30 14:25:33 +00:00
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2019-08-22 13:10:47 +00:00
|
|
|
|
export const normalizeHistory = (history, interval) => {
|
|
|
|
|
if (interval === 1 || interval === 7) {
|
|
|
|
|
const hoursAgo = subHours(Date.now(), 24 * interval);
|
|
|
|
|
return history.map((item, index) => ({
|
|
|
|
|
x: dateFormat(addHours(hoursAgo, index), 'D MMM HH:00'),
|
|
|
|
|
y: round(item, 2),
|
|
|
|
|
}));
|
2018-10-10 14:54:21 +00:00
|
|
|
|
}
|
2018-08-30 14:25:33 +00:00
|
|
|
|
|
2019-08-22 13:10:47 +00:00
|
|
|
|
const daysAgo = subDays(Date.now(), interval - 1);
|
|
|
|
|
return history.map((item, index) => ({
|
|
|
|
|
x: dateFormat(addDays(daysAgo, index), 'D MMM YYYY'),
|
|
|
|
|
y: round(item, 2),
|
|
|
|
|
}));
|
|
|
|
|
};
|
2018-08-30 14:25:33 +00:00
|
|
|
|
|
2019-08-22 13:10:47 +00:00
|
|
|
|
export const normalizeTopStats = stats => (
|
|
|
|
|
stats.map(item => ({
|
|
|
|
|
name: Object.keys(item)[0],
|
|
|
|
|
count: Object.values(item)[0],
|
|
|
|
|
}))
|
|
|
|
|
);
|
2018-08-30 14:25:33 +00:00
|
|
|
|
|
2019-11-28 14:59:55 +00:00
|
|
|
|
export const addClientInfo = (data, clients, param) => (
|
|
|
|
|
data.map((row) => {
|
|
|
|
|
const clientIp = row[param];
|
|
|
|
|
const info = clients.find(item => item[clientIp]) || '';
|
|
|
|
|
return {
|
|
|
|
|
...row,
|
|
|
|
|
info: (info && info[clientIp]) || '',
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
2018-08-30 14:25:33 +00:00
|
|
|
|
export const normalizeFilteringStatus = (filteringStatus) => {
|
2019-09-12 13:19:35 +00:00
|
|
|
|
const {
|
|
|
|
|
enabled, filters, user_rules: userRules, interval,
|
|
|
|
|
} = filteringStatus;
|
|
|
|
|
const newFilters = filters
|
|
|
|
|
? filters.map((filter) => {
|
|
|
|
|
const {
|
|
|
|
|
id,
|
|
|
|
|
url,
|
|
|
|
|
enabled,
|
|
|
|
|
last_updated,
|
|
|
|
|
name = 'Default name',
|
|
|
|
|
rules_count: rules_count = 0,
|
|
|
|
|
} = filter;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
id,
|
|
|
|
|
url,
|
|
|
|
|
enabled,
|
|
|
|
|
lastUpdated: last_updated ? formatDateTime(last_updated) : '–',
|
|
|
|
|
name,
|
|
|
|
|
rulesCount: rules_count,
|
|
|
|
|
};
|
|
|
|
|
})
|
|
|
|
|
: [];
|
2018-08-30 14:25:33 +00:00
|
|
|
|
const newUserRules = Array.isArray(userRules) ? userRules.join('\n') : '';
|
2019-09-12 13:19:35 +00:00
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
enabled,
|
|
|
|
|
userRules: newUserRules,
|
|
|
|
|
filters: newFilters,
|
|
|
|
|
interval,
|
|
|
|
|
};
|
2018-08-30 14:25:33 +00:00
|
|
|
|
};
|
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-08-22 15:50:58 +00:00
|
|
|
|
if (port && !isStandardWebPort) {
|
|
|
|
|
if (ip.includes(':') && !ip.includes('[')) {
|
2019-02-06 14:32:32 +00:00
|
|
|
|
address = `http://[${ip}]:${port}`;
|
2019-08-22 15:50:58 +00:00
|
|
|
|
} else {
|
2019-02-06 14:32:32 +00:00
|
|
|
|
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
|
|
|
|
|
2019-03-06 14:53:04 +00:00
|
|
|
|
export const normalizeTextarea = text => text && text.replace(/[;, ]/g, '\n').split('\n').filter(n => n);
|
2019-03-20 14:04:32 +00:00
|
|
|
|
|
2019-09-23 13:02:47 +00:00
|
|
|
|
export const getClientInfo = (clients, ip) => {
|
2019-11-28 11:47:06 +00:00
|
|
|
|
const client = clients
|
|
|
|
|
.find(item => item.ip_addrs && item.ip_addrs.find(clientIp => clientIp === ip));
|
|
|
|
|
|
|
|
|
|
if (!client) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { name, whois_info } = client;
|
|
|
|
|
const whois = Object.keys(whois_info).length > 0 ? whois_info : '';
|
|
|
|
|
|
|
|
|
|
return { name, whois };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getAutoClientInfo = (clients, ip) => {
|
2019-03-20 14:04:32 +00:00
|
|
|
|
const client = clients.find(item => ip === item.ip);
|
2019-09-23 13:02:47 +00:00
|
|
|
|
|
|
|
|
|
if (!client) {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { name, whois_info } = client;
|
2019-09-24 12:28:59 +00:00
|
|
|
|
const whois = Object.keys(whois_info).length > 0 ? whois_info : '';
|
2019-09-23 13:02:47 +00:00
|
|
|
|
|
|
|
|
|
return { name, whois };
|
2019-03-20 14:04:32 +00:00
|
|
|
|
};
|
2019-05-22 14:59:57 +00:00
|
|
|
|
|
|
|
|
|
export const sortClients = (clients) => {
|
|
|
|
|
const compare = (a, b) => {
|
|
|
|
|
const nameA = a.name.toUpperCase();
|
|
|
|
|
const nameB = b.name.toUpperCase();
|
|
|
|
|
|
|
|
|
|
if (nameA > nameB) {
|
2019-05-22 15:28:16 +00:00
|
|
|
|
return 1;
|
2019-05-22 14:59:57 +00:00
|
|
|
|
} else if (nameA < nameB) {
|
2019-05-22 15:28:16 +00:00
|
|
|
|
return -1;
|
2019-05-22 14:59:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-05-22 15:28:16 +00:00
|
|
|
|
return 0;
|
2019-05-22 14:59:57 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return clients.sort(compare);
|
|
|
|
|
};
|
2019-07-18 11:52:47 +00:00
|
|
|
|
|
|
|
|
|
export const toggleAllServices = (services, change, isSelected) => {
|
|
|
|
|
services.forEach(service => change(`blocked_services.${service.id}`, isSelected));
|
|
|
|
|
};
|
2019-09-03 08:03:47 +00:00
|
|
|
|
|
|
|
|
|
export const secondsToMilliseconds = (seconds) => {
|
|
|
|
|
if (seconds) {
|
|
|
|
|
return seconds * 1000;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return seconds;
|
|
|
|
|
};
|
2019-09-12 13:19:35 +00:00
|
|
|
|
|
|
|
|
|
export const normalizeRulesTextarea = text => text && text.replace(/^\n/g, '').replace(/\n\s*\n/g, '\n');
|
2019-09-17 14:37:26 +00:00
|
|
|
|
|
|
|
|
|
export const isVersionGreater = (currentVersion, previousVersion) => (
|
|
|
|
|
versionCompare(currentVersion, previousVersion) === -1
|
|
|
|
|
);
|
2019-09-24 12:28:59 +00:00
|
|
|
|
|
|
|
|
|
export const normalizeWhois = (whois) => {
|
|
|
|
|
if (Object.keys(whois).length > 0) {
|
|
|
|
|
const {
|
|
|
|
|
city, country, ...values
|
|
|
|
|
} = whois;
|
|
|
|
|
let location = (country && country) || '';
|
|
|
|
|
|
|
|
|
|
if (city && location) {
|
|
|
|
|
location = `${location}, ${city}`;
|
|
|
|
|
} else if (city) {
|
|
|
|
|
location = city;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (location) {
|
|
|
|
|
return {
|
|
|
|
|
location,
|
|
|
|
|
...values,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { ...values };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return whois;
|
|
|
|
|
};
|
2019-09-20 12:05:10 +00:00
|
|
|
|
|
|
|
|
|
export const isValidQuestionType = type => type && DNS_RECORD_TYPES.includes(type.toUpperCase());
|
2019-10-15 09:28:49 +00:00
|
|
|
|
|
2019-10-15 09:31:01 +00:00
|
|
|
|
export const getPathWithQueryString = (path, params) => {
|
2019-10-15 09:28:49 +00:00
|
|
|
|
const searchParams = new URLSearchParams(params);
|
|
|
|
|
|
|
|
|
|
return `${path}?${searchParams.toString()}`;
|
|
|
|
|
};
|
2019-11-28 14:59:55 +00:00
|
|
|
|
|
|
|
|
|
export const getParamsForClientsSearch = (data, param) => {
|
|
|
|
|
const uniqueClients = uniqBy(data, param);
|
|
|
|
|
return uniqueClients
|
|
|
|
|
.reduce((acc, item, idx) => {
|
|
|
|
|
const key = `ip${idx}`;
|
|
|
|
|
acc[key] = item[param];
|
|
|
|
|
return acc;
|
|
|
|
|
}, {});
|
|
|
|
|
};
|