From 4fcfd27b087434d8e6976310744f25eab530253f Mon Sep 17 00:00:00 2001 From: Tristan Date: Thu, 13 Feb 2020 03:54:58 -0500 Subject: [PATCH] treehouses blocker (fixes #657) (fixes #157) (#658) Co-authored-by: dogi --- .codeclimate.yml | 1 + .travis.yml | 3 + README.md | 1 + _treehouses | 8 ++- cli.sh | 5 ++ modules/blocker.sh | 119 +++++++++++++++++++++++++++++++++++ modules/config.sh | 1 + modules/help.sh | 1 + package.json | 2 +- templates/blocker/blocker.sh | 11 ++++ 10 files changed, 149 insertions(+), 3 deletions(-) create mode 100644 modules/blocker.sh create mode 100755 templates/blocker/blocker.sh diff --git a/.codeclimate.yml b/.codeclimate.yml index 0b4c34de2..a3d8dc1bf 100644 --- a/.codeclimate.yml +++ b/.codeclimate.yml @@ -1,6 +1,7 @@ engines: shellcheck: enabled: true + shell: bash checks: SC1090: enabled: false diff --git a/.travis.yml b/.travis.yml index 91a960bc8..e0808f3d4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,9 @@ install: - auto-changelog - ls -al - cat CHANGELOG.md +script: +- ./templates/blocker/blocker.sh +- ls -al ./templates/blocker/ deploy: provider: npm edge: true diff --git a/README.md b/README.md index c2a407d8a..bb4d3b5a0 100644 --- a/README.md +++ b/README.md @@ -95,4 +95,5 @@ cron [list|add|delete|deleteall] adds, deletes a custom cron job or del usb [on|off] turns usb ports on or off remote [status|upgrade|services] helps with treehouses remote android app log <0|1|2|3|4|show|max> gets/sets log level and shows log +blocker <0|1|2|3|4||max> website blocking levels using /etc/hosts ``` diff --git a/_treehouses b/_treehouses index 91fc9f8f2..ebd963209 100644 --- a/_treehouses +++ b/_treehouses @@ -5,7 +5,7 @@ _treehouses_complete() local cur prev # Valid top-level completions - commands="ap aphidden apchannel bluetooth bluetoothid bootoption bridge burn button camera clone \ + commands="ap aphidden apchannel blocker bluetooth bluetoothid bootoption bridge burn button camera clone \ container coralenv cron default detect detectrpi discover ethernet expandfs \ feedback help image internet led locale log memory networkmode ntp openvpn password \ rebootneeded reboots rename restore rtc services speedtest ssh sshkey sshtunnel \ @@ -13,6 +13,7 @@ _treehouses_complete() ap_cmdas="local internet" aphidden_cmds="local internet" apchannel_cmds="1 2 3 4 5 6 7 8 9 10 11" + blocker_cmds="0 1 2 3 4 max" bluetooth_cmds="on off pause mac id" bootoption_cmds="console desktop" bootoption_second_cmds="autologin" @@ -33,7 +34,7 @@ _treehouses_complete() rtc_on_cmds="ds3231 rasclock" services_cmds="available installed running ports" cron_cmds="list 0W tor timestamp add delete deleteall" - sshkey_cmds="add list delete deleteal addgithubusername deletegithubusername addgithubgroup" + sshkey_cmds="add list delete deleteall addgithubusername deletegithubusername addgithubgroup" ssh_cmds="on off" sshtunnel_cmds="add remove list key notice" temperature_cmds="celsius fahrenheit" @@ -64,6 +65,9 @@ _treehouses_complete() "apchannel") COMPREPLY=( $(compgen -W "$apchannel_cmds" -- $cur) ) #cannot ascending order ;; + "blocker") + COMPREPLY=( $(compgen -W "$blocker_cmds" -- $cur) ) + ;; "bluetooth") COMPREPLY=( $(compgen -W "$bluetooth_cmds" -- $cur) ) ;; diff --git a/cli.sh b/cli.sh index 4fce50df4..9b7755258 100755 --- a/cli.sh +++ b/cli.sh @@ -62,6 +62,7 @@ source "$SCRIPTFOLDER/modules/discover.sh" source "$SCRIPTFOLDER/modules/camera.sh" source "$SCRIPTFOLDER/modules/usb.sh" source "$SCRIPTFOLDER/modules/remote.sh" +source "$SCRIPTFOLDER/modules/blocker.sh" case $1 in expandfs) @@ -308,6 +309,10 @@ case $1 in checkroot log "$2" "$3" ;; + blocker) + checkroot + blocker "$2" + ;; help) help "$2" ;; diff --git a/modules/blocker.sh b/modules/blocker.sh new file mode 100644 index 000000000..471cd5bbd --- /dev/null +++ b/modules/blocker.sh @@ -0,0 +1,119 @@ +function apply_blocker { + local file_nh folder_nh hostn + if [ ! -f $TEMPLATES/blocker/1_hosts ]; then + ($TEMPLATES/blocker/blocker.sh) + fi + folder_nh="$TEMPLATES/blocker" + case "$BLOCKER" in + "1") + file_nh=$(<$folder_nh/1_hosts) + ;; + "2") + file_nh=$(<$folder_nh/2_hosts) + ;; + "3") + file_nh=$(<$folder_nh/3_hosts) + ;; + "4") + file_nh=$(<$folder_nh/4_hosts) + ;; + "max") + file_nh=$(<$folder_nh/5_hosts) + ;; + esac + hostn=$(hostname) + cp "$TEMPLATES/hosts" "$TEMPLATES/hosts1" + sed -i "s/HOSTNAME/$hostn/g" "$TEMPLATES/hosts1" + sync;sync;sync; + cp "$TEMPLATES/hosts1" /etc/hosts + rm -f "$TEMPLATES/hosts1" + if [ ! $BLOCKER = "0" ]; then + echo "$file_nh" >> /etc/hosts + fi + sync;sync;sync; + logit "$1" +} + +function blocker { + case "$1" in + "") + case "$BLOCKER" in + "0") + logit "blocker 0: blocker is disabled" + ;; + "1") + logit "blocker 1: level is set to ads (adware + malware)" + ;; + "2") + logit "blocker 2: level is set to ads + porn" + ;; + "3") + logit "blocker 3: level is set to ads + gambling + porn" + ;; + "4") + logit "blocker 4: level is set to ads + fakenews + gambling + porn" + ;; + "max") + logit "blocker X: level is set to ads + fakenews + gambling + porn + social" + ;; + esac + exit 0; + ;; + "0") + BLOCKER=0 + apply_blocker "blocker 0: blocker disabled" + ;; + "1") + BLOCKER=1 + apply_blocker "blocker 1: level set to ads (adware + malware)" + ;; + "2") + BLOCKER=2 + apply_blocker "blocker 2: level set to ads + porn" + ;; + "3") + BLOCKER=3 + apply_blocker "blocker 3: level set to ads + gambling + porn" + ;; + "4") + BLOCKER=4 + apply_blocker "blocker 4: level set to ads + fakenews + gambling + porn" + ;; + "max") + BLOCKER=max + apply_blocker "blocker X: level set to ads + fakenews + gambling + porn + social" + ;; + *) + log_and_exit1 "Error: only '0' '1' '2' '3' '4' 'max' options are supported" + ;; + esac + conf_var_update "BLOCKER" "$BLOCKER" +} + +function blocker_help { + echo + echo "Usage: $BASENAME blocker <0|1|2|3|4|max>" + echo + echo "Example:" + echo " $BASENAME blocker" + echo " blocker 0: blocker is disabled" + echo + echo " $BASENAME blocker 0" + echo " blocker 0: blocker disabled" + echo + echo " $BASENAME blocker 1" + echo " blocker 1: level set to ads (adware + malware)" + echo + echo " $BASENAME blocker 2" + echo " blocker 2: level set to ads + porn" + echo + echo " $BASENAME blocker 3" + echo " blocker 3: level set to ads + gambling + porn" + echo + echo " $BASENAME blocker 4" + echo " blocker 4: level set to ads + fakenews + gambling + porn" + echo + echo " $BASENAME blocker max" + echo " blocker X: level set to ads + fakenews + gambling + porn + social" + echo +} diff --git a/modules/config.sh b/modules/config.sh index 5abde9b93..3580c6d6a 100644 --- a/modules/config.sh +++ b/modules/config.sh @@ -9,6 +9,7 @@ NC='\033[0m' # global config variables (defaults) LOGFILE=/dev/null LOG=0 +BLOCKER=0 if [[ -f "$CONFIGFILE" ]]; then source "$CONFIGFILE" diff --git a/modules/help.sh b/modules/help.sh index 797be4d98..8dee3f871 100644 --- a/modules/help.sh +++ b/modules/help.sh @@ -66,6 +66,7 @@ function help_default { echo " usb [on|off] turns usb ports on or off" echo " remote [status|upgrade|services] helps with treehouses remote android app" echo " log <0|1|2|3|4|show|max> gets/sets log level and shows log" + echo " blocker <0|1|2|3|4||max> website blocking levels using /etc/hosts" echo } diff --git a/package.json b/package.json index a0017c8d4..bc1a61253 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@treehouses/cli", - "version": "1.13.15", + "version": "1.13.16", "description": "Thin command-line interface for Raspberry Pi low level configuration.", "main": "cli.sh", "bin": { diff --git a/templates/blocker/blocker.sh b/templates/blocker/blocker.sh new file mode 100755 index 000000000..d94304387 --- /dev/null +++ b/templates/blocker/blocker.sh @@ -0,0 +1,11 @@ +#!/bin/bash +# this file needs to be excuted on the base of the repo with `./templates/blocker/blocker.sh` + +# Credits: https://github.com/StevenBlack/hosts +# The MIT License (MIT) Copyright © 2020 Steven Black +# https://github.com/StevenBlack/hosts/blob/master/license.txt +wget -q -O ./templates/blocker/1_hosts https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts +wget -q -O ./templates/blocker/2_hosts https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/porn/hosts +wget -q -O ./templates/blocker/3_hosts https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/gambling-porn/hosts +wget -q -O ./templates/blocker/4_hosts https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-gambling-porn/hosts +wget -q -O ./templates/blocker/5_hosts https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-gambling-porn-social/hosts