2020-06-18 19:53:02 +00:00
|
|
|
import React from 'react';
|
2018-08-30 14:25:33 +00:00
|
|
|
import ReactTable from 'react-table';
|
|
|
|
|
import PropTypes from 'prop-types';
|
2020-09-01 13:30:30 +00:00
|
|
|
import { Trans, useTranslation } from 'react-i18next';
|
2018-08-30 14:25:33 +00:00
|
|
|
|
2020-09-01 13:30:30 +00:00
|
|
|
import { shallowEqual, useDispatch, useSelector } from 'react-redux';
|
|
|
|
|
import classNames from 'classnames';
|
2018-08-30 14:25:33 +00:00
|
|
|
import Card from '../ui/Card';
|
2018-10-12 13:02:01 +00:00
|
|
|
import Cell from '../ui/Cell';
|
2018-08-30 14:25:33 +00:00
|
|
|
|
2020-09-24 12:48:37 +00:00
|
|
|
import { getPercent, sortIp } from '../../helpers/helpers';
|
2021-06-29 12:53:28 +00:00
|
|
|
import { BLOCK_ACTIONS, STATUS_COLORS } from '../../helpers/constants';
|
2020-09-01 13:30:30 +00:00
|
|
|
import { toggleClientBlock } from '../../actions/access';
|
|
|
|
|
import { renderFormattedClientCell } from '../../helpers/renderFormattedClientCell';
|
2020-09-24 12:48:37 +00:00
|
|
|
import { getStats } from '../../actions/stats';
|
2018-10-12 13:02:01 +00:00
|
|
|
|
2019-08-27 13:43:58 +00:00
|
|
|
const getClientsPercentColor = (percent) => {
|
|
|
|
|
if (percent > 50) {
|
|
|
|
|
return STATUS_COLORS.green;
|
2020-05-29 09:53:40 +00:00
|
|
|
}
|
|
|
|
|
if (percent > 10) {
|
2019-08-27 13:43:58 +00:00
|
|
|
return STATUS_COLORS.yellow;
|
|
|
|
|
}
|
|
|
|
|
return STATUS_COLORS.red;
|
|
|
|
|
};
|
2018-10-12 13:02:01 +00:00
|
|
|
|
2020-09-01 13:30:30 +00:00
|
|
|
const CountCell = (row) => {
|
|
|
|
|
const { value, original: { ip } } = row;
|
|
|
|
|
const numDnsQueries = useSelector((state) => state.stats.numDnsQueries, shallowEqual);
|
|
|
|
|
|
|
|
|
|
const percent = getPercent(numDnsQueries, value);
|
2020-05-22 14:06:05 +00:00
|
|
|
const percentColor = getClientsPercentColor(percent);
|
2019-03-20 14:04:32 +00:00
|
|
|
|
2020-09-01 13:30:30 +00:00
|
|
|
return <Cell value={value} percent={percent} color={percentColor} search={ip} />;
|
2020-05-22 14:06:05 +00:00
|
|
|
};
|
2018-10-12 13:02:01 +00:00
|
|
|
|
2020-09-24 12:48:37 +00:00
|
|
|
const renderBlockingButton = (ip, disallowed, disallowed_rule) => {
|
2020-09-01 13:30:30 +00:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const processingSet = useSelector((state) => state.access.processingSet);
|
|
|
|
|
|
2020-09-05 07:22:47 +00:00
|
|
|
const buttonClass = classNames('button-action button-action--main', {
|
2020-09-24 12:48:37 +00:00
|
|
|
'button-action--unblock': disallowed,
|
2020-09-01 13:30:30 +00:00
|
|
|
});
|
|
|
|
|
|
2020-09-24 12:48:37 +00:00
|
|
|
const toggleClientStatus = async (ip, disallowed, disallowed_rule) => {
|
|
|
|
|
const confirmMessage = disallowed
|
|
|
|
|
? t('client_confirm_unblock', { ip: disallowed_rule })
|
|
|
|
|
: `${t('adg_will_drop_dns_queries')} ${t('client_confirm_block', { ip })}`;
|
2020-09-01 13:30:30 +00:00
|
|
|
|
2020-09-05 07:22:47 +00:00
|
|
|
if (window.confirm(confirmMessage)) {
|
2020-09-24 12:48:37 +00:00
|
|
|
await dispatch(toggleClientBlock(ip, disallowed, disallowed_rule));
|
|
|
|
|
await dispatch(getStats());
|
2020-09-01 13:30:30 +00:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2020-09-24 12:48:37 +00:00
|
|
|
const onClick = () => toggleClientStatus(ip, disallowed, disallowed_rule);
|
|
|
|
|
|
|
|
|
|
const text = disallowed ? BLOCK_ACTIONS.UNBLOCK : BLOCK_ACTIONS.BLOCK;
|
2020-09-01 13:30:30 +00:00
|
|
|
|
2020-09-24 12:48:37 +00:00
|
|
|
const isNotInAllowedList = disallowed && disallowed_rule === '';
|
2021-01-27 15:32:13 +00:00
|
|
|
return (
|
|
|
|
|
<div className="table__action pl-4">
|
|
|
|
|
<button
|
2020-09-24 12:48:37 +00:00
|
|
|
type="button"
|
|
|
|
|
className={buttonClass}
|
|
|
|
|
onClick={isNotInAllowedList ? undefined : onClick}
|
|
|
|
|
disabled={isNotInAllowedList || processingSet}
|
|
|
|
|
title={t(isNotInAllowedList ? 'client_not_in_allowed_clients' : text)}
|
2021-01-27 15:32:13 +00:00
|
|
|
>
|
|
|
|
|
<Trans>{text}</Trans>
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2020-01-30 10:58:54 +00:00
|
|
|
};
|
|
|
|
|
|
2020-09-01 13:30:30 +00:00
|
|
|
const ClientCell = (row) => {
|
2020-09-24 12:48:37 +00:00
|
|
|
const { value, original: { info, info: { disallowed, disallowed_rule } } } = row;
|
2020-09-01 13:30:30 +00:00
|
|
|
|
|
|
|
|
return <>
|
2020-09-05 07:22:47 +00:00
|
|
|
<div className="logs__row logs__row--overflow logs__row--column d-flex align-items-center">
|
2020-09-01 13:30:30 +00:00
|
|
|
{renderFormattedClientCell(value, info, true)}
|
2020-09-24 12:48:37 +00:00
|
|
|
{renderBlockingButton(value, disallowed, disallowed_rule)}
|
2020-09-01 13:30:30 +00:00
|
|
|
</div>
|
|
|
|
|
</>;
|
2020-05-22 14:06:05 +00:00
|
|
|
};
|
2019-08-22 13:10:47 +00:00
|
|
|
|
2019-08-27 13:43:58 +00:00
|
|
|
const Clients = ({
|
2020-01-30 10:58:54 +00:00
|
|
|
refreshButton,
|
|
|
|
|
subtitle,
|
2020-09-01 13:30:30 +00:00
|
|
|
}) => {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const topClients = useSelector((state) => state.stats.topClients, shallowEqual);
|
|
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
return (
|
|
|
|
|
<Card
|
2020-09-01 13:30:30 +00:00
|
|
|
title={t('top_clients')}
|
|
|
|
|
subtitle={subtitle}
|
|
|
|
|
bodyType="card-table"
|
|
|
|
|
refresh={refreshButton}
|
2021-01-27 15:32:13 +00:00
|
|
|
>
|
|
|
|
|
<ReactTable
|
2020-09-01 13:30:30 +00:00
|
|
|
data={topClients.map(({
|
|
|
|
|
name: ip, count, info, blocked,
|
|
|
|
|
}) => ({
|
|
|
|
|
ip,
|
|
|
|
|
count,
|
|
|
|
|
info,
|
|
|
|
|
blocked,
|
|
|
|
|
}))}
|
|
|
|
|
columns={[
|
|
|
|
|
{
|
2021-01-27 15:32:13 +00:00
|
|
|
Header: <Trans>client_table_header</Trans>,
|
2020-09-01 13:30:30 +00:00
|
|
|
accessor: 'ip',
|
|
|
|
|
sortMethod: sortIp,
|
|
|
|
|
Cell: ClientCell,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Header: <Trans>requests_count</Trans>,
|
|
|
|
|
accessor: 'count',
|
|
|
|
|
minWidth: 180,
|
|
|
|
|
maxWidth: 200,
|
|
|
|
|
Cell: CountCell,
|
|
|
|
|
},
|
|
|
|
|
]}
|
|
|
|
|
showPagination={false}
|
|
|
|
|
noDataText={t('no_clients_found')}
|
|
|
|
|
minRows={6}
|
|
|
|
|
defaultPageSize={100}
|
|
|
|
|
className="-highlight card-table-overflow--limited clients__table"
|
|
|
|
|
getTrProps={(_state, rowInfo) => {
|
|
|
|
|
if (!rowInfo) {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-24 12:48:37 +00:00
|
|
|
const { info: { disallowed } } = rowInfo.original;
|
2020-09-01 13:30:30 +00:00
|
|
|
|
2020-09-24 12:48:37 +00:00
|
|
|
return disallowed ? { className: 'logs__row--red' } : {};
|
2020-09-01 13:30:30 +00:00
|
|
|
}}
|
2021-01-27 15:32:13 +00:00
|
|
|
/>
|
|
|
|
|
</Card>
|
|
|
|
|
);
|
2020-09-01 13:30:30 +00:00
|
|
|
};
|
2018-08-30 14:25:33 +00:00
|
|
|
|
|
|
|
|
Clients.propTypes = {
|
2018-10-12 13:02:01 +00:00
|
|
|
refreshButton: PropTypes.node.isRequired,
|
2019-08-22 13:10:47 +00:00
|
|
|
subtitle: PropTypes.string.isRequired,
|
2018-08-30 14:25:33 +00:00
|
|
|
};
|
|
|
|
|
|
2020-09-01 13:30:30 +00:00
|
|
|
export default Clients;
|