Skip to content

Commit

Permalink
refactor: improve blocking apps codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
johackim committed May 3, 2024
1 parent 0cb2c2d commit 031703a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
26 changes: 26 additions & 0 deletions __tests__/block.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { config, readConfig } from '../src/config';
import {
getBlockedApps,
blockDistraction,
isWithinTimeRange,
unblockDistraction,
isValidDistraction,
isDistractionBlocked,
getRunningBlockedApps,
} from '../src/block';

beforeEach(() => {
Expand Down Expand Up @@ -135,3 +137,27 @@ test('Should update date when blocking a distraction', async () => {
const date = new Date(readConfig().date).getTime();
expect(date).toBeGreaterThanOrEqual(currentDate);
});

test('Should get all blocked apps', async () => {
const currentDate = new Date('2021-01-01T22:00:00Z');
jest.spyOn(global, 'Date').mockImplementation(() => currentDate);
config.whitelist = [{ name: 'chromium' }];
config.blocklist = [
{ name: 'node' },
{ name: 'chromium' },
{ name: 'firefox', time: '0h-20h' },
{ name: 'example.com' },
];

const blockedApps = getBlockedApps();

expect(blockedApps).toEqual(['node']);
});

test('Should get running blocked apps', () => {
config.blocklist = [{ name: 'node' }, { name: 'firefox' }];

const runningBlockedApps = getRunningBlockedApps();

expect(runningBlockedApps).toContainEqual({ name: 'node', pid: expect.any(Number) });
});
21 changes: 20 additions & 1 deletion src/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs';
import { config, editConfig } from './config';
import { isDistractionWhitelisted } from './whitelist';
import { DOMAIN_REGEX } from './constants';
import { removeDuplicates, getRootDomain, getTimeType, createTimeout } from './utils';
import { removeDuplicates, getRootDomain, getRunningApps, getTimeType, createTimeout } from './utils';

export const blockDistraction = async (distraction) => {
config.blocklist = removeDuplicates([...config.blocklist, distraction]);
Expand Down Expand Up @@ -76,3 +76,22 @@ export const isValidDistraction = (distraction) => {

return isValidDomain(name) || isValidApp(name);
};

export const getBlockedApps = () => {
const { blocklist } = config;

return blocklist
.filter(({ name }) => !isValidDomain(name) && !name.includes('*'))
.filter(({ name }) => !isDistractionWhitelisted(name))
.filter(({ time }) => isWithinTimeRange(time))
.map(({ name }) => name);
};

export const getRunningBlockedApps = () => {
const runningApps = getRunningApps();
const blockedApps = getBlockedApps();

return runningApps
.filter((a) => blockedApps?.includes(a.cmd) || blockedApps?.includes(a.bin) || blockedApps?.includes(a.name))
.map(({ pid, cmd, name }) => ({ pid: Number(pid), name: blockedApps.find((b) => b === cmd || b === name) }));
};
23 changes: 4 additions & 19 deletions src/daemon.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
import fs from 'fs';
import { execSync } from 'child_process';
import { config, editConfig } from './config';
import { isDistractionWhitelisted } from './whitelist';
import { isWithinTimeRange, isValidDomain } from './block';
import { getRunningBlockedApps } from './block';
import { isSudo, sendNotification } from './utils';
import { DNS_SERVER, RESOLV_CONF_PATH } from './constants';
import { isSudo, sendNotification, getRunningApps } from './utils';

export const getRunningBlockedApps = () => {
const blockedApps = config.blocklist
.filter(({ name }) => !isValidDomain(name) && !name.includes('*'))
.filter(({ name }) => !isDistractionWhitelisted(name))
.filter(({ time }) => isWithinTimeRange(time))
.map(({ name }) => name);

const runningBlockedApps = getRunningApps()
.filter((a) => blockedApps?.includes(a.cmd) || blockedApps?.includes(a.bin) || blockedApps?.includes(a.name))
.map((p) => ({ ...p, name: blockedApps.find((b) => b === p.cmd || b === p.name) }));

return runningBlockedApps || [];
};

export const updateResolvConf = (dnsServer = DNS_SERVER) => {
execSync(`chattr -i ${RESOLV_CONF_PATH}`);
Expand All @@ -27,9 +12,9 @@ export const updateResolvConf = (dnsServer = DNS_SERVER) => {
};

export const handleAppBlocking = () => {
const blockedApps = getRunningBlockedApps();
const runningBlockedApps = getRunningBlockedApps();

for (const app of blockedApps) {
for (const app of runningBlockedApps) {
try {
execSync(`kill -9 ${app.pid} > /dev/null 2>&1`);
console.log(`Blocking ${app.name}`);
Expand Down

0 comments on commit 031703a

Please sign in to comment.