Skip to content

Commit

Permalink
simple-adblock: better start/stop/reload/uninstall
Browse files Browse the repository at this point in the history
Signed-off-by: Stan Grishin <[email protected]>
  • Loading branch information
stangri committed Jan 26, 2018
1 parent 74eecfe commit 4c06b95
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 80 deletions.
16 changes: 14 additions & 2 deletions net/simple-adblock/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=simple-adblock
PKG_VERSION:=1.5.7
PKG_RELEASE:=10
PKG_VERSION:=1.5.8
PKG_RELEASE:=1
PKG_LICENSE:=GPL-3.0+
PKG_MAINTAINER:=Stan Grishin <[email protected]>

Expand Down Expand Up @@ -47,4 +47,16 @@ define Package/$(PKG_NAME)/install
$(INSTALL_CONF) ./files/simple-adblock.conf $(1)/etc/config/simple-adblock
endef

define Package/$(PKG_NAME)/prerm
#!/bin/sh
# check if we are on real system
if [ -z "$${IPKG_INSTROOT}" ]; then
echo "Stopping service and removing rc.d symlink for simple-adblock"
/etc/init.d/simple-adblock stop || true
/etc/init.d/simple-adblock killcache || true
/etc/init.d/simple-adblock disable
fi
exit 0
endef

$(eval $(call BuildPackage,$(PKG_NAME)))
30 changes: 13 additions & 17 deletions net/simple-adblock/files/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ If you want a more robust AdBlocking, supporting free memory detection and compl


## Requirements
This service requires the following packages to be installed on your router: ```dnsmasq``` or ```dnsmasq-full``` and either ```wget``` and ```libopenssl``` (for OpenWrt CC 15.05.1) or ```uclient-fetch``` and ```libustream-mbedtls``` (for OpenWrt DD trunk and all LEDE Project release and snapshot builds). Additionally installation of ```coreutils-sort``` is highly recommended as it speeds up blocklist processing.
This service requires the following packages to be installed on your router: ```dnsmasq``` or ```dnsmasq-full``` and either ```ca-certificates```, ```wget``` and ```libopenssl``` (for OpenWrt 15.05.1) or ```uclient-fetch``` and ```libustream-mbedtls``` (for OpenWrt DD trunk and all LEDE Project builds). Additionally installation of ```coreutils-sort``` is highly recommended as it speeds up blocklist processing.

To satisfy the requirements for connect to your router via ssh and run the following commands:
###### OpenWrt CC 15.05.1
###### OpenWrt 15.05.1
```sh
opkg update; opkg install wget libopenssl coreutils-sort dnsmasq
opkg update; opkg install ca-certificates wget libopenssl coreutils-sort dnsmasq
```

###### LEDE Project and OpenWrt DD trunk
###### LEDE Project 17.01.x and OpenWrt 18.xx or later
```sh
opkg update; opkg install uclient-fetch libustream-mbedtls coreutils-sort dnsmasq
```
Expand Down Expand Up @@ -65,15 +65,15 @@ If ```simple-adblock``` and ```luci-app-simple-adblock``` packages are not foun
#### Add custom repo to your router
If your router is not set up with the access to repository containing these packages you will need to add custom repository to your router by connecting to your router via ssh and running the following commands:

###### OpenWrt CC 15.05.1
###### OpenWrt 15.05.1
```sh
opkg update; opkg install wget libopenssl
opkg update; opkg install ca-certificates wget libopenssl
echo -e -n 'untrusted comment: public key 7ffc7517c4cc0c56\nRWR//HUXxMwMVnx7fESOKO7x8XoW4/dRidJPjt91hAAU2L59mYvHy0Fa\n' > /tmp/stangri-repo.pub && opkg-key add /tmp/stangri-repo.pub
! grep -q 'stangri_repo' /etc/opkg/customfeeds.conf && echo 'src/gz stangri_repo https://raw.githubusercontent.com/stangri/openwrt-repo/master' >> /etc/opkg/customfeeds.conf
opkg update
```

###### LEDE Project and OpenWrt DD trunk
###### LEDE Project and OpenWrt 18.xx or later
```sh
opkg update; opkg install uclient-fetch libustream-mbedtls
echo -e -n 'untrusted comment: public key 7ffc7517c4cc0c56\nRWR//HUXxMwMVnx7fESOKO7x8XoW4/dRidJPjt91hAAU2L59mYvHy0Fa\n' > /tmp/stangri-repo.pub && opkg-key add /tmp/stangri-repo.pub
Expand Down Expand Up @@ -109,23 +109,19 @@ In general, whatever domain is specified to be whitelisted; it, along with with
Please head to [OpenWrt Forum](https://forum.openwrt.org/viewtopic.php?pid=307950) or [LEDE Project Forum](https://forum.lede-project.org/t/simple-adblock-fast-lean-and-fully-uci-luci-configurable-adblocking/1327/) for discussion of this package.

## What's New
1.5.8:
- Better start/stop/reload logic.
- Better uninstall logic.
- Better start/stop/reload from Web UI.
- New command-line ```check``` command.

1.5.7:
- Much stricter filters for hosts and domains lists resulting in better garbage removal.

1.5.6:
- Better handling of service start/enable from Web UI and enabled flag management.

1.5.5:
- Implemented support to set one of the router LEDs on/off based on the AdBlocking status.
- Fixed the output bug when verbosity=1.

1.5.3:
- No longer using enabled in config file, Simple AdBlocking Web UI now enables/disables service directly.

1.5.1:
- Reworked console/system log output logic and formatting.

1.5.0:
- Processes already downloaded lists in the background while downloading next list from config, dramatically increasing overall speed.

1.0.0:
Expand Down
1 change: 0 additions & 1 deletion net/simple-adblock/files/simple-adblock.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ config simple-adblock 'config'
option enabled '0'
option verbosity '2'
option force_dns '1'
option run_in_background '0'
option hosts_file '/var/dnsmasq.d/simple-adblock'
list whitelist_domain 'raw.githubusercontent.com'
# list blacklist_hosts_url 'http://support.it-mate.co.uk/downloads/hosts.txt'
Expand Down
151 changes: 91 additions & 60 deletions net/simple-adblock/files/simple-adblock.init
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ PKG_VERSION=

export START=94
export USE_PROCD=1
#PROCD_DEBUG=1

readonly A_TMP='/var/hosts.allowed.tmp'
readonly B_TMP='/var/hosts.blocked.tmp'
Expand All @@ -12,35 +11,42 @@ readonly dl='wget --no-check-certificate -qO-'
readonly h_filter='/localhost/d;/^#/d;/^[^0-9]/d;s/^0\.0\.0\.0.//;s/^127\.0\.0\.1.//;s/[[:space:]]*#.*$//;s/[[:cntrl:]]$//;s/[[:space:]]//g;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;'
readonly d_filter='/^#/d;s/[[:space:]]*#.*$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;'
readonly f_filter='s|^|local=/|;s|$|/|'
readonly _ok_='\033[0;32m\xe2\x9c\x93\033[0m'
readonly _fail_='\033[0;31m\xe2\x9c\x97\033[0m'
readonly __ok__='\033[0;32m[\xe2\x9c\x93]\033[0m'
readonly __fail__='\033[0;31m[\xe2\x9c\x97]\033[0m'
readonly _error_='\033[0;31mERROR\033[0m'
readonly _OK_='\033[0;32m\xe2\x9c\x93\033[0m'
readonly _FAIL_='\033[0;31m\xe2\x9c\x97\033[0m'
readonly __OK__='\033[0;32m[\xe2\x9c\x93]\033[0m'
readonly __FAIL__='\033[0;31m[\xe2\x9c\x97]\033[0m'
readonly _ERROR_='\033[0;31mERROR\033[0m'

readonly packageName="simple-adblock"
export EXTRA_COMMANDS="check killcache"
export EXTRA_HELP=" check Checks if specified <string> is found in current blacklist"

readonly packageName='simple-adblock'
readonly serviceName="$packageName $PKG_VERSION"
ok() { case $verbosity in 1) output "$_ok_";; 2) output "$__ok__\n";; esac; }
okn() { case $verbosity in 1) output "$_ok_\n";; 2) output "$__ok__\n";; esac; }
fail() { case $verbosity in 1) output "$_fail_";; 2) output "$__fail__\n";; esac; }
failn() { case $verbosity in 1) output "$_fail_\n";; 2) output "$__fail__\n";; esac; }
ok() { case $verbosity in 1) output "$_OK_";; 2) output "$__OK__\n";; esac; }
okn() { case $verbosity in 1) output "$_OK_\n";; 2) output "$__OK__\n";; esac; }
fail() { case $verbosity in 1) output "$_FAIL_";; 2) output "$__FAIL__\n";; esac; }
failn() { case $verbosity in 1) output "$_FAIL_\n";; 2) output "$__FAIL__\n";; esac; }
output() { [[ $# -ne 1 ]] && { [[ ! $((verbosity & $1)) -gt 0 ]] && return 0 || shift; }; local msg; msg=$(echo -n "${1/$serviceName /service }" | sed 's|\\033\[[0-9]\?;\?[0-9]\?[0-9]\?m||g'); [[ -t 1 ]] && echo -e -n "$1"; [[ $(echo -e -n "$msg" | wc -l) -gt 0 ]] && logger -t "${packageName:-service} [$$]" "$(echo -e -n ${logmsg}${msg})" && logmsg='' || logmsg=${logmsg}${msg}; }
led_on(){ [[ -n "$led" && -e "$led/trigger" ]] && echo "default-on" > "$led/trigger"; }
led_off(){ [[ -n "$led" && -e "$led/trigger" ]] && echo "none" > "$led/trigger"; }
export verbosity force_dns bgrun debug led wan_if wan_gw wanphysdev hosts_file
export serviceEnabled verbosity force_dns debug led wan_if wan_gw wanphysdev hosts_file

boot() { ( sleep 120 && rc_procd start_service && rc_procd service_triggers | cat &); }

is_enabled () {
local c=1 enabled
config_load $packageName
config_get_bool enabled 'config' 'enabled' 1
config_get_bool bgrun 'config' 'run_in_background' 0
config_get_bool force_dns 'config' 'force_dns' 1
config_get_bool debug 'config' 'debug' 0
config_get verbosity 'config' 'verbosity' '2'
config_get hosts_file 'config' 'hosts_file' "/var/dnsmasq.d/${packageName}"
config_get led 'config' 'led'
load_package_config() {
config_load "$packageName"
config_get_bool serviceEnabled 'config' 'enabled' 1
config_get_bool force_dns 'config' 'force_dns' 1
config_get_bool debug 'config' 'debug' 0
config_get verbosity 'config' 'verbosity' '2'
config_get hosts_file 'config' 'hosts_file' "/var/dnsmasq.d/${packageName}"
config_get led 'config' 'led'
source /lib/functions/network.sh
}

is_enabled() {
local sleepCount=1
load_package_config

if [ "$debug" -ne 0 ]; then
exec 1>>/tmp/simple-adblock.log
Expand All @@ -49,30 +55,31 @@ is_enabled () {
fi

led="${led:+/sys/class/leds/$led}"
[[ $enabled -gt 0 ]] || { output "$_error_: $serviceName is not enabled.\n"; return 1; }
source /lib/functions/network.sh
[ $serviceEnabled -gt 0 ] || return 1
while : ; do
network_flush_cache; network_find_wan wan_if; network_get_gateway wan_gw $wan_if;
[[ $c -ge 25 || -n "$wan_gw" ]] && break
output "$serviceName waiting for wan gateway...\n"; sleep 2; let "c+=1";
[[ $sleepCount -ge 25 || -n "$wan_gw" ]] && break
output "$serviceName waiting for wan gateway...\n"; sleep 2; let "sleepCount+=1";
done
[ -n "$wan_gw" ] && return 0 || { output "$_error_: $serviceName failed to discover WAN gateway.\n"; return 1; }
[ -n "$wan_gw" ] && return 0
output "$_ERROR_: $serviceName failed to discover WAN gateway.\n"; return 1;
}

reset_iptables() {
[[ $force_dns -eq 0 ]] && return 0
iptables_destroy() {
[ $force_dns -eq 0 ] && return 0
[ -z "$packageName" ] && return 1
iptables-save | grep -Fv -- "$packageName" | iptables-restore
lsmod | grep -q ip6table_nat && ip6tables-save | grep -Fv -- "$packageName" | ip6tables-restore
[ ! "$1" == "quiet" ] && output 'No longer forcing local DNS server.\n'
[ -z "$1" ] && output 'No longer forcing local DNS server.\n'
}

set_iptables() {
iptables_create() {
local ip ipv6 label ipv6wan brname
network_get_ipaddr ip lan; network_get_ipaddr6 ipv6 lan; network_get_device brname lan; network_get_physdev wanphysdev wan;
ipv6wan=$(ifconfig $wanphysdev | grep inet6 | awk '{print $3}')

if [[ $force_dns -ne 0 ]]; then
iptables_destroy 'quiet'
if [ $force_dns -ne 0 ]; then
[ -n "$ip" ] && iptables -t nat -A prerouting_rule -i $brname -p tcp --dport 53 -j DNAT --to $ip -m comment --comment "$packageName"
[ -n "$ip" ] && iptables -t nat -A prerouting_rule -i $brname -p udp --dport 53 -j DNAT --to $ip -m comment --comment "$packageName"
if [[ -n "$ipv6" && -n "$ipv6wan" ]] && lsmod | grep -q ip6table_nat; then
Expand All @@ -82,16 +89,27 @@ set_iptables() {
else
label="$ip"
fi
[ -n "$label" ] && output "Forcing local DNS server: $label.\n" || output "$_error_: $serviceName failed to obtain LAN IP address for DNS forcing!\n"
if [ -z "$1" ]; then
if [ -n "$label" ]; then
output "Forcing local DNS server: $label.\n"
else
output "$_ERROR_: $serviceName failed to obtain LAN IP address for DNS forcing!\n"
fi
fi
fi
}

stop_adblocking () {
[ -f $hosts_file ] && mv $hosts_file $T_TMP
output 3 "Restarting dnsmasq "
led_off
/etc/init.d/dnsmasq restart >/dev/null 2>&1
[[ $? -eq 0 ]] && { okn; output "$serviceName stopped.\n"; } || { failn; output "$_error_: $serviceName failed to reload dnsmasq!\n"; }
stop_adblocking() {
load_package_config
[ -f $hosts_file ] && mv $hosts_file $T_TMP
output 3 "Restarting dnsmasq "
led_off
/etc/init.d/dnsmasq restart >/dev/null 2>&1
if [[ $? -eq 0 ]]; then
okn; output "$serviceName stopped.\n";
else
failn; output "$_ERROR_: $serviceName failed to reload dnsmasq!\n";
fi
}

process_url() {
Expand All @@ -108,7 +126,7 @@ process_url() {
{ sed -i "$filter" "$R_TMP"; cat "$R_TMP" >> "$D_TMP"; rm -f "$R_TMP"; } &
}

start_adblocking () {
start_adblocking() {
local whitelist_domains blacklist_domains whitelist_domains_urls blacklist_domains_urls blacklist_hosts_urls
config_get whitelist_domains 'config' 'whitelist_domain'
config_get blacklist_domains 'config' 'blacklist_domain'
Expand All @@ -119,6 +137,7 @@ start_adblocking () {
local hf w_filter

[ ! -d ${hosts_file%/*} ] && mkdir -p ${hosts_file%/*}
[ ! -f "$hosts_file" ] && touch "$hosts_file"
if [[ -s $T_TMP && ! "$1" == "reload" ]]; then
output 3 'Found existing data file, reusing it '
mv $T_TMP $hosts_file && okn || failn
Expand Down Expand Up @@ -166,38 +185,50 @@ start_adblocking () {
/etc/init.d/dnsmasq restart >/dev/null 2>&1
if [[ $? -eq 0 ]]; then
led_on; okn;
output "$serviceName blocking $(wc -l < $hosts_file) domains $_ok_\n"
output "$serviceName blocking $(wc -l < $hosts_file) domains $_OK_\n"
else
failn; output "$_error_: $serviceName failed to reload dnsmasq!\n";
failn; output "$_ERROR_: $serviceName failed to reload dnsmasq!\n";
exit 1
fi
else
output "$_error_: $serviceName failed to create its data file!\n"
output "$_ERROR_: $serviceName failed to create its data file!\n"
exit 1
fi
}

reload_service () {
is_enabled || return 1
[[ -t 1 && $bgrun -eq 1 ]] && output "Reloading $serviceName...\n"
[[ ! -t 1 || $bgrun -eq 1 ]] && (start_adblocking 'reload' | cat &) || start_adblocking 'reload'
check() {
load_package_config
local string="$1"
if [ ! -f $hosts_file ]; then
echo "No local blacklist ($hosts_file) found."
elif [ -z "$string" ]; then
echo "Usage: /etc/init.d/${serviceName} check <string>"
elif grep -m1 -q $string $hosts_file; then
echo "Found $(grep $string $hosts_file | wc -l) matches for $string in $hosts_file:"
grep $string $hosts_file | sed 's|local=/||;s|/$||;'
else
echo "The $string is not found in current blacklist."
fi
}

start_service () {
start_service() {
is_enabled || return 1
[[ -t 1 && $bgrun -eq 1 ]] && output "Starting $serviceName...\n"
reset_iptables quiet
set_iptables
[[ ! -t 1 || $bgrun -eq 1 ]] && (start_adblocking $1 | cat &) || start_adblocking $1
if [ -f "$hosts_file" ]; then
output "Reloading $serviceName...\n"
iptables_create 'quiet'
start_adblocking 'reload'
else
output "Starting $serviceName...\n"
iptables_create
start_adblocking
fi
}

stop_service () {
is_enabled || return 1
[[ -t 1 && $bgrun -eq 1 ]] && output "Stopping $serviceName...\n"
reset_iptables
[[ ! -t 1 || $bgrun -eq 1 ]] && (stop_adblocking | cat &) || stop_adblocking
stop_service() {
load_package_config
output "Stopping $serviceName...\n"
iptables_destroy
stop_adblocking
}

service_triggers () {
procd_add_reload_trigger 'simple-adblock'
}
killcache() { [ -s $T_TMP ] && rm -f $T_TMP; }

0 comments on commit 4c06b95

Please sign in to comment.