From 4996513636ca2cdd16537305bd80fd8fc876c7d6 Mon Sep 17 00:00:00 2001 From: Justin Reichardt Date: Tue, 24 Aug 2021 10:26:16 -0500 Subject: [PATCH] Changed project to rhosts rhosts is the new c implementation of the project. It is suppose to be compilable for GNU/Linux and Windows minimum. --- Makefile | 55 ++++++++++------ Readme.md | 6 +- src/removeadhosts.path | 10 --- src/removeadhosts.sh | 65 ------------------- src/systemd/rhosts.path | 9 +++ .../rhosts.service} | 2 +- .../rhosts.timer} | 0 7 files changed, 46 insertions(+), 101 deletions(-) delete mode 100644 src/removeadhosts.path delete mode 100755 src/removeadhosts.sh create mode 100644 src/systemd/rhosts.path rename src/{removeadhosts.service => systemd/rhosts.service} (76%) rename src/{removeadhosts.timer => systemd/rhosts.timer} (100%) diff --git a/Makefile b/Makefile index 412a589..d0fcad0 100644 --- a/Makefile +++ b/Makefile @@ -1,29 +1,42 @@ dir: - if [ ! -d /usr/local/share/removeadhosts ];then mkdir -p /usr/local/share/removeadhosts;fi - if [ ! -d /etc/removeadhosts ];then mkdir /etc/removeadhosts;fi -install: dir - touch /etc/removeadhosts/{adlist.txt,adlistings.txt - cp src/* /usr/local/share/removeadhosts/ - chown -R root:root /usr/local/share/removeadhosts - chmod +x /usr/local/share/removeadhosts/removeadhosts.sh - ln -s /usr/local/share/removeadhosts/removeadhosts.service /etc/systemd/system/ - ln -s /usr/local/share/removeadhosts/removeadhosts.timer /etc/systemd/system/ - ln -s /usr/local/share/removeadhosts/removeadhosts.path /etc/systemd/system/ + if [ ! -d /usr/local/share/rhosts ];then mkdir -p /usr/local/share/rhosts;else echo "/usr/local/share/rhosts already exists";fi + if [ ! -d /etc/rhosts ];then mkdir /etc/rhosts;else echo "/etc/rhosts already exists";fi + if [ ! -d build ];then mkdir build;else echo "build already exists";fi +build: dir + gcc src/rhosts.c -o build/rhosts +clean: + - rm -rf build +install: build + if [ ! -e /etc/rhosts/rhosts.cfg ];then touch /etc/rhosts/rhosts.cfg;else echo "/etc/rhosts/rhosts.cfg exists";fi + cp -r src/systemd /usr/local/share/rhosts/ + cp build/rhosts /usr/local/bin/ + chown -R root:root /usr/local/bin/rhosts + chmod +x /usr/local/bin/rhosts + if [ ! -e /etc/systemd/system/rhosts.service ];then \ + ln -s /usr/local/share/rhosts/systemd/rhosts.service /etc/systemd/system/;else \ + echo "/etc/systemd/system/rhosts.service already exist";fi + if [ ! -e /etc/systemd/system/rhosts.timer ];then \ + ln -s /usr/local/share/rhosts/systemd/rhosts.timer /etc/systemd/system/;else \ + echo "/etc/systemd/system/rhosts.timer already exist";fi + if [ ! -e /etc/systemd/system/rhosts.path ];then \ + ln -s /usr/local/share/rhosts/systemd/rhosts.path /etc/systemd/system/;else \ + echo "/etc/systemd/system/rhosts.path already exist";fi systemctl daemon-reload activate: install - systemctl enable removeadhosts.timer - systemctl start removeadhosts.timer - systemctl enable removeadhosts.path - systemctl start removeadhosts.path + systemctl enable rhosts.timer + systemctl start rhosts.timer + systemctl enable rhosts.path + systemctl start rhosts.path deactivate: - systemctl disable removeadhosts.timer - systemctl stop removeadhosts.timer - systemctl disable removeadhosts.path - systemctl stop removeadhosts.path + - systemctl disable rhosts.timer + - systemctl stop rhosts.timer + - systemctl disable rhosts.path + - systemctl stop rhosts.path remove: deactivate - rm -f /etc/systemd/system/removeadhosts* - rm -fr /usr/local/share/removeadhosts/ + rm -f /etc/systemd/system/rhosts* + rm -fr /usr/local/share/rhosts/ + rm -f /usr/local/bin/rhosts purge: remove - rm -fr /etc/removeadhosts + rm -fr /etc/rhosts reinstall: remove activate diff --git a/Readme.md b/Readme.md index 50274bb..6378439 100644 --- a/Readme.md +++ b/Readme.md @@ -1,13 +1,11 @@ -# Removeadhosts +# rhosts -This reroutes sites to 0.0.0.0 in order to block them from being reached by adding them automatically to the hosts file. This was designed off of how Pihole works to block ads but this functions at a local level instead of the entire network. +This reroutes sites to 0.0.0.0 in order to block them from being reached by adding them automatically to the hosts file. ## Requirements to install and run - make -- curl - ## Install sudo make install diff --git a/src/removeadhosts.path b/src/removeadhosts.path deleted file mode 100644 index 1de17ac..0000000 --- a/src/removeadhosts.path +++ /dev/null @@ -1,10 +0,0 @@ -[Unit] -Description="Monitor the /etc/passwd file for changes" - -[Path] -PathModified=/etc/removeadhosts/adlist.txt -PathModified=/etc/removeadhosts/adlistings.txt -Unit=removeadhosts.service - -[Install] -WantedBy=multi-user.target diff --git a/src/removeadhosts.sh b/src/removeadhosts.sh deleted file mode 100755 index abb4e8b..0000000 --- a/src/removeadhosts.sh +++ /dev/null @@ -1,65 +0,0 @@ -#!/bin/bash - -# Create temporary files to hold the old configs -sed -n '1,/# start of removeadhosts/p' /etc/hosts > /tmp/removeadhosts-head -sed -n '/# start of removeadhosts/,/# Custom ad list/p' /etc/hosts | sed -e '/# Custom ad list/d' -e '1d' > /tmp/removeadhosts-curl - -# Copy back all the custom entries -cat /tmp/removeadhosts-head | tee /etc/hosts >/dev/null -rm /tmp/removeadhosts-head -echo "Appended old hosts" -if grep -qinE '# start of removeadhosts' /etc/hosts -then - echo "This has been run before" -else - echo "First Run" - echo '# start of removeadhosts' >> /etc/hosts -fi - - -# Download entries from the listings list -echo 'Downloading ad list' -if [ -e /etc/removeadhosts/adlistings.txt ] -then - cat /etc/removeadhosts/adlistings.txt | \ - while read SITE; do - ESC_SITE=$(printf '%s\n' "$SITE" | sed -e 's/[\/&]/\\&/g') - echo "# removeadhosts site - $SITE" >> /tmp/removeadhosts-curlbuff - RC=0 ; curl $SITE >> /tmp/removeadhosts-curlbuff 2>/dev/null || RC=$? - echo "# removeadhosts site - $SITE - end" >> /tmp/removeadhosts-curlbuff - if [ $(cat /tmp/removeadhosts-curlbuff | wc -l) -lt 3 ] || [ ! $RC -eq 0 ] - then - if [ $(sed -n "/removeadhosts site - $ESC_SITE/,/removeadhosts site - $ESC_SITE - end/p" /tmp/removeadhosts-curl | wc -l) -gt 2 ] - then - echo "Keeping old version of $SITE" - if [ $(cat /tmp/removeadhosts-curlbuff | wc -l) -eq 2 ] - then - echo "Nothing was downloaded" - else - echo "New version is $(cat /tmp/removeadhosts-curlbuff | wc -l) lines long" - fi - sed -n "/removeadhosts site - $ESC_SITE/,/removeadhosts site - $EXC_SITE - end/p" /tmp/removeadhosts-curl | tee -a /etc/hosts > /dev/null - else - echo "Unable to add $SITE" - - fi - else - echo "Updating $(cat /tmp/removeadhosts-curlbuff | wc -l) lines from $SITE" - cat /tmp/removeadhosts-curlbuff | tee -a /etc/hosts >/dev/null - fi - done -fi -rm /tmp/removeadhosts-curl -rm /tmp/removeadhosts-curlbuff - - -# Add entries from adlist -echo 'Adding custom items from /etc/removeadhosts' -if [ -e /etc/removeadhosts/adlist.txt ] -then - echo "# Custom ad list" >> /etc/hosts - cat /etc/removeadhosts/adlist.txt | \ - while read CMD; do - echo "0.0.0.0 $CMD" >> /etc/hosts - done -fi diff --git a/src/systemd/rhosts.path b/src/systemd/rhosts.path new file mode 100644 index 0000000..f76c9c8 --- /dev/null +++ b/src/systemd/rhosts.path @@ -0,0 +1,9 @@ +[Unit] +Description="Monitor the /etc/passwd file for changes" + +[Path] +PathModified=/etc/rhosts.cfg +Unit=rhosts.service + +[Install] +WantedBy=multi-user.target diff --git a/src/removeadhosts.service b/src/systemd/rhosts.service similarity index 76% rename from src/removeadhosts.service rename to src/systemd/rhosts.service index b02cca0..50794f8 100644 --- a/src/removeadhosts.service +++ b/src/systemd/rhosts.service @@ -5,7 +5,7 @@ After=network.target network-online.target [Service] Type=oneshot -ExecStart=/usr/local/share/removeadhosts/removeadhosts.sh +ExecStart=/usr/local/bin/rhosts [Install] WantedBy=multi-user.target diff --git a/src/removeadhosts.timer b/src/systemd/rhosts.timer similarity index 100% rename from src/removeadhosts.timer rename to src/systemd/rhosts.timer