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';
|
2020-05-29 09:53:40 +00:00
|
|
|
import ipaddr from 'ipaddr.js';
|
2020-07-13 13:06:56 +00:00
|
|
|
import queryString from 'query-string';
|
2020-06-17 21:36:19 +00:00
|
|
|
import { getTrackerData } from './trackers/trackers';
|
2018-08-30 14:25:33 +00:00
|
|
|
|
2019-02-19 16:19:40 +00:00
|
|
|
import {
|
|
|
|
CHECK_TIMEOUT,
|
2020-01-24 15:59:38 +00:00
|
|
|
DEFAULT_DATE_FORMAT_OPTIONS,
|
|
|
|
DEFAULT_LANGUAGE,
|
2020-06-17 21:36:19 +00:00
|
|
|
DEFAULT_TIME_FORMAT,
|
|
|
|
DETAILED_DATE_FORMAT_OPTIONS,
|
2020-08-19 15:23:05 +00:00
|
|
|
DHCP_VALUES_PLACEHOLDERS,
|
2020-01-22 14:25:50 +00:00
|
|
|
FILTERED,
|
2020-06-17 21:36:19 +00:00
|
|
|
FILTERED_STATUS,
|
2020-05-29 09:53:40 +00:00
|
|
|
IP_MATCH_LIST_STATUS,
|
2020-06-17 21:36:19 +00:00
|
|
|
STANDARD_DNS_PORT,
|
|
|
|
STANDARD_HTTPS_PORT,
|
|
|
|
STANDARD_WEB_PORT,
|
2019-02-19 16:19:40 +00:00
|
|
|
} from './constants';
|
2018-10-12 12:23:21 +00:00
|
|
|
|
2020-01-17 09:57:30 +00:00
|
|
|
/**
|
2020-06-05 10:28:13 +00:00
|
|
|
* @param time {string} The time to format
|
2020-07-24 10:45:46 +00:00
|
|
|
* @param options {string}
|
2020-06-05 10:28:13 +00:00
|
|
|
* @returns {string} Returns the time in the format HH:mm:ss
|
2020-01-17 09:57:30 +00:00
|
|
|
*/
|
2020-06-17 21:36:19 +00:00
|
|
|
export const formatTime = (time, options = DEFAULT_TIME_FORMAT) => {
|
2018-08-30 14:25:33 +00:00
|
|
|
const parsedTime = dateParse(time);
|
2020-06-17 21:36:19 +00:00
|
|
|
return dateFormat(parsedTime, options);
|
2018-08-30 14:25:33 +00:00
|
|
|
};
|
|
|
|
|
2020-01-17 09:57:30 +00:00
|
|
|
/**
|
2020-06-05 10:28:13 +00:00
|
|
|
* @param dateTime {string} The date to format
|
|
|
|
* @param [options] {object} Date.prototype.toLocaleString([locales[, options]]) options argument
|
|
|
|
* @returns {string} Returns the date and time in the specified format
|
2020-01-17 09:57:30 +00:00
|
|
|
*/
|
2020-01-24 15:59:38 +00:00
|
|
|
export const formatDateTime = (dateTime, options = DEFAULT_DATE_FORMAT_OPTIONS) => {
|
2020-06-05 10:28:13 +00:00
|
|
|
const { language } = navigator;
|
|
|
|
const currentLanguage = (language.slice(0, 2) === 'en' || !language) ? 'en-GB' : language;
|
|
|
|
|
|
|
|
const parsedTime = new Date(dateTime);
|
2019-09-12 13:19:35 +00:00
|
|
|
|
|
|
|
return parsedTime.toLocaleString(currentLanguage, options);
|
|
|
|
};
|
|
|
|
|
2020-06-05 10:28:13 +00:00
|
|
|
/**
|
|
|
|
* @param dateTime {string} The date to format
|
|
|
|
* @returns {string} Returns the date and time in the format with the full month name
|
|
|
|
*/
|
2020-05-22 14:06:05 +00:00
|
|
|
export const formatDetailedDateTime = (dateTime) => formatDateTime(
|
|
|
|
dateTime, DETAILED_DATE_FORMAT_OPTIONS,
|
|
|
|
);
|
2020-01-24 15:59:38 +00:00
|
|
|
|
2020-05-22 14:06:05 +00:00
|
|
|
export const normalizeLogs = (logs) => logs.map((log) => {
|
2018-08-30 14:25:33 +00:00
|
|
|
const {
|
2020-06-17 21:36:19 +00:00
|
|
|
answer,
|
|
|
|
answer_dnssec,
|
|
|
|
client,
|
|
|
|
client_proto,
|
|
|
|
elapsedMs,
|
2018-08-30 14:25:33 +00:00
|
|
|
question,
|
2018-09-03 12:55:20 +00:00
|
|
|
reason,
|
2020-06-17 21:36:19 +00:00
|
|
|
status,
|
|
|
|
time,
|
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-11-29 15:05:15 +00:00
|
|
|
original_answer,
|
2020-06-17 21:36:19 +00:00
|
|
|
upstream,
|
2018-08-30 14:25:33 +00:00
|
|
|
} = log;
|
2020-06-17 21:36:19 +00:00
|
|
|
|
2018-08-30 14:25:33 +00:00
|
|
|
const { host: domain, type } = question;
|
2020-06-17 21:36:19 +00:00
|
|
|
|
2020-07-13 12:23:13 +00:00
|
|
|
const processResponse = (data) => (data ? data.map((response) => {
|
2018-08-30 14:25:33 +00:00
|
|
|
const { value, type, ttl } = response;
|
|
|
|
return `${type}: ${value} (ttl=${ttl})`;
|
2020-07-13 12:23:13 +00:00
|
|
|
}) : []);
|
2020-06-17 21:36:19 +00:00
|
|
|
|
2018-08-30 14:25:33 +00:00
|
|
|
return {
|
2018-10-12 13:58:48 +00:00
|
|
|
time,
|
2018-08-30 14:25:33 +00:00
|
|
|
domain,
|
|
|
|
type,
|
2020-07-13 12:23:13 +00:00
|
|
|
response: processResponse(answer),
|
2018-09-03 12:55:20 +00:00
|
|
|
reason,
|
|
|
|
client,
|
2020-06-17 21:36:19 +00:00
|
|
|
client_proto,
|
2018-10-30 14:27:47 +00:00
|
|
|
filterId,
|
2018-09-03 12:55:20 +00:00
|
|
|
rule,
|
2019-08-30 13:03:36 +00:00
|
|
|
status,
|
2019-11-29 15:05:15 +00:00
|
|
|
serviceName: service_name,
|
|
|
|
originalAnswer: original_answer,
|
2020-07-13 12:23:13 +00:00
|
|
|
originalResponse: processResponse(original_answer),
|
|
|
|
tracker: getTrackerData(domain),
|
2020-06-17 21:36:19 +00:00
|
|
|
answer_dnssec,
|
|
|
|
elapsedMs,
|
|
|
|
upstream,
|
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
|
|
|
|
2020-05-22 14:06:05 +00:00
|
|
|
export const normalizeTopStats = (stats) => (
|
|
|
|
stats.map((item) => ({
|
2019-08-22 13:10:47 +00:00
|
|
|
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];
|
2020-05-22 14:06:05 +00:00
|
|
|
const info = clients.find((item) => item[clientIp]) || '';
|
2019-11-28 14:59:55 +00:00
|
|
|
return {
|
|
|
|
...row,
|
2020-07-03 16:17:58 +00:00
|
|
|
info: info?.[clientIp] ?? '',
|
2019-11-28 14:59:55 +00:00
|
|
|
};
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2020-05-22 14:06:05 +00:00
|
|
|
export const normalizeFilters = (filters) => (
|
2020-02-26 16:58:25 +00:00
|
|
|
filters ? filters.map((filter) => {
|
|
|
|
const {
|
|
|
|
id,
|
|
|
|
url,
|
|
|
|
enabled,
|
|
|
|
last_updated,
|
|
|
|
name = 'Default name',
|
2020-05-22 14:06:05 +00:00
|
|
|
rules_count = 0,
|
2020-02-26 16:58:25 +00:00
|
|
|
} = filter;
|
|
|
|
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
url,
|
|
|
|
enabled,
|
|
|
|
lastUpdated: last_updated,
|
|
|
|
name,
|
|
|
|
rulesCount: rules_count,
|
|
|
|
};
|
|
|
|
}) : []
|
|
|
|
);
|
|
|
|
|
2018-08-30 14:25:33 +00:00
|
|
|
export const normalizeFilteringStatus = (filteringStatus) => {
|
2019-09-12 13:19:35 +00:00
|
|
|
const {
|
2020-02-26 16:58:25 +00:00
|
|
|
enabled, filters, user_rules: userRules, interval, whitelist_filters,
|
2019-09-12 13:19:35 +00:00
|
|
|
} = filteringStatus;
|
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,
|
2020-02-26 16:58:25 +00:00
|
|
|
filters: normalizeFilters(filters),
|
|
|
|
whitelistFilters: normalizeFilters(whitelist_filters),
|
2019-09-12 13:19:35 +00:00
|
|
|
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
|
|
|
|
2020-05-29 09:53:40 +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) => {
|
2020-05-22 14:06:05 +00:00
|
|
|
const onlyIPv6 = option.ip_addresses.every((ip) => ip.includes(':'));
|
2020-08-19 15:23:05 +00:00
|
|
|
let [interfaceIP] = option.ip_addresses;
|
2019-02-04 14:13:59 +00:00
|
|
|
|
|
|
|
if (!onlyIPv6) {
|
|
|
|
option.ip_addresses.forEach((ip) => {
|
|
|
|
if (!ip.includes(':')) {
|
|
|
|
interfaceIP = ip;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return interfaceIP;
|
|
|
|
};
|
|
|
|
|
2020-08-19 15:23:05 +00:00
|
|
|
export const getIpList = (interfaces) => Object.values(interfaces)
|
|
|
|
.reduce((acc, curr) => acc.concat(curr.ip_addresses), [])
|
|
|
|
.sort();
|
2019-02-04 14:13:59 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-22 14:06:05 +00:00
|
|
|
const rmTimeout = (t) => t && clearTimeout(t);
|
2019-02-21 15:28:23 +00:00
|
|
|
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
|
|
|
|
2020-07-15 09:35:37 +00:00
|
|
|
/**
|
|
|
|
* @param {string} text
|
|
|
|
* @returns []string
|
|
|
|
*/
|
|
|
|
export const splitByNewLine = (text) => text.split('\n')
|
|
|
|
.filter((n) => n.trim());
|
2020-01-30 10:58:54 +00:00
|
|
|
|
2020-07-15 09:35:37 +00:00
|
|
|
/**
|
|
|
|
* @param {string} text
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
export const trimMultilineString = (text) => splitByNewLine(text)
|
2020-07-15 17:55:13 +00:00
|
|
|
.map((line) => line.trim())
|
|
|
|
.join('\n');
|
2020-07-15 09:35:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} text
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
export const removeEmptyLines = (text) => splitByNewLine(text)
|
|
|
|
.join('\n');
|
2019-03-20 14:04:32 +00:00
|
|
|
|
2020-01-13 14:41:59 +00:00
|
|
|
/**
|
|
|
|
* Normalizes the topClients array
|
|
|
|
*
|
|
|
|
* @param {Object[]} topClients
|
|
|
|
* @param {string} topClients.name
|
|
|
|
* @param {number} topClients.count
|
|
|
|
* @param {Object} topClients.info
|
|
|
|
* @param {string} topClients.info.name
|
|
|
|
* @returns {Object} normalizedTopClients
|
|
|
|
* @returns {Object.<string, number>} normalizedTopClients.auto - auto clients
|
|
|
|
* @returns {Object.<string, number>} normalizedTopClients.configured - configured clients
|
|
|
|
*/
|
2020-05-22 14:06:05 +00:00
|
|
|
export const normalizeTopClients = (topClients) => topClients.reduce(
|
2020-06-05 10:28:13 +00:00
|
|
|
(acc, clientObj) => {
|
2020-05-22 14:06:05 +00:00
|
|
|
const { name, count, info: { name: infoName } } = clientObj;
|
2020-06-05 10:28:13 +00:00
|
|
|
acc.auto[name] = count;
|
|
|
|
acc.configured[infoName] = count;
|
|
|
|
return acc;
|
2020-05-29 09:53:40 +00:00
|
|
|
}, {
|
|
|
|
auto: {},
|
|
|
|
configured: {},
|
|
|
|
},
|
2020-05-22 14:06:05 +00:00
|
|
|
);
|
2019-12-20 11:15:57 +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;
|
2020-05-29 09:53:40 +00:00
|
|
|
}
|
|
|
|
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) => {
|
2020-05-22 14:06:05 +00:00
|
|
|
services.forEach((service) => change(`blocked_services.${service.id}`, isSelected));
|
2019-07-18 11:52:47 +00:00
|
|
|
};
|
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
|
|
|
|
2020-07-03 16:17:58 +00:00
|
|
|
export const normalizeRulesTextarea = (text) => text?.replace(/^\n/g, '')
|
2020-05-29 09:53:40 +00:00
|
|
|
.replace(/\n\s*\n/g, '\n');
|
2019-09-17 14:37:26 +00:00
|
|
|
|
2019-09-24 12:28:59 +00:00
|
|
|
export const normalizeWhois = (whois) => {
|
|
|
|
if (Object.keys(whois).length > 0) {
|
|
|
|
const {
|
|
|
|
city, country, ...values
|
|
|
|
} = whois;
|
2020-07-03 16:17:58 +00:00
|
|
|
let location = country || '';
|
2019-09-24 12:28:59 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
}, {});
|
|
|
|
};
|
2020-01-10 16:47:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates onBlur handler that can normalize input if normalization function is specified
|
|
|
|
*
|
|
|
|
* @param {Object} event
|
|
|
|
* @param {Object} event.target
|
|
|
|
* @param {string} event.target.value
|
|
|
|
* @param {Object} input
|
|
|
|
* @param {function} input.onBlur
|
|
|
|
* @param {function} [normalizeOnBlur]
|
|
|
|
* @returns {function}
|
|
|
|
*/
|
|
|
|
export const createOnBlurHandler = (event, input, normalizeOnBlur) => (
|
|
|
|
normalizeOnBlur
|
|
|
|
? input.onBlur(normalizeOnBlur(event.target.value))
|
|
|
|
: input.onBlur());
|
2020-01-22 14:25:50 +00:00
|
|
|
|
2020-05-22 14:06:05 +00:00
|
|
|
export const checkFiltered = (reason) => reason.indexOf(FILTERED) === 0;
|
|
|
|
export const checkRewrite = (reason) => reason === FILTERED_STATUS.REWRITE;
|
|
|
|
export const checkRewriteHosts = (reason) => reason === FILTERED_STATUS.REWRITE_HOSTS;
|
|
|
|
export const checkBlackList = (reason) => reason === FILTERED_STATUS.FILTERED_BLACK_LIST;
|
|
|
|
export const checkWhiteList = (reason) => reason === FILTERED_STATUS.NOT_FILTERED_WHITE_LIST;
|
|
|
|
// eslint-disable-next-line max-len
|
|
|
|
export const checkNotFilteredNotFound = (reason) => reason === FILTERED_STATUS.NOT_FILTERED_NOT_FOUND;
|
|
|
|
export const checkSafeSearch = (reason) => reason === FILTERED_STATUS.FILTERED_SAFE_SEARCH;
|
|
|
|
export const checkSafeBrowsing = (reason) => reason === FILTERED_STATUS.FILTERED_SAFE_BROWSING;
|
|
|
|
export const checkParental = (reason) => reason === FILTERED_STATUS.FILTERED_PARENTAL;
|
|
|
|
export const checkBlockedService = (reason) => reason === FILTERED_STATUS.FILTERED_BLOCKED_SERVICE;
|
2020-02-26 16:58:25 +00:00
|
|
|
|
|
|
|
export const getCurrentFilter = (url, filters) => {
|
2020-07-03 16:17:58 +00:00
|
|
|
const filter = filters?.find((item) => url === item.url);
|
2020-02-26 16:58:25 +00:00
|
|
|
|
|
|
|
if (filter) {
|
|
|
|
const { enabled, name, url } = filter;
|
2020-05-29 09:53:40 +00:00
|
|
|
return {
|
|
|
|
enabled,
|
|
|
|
name,
|
|
|
|
url,
|
|
|
|
};
|
2020-02-26 16:58:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-29 09:53:40 +00:00
|
|
|
return {
|
|
|
|
name: '',
|
|
|
|
url: '',
|
|
|
|
};
|
2020-02-26 16:58:25 +00:00
|
|
|
};
|
2020-03-17 12:15:19 +00:00
|
|
|
|
2020-05-29 13:08:01 +00:00
|
|
|
/**
|
2020-07-06 16:58:44 +00:00
|
|
|
* @param {object} initialValues
|
|
|
|
* @param {object} values
|
2020-05-29 13:08:01 +00:00
|
|
|
* @returns {object} Returns different values of objects
|
|
|
|
*/
|
|
|
|
export const getObjDiff = (initialValues, values) => Object.entries(values)
|
|
|
|
.reduce((acc, [key, value]) => {
|
|
|
|
if (value !== initialValues[key]) {
|
|
|
|
acc[key] = value;
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
2020-03-17 12:15:19 +00:00
|
|
|
/**
|
2020-06-05 10:28:13 +00:00
|
|
|
* @param num {number} to format
|
|
|
|
* @returns {string} Returns a string with a language-sensitive representation of this number
|
2020-03-17 12:15:19 +00:00
|
|
|
*/
|
|
|
|
export const formatNumber = (num) => {
|
|
|
|
const currentLanguage = i18n.languages[0] || DEFAULT_LANGUAGE;
|
|
|
|
return num.toLocaleString(currentLanguage);
|
|
|
|
};
|
2020-05-29 09:53:40 +00:00
|
|
|
|
2020-07-06 16:58:44 +00:00
|
|
|
/**
|
|
|
|
* @param arr {array}
|
|
|
|
* @param key {string}
|
|
|
|
* @param value {string}
|
|
|
|
* @returns {object}
|
|
|
|
*/
|
|
|
|
export const getMap = (arr, key, value) => arr.reduce((acc, curr) => {
|
|
|
|
acc[curr[key]] = curr[value];
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
2020-05-29 09:53:40 +00:00
|
|
|
/**
|
|
|
|
* @param parsedIp {object} ipaddr.js IPv4 or IPv6 object
|
2020-07-24 10:45:46 +00:00
|
|
|
* @param parsedCidr {array} ipaddr.js CIDR array
|
2020-05-29 09:53:40 +00:00
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2020-07-24 10:45:46 +00:00
|
|
|
const isIpMatchCidr = (parsedIp, parsedCidr) => {
|
2020-05-29 09:53:40 +00:00
|
|
|
try {
|
|
|
|
const cidrIpVersion = parsedCidr[0].kind();
|
|
|
|
const ipVersion = parsedIp.kind();
|
|
|
|
|
|
|
|
return ipVersion === cidrIpVersion && parsedIp.match(parsedCidr);
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-24 10:45:46 +00:00
|
|
|
/**
|
|
|
|
* The purpose of this method is to quickly check
|
|
|
|
* if this IP can possibly be in the specified CIDR range.
|
|
|
|
*
|
|
|
|
* @param ip {string}
|
|
|
|
* @param listItem {string}
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
const isIpQuickMatchCIDR = (ip, listItem) => {
|
|
|
|
const ipv6 = ip.indexOf(':') !== -1;
|
|
|
|
const cidrIpv6 = listItem.indexOf(':') !== -1;
|
|
|
|
if (ipv6 !== cidrIpv6) {
|
|
|
|
// CIDR is for a different IP type
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cidrIpv6) {
|
|
|
|
// We don't do quick check for IPv6 addresses
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const idx = listItem.indexOf('/');
|
|
|
|
if (idx === -1) {
|
|
|
|
// Not a CIDR, return false immediately
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const cidrIp = listItem.substring(0, idx);
|
|
|
|
const cidrRange = parseInt(listItem.substring(idx + 1), 10);
|
|
|
|
if (Number.isNaN(cidrRange)) {
|
|
|
|
// Not a valid CIDR
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const parts = cidrIp.split('.');
|
|
|
|
if (parts.length !== 4) {
|
|
|
|
// Invalid IP, return immediately
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now depending on the range we check if the IP can possibly be in that range
|
|
|
|
if (cidrRange < 8) {
|
|
|
|
// Use the slow approach
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cidrRange < 16) {
|
|
|
|
// Check the first part
|
|
|
|
// Example: 0.0.0.0/8 matches 0.*.*.*
|
|
|
|
return ip.indexOf(`${parts[0]}.`) === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cidrRange < 24) {
|
|
|
|
// Check the first two parts
|
|
|
|
// Example: 0.0.0.0/16 matches 0.0.*.*
|
|
|
|
return ip.indexOf(`${parts[0]}.${parts[1]}.`) === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cidrRange <= 32) {
|
|
|
|
// Check the first two parts
|
|
|
|
// Example: 0.0.0.0/16 matches 0.0.*.*
|
|
|
|
return ip.indexOf(`${parts[0]}.${parts[1]}.${parts[2]}.`) === 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// range for IPv4 CIDR cannot be more than 32
|
|
|
|
// no need to check further, this CIDR is invalid
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2020-05-29 09:53:40 +00:00
|
|
|
/**
|
|
|
|
* @param ip {string}
|
|
|
|
* @param list {string}
|
|
|
|
* @returns {'EXACT' | 'CIDR' | 'NOT_FOND'}
|
|
|
|
*/
|
|
|
|
export const getIpMatchListStatus = (ip, list) => {
|
|
|
|
if (!ip || !list) {
|
|
|
|
return IP_MATCH_LIST_STATUS.NOT_FOUND;
|
|
|
|
}
|
|
|
|
|
|
|
|
const listArr = list.trim()
|
|
|
|
.split('\n');
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (let i = 0; i < listArr.length; i += 1) {
|
|
|
|
const listItem = listArr[i];
|
|
|
|
|
2020-07-24 10:45:46 +00:00
|
|
|
if (ip === listItem.trim()) {
|
2020-05-29 09:53:40 +00:00
|
|
|
return IP_MATCH_LIST_STATUS.EXACT;
|
|
|
|
}
|
|
|
|
|
2020-07-24 10:45:46 +00:00
|
|
|
// Using ipaddr.js is quite slow so we first do a quick check
|
|
|
|
// to see if it's possible that this IP may be in the specified CIDR range
|
|
|
|
if (isIpQuickMatchCIDR(ip, listItem)) {
|
|
|
|
const parsedIp = ipaddr.parse(ip);
|
|
|
|
const isItemAnIp = ipaddr.isValid(listItem);
|
|
|
|
const parsedItem = isItemAnIp ? ipaddr.parse(listItem) : ipaddr.parseCIDR(listItem);
|
|
|
|
|
|
|
|
if (isItemAnIp && parsedIp.toString() === parsedItem.toString()) {
|
|
|
|
return IP_MATCH_LIST_STATUS.EXACT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isItemAnIp && isIpMatchCidr(parsedIp, parsedItem)) {
|
|
|
|
return IP_MATCH_LIST_STATUS.CIDR;
|
|
|
|
}
|
2020-05-29 09:53:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return IP_MATCH_LIST_STATUS.NOT_FOUND;
|
|
|
|
} catch (e) {
|
2020-07-24 10:45:46 +00:00
|
|
|
console.error(e);
|
2020-05-29 09:53:40 +00:00
|
|
|
return IP_MATCH_LIST_STATUS.NOT_FOUND;
|
|
|
|
}
|
|
|
|
};
|
2020-07-03 13:53:53 +00:00
|
|
|
|
2020-06-17 21:36:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} elapsedMs
|
|
|
|
* @param {function} t translate
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
export const formatElapsedMs = (elapsedMs, t) => {
|
|
|
|
const formattedElapsedMs = parseInt(elapsedMs, 10) || parseFloat(elapsedMs)
|
|
|
|
.toFixed(2);
|
|
|
|
return `${formattedElapsedMs} ${t('milliseconds_abbreviation')}`;
|
|
|
|
};
|
2020-07-03 08:14:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param language {string}
|
|
|
|
*/
|
|
|
|
export const setHtmlLangAttr = (language) => {
|
|
|
|
window.document.documentElement.lang = language;
|
|
|
|
};
|
2020-07-03 13:58:23 +00:00
|
|
|
|
2020-07-03 13:53:53 +00:00
|
|
|
/**
|
|
|
|
* @param values {object}
|
|
|
|
* @returns {object}
|
|
|
|
*/
|
|
|
|
export const selectCompletedFields = (values) => Object.entries(values)
|
|
|
|
.reduce((acc, [key, value]) => {
|
|
|
|
if (value || value === 0) {
|
|
|
|
acc[key] = value;
|
|
|
|
}
|
|
|
|
return acc;
|
|
|
|
}, {});
|
2020-07-13 12:23:13 +00:00
|
|
|
|
2020-07-13 13:06:56 +00:00
|
|
|
/**
|
|
|
|
* @param {string} search
|
|
|
|
* @param {string} [response_status]
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
export const getLogsUrlParams = (search, response_status) => `?${queryString.stringify({
|
|
|
|
search,
|
|
|
|
response_status,
|
|
|
|
})}`;
|
|
|
|
|
2020-08-19 15:23:05 +00:00
|
|
|
export const processContent = (
|
|
|
|
content,
|
|
|
|
) => (Array.isArray(content)
|
|
|
|
? content.filter(([, value]) => value)
|
|
|
|
.reduce((acc, val) => acc.concat(val), [])
|
|
|
|
: content);
|
2020-08-13 08:01:23 +00:00
|
|
|
/**
|
|
|
|
* @param object {object}
|
|
|
|
* @param sortKey {string}
|
|
|
|
* @returns {string[]}
|
|
|
|
*/
|
|
|
|
export const getObjectKeysSorted = (object, sortKey) => Object.entries(object)
|
|
|
|
.sort(([, { [sortKey]: order1 }], [, { [sortKey]: order2 }]) => order1 - order2)
|
|
|
|
.map(([key]) => key);
|
2020-08-13 09:16:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ip
|
|
|
|
* @returns {[IPv4|IPv6, 33|129]}
|
|
|
|
*/
|
|
|
|
const getParsedIpWithPrefixLength = (ip) => {
|
|
|
|
const MAX_PREFIX_LENGTH_V4 = 32;
|
|
|
|
const MAX_PREFIX_LENGTH_V6 = 128;
|
|
|
|
|
|
|
|
const parsedIp = ipaddr.parse(ip);
|
|
|
|
const prefixLength = parsedIp.kind() === 'ipv4' ? MAX_PREFIX_LENGTH_V4 : MAX_PREFIX_LENGTH_V6;
|
|
|
|
|
|
|
|
// Increment prefix length to always put IP after CIDR, e.g. 127.0.0.1/32, 127.0.0.1
|
|
|
|
return [parsedIp, prefixLength + 1];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for IP and CIDR comparison (supports both v4 and v6)
|
|
|
|
* @param item - ip or cidr
|
|
|
|
* @returns {number[]}
|
|
|
|
*/
|
|
|
|
const getAddressesComparisonBytes = (item) => {
|
|
|
|
// Sort ipv4 before ipv6
|
|
|
|
const IP_V4_COMPARISON_CODE = 0;
|
|
|
|
const IP_V6_COMPARISON_CODE = 1;
|
|
|
|
|
|
|
|
const [parsedIp, cidr] = ipaddr.isValid(item)
|
|
|
|
? getParsedIpWithPrefixLength(item)
|
|
|
|
: ipaddr.parseCIDR(item);
|
|
|
|
|
|
|
|
const [normalizedBytes, ipVersionComparisonCode] = parsedIp.kind() === 'ipv4'
|
|
|
|
? [parsedIp.toIPv4MappedAddress().parts, IP_V4_COMPARISON_CODE]
|
|
|
|
: [parsedIp.parts, IP_V6_COMPARISON_CODE];
|
|
|
|
|
|
|
|
return [ipVersionComparisonCode, ...normalizedBytes, cidr];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare function for IP and CIDR sort in ascending order (supports both v4 and v6)
|
|
|
|
* @param a
|
|
|
|
* @param b
|
|
|
|
* @returns {number} -1 | 0 | 1
|
|
|
|
*/
|
|
|
|
export const sortIp = (a, b) => {
|
|
|
|
try {
|
|
|
|
const comparisonBytesA = getAddressesComparisonBytes(a);
|
|
|
|
const comparisonBytesB = getAddressesComparisonBytes(b);
|
|
|
|
|
|
|
|
for (let i = 0; i < comparisonBytesA.length; i += 1) {
|
|
|
|
const byteA = comparisonBytesA[i];
|
|
|
|
const byteB = comparisonBytesB[i];
|
|
|
|
|
|
|
|
if (byteA === byteB) {
|
|
|
|
// eslint-disable-next-line no-continue
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return byteA > byteB ? 1 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
};
|
2020-08-19 15:23:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ip {string}
|
|
|
|
* @param gateway_ip {string}
|
|
|
|
* @returns {{range_end: string, subnet_mask: string, range_start: string,
|
|
|
|
* lease_duration: string, gateway_ip: string}}
|
|
|
|
*/
|
|
|
|
export const calculateDhcpPlaceholdersIpv4 = (ip, gateway_ip) => {
|
|
|
|
const LAST_OCTET_IDX = 3;
|
|
|
|
const LAST_OCTET_RANGE_START = 100;
|
|
|
|
const LAST_OCTET_RANGE_END = 200;
|
|
|
|
|
|
|
|
const addr = ipaddr.parse(ip);
|
|
|
|
addr.octets[LAST_OCTET_IDX] = LAST_OCTET_RANGE_START;
|
|
|
|
const range_start = addr.toString();
|
|
|
|
|
|
|
|
addr.octets[LAST_OCTET_IDX] = LAST_OCTET_RANGE_END;
|
|
|
|
const range_end = addr.toString();
|
|
|
|
|
|
|
|
const {
|
|
|
|
subnet_mask,
|
|
|
|
lease_duration,
|
|
|
|
} = DHCP_VALUES_PLACEHOLDERS.ipv4;
|
|
|
|
|
|
|
|
return {
|
|
|
|
gateway_ip: gateway_ip || ip,
|
|
|
|
subnet_mask,
|
|
|
|
range_start,
|
|
|
|
range_end,
|
|
|
|
lease_duration,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const calculateDhcpPlaceholdersIpv6 = () => {
|
|
|
|
const {
|
|
|
|
range_start,
|
|
|
|
range_end,
|
|
|
|
lease_duration,
|
|
|
|
} = DHCP_VALUES_PLACEHOLDERS.ipv6;
|
|
|
|
|
|
|
|
return {
|
|
|
|
range_start,
|
|
|
|
range_end,
|
|
|
|
lease_duration,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add ip_addresses property - concatenated ipv4_addresses and ipv6_addresses for every interface
|
|
|
|
* @param interfaces
|
|
|
|
* @param interfaces.ipv4_addresses {string[]}
|
|
|
|
* @param interfaces.ipv6_addresses {string[]}
|
|
|
|
* @returns interfaces Interfaces enriched with ip_addresses property
|
|
|
|
*/
|
|
|
|
export const enrichWithConcatenatedIpAddresses = (interfaces) => Object.entries(interfaces)
|
|
|
|
.reduce((acc, [k, v]) => {
|
|
|
|
const ipv4_addresses = v.ipv4_addresses ?? [];
|
|
|
|
const ipv6_addresses = v.ipv6_addresses ?? [];
|
|
|
|
|
|
|
|
acc[k].ip_addresses = ipv4_addresses.concat(ipv6_addresses);
|
|
|
|
return acc;
|
|
|
|
}, interfaces);
|