Skip to content

Commit

Permalink
Make variable names more expressive
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtemBaskal committed May 27, 2020
1 parent 3d38f21 commit 92f4c34
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 59 deletions.
59 changes: 22 additions & 37 deletions client/src/components/Dashboard/Clients.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import { Trans, withTranslation } from 'react-i18next';
import Card from '../ui/Card';
import Cell from '../ui/Cell';

import { getPercent, isClientInIpsOrCidrs } from '../../helpers/helpers';
import { BLOCKED_CLIENT, STATUS_COLORS } from '../../helpers/constants';
import { getPercent, getIpMatchListStatus } from '../../helpers/helpers';
import { IP_MATCH_LIST_STATUS, STATUS_COLORS } from '../../helpers/constants';
import { formatClientCell } from '../../helpers/formatClientCell';

const getClientsPercentColor = (percent) => {
if (percent > 50) {
return STATUS_COLORS.green;
} if (percent > 10) {
}
if (percent > 10) {
return STATUS_COLORS.yellow;
}
return STATUS_COLORS.red;
Expand All @@ -27,20 +28,18 @@ const countCell = (dnsQueries) => function cell(row) {
return <Cell value={value} percent={percent} color={percentColor} />;
};

const renderBlockingButton = (blocked, ip, handleClick, processing) => {
let buttonProps = {
className: 'btn-outline-danger',
text: 'block_btn',
type: 'block',
};

if (blocked) {
buttonProps = {
const renderBlockingButton = (ipMatchListStatus, ip, handleClick, processing) => {
const buttonProps = ipMatchListStatus === IP_MATCH_LIST_STATUS.NOT_FOUND
? {
className: 'btn-outline-danger',
text: 'block_btn',
type: 'block',
}
: {
className: 'btn-outline-secondary',
text: 'unblock_btn',
type: 'unblock',
};
}

return (
<div className="table__action">
Expand All @@ -56,25 +55,18 @@ const renderBlockingButton = (blocked, ip, handleClick, processing) => {
);
};

const isBlockedClient = (rawClients, client) => {
if (!rawClients || !client) {
return false;
}
return isClientInIpsOrCidrs(rawClients, client);
};

const clientCell = (t, toggleClientStatus, processing, disallowedClients) => function cell(row) {
const { value } = row;
const blocked = isBlockedClient(disallowedClients, value);
const ipMatchListStatus = getIpMatchListStatus(value, disallowedClients);

return (
<Fragment>
<div className="logs__row logs__row--overflow logs__row--column">
{formatClientCell(row, t)}
</div>
{blocked !== BLOCKED_CLIENT.CIDR
&& renderBlockingButton(blocked, value, toggleClientStatus, processing)}
</Fragment>
<Fragment>
<div className="logs__row logs__row--overflow logs__row--column">
{formatClientCell(row, t)}
</div>
{ipMatchListStatus !== IP_MATCH_LIST_STATUS.CIDR
&& renderBlockingButton(ipMatchListStatus, value, toggleClientStatus, processing)}
</Fragment>
);
};

Expand Down Expand Up @@ -130,15 +122,8 @@ const Clients = ({

const { ip } = rowInfo.original;

if (isBlockedClient(disallowedClients, ip)) {
return {
className: 'red',
};
}

return {
className: '',
};
return getIpMatchListStatus(ip, disallowedClients)
=== IP_MATCH_LIST_STATUS.NOT_FOUND ? {} : { className: 'red' };
}}
/>
</Card>
Expand Down
7 changes: 4 additions & 3 deletions client/src/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,8 @@ export const DNS_REQUEST_OPTIONS = {
FASTEST_ADDR: 'fastest_addr',
};

export const BLOCKED_CLIENT = {
IP: 'IP',
CIDR: 'CIDR',
export const IP_MATCH_LIST_STATUS = {
NOT_FOUND: 'NOT_FOUND', // not found in the list
EXACT: 'EXACT', // found exact match (ip === ip)
CIDR: 'CIDR', // the ip is in the specified CIDR range
};
44 changes: 25 additions & 19 deletions client/src/helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
DEFAULT_LANGUAGE,
FILTERED_STATUS,
FILTERED,
BLOCKED_CLIENT,
IP_MATCH_LIST_STATUS,
} from './constants';

/**
Expand Down Expand Up @@ -501,7 +501,7 @@ export const normalizeMultiline = (multiline) => `${normalizeTextarea(multiline)
* @param cidr {string}
* @returns {boolean}
*/
export const isIpInCidr = (ip, cidr) => {
export const isIpMatchCidr = (ip, cidr) => {
try {
const [cidrIp] = cidr.split('/');
const cidrIpVersion = ipaddr.parse(cidrIp)
Expand All @@ -519,24 +519,30 @@ export const isIpInCidr = (ip, cidr) => {
};

/**
* @param rawClients {string}
* @param currentClient {string}
* @returns {boolean | 'CIDR' | 'IP'}
* @param ip {string}
* @param list {string}
* @returns {'EXACT' | 'CIDR' | 'NOT_FOND'}
*/
export const isClientInIpsOrCidrs = (rawClients, currentClient) => rawClients.split('\n')
.reduce((isClientInList, rawClient) => {
if (isClientInList) {
return isClientInList;
}
export const getIpMatchListStatus = (ip, list) => {
if (!ip || !list) {
return IP_MATCH_LIST_STATUS.NOT_FOUND;
}

if (rawClient === currentClient) {
return BLOCKED_CLIENT.IP;
}
return list.split('\n')
.reduce((ipMatchList, listItem) => {
if (ipMatchList !== IP_MATCH_LIST_STATUS.NOT_FOUND) {
return ipMatchList;
}

if (rawClient.includes('/') && isIpInCidr(currentClient, rawClient)) {
return BLOCKED_CLIENT.CIDR;
}
if (listItem === ip) {
return IP_MATCH_LIST_STATUS.EXACT;
}

return false;
},
false);
if (listItem.includes('/') && isIpMatchCidr(ip, listItem)) {
return IP_MATCH_LIST_STATUS.CIDR;
}

return IP_MATCH_LIST_STATUS.NOT_FOUND;
},
IP_MATCH_LIST_STATUS.NOT_FOUND);
};

0 comments on commit 92f4c34

Please sign in to comment.