First commit
This commit is contained in:
commit
7d38e2bf78
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
hb
|
8
Makefile
Normal file
8
Makefile
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
CC=gcc
|
||||||
|
CFLAGS=-I.
|
||||||
|
|
||||||
|
hello: main.c
|
||||||
|
$(CC) -o hb main.c
|
||||||
|
|
||||||
|
install: hello
|
||||||
|
install -m 744 hb /usr/local/bin
|
16
README.md
Normal file
16
README.md
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
# HB
|
||||||
|
|
||||||
|
The Host Blocker command.
|
||||||
|
|
||||||
|
This is a system utility written in C that allows one to quickly black hole hosts in the /etc/hosts file, keeping one from resolving any DNS for those hosts and permanently blocking them on the system.
|
||||||
|
|
||||||
|
# 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.
|
||||||
|
3. Log dns requests that look at the hosts file -- To see how many go where
|
65
main.c
Normal file
65
main.c
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
const char *blockString = "0.0.0.0 ";
|
||||||
|
|
||||||
|
FILE *fopenHostsFile(int mode)
|
||||||
|
{
|
||||||
|
switch(mode)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
return fopen("/etc/hosts", "r");
|
||||||
|
case 1:
|
||||||
|
return fopen("/etc/hosts", "a");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Block a hosts using the hostsFile in FILE
|
||||||
|
*/
|
||||||
|
void blockHost(FILE *hostsFile, char *host)
|
||||||
|
{
|
||||||
|
// Buffer to hold our block rule.
|
||||||
|
char blockRule[256];
|
||||||
|
|
||||||
|
// Copy the blockstring and host to the blockrule.
|
||||||
|
strcpy(blockRule, blockString);
|
||||||
|
strcat(blockRule, host);
|
||||||
|
strcat(blockRule, "\n");
|
||||||
|
|
||||||
|
// Add a null terminator..
|
||||||
|
blockRule[strlen(blockRule)] = 0;
|
||||||
|
fputs(blockRule, hostsFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Entrypoint.
|
||||||
|
*/
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
FILE *hostsFile = NULL;
|
||||||
|
|
||||||
|
if (getuid() != 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "hb: Must run as root using sudo!\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < argc; i++)
|
||||||
|
{
|
||||||
|
if (strcmp(argv[i], "add") == 0)
|
||||||
|
{
|
||||||
|
hostsFile = fopenHostsFile(1);
|
||||||
|
blockHost(hostsFile, argv[i+1]);
|
||||||
|
}
|
||||||
|
else if (strcmp(argv[i], "delete") == 0)
|
||||||
|
{
|
||||||
|
fprintf(stdout, "Soon to be implemented!\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hostsFile != NULL)
|
||||||
|
fclose(hostsFile);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user