From cb36f1790a7b57eca22a9f1db1c6bb43769eff35 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 18 Jan 2020 07:32:50 -0500 Subject: [PATCH 1/3] More refactorings, pretty much done! --- main.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/main.c b/main.c index ae1ab9c..354a13a 100644 --- a/main.c +++ b/main.c @@ -31,8 +31,7 @@ static char *CONFIG; * @param mode whichever mode host file you want to open * @return FILE * */ -FILE *fopenHostsFile(char *mode) -{ +FILE *fopenHostsFile(char *mode) { return fopen(HOSTFILE, mode); } @@ -71,13 +70,13 @@ void modifyHostsFile(char *oldhost, char *newhost, int deleteHost) { newHostsFile[strlen(newHostsFile)] = EOF; // // Seek to 0 -// fseek(hostsFile, 0, SEEK_SET); + fseek(hostsFile, 0, SEEK_SET); fclose(hostsFile); // If this failed, write an error message to stderr -// ONFAILED(0, (fwrite(newHostsFile, sizeof(char), sizeof(newHostsFile), hostsFile))) { -// fprintf(stderr, "Did not write anything!\n"); -// } + ONFAILED(0, (fwrite(newHostsFile, sizeof(char), sizeof(newHostsFile), hostsFile))) { + fprintf(stderr, "Did not write anything!\n"); + } fprintf(stderr, "New hosts file: \n%s\n", newHostsFile); // fclose(hostsFile); } @@ -86,8 +85,7 @@ void modifyHostsFile(char *oldhost, char *newhost, int deleteHost) { /** * Block a hosts using the hostsFile in FILE */ -void blockHost(char *host) -{ +void blockHost(char *host) { FILE *hostsFile = fopenHostsFile("a"); char blockRule[256]; sprintf(blockRule, "%s %s\n", blockString, host); -- 2.17.1 From fea10c2d6e2a42d7ab2d81fc37a14b02a4c0826c Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 18 Jan 2020 07:38:32 -0500 Subject: [PATCH 2/3] Documentation --- README.md | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 4d2d3d1..7d40b16 100644 --- a/README.md +++ b/README.md @@ -6,21 +6,27 @@ This is a system utility written in C that allows one to quickly black hole host # How it Works +The program "blackholes" hosts by setting their entries in /etc/hosts to be +`0.0.0.0` so that when one tries to resolve the host in the browser the host will +resolve to `0.0.0.0`. + The program will wake up in a configurable timeframe continuously, reading a config file into memory and making appropriate changes to the /etc/hosts based on what it reads from the config. +Starting the program like this allows for the program to run as a daemon: + +```bash +sudo hb -period -config -daemon +``` + +The config file at the moment is just a newline separated list of hosts that will +be added to the /etc/hosts file. + # Motivations I would like to get better with C, and will be using C much more in the near future. In addition, I have issues with browsing to sites that hurt my productivity and am always on the command line. # TODO -1. Get delete working for my /etc/hosts editor. -2. Daemonize this program to be started up by systemd. - - As part of daemonization, remember and remove added hosts on teardown. - - Can do this with an atexit handler, and a routine in main. -3. Log dns requests that look at the hosts file -- To see how many go where -4. As part of daemonization -- only block hosts within a time limit. - - Sleep, wakeup, check if time has passed or not, block/unblock host, - sleep again. -5. Read hosts to block from a config file (to not pollute the /etc/hosts file) -6. Design a unified interface for reading a hosts file. +- Debug logging +- Systemd unit file for running as a daemon + -- 2.17.1 From 91c01dd1920b6a1e5977627d5dc290c4ba9b9205 Mon Sep 17 00:00:00 2001 From: Jacob Date: Sat, 18 Jan 2020 07:43:02 -0500 Subject: [PATCH 3/3] Wrong place to close hosts file --- main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index 354a13a..7dd0129 100644 --- a/main.c +++ b/main.c @@ -71,14 +71,14 @@ void modifyHostsFile(char *oldhost, char *newhost, int deleteHost) { // // Seek to 0 fseek(hostsFile, 0, SEEK_SET); - fclose(hostsFile); +// fclose(hostsFile); // If this failed, write an error message to stderr ONFAILED(0, (fwrite(newHostsFile, sizeof(char), sizeof(newHostsFile), hostsFile))) { fprintf(stderr, "Did not write anything!\n"); } fprintf(stderr, "New hosts file: \n%s\n", newHostsFile); -// fclose(hostsFile); + fclose(hostsFile); } -- 2.17.1