Skip to content

Commit

Permalink
feat: add whitelist time & password support
Browse files Browse the repository at this point in the history
  • Loading branch information
johackim committed Apr 6, 2024
1 parent 248b2b2 commit ba0d820
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
16 changes: 15 additions & 1 deletion src/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ export const unblockCmd = (name) => {
};

export const whitelistCmd = (name) => {
const time = getParam('--time') || getParam('-t');
const password = getParam('--password') || getParam('-p');
const distraction = { name, time };

if (isValidPassword(password)) {
disableShieldMode(password);
console.log('Shield mode disabled.');
whitelistDistraction(distraction);
console.log(`Whitelisting ${name}`);
enableShieldMode(password);
console.log('Shield mode enabled.');
return;
}

if (config?.shield) {
console.log('You must disable the shield mode first.');
return;
Expand All @@ -70,7 +84,7 @@ export const whitelistCmd = (name) => {
return;
}

whitelistDistraction({ name });
whitelistDistraction(distraction);
console.log(`Whitelisting ${name}`);
};

Expand Down
8 changes: 5 additions & 3 deletions src/daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@ const handleAppBlocking = () => {
};

const handleTimeout = () => {
const blocklist = config?.blocklist.filter(({ timeout }) => {
const list = [...config.blocklist, ...config.whitelist];

const listWithoutTimeout = list.filter(({ timeout }) => {
if (!timeout) return true;
return timeout >= Math.floor(Date.now() / 1000);
});

if (blocklist?.length === config?.blocklist.length) return;
if (listWithoutTimeout.length === list.length) return;

editConfig({ ...config, blocklist });
editConfig(config);
};

const cleanUpAndExit = () => {
Expand Down
10 changes: 10 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ export const editConfig = ({ blocklist = [], whitelist = [], shield, password, p
config.whitelist = removeDuplicates(config.shield ? config.whitelist : whitelist);
config.blocklist = removeDuplicates(config.shield ? [...config.blocklist, ...blocklist] : blocklist);
config.blocklist = config.blocklist.filter(({ timeout }) => !timeout || timeout >= Math.floor(Date.now() / 1000));
config.whitelist = config.whitelist.filter(({ timeout }) => !timeout || timeout >= Math.floor(Date.now() / 1000));

if (isValidPassword(password)) {
unblockRoot();
Expand Down Expand Up @@ -191,13 +192,21 @@ export const unblockDistraction = (distraction) => {

export const whitelistDistraction = (distraction) => {
config.whitelist.push(distraction);
config.whitelist = config.whitelist.map((d) => {
if (getTimeType(d.time) === 'duration') {
return { ...d, timeout: createTimeout(d.time) };
}

return d;
});
sendDataToSocket(config);
};

export const getRootDomain = (domain) => domain.split('.').slice(-2).join('.');

export const isDistractionWhitelisted = (distraction) => {
if (config.whitelist.some((d) => d.name === distraction)) return true;
if (config.whitelist.some((d) => d.name === '*')) return true;
if (config.whitelist.some((d) => d.name === `*.${getRootDomain(distraction)}`)) return true;

return false;
Expand Down Expand Up @@ -241,6 +250,7 @@ export const sendNotification = (title, message) => {
export const getRunningBlockedApps = () => {
const blockedApps = config.blocklist
.filter(({ name }) => !isValidDomain(name) && !name.includes('*'))
.filter(({ name }) => !isDistractionWhitelisted(name))
.filter(({ time }) => isWithinTimeRange(time))
.map(({ name }) => name);

Expand Down

0 comments on commit ba0d820

Please sign in to comment.