adblock: update 1.4.9
[feed/packages.git] / net / adblock / files / adblock.init
1 #!/bin/sh /etc/rc.common
2 #
3
4 START=99
5 EXTRA_COMMANDS="toggle stats cfgup query"
6 EXTRA_HELP=" toggle Toggle adblock 'on' or 'off'
7 stats Update adblock statistics
8 cfgup Update adblock configuration file
9 query <DOMAIN> Query active blocklists for specific domain"
10
11 adb_debug=0
12 adb_pid="${$}"
13 adb_script="/usr/bin/adblock-update.sh"
14 adb_helper="/usr/bin/adblock-helper.sh"
15 adb_pidfile="/var/run/adblock.pid"
16 bg_parm="&"
17
18 if [ $((adb_debug)) -eq 0 ]
19 then
20 exec 2>/dev/null
21 fi
22
23 if [ -r "${adb_pidfile}" ]
24 then
25 logger -s -t "adblock[${adb_pid}] error" "adblock service already running ($(cat ${adb_pidfile}))" 2>&1
26 exit 255
27 fi
28
29 . "${adb_helper}"
30 f_envload
31
32 if [ "${adb_restricted}" = "1" ]
33 then
34 adb_uci="$(which true)"
35 fi
36
37 boot()
38 {
39 return 0
40 }
41
42 start()
43 {
44 if [ -t 1 ]
45 then
46 unset bg_parm
47 fi
48 eval "${adb_script}" ${bg_parm}
49 return 0
50 }
51
52 restart()
53 {
54 stop
55 start
56 }
57
58 reload()
59 {
60 reload="true"
61 stop
62 start
63 }
64
65 stop()
66 {
67 f_rmdns
68 f_rmuhttpd
69 config_foreach f_rmconfig source
70 if [ -z "${reload}" ]
71 then
72 f_rmfirewall
73 fi
74 if [ -n "${rm_dns}" ] || [ -n "${rm_uhttpd}" ] || [ -n "${rm_fw}" ] || [ -n "$(${adb_uci} -q changes adblock)" ]
75 then
76 "${adb_uci}" -q commit adblock
77 f_log "all adblock related services stopped"
78 fi
79 return 0
80 }
81
82 toggle()
83 {
84 if [ -d "${adb_dnshidedir}" ]
85 then
86 list_dns="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
87 list_dnshide="$(find "${adb_dnshidedir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
88 if [ -n "${list_dns}" ]
89 then
90 source="${adb_dnsdir}/${adb_dnsprefix}"
91 target="${adb_dnshidedir}"
92 pos="off"
93 elif [ -n "${list_dnshide}" ]
94 then
95 source="${adb_dnshidedir}/${adb_dnsprefix}"
96 target="${adb_dnsdir}"
97 pos="on"
98 fi
99 if [ -n "${list_dns}" ] || [ -n "${list_dnshide}" ]
100 then
101 mv -f "${source}"* "${target}"
102 /etc/init.d/dnsmasq restart
103 "${adb_uci}" -q set "adblock.global.adb_dnstoggle=${pos}"
104 "${adb_uci}" -q commit "adblock"
105 f_log "adblock toggle switched '${pos}'"
106 fi
107 fi
108 return 0
109 }
110
111 stats()
112 {
113 f_statistics
114 "${adb_uci}" -q commit "adblock"
115 return 0
116 }
117
118 cfgup()
119 {
120 stop
121 cp -pf "/etc/adblock/adblock.conf.default" "/etc/config/adblock"
122 rc=$?
123 if [ $((rc)) -eq 0 ]
124 then
125 f_log "default adblock configuration applied, please check the settings in '/etc/config/adblock'"
126 else
127 f_log "default adblock configuration not found, please re-install the package via 'opkg install adblock --force-maintainer'"
128 fi
129 return 0
130 }
131
132 query()
133 {
134 domain="${1}"
135 tld="${domain#*.}"
136 list_dns="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
137 if [ -z "${list_dns}" ]
138 then
139 f_log "no active blocklists found, please start adblock first"
140 elif [ -z "${domain}" ] || [ "${domain}" = "${tld}" ]
141 then
142 f_log "invalid domain query input, please submit a specific (sub-)domain, i.e. 'www.abc.xyz'"
143 else
144 while [ "${domain}" != "${tld}" ]
145 do
146 search="${domain//./\.}"
147 result="$(grep -Hm 1 "[/\.]${search}/" "${adb_dnsdir}/adb_list"* | awk -F ':|/' '{print " "$4"\t: "$6}')"
148 count="$(grep -hc "[/\.]${search}/" "${adb_dnsdir}/adb_list"* | awk '{sum += $1} END {printf sum}')"
149 printf "%s\n" "=> distinct results for domain '${domain}' (overall ${count})"
150 if [ -z "${result}" ]
151 then
152 printf "%s\n" " no matches in active blocklists"
153 else
154 printf "%s\n" "${result}"
155 fi
156 domain="${tld}"
157 tld="${domain#*.}"
158 done
159 fi
160 return 0
161 }