You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1145 lines
36 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright 2017-2020 Stan Grishin (stangri@melmac.net)
  3. # shellcheck disable=SC2039,SC1091,SC2016
  4. PKG_VERSION='dev-test'
  5. # shellcheck disable=SC2034
  6. START=94
  7. # shellcheck disable=SC2034
  8. USE_PROCD=1
  9. LC_ALL=C
  10. extra_command 'check' 'Checks if specified domain is found in current block-list'
  11. extra_command 'dl' 'Force-downloads all enabled block-list'
  12. extra_command 'killcache' 'Delete all cached files'
  13. extra_command 'sizes' 'Displays the file-sizes of enabled block-listo'
  14. extra_command 'show' 'Shows the service last-run status'
  15. extra_command 'version' 'Show version information'
  16. readonly packageName='simple-adblock'
  17. readonly serviceName="$packageName $PKG_VERSION"
  18. readonly addnhostsFile="/var/run/${packageName}.addnhosts"
  19. readonly addnhostsCache="/var/run/${packageName}.addnhosts.cache"
  20. readonly addnhostsGzip="/etc/${packageName}.addnhosts.gz"
  21. readonly addnhostsOutputFilter='s|^|127.0.0.1 |;s|$||'
  22. readonly addnhostsOutputFilterIPv6='s|^|:: |;s|$||'
  23. readonly dnsmasqFile="/var/dnsmasq.d/${packageName}"
  24. readonly dnsmasqCache="/var/run/${packageName}.dnsmasq.cache"
  25. readonly dnsmasqGzip="/etc/${packageName}.dnsmasq.gz"
  26. readonly dnsmasqOutputFilter='s|^|local=/|;s|$|/|'
  27. readonly ipsetFile="/var/dnsmasq.d/${packageName}.ipset"
  28. readonly ipsetCache="/var/run/${packageName}.ipset.cache"
  29. readonly ipsetGzip="/etc/${packageName}.ipset.gz"
  30. readonly ipsetOutputFilter='s|^|ipset=/|;s|$|/adb|'
  31. readonly serversFile="/var/run/${packageName}.servers"
  32. readonly serversCache="/var/run/${packageName}.servers.cache"
  33. readonly serversGzip="/etc/${packageName}.servers.gz"
  34. readonly serversOutputFilter='s|^|server=/|;s|$|/|'
  35. readonly unboundFile="/var/lib/unbound/adb_list.${packageName}"
  36. readonly unboundCache="/var/run/${packageName}.unbound.cache"
  37. readonly unboundGzip="/etc/${packageName}.unbound.gz"
  38. readonly unboundOutputFilter='s|^|local-zone: "|;s|$|" static|'
  39. readonly A_TMP="/var/${packageName}.hosts.a.tmp"
  40. readonly B_TMP="/var/${packageName}.hosts.b.tmp"
  41. readonly jsonFile="/var/run/${packageName}.json"
  42. readonly sharedMemoryError="/dev/shm/$packageName-error"
  43. readonly sharedMemoryOutput="/dev/shm/$packageName-output"
  44. readonly hostsFilter='/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;/[^[:alnum:]_.-]/d;'
  45. readonly domainsFilter='/^#/d;s/[[:space:]]*#.*$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
  46. readonly _OK_='\033[0;32m\xe2\x9c\x93\033[0m'
  47. readonly _FAIL_='\033[0;31m\xe2\x9c\x97\033[0m'
  48. readonly __OK__='\033[0;32m[\xe2\x9c\x93]\033[0m'
  49. readonly __FAIL__='\033[0;31m[\xe2\x9c\x97]\033[0m'
  50. readonly _ERROR_='\033[0;31mERROR\033[0m'
  51. readonly _WARNING_='\033[0;33mWARNING\033[0m'
  52. version() { echo "$PKG_VERSION"; }
  53. getStatusText() {
  54. local _ret
  55. case "$1" in
  56. statusNoInstall) _ret="$serviceName is not installed or not found";;
  57. statusStopped) _ret="Stopped";;
  58. statusStarting) _ret="Starting";;
  59. statusRestarting) _ret="Restarting";;
  60. statusForceReloading) _ret="Force Reloading";;
  61. statusDownloading) _ret="Downloading";;
  62. statusProcessing) _ret="Processing";;
  63. statusError) _ret="Error";;
  64. statusWarning) _ret="Warning";;
  65. statusFail) _ret="Fail";;
  66. statusSuccess) _ret="Success";;
  67. esac
  68. printf "%b" "$_ret"
  69. }
  70. getErrorText() {
  71. local _ret
  72. case "$1" in
  73. errorOutputFileCreate) _ret="failed to create $outputFile file";;
  74. errorFailDNSReload) _ret="failed to restart/reload DNS resolver";;
  75. errorSharedMemory) _ret="failed to access shared memory";;
  76. errorSorting) _ret="failed to sort data file";;
  77. errorOptimization) _ret="failed to optimize data file";;
  78. errorAllowListProcessing) _ret="failed to process allow-list";;
  79. errorDataFileFormatting) _ret="failed to format data file";;
  80. errorMovingDataFile) _ret="failed to move data file '${A_TMP}' to '${outputFile}'";;
  81. errorCreatingCompressedCache) _ret="failed to create compressed cache";;
  82. errorRemovingTempFiles) _ret="failed to remove temporary files";;
  83. errorRestoreCompressedCache) _ret="failed to unpack compressed cache";;
  84. errorRestoreCache) _ret="failed to move '$outputCache' to '$outputFile'";;
  85. errorOhSnap) _ret="failed to create block-list or restart DNS resolver";;
  86. errorStopping) _ret="failed to stop $serviceName";;
  87. errorDNSReload) _ret="failed to reload/restart DNS resolver";;
  88. errorDownloadingConfigUpdate) _ret="failed to download Config Update file";;
  89. errorDownloadingList) _ret="failed to download";;
  90. errorParsingList) _ret="failed to parse";;
  91. errorParsingConfigUpdate) _ret="failed to parse Config Update file";;
  92. esac
  93. printf "%b" "$_ret"
  94. }
  95. output_ok() { output 1 "$_OK_"; output 2 "$__OK__\\n"; }
  96. output_okn() { output 1 "$_OK_\\n"; output 2 "$__OK__\\n"; }
  97. output_fail() { output 1 "$_FAIL_"; output 2 "$__FAIL__\\n"; }
  98. output_failn() { output 1 "$_FAIL_\\n"; output 2 "$__FAIL__\\n"; }
  99. str_replace() { printf "%b" "$1" | sed -e "s/$(printf "%b" "$2")/$(printf "%b" "$3")/g"; }
  100. str_contains() { test "$1" != "$(str_replace "$1" "$2" '')"; }
  101. compare_values() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; }
  102. is_chaos_calmer() { ubus -S call system board | grep -q 'Chaos Calmer'; }
  103. led_on(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'default-on' > "${1}/trigger" 2>&1; fi; }
  104. led_off(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'none' > "${1}/trigger" 2>&1; fi; }
  105. dnsmasq_hup() { killall -q -HUP dnsmasq; }
  106. dnsmasq_kill() { killall -q -KILL dnsmasq; }
  107. dnsmasq_restart() { /etc/init.d/dnsmasq restart >/dev/null 2>&1; }
  108. unbound_restart() { /etc/init.d/unbound restart >/dev/null 2>&1; }
  109. is_force_dns_active() { iptables-save | grep -q -w -- '--dport 53'; }
  110. is_present() { command -v "$1" >/dev/null 2>&1; }
  111. output() {
  112. # Can take a single parameter (text) to be output at any verbosity
  113. # Or target verbosity level and text to be output at specifc verbosity
  114. local msg memmsg logmsg
  115. if [ $# -ne 1 ]; then
  116. if [ $((verbosity & $1)) -gt 0 ] || [ "$verbosity" = "$1" ]; then shift; else return 0; fi
  117. fi
  118. [ -t 1 ] && printf "%b" "$1"
  119. msg="${1//$serviceName /service }";
  120. if [ "$(printf "%b" "$msg" | wc -l)" -gt 0 ]; then
  121. [ -s "$sharedMemoryOutput" ] && memmsg="$(cat "$sharedMemoryOutput")"
  122. logmsg="$(printf "%b" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
  123. logger -t "${packageName:-service} [$$]" "$(printf "%b" "$logmsg")"
  124. rm -f "$sharedMemoryOutput"
  125. else
  126. printf "%b" "$msg" >> "$sharedMemoryOutput"
  127. fi
  128. }
  129. serviceEnabled=''; forceDNS=''; forceDNSPorts=''; parallelDL=''; debug='';
  130. compressedCache=''; ipv6Enabled=''; configUpdateEnabled=''; configUpdateURL='';
  131. bootDelay=''; dlTimeout=''; curlRetry=''; verbosity=''; led='';
  132. targetDNS=''; dnsInstance='';
  133. allowed_domains=''; allowed_domains_urls='';
  134. blocked_domains=''; blocked_domains_urls=''; blocked_hosts_urls='';
  135. dl_command=''; dl_flag=''; isSSLSupported=''; allowIDN='';
  136. outputFilter=''; outputFilterIPv6=''; outputFile=''; outputGzip=''; outputCache='';
  137. awk='awk';
  138. load_package_config() {
  139. config_load "$packageName"
  140. config_get_bool serviceEnabled 'config' 'enabled' 1
  141. config_get_bool forceDNS 'config' 'force_dns' 1
  142. config_get_bool parallelDL 'config' 'parallel_downloads' 1
  143. config_get_bool debug 'config' 'debug' 0
  144. config_get_bool compressedCache 'config' 'compressed_cache' 0
  145. config_get_bool ipv6Enabled 'config' 'ipv6_enabled' 0
  146. config_get_bool configUpdateEnabled 'config' 'config_update_enabled' 0
  147. config_get forceDNSPorts 'config' 'force_dns_port' '53 853'
  148. config_get bootDelay 'config' 'boot_delay' '120'
  149. config_get dlTimeout 'config' 'download_timeout' '20'
  150. config_get curlRetry 'config' 'curl_retry' '3'
  151. config_get verbosity 'config' 'verbosity' '2'
  152. config_get led 'config' 'led'
  153. config_get targetDNS 'config' 'dns' 'dnsmasq.servers'
  154. config_get dnsInstance 'config' 'dns_instance' '0'
  155. config_get allowed_domains 'config' 'allowed_domain'
  156. config_get allowed_domains_urls 'config' 'allowed_domains_url'
  157. config_get blocked_domains 'config' 'blocked_domain'
  158. config_get blocked_domains_urls 'config' 'blocked_domains_url'
  159. config_get blocked_hosts_urls 'config' 'blocked_hosts_url'
  160. config_get configUpdateURL 'config' 'config_update_url' 'https://cdn.jsdelivr.net/gh/openwrt/packages/net/simple-adblock/files/simple-adblock.conf.update'
  161. if [ "$targetDNS" != 'dnsmasq.addnhosts' ] && [ "$targetDNS" != 'dnsmasq.conf' ] && \
  162. [ "$targetDNS" != 'dnsmasq.servers' ] && [ "$targetDNS" != 'unbound.adb_list' ] && \
  163. [ "$targetDNS" != 'dnsmasq.ipset' ] ; then
  164. targetDNS='dnsmasq.servers'
  165. fi
  166. case "$targetDNS" in
  167. dnsmasq.addnhosts)
  168. outputFilter="$addnhostsOutputFilter"
  169. outputFile="$addnhostsFile"
  170. outputCache="$addnhostsCache"
  171. outputGzip="$addnhostsGzip"
  172. [ "$ipv6Enabled" -gt 0 ] && outputFilterIPv6="$addnhostsOutputFilterIPv6"
  173. rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
  174. rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
  175. rm -f "$serversFile" "$serversCache" "$serversGzip"
  176. rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
  177. ;;
  178. dnsmasq.conf)
  179. outputFilter="$dnsmasqOutputFilter"
  180. outputFile="$dnsmasqFile"
  181. outputCache="$dnsmasqCache"
  182. outputGzip="$dnsmasqGzip"
  183. rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
  184. rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
  185. rm -f "$serversFile" "$serversCache" "$serversGzip"
  186. rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
  187. ;;
  188. dnsmasq.ipset)
  189. outputFilter="$ipsetOutputFilter"
  190. outputFile="$ipsetFile"
  191. outputCache="$ipsetCache"
  192. outputGzip="$ipsetGzip"
  193. rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
  194. rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
  195. rm -f "$serversFile" "$serversCache" "$serversGzip"
  196. rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
  197. ;;
  198. dnsmasq.servers)
  199. outputFilter="$serversOutputFilter"
  200. outputFile="$serversFile"
  201. outputCache="$serversCache"
  202. outputGzip="$serversGzip"
  203. rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
  204. rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
  205. rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
  206. rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
  207. ;;
  208. unbound.adb_list)
  209. outputFilter="$unboundOutputFilter"
  210. outputFile="$unboundFile"
  211. outputCache="$unboundCache"
  212. outputGzip="$unboundGzip"
  213. rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
  214. rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
  215. rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
  216. rm -f "$serversFile" "$serversCache" "$serversGzip"
  217. ;;
  218. esac
  219. if [ -z "${verbosity##*[!0-9]*}" ] || [ "$verbosity" -lt 0 ] || [ "$verbosity" -gt 2 ]; then
  220. verbosity=1
  221. fi
  222. . /lib/functions/network.sh
  223. . /usr/share/libubox/jshn.sh
  224. is_present 'gawk' && awk='gawk'
  225. if ! is_present '/usr/libexec/grep-gnu' || ! is_present '/usr/libexec/sed-gnu' || \
  226. ! is_present '/usr/libexec/sort-coreutils' || ! is_present 'gawk'; then
  227. local s="opkg update; opkg --force-overwrite install"
  228. is_present 'gawk' || s="$s gawk"
  229. is_present '/usr/libexec/grep-gnu' || s="$s grep"
  230. is_present '/usr/libexec/sed-gnu' || s="$s sed"
  231. is_present '/usr/libexec/sort-coreutils' || s="$s coreutils-sort"
  232. output "$_WARNING_: Some recommended packages are missing, install them by running:\\n"
  233. output "$s;\\n"
  234. fi
  235. # Prefer curl because it supports the file:// scheme.
  236. if is_present 'curl'; then
  237. dl_command="curl --insecure --retry $curlRetry --connect-timeout $dlTimeout --silent"
  238. dl_flag="-o"
  239. elif is_present wget && wget --version 2>/dev/null | grep -q "+https"; then
  240. dl_command="wget --no-check-certificate --timeout $dlTimeout -q"
  241. dl_flag="-O"
  242. else
  243. dl_command="uclient-fetch --no-check-certificate --timeout $dlTimeout -q"
  244. dl_flag="-O"
  245. fi
  246. led="${led:+/sys/class/leds/$led}"
  247. if curl --version 2>/dev/null | grep -q "https" \
  248. || wget --version 2>/dev/null | grep -q "+https" \
  249. || grep -q "libustream-mbedtls" /usr/lib/opkg/status \
  250. || grep -q "libustream-openssl" /usr/lib/opkg/status \
  251. || grep -q "libustream-wolfssl" /usr/lib/opkg/status; then
  252. isSSLSupported=1
  253. else
  254. unset isSSLSupported
  255. fi
  256. }
  257. is_enabled() {
  258. wan_if=''; wan_gw='';
  259. load_package_config
  260. if [ "$debug" -ne 0 ]; then
  261. exec 1>>/tmp/simple-adblock.log
  262. exec 2>&1
  263. set -x
  264. fi
  265. if [ "$serviceEnabled" -eq 0 ]; then
  266. case "$1" in
  267. on_start)
  268. output "$packageName is currently disabled.\\n"
  269. output "Run the following commands before starting service again:\\n"
  270. output "uci set ${packageName}.config.enabled='1'; uci commit $packageName;\\n"
  271. ;;
  272. esac
  273. return 1
  274. fi
  275. case $targetDNS in
  276. dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers)
  277. if dnsmasq -v 2>/dev/null | grep -q 'no-IDN' || ! dnsmasq -v 2>/dev/null | grep -q -w 'IDN'; then
  278. allowIDN=0
  279. else
  280. allowIDN=1
  281. fi
  282. ;;
  283. unbound.adb_list)
  284. allowIDN=1;;
  285. esac
  286. case $targetDNS in
  287. dnsmasq.ipset)
  288. if dnsmasq -v 2>/dev/null | grep -q 'no-ipset' || ! dnsmasq -v 2>/dev/null | grep -q -w 'ipset'; then
  289. output "$_ERROR_: DNSMASQ ipset support is enabled in $packageName, but DNSMASQ is either not installed or installed DNSMASQ does not support ipsets!\\n"
  290. targetDNS='dnsmasq.servers'
  291. fi
  292. if ! ipset help hash:net >/dev/null 2>&1; then
  293. output "$_ERROR_: DNSMASQ ipset support is enabled in $packageName, but ipset is either not installed or installed ipset does not support 'hash:net' type!\\n"
  294. targetDNS='dnsmasq.servers'
  295. fi
  296. ;;
  297. esac
  298. [ ! -d "${outputFile%/*}" ] && mkdir -p "${outputFile%/*}"
  299. [ ! -d "${outputCache%/*}" ] && mkdir -p "${outputFile%/*}"
  300. [ ! -d "${outputGzip%/*}" ] && mkdir -p "${outputFile%/*}"
  301. cacheOps 'testGzip' && return 0
  302. network_flush_cache; network_find_wan wan_if; network_get_gateway wan_gw "$wan_if";
  303. [ -n "$wan_gw" ] && return 0
  304. output "$_ERROR_: $serviceName failed to discover WAN gateway.\\n"; return 1;
  305. }
  306. dnsmasqOps() {
  307. local cfg="$1" param="$2"
  308. case "$param" in
  309. dnsmasq.addnhosts)
  310. if [ "$(uci -q get dhcp."$cfg".serversfile)" = "$serversFile" ]; then
  311. uci -q del dhcp."$cfg".serversfile
  312. fi
  313. if ! uci -q get dhcp."$cfg".addnhosts | grep -q "$addnhostsFile"; then
  314. uci add_list dhcp."$cfg".addnhosts="$addnhostsFile"
  315. fi
  316. ;;
  317. dnsmasq.conf|dnsmasq.ipset|unbound.adb_list|cleanup)
  318. uci -q del_list dhcp."$cfg".addnhosts="$addnhostsFile"
  319. if [ "$(uci -q get dhcp."$cfg".serversfile)" = "$serversFile" ]; then
  320. uci -q del dhcp."$cfg".serversfile
  321. fi
  322. ;;
  323. dnsmasq.servers)
  324. uci -q del_list dhcp."$cfg".addnhosts="$addnhostsFile"
  325. if [ "$(uci -q get dhcp."$cfg".serversfile)" != "$serversFile" ]; then
  326. uci set dhcp."$cfg".serversfile="$serversFile"
  327. fi
  328. ;;
  329. esac
  330. }
  331. dnsOps() {
  332. local param output_text i
  333. case $1 in
  334. on_start)
  335. if [ ! -s "$outputFile" ]; then
  336. jsonOps set status "statusFail"
  337. jsonOps add error "errorOutputFileCreate"
  338. output "$_ERROR_: $(getErrorText 'errorOutputFileCreate')!\\n"
  339. return 1
  340. fi
  341. config_load 'dhcp'
  342. if [ "$dnsInstance" = "*" ]; then
  343. config_foreach dnsmasqOps 'dnsmasq' "$targetDNS"
  344. elif [ -n "$dnsInstance" ]; then
  345. for i in $dnsInstance; do
  346. dnsmasqOps "@dnsmasq[$i]" "$targetDNS"
  347. done
  348. fi
  349. case "$targetDNS" in
  350. dnsmasq.addnhosts|dnsmasq.servers)
  351. param=dnsmasq_hup
  352. output_text='Reloading DNSMASQ'
  353. ;;
  354. dnsmasq.conf|dnsmasq.ipset)
  355. param=dnsmasq_restart
  356. output_text='Restarting DNSMASQ'
  357. ;;
  358. unbound.adb_list)
  359. param=unbound_restart
  360. output_text='Restarting Unbound'
  361. ;;
  362. esac
  363. if [ -n "$(uci changes dhcp)" ]; then
  364. uci commit dhcp
  365. if [ "$param" = 'unbound_restart' ]; then
  366. param='dnsmasq_restart; unbound_restart;'
  367. output_text='Restarting Unbound/DNSMASQ'
  368. else
  369. param=dnsmasq_restart
  370. output_text='Restarting DNSMASQ'
  371. fi
  372. fi
  373. output 1 "$output_text "
  374. output 2 "$output_text "
  375. jsonOps set message "$output_text"
  376. if eval "$param"; then
  377. jsonOps set status "statusSuccess"
  378. led_on "$led"
  379. output_okn
  380. else
  381. output_fail
  382. jsonOps set status "statusFail"
  383. jsonOps add error "errorDNSReload"
  384. output "$_ERROR_: $(getErrorText 'errorDNSReload')!\\n"
  385. return 1
  386. fi
  387. ;;
  388. on_stop)
  389. case "$targetDNS" in
  390. dnsmasq.addnhosts | dnsmasq.servers)
  391. param=dnsmasq_hup
  392. ;;
  393. dnsmasq.conf | dnsmasq.ipset)
  394. param=dnsmasq_restart
  395. ;;
  396. unbound.adb_list)
  397. param=unbound_restart
  398. ;;
  399. esac
  400. if [ -n "$(uci changes dhcp)" ]; then
  401. uci -q commit dhcp
  402. if [ "$param" = 'unbound_restart' ]; then
  403. param='dnsmasq_restart; unbound_restart;'
  404. else
  405. param=dnsmasq_restart
  406. fi
  407. fi
  408. eval "$param"
  409. return $?
  410. ;;
  411. quiet)
  412. case "$targetDNS" in
  413. dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers)
  414. param=dnsmasq_restart
  415. ;;
  416. unbound.adb_list)
  417. param=unbound_restart
  418. ;;
  419. esac
  420. eval "$param"
  421. return $?
  422. ;;
  423. esac
  424. }
  425. jsonOps() {
  426. # shellcheck disable=SC2034
  427. local action="$1" param="$2" value="$3"
  428. local status message error stats
  429. local reload restart curReload curRestart ret i
  430. if [ -s "$jsonFile" ]; then
  431. json_load_file "$jsonFile" 2>/dev/null
  432. json_select 'data' 2>/dev/null
  433. for i in status message error stats reload restart; do
  434. json_get_var $i "$i" 2>/dev/null
  435. done
  436. fi
  437. case "$action" in
  438. get)
  439. case "$param" in
  440. triggers)
  441. curReload="$parallelDL $debug $dlTimeout $allowed_domains $blocked_domains $allowed_domains_urls $blocked_domains_urls $blocked_hosts_urls $targetDNS $configUpdateEnabled $configUpdateURL"
  442. curRestart="$compressedCache $forceDNS $led $forceDNSPorts"
  443. if [ ! -s "$jsonFile" ]; then
  444. ret='on_boot'
  445. elif [ "$curReload" != "$reload" ]; then
  446. ret='download'
  447. elif [ "$curRestart" != "$restart" ]; then
  448. ret='restart'
  449. fi
  450. printf "%b" "$ret"
  451. return;;
  452. *)
  453. printf "%b" "$(eval echo "\$$param")"; return;;
  454. esac
  455. ;;
  456. add)
  457. if [ -n "$(eval echo "\$$param")" ]; then
  458. value="$(eval echo "\$$param") ${value}"
  459. fi
  460. eval "$param"='${value}'
  461. ;;
  462. del)
  463. case "$param" in
  464. all)
  465. unset status message error stats;;
  466. triggers)
  467. unset reload restart;;
  468. *)
  469. unset "$param";;
  470. esac
  471. ;;
  472. set)
  473. case "$param" in
  474. triggers)
  475. reload="$parallelDL $debug $dlTimeout $allowed_domains $blocked_domains $allowed_domains_urls $blocked_domains_urls $blocked_hosts_urls $targetDNS $configUpdateEnabled $configUpdateURL"
  476. restart="$compressedCache $forceDNS $led $forceDNSPorts"
  477. ;;
  478. *)
  479. eval "$param"='$value';;
  480. esac
  481. ;;
  482. esac
  483. json_init
  484. json_add_object 'data'
  485. json_add_string version "$PKG_VERSION"
  486. json_add_string status "$status"
  487. json_add_string message "$message"
  488. json_add_string error "$error"
  489. json_add_string stats "$stats"
  490. json_add_string reload "$reload"
  491. json_add_string restart "$restart"
  492. json_close_object
  493. json_dump > "$jsonFile"
  494. sync
  495. }
  496. cacheOps() {
  497. local R_TMP
  498. case "$1" in
  499. create|backup)
  500. [ -s "$outputFile" ] && { mv -f "$outputFile" "$outputCache"; true > "$outputFile"; } >/dev/null 2>/dev/null
  501. return $?
  502. ;;
  503. restore|use)
  504. [ -s "$outputCache" ] && mv "$outputCache" "$outputFile" >/dev/null 2>/dev/null
  505. return $?
  506. ;;
  507. test)
  508. [ -s "$outputCache" ]
  509. return $?
  510. ;;
  511. testGzip)
  512. [ -s "$outputGzip" ] && gzip -t -c "$outputGzip"
  513. return $?
  514. ;;
  515. createGzip)
  516. R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
  517. if gzip < "$outputFile" > "$R_TMP"; then
  518. if mv "$R_TMP" "$outputGzip"; then
  519. rm -f "$R_TMP"
  520. return 0
  521. else
  522. rm -f "$R_TMP"
  523. return 1
  524. fi
  525. else
  526. return 1
  527. fi
  528. ;;
  529. expand|unpack|expandGzip|unpackGzip)
  530. [ -s "$outputGzip" ] && gzip -dc < "$outputGzip" > "$outputCache"
  531. return $?
  532. ;;
  533. esac
  534. }
  535. process_url() {
  536. local label type D_TMP R_TMP
  537. if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then return 1; fi
  538. label="${1##*//}"; label="${label%%/*}";
  539. if [ "$2" = 'hosts' ]; then
  540. label="Hosts: $label"; filter="$hostsFilter";
  541. else
  542. label="Domains: $label"; filter="$domainsFilter";
  543. fi
  544. if [ "$3" = 'blocked' ]; then
  545. type='Blocked'; D_TMP="$B_TMP";
  546. else
  547. type='Allowed'; D_TMP="$A_TMP";
  548. fi
  549. if [ "${1:0:5}" == "https" ] && [ -z "$isSSLSupported" ]; then
  550. output 1 "$_FAIL_"
  551. output 2 "[DL] $type $label $__FAIL__\\n"
  552. echo "errorNoSSLSupport|${1}" >> "$sharedMemoryError"
  553. return 0
  554. fi
  555. while [ -z "$R_TMP" ] || [ -e "$R_TMP" ]; do
  556. R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
  557. done
  558. if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev/null || [ ! -s "$R_TMP" ]; then
  559. output 1 "$_FAIL_"
  560. output 2 "[DL] $type $label $__FAIL__\\n"
  561. echo "errorDownloadingList|${1}" >> "$sharedMemoryError"
  562. else
  563. sed -i "$filter" "$R_TMP"
  564. if [ ! -s "$R_TMP" ]; then
  565. output 1 "$_FAIL_"
  566. output 2 "[DL] $type $label $__FAIL__\\n"
  567. echo "errorParsingList|${1}" >> "$sharedMemoryError"
  568. else
  569. cat "${R_TMP}" >> "$D_TMP"
  570. output 1 "$_OK_"
  571. output 2 "[DL] $type $label $__OK__\\n"
  572. fi
  573. fi
  574. rm -f "$R_TMP"
  575. return 0
  576. }
  577. process_config_update() {
  578. local label R_TMP
  579. [ "$configUpdateEnabled" -eq 0 ] && return 0
  580. label="${1##*//}"; label="${label%%/*}";
  581. while [ -z "$R_TMP" ] || [ -e "$R_TMP" ]; do
  582. R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
  583. done
  584. if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev/null || [ ! -s "$R_TMP" ]; then
  585. output 1 "$_FAIL_"
  586. output 2 "[DL] Config Update: $label $__FAIL__\\n"
  587. jsonOps add error "errorDownloadingConfigUpdate"
  588. else
  589. if [ -s "$R_TMP" ] && sed -f "$R_TMP" -i /etc/config/simple-adblock 2>/dev/null; then
  590. output 1 "$_OK_"
  591. output 2 "[DL] Config Update: $label $__OK__\\n"
  592. else
  593. output 1 "$_FAIL_"
  594. output 2 "[DL] Config Update: $label $__FAIL__\\n"
  595. jsonOps add error "errorParsingConfigUpdate"
  596. fi
  597. fi
  598. rm -f "$R_TMP"
  599. return 0
  600. }
  601. download_lists() {
  602. local hf w_filter j=0 R_TMP
  603. jsonOps set message "$(getStatusText "statusDownloading")..."
  604. jsonOps set status "statusDownloading"
  605. rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
  606. if [ "$($awk '/^MemFree/ {print int($2/1000)}' "/proc/meminfo")" -lt 32 ]; then
  607. output 3 'Low free memory, restarting resolver... '
  608. if dnsOps 'quiet'; then
  609. output_okn
  610. else
  611. output_fail
  612. fi
  613. fi
  614. touch $A_TMP; touch $B_TMP;
  615. output 1 'Downloading lists '
  616. process_config_update "$configUpdateURL"
  617. rm -f "$sharedMemoryError"
  618. if [ -n "$blocked_hosts_urls" ]; then
  619. for hf in ${blocked_hosts_urls}; do
  620. if [ "$parallelDL" -gt 0 ]; then
  621. process_url "$hf" 'hosts' 'blocked' &
  622. else
  623. process_url "$hf" 'hosts' 'blocked'
  624. fi
  625. done
  626. fi
  627. if [ -n "$blocked_domains_urls" ]; then
  628. for hf in ${blocked_domains_urls}; do
  629. if [ "$parallelDL" -gt 0 ]; then
  630. process_url "$hf" 'domains' 'blocked' &
  631. else
  632. process_url "$hf" 'domains' 'blocked'
  633. fi
  634. done
  635. fi
  636. if [ -n "$allowed_domains_urls" ]; then
  637. for hf in ${allowed_domains_urls}; do
  638. if [ "$parallelDL" -gt 0 ]; then
  639. process_url "$hf" 'domains' 'allowed' &
  640. else
  641. process_url "$hf" 'domains' 'allowed'
  642. fi
  643. done
  644. fi
  645. wait
  646. output 1 '\n'
  647. if [ -s "$sharedMemoryError" ]; then
  648. while IFS= read -r line; do
  649. jsonOps add error "$line"
  650. done < "$sharedMemoryError"
  651. rm -f "$sharedMemoryError"
  652. fi
  653. [ -n "$blocked_domains" ] && for hf in ${blocked_domains}; do echo "$hf" | sed "$domainsFilter" >> $B_TMP; done
  654. allowed_domains="${allowed_domains}
  655. $(cat $A_TMP)"
  656. [ -n "$allowed_domains" ] && for hf in ${allowed_domains}; do hf="$(echo "$hf" | sed 's/\./\\./g')"; w_filter="$w_filter/${hf}$/d;"; done
  657. [ ! -s "$B_TMP" ] && return 1
  658. output 1 'Processing downloads '
  659. output 2 'Sorting combined list '
  660. jsonOps set message "$(getStatusText "statusProcessing"): sorting combined list"
  661. if [ "$allowIDN" -gt 0 ]; then
  662. if sort -u "$B_TMP" > "$A_TMP"; then
  663. output_ok
  664. else
  665. output_failn
  666. jsonOps add error "errorSorting"
  667. fi
  668. else
  669. if sort -u "$B_TMP" | grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
  670. output_ok
  671. else
  672. output_failn
  673. jsonOps add error "errorSorting"
  674. fi
  675. fi
  676. if [ "$targetDNS" = 'dnsmasq.conf' ] || \
  677. [ "$targetDNS" = 'dnsmasq.ipset' ] || \
  678. [ "$targetDNS" = 'dnsmasq.servers' ] || \
  679. [ "$targetDNS" = 'unbound.adb_list' ]; then
  680. # TLD optimization written by Dirk Brenken (dev@brenken.org)
  681. output 2 'Optimizing combined list '
  682. jsonOps set message "$(getStatusText "statusProcessing"): optimizing combined list"
  683. # sed -E 'G;:t;s/(.*)(\.)(.*)(\n)(.*)/\1\4\5\2\3/;tt;s/(.*)\n(\.)(.*)/\3\2\1/' is actually slower than command below
  684. if $awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$A_TMP" > "$B_TMP"; then
  685. if sort "$B_TMP" > "$A_TMP"; then
  686. if $awk '{if(NR=1){tld=$NF};while(getline){if($NF!~tld"\\."){print tld;tld=$NF}}print tld}' "$A_TMP" > "$B_TMP"; then
  687. if $awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$B_TMP" > "$A_TMP"; then
  688. if sort -u "$A_TMP" > "$B_TMP"; then
  689. output_ok
  690. else
  691. output_failn
  692. jsonOps add error "errorOptimization"
  693. mv "$A_TMP" "$B_TMP"
  694. fi
  695. else
  696. output_failn
  697. jsonOps add error "errorOptimization"
  698. fi
  699. else
  700. output_failn
  701. jsonOps add error "errorOptimization"
  702. mv "$A_TMP" "$B_TMP"
  703. fi
  704. else
  705. output_failn
  706. jsonOps add error "errorOptimization"
  707. fi
  708. else
  709. output_failn
  710. jsonOps add error "errorOptimization"
  711. mv "$A_TMP" "$B_TMP"
  712. fi
  713. else
  714. mv "$A_TMP" "$B_TMP"
  715. fi
  716. output 2 'Allowing domains '
  717. jsonOps set message "$(getStatusText "statusProcessing"): allowing domains"
  718. if sed -i "$w_filter" "$B_TMP"; then
  719. output_ok
  720. else
  721. output_failn
  722. jsonOps add error "errorAllowListProcessing"
  723. fi
  724. output 2 'Formatting merged file '
  725. jsonOps set message "$(getStatusText "statusProcessing"): formatting merged file"
  726. if [ -z "$outputFilterIPv6" ]; then
  727. if sed "$outputFilter" "$B_TMP" > "$A_TMP"; then
  728. output_ok
  729. else
  730. output_failn
  731. jsonOps add error "errorDataFileFormatting"
  732. fi
  733. else
  734. case "$targetDNS" in
  735. dnsmasq.addnhosts)
  736. if sed "$outputFilter" "$B_TMP" > "$A_TMP" && \
  737. sed "$outputFilterIPv6" "$B_TMP" >> "$A_TMP"; then
  738. output_ok
  739. else
  740. output_failn
  741. jsonOps add error "errorDataFileFormatting"
  742. fi
  743. ;;
  744. esac
  745. fi
  746. case "$targetDNS" in
  747. dnsmasq.addnhosts)
  748. output 2 'Creating DNSMASQ addnhosts file '
  749. jsonOps set message "$(getStatusText "statusProcessing"): creating DNSMASQ addnhosts file"
  750. ;;
  751. dnsmasq.conf)
  752. output 2 'Creating DNSMASQ config file '
  753. jsonOps set message "$(getStatusText "statusProcessing"): creating DNSMASQ config file"
  754. ;;
  755. dnsmasq.ipset)
  756. output 2 'Creating DNSMASQ ipset file '
  757. jsonOps set message "$(getStatusText "statusProcessing"): creating DNSMASQ ipset file"
  758. ;;
  759. dnsmasq.servers)
  760. output 2 'Creating DNSMASQ servers file '
  761. jsonOps set message "$(getStatusText "statusProcessing"): creating DNSMASQ servers file"
  762. ;;
  763. unbound.adb_list)
  764. output 2 'Creating Unbound adb_list file '
  765. jsonOps set message "$(getStatusText "statusProcessing"): creating Unbound adb_list file"
  766. ;;
  767. esac
  768. if mv "$A_TMP" "$outputFile"; then
  769. output_ok
  770. else
  771. output_failn
  772. jsonOps add error "errorMovingDataFile"
  773. fi
  774. if [ "$compressedCache" -gt 0 ]; then
  775. output 2 'Creating compressed cache '
  776. jsonOps set message "$(getStatusText "statusProcessing"): creating compressed cache"
  777. if cacheOps 'createGzip'; then
  778. output_ok
  779. else
  780. output_failn
  781. jsonOps add error "errorCreatingCompressedCache"
  782. fi
  783. else
  784. rm -f "$outputGzip"
  785. fi
  786. output 2 'Removing temporary files '
  787. jsonOps set message "$(getStatusText "statusProcessing"): removing temporary files"
  788. rm -f "/tmp/${packageName}_tmp.*" "$A_TMP" "$B_TMP" "$outputCache" || j=1
  789. if [ $j -eq 0 ]; then
  790. output_ok
  791. else
  792. output_failn
  793. jsonOps add error "errorRemovingTempFiles"
  794. fi
  795. output 1 '\n'
  796. }
  797. boot() {
  798. load_package_config
  799. sleep "$bootDelay"
  800. rc_procd start_service 'on_boot' && rc_procd service_triggers
  801. }
  802. start_service() {
  803. is_enabled 'on_start' || return 1
  804. local action status error message stats c
  805. status="$(jsonOps get status)"
  806. error="$(jsonOps get error)"
  807. message="$(jsonOps get message)"
  808. stats="$(jsonOps get stats)"
  809. action="$(jsonOps get triggers)"
  810. if [ "$action" = 'on_boot' ] || [ "$1" = 'on_boot' ]; then
  811. if cacheOps 'testGzip' || cacheOps 'test'; then
  812. action='restore'
  813. else
  814. action='download'
  815. fi
  816. elif [ "$action" = 'download' ] || [ "$1" = 'download' ] || [ -n "$error" ]; then
  817. action='download'
  818. elif [ ! -s "$outputFile" ]; then
  819. if cacheOps 'testGzip' || cacheOps 'test'; then
  820. action='restore'
  821. else
  822. action='download'
  823. fi
  824. elif [ "$action" = 'restart' ] || [ "$1" = 'restart' ]; then
  825. action='restart'
  826. elif [ -s "$outputFile" ] && [ "$status" = "statusSuccess" ] && [ -z "$error" ]; then
  827. showstatus
  828. exit 0
  829. else
  830. action='download'
  831. fi
  832. jsonOps del all
  833. jsonOps set triggers
  834. procd_open_instance 'main'
  835. procd_set_param command /bin/true
  836. procd_set_param stdout 1
  837. procd_set_param stderr 1
  838. procd_open_data
  839. json_add_array firewall
  840. if [ "$forceDNS" -ne 0 ]; then
  841. for c in $forceDNSPorts; do
  842. if netstat -tuln | grep LISTEN | grep ":${c}" >/dev/null 2>&1; then
  843. json_add_object ''
  844. json_add_string type 'redirect'
  845. json_add_string target 'DNAT'
  846. json_add_string src 'lan'
  847. json_add_string proto 'tcp udp'
  848. json_add_string src_dport "$c"
  849. json_add_string dest_port "$c"
  850. json_add_string reflection '0'
  851. json_close_object
  852. else
  853. json_add_object ''
  854. json_add_string type 'rule'
  855. json_add_string src 'lan'
  856. json_add_string dest '*'
  857. json_add_string proto 'tcp udp'
  858. json_add_string dest_port "$c"
  859. json_add_string target 'REJECT'
  860. json_close_object
  861. fi
  862. done
  863. fi
  864. if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
  865. json_add_object ''
  866. json_add_string type 'ipset'
  867. json_add_string name 'adb'
  868. json_add_string match 'dest_net'
  869. json_add_string storage 'hash'
  870. json_close_object
  871. json_add_object ''
  872. json_add_string type 'rule'
  873. json_add_string ipset 'adb'
  874. json_add_string src 'lan'
  875. json_add_string dest '*'
  876. json_add_string proto 'tcp udp'
  877. json_add_string target 'REJECT'
  878. json_close_object
  879. fi
  880. json_close_array
  881. procd_close_data
  882. procd_close_instance
  883. if [ "$action" = 'restore' ]; then
  884. output 0 "Starting $serviceName... "
  885. output 3 "Starting $serviceName...\\n"
  886. jsonOps set status "statusStarting"
  887. if cacheOps 'testGzip' && ! cacheOps 'test' && [ ! -s "$outputFile" ]; then
  888. output 3 'Found compressed cache file, unpacking it '
  889. jsonOps set message 'found compressed cache file, unpacking it.'
  890. if cacheOps 'unpackGzip'; then
  891. output_okn
  892. else
  893. output_fail
  894. jsonOps add error "errorRestoreCompressedCache"
  895. output "$_ERROR_: $(getErrorText 'errorRestoreCompressedCache')!\\n"
  896. action='download'
  897. fi
  898. fi
  899. if cacheOps 'test' && [ ! -s "$outputFile" ]; then
  900. output 3 'Found cache file, reusing it '
  901. jsonOps set message 'found cache file, reusing it.'
  902. if cacheOps 'restore'; then
  903. output_okn
  904. dnsOps 'on_start'
  905. else
  906. output_fail
  907. jsonOps add error "errorRestoreCache"
  908. output "$_ERROR_: $(getErrorText 'errorRestoreCache')!\\n"
  909. action='download'
  910. fi
  911. fi
  912. fi
  913. if [ "$action" = 'download' ]; then
  914. if [ -s "$outputFile" ] || cacheOps 'test' || cacheOps 'testGzip'; then
  915. output 0 "Force-reloading $serviceName... "
  916. output 3 "Force-reloading $serviceName...\\n"
  917. jsonOps set status "statusForceReloading"
  918. else
  919. output 0 "Starting $serviceName... "
  920. output 3 "Starting $serviceName...\\n"
  921. jsonOps set status "statusStarting"
  922. fi
  923. download_lists
  924. dnsOps 'on_start'
  925. fi
  926. if [ "$action" = 'restart' ]; then
  927. output 0 "Restarting $serviceName... "
  928. output 3 "Restarting $serviceName...\\n"
  929. jsonOps set status "statusRestarting"
  930. dnsOps 'on_start'
  931. fi
  932. if [ "$action" = 'start' ]; then
  933. output 0 "Starting $serviceName... "
  934. output 3 "Starting $serviceName...\\n"
  935. jsonOps set status "statusStarting"
  936. dnsOps 'on_start'
  937. fi
  938. if [ -s "$outputFile" ] && [ "$(jsonOps get status)" != "statusFail" ]; then
  939. output 0 "$__OK__\\n";
  940. jsonOps del message
  941. jsonOps set status "statusSuccess"
  942. jsonOps set stats "$serviceName is blocking $(wc -l < "$outputFile") domains (with ${targetDNS})"
  943. showstatus
  944. else
  945. output 0 "$__FAIL__\\n";
  946. jsonOps set status "statusFail"
  947. jsonOps add error "errorOhSnap"
  948. showstatus
  949. fi
  950. }
  951. service_started() { procd_set_config_changed firewall; }
  952. service_stopped() { procd_set_config_changed firewall; }
  953. restart_service() { rc_procd start_service 'restart'; }
  954. reload_service() { restart_service; }
  955. restart() { restart_service; }
  956. reload() { restart_service; }
  957. dl() { rc_procd start_service 'download'; }
  958. killcache() {
  959. rm -f "$addnhostsCache" "$addnhostsGzip"
  960. rm -f "$dnsmasqCache" "$dnsmasqGzip"
  961. rm -f "$ipsetCache" "$ipsetGzip"
  962. rm -f "$serversCache" "$serversGzip"
  963. rm -f "$unboundCache" "$unboundGzip"
  964. config_load 'dhcp'
  965. config_foreach dnsmasqOps 'dnsmasq' 'cleanup'
  966. uci -q commit 'dhcp'
  967. return 0
  968. }
  969. show() { showstatus; }
  970. status_service() { showstatus; }
  971. showstatus() {
  972. local c url status message error stats
  973. status="$(jsonOps get status)"
  974. message="$(jsonOps get message)"
  975. error="$(jsonOps get error)"
  976. stats="$(jsonOps get stats)"
  977. if [ "$status" = "statusSuccess" ]; then
  978. output "$stats "; output_okn;
  979. else
  980. [ -n "$status" ] && status="$(getStatusText "$status")"
  981. if [ -n "$status" ] && [ -n "$message" ]; then
  982. status="${status}: $message"
  983. fi
  984. [ -n "$status" ] && output "$serviceName $status\\n"
  985. fi
  986. if [ -n "$error" ]; then
  987. for c in $error; do
  988. url="${c##*|}"
  989. c="${c%|*}"
  990. case "$c" in
  991. errorDownloadingList|errorParsingList)
  992. output "$_ERROR_: $(getErrorText "$c") $url!\\n";;
  993. *)
  994. output "$_ERROR_: $(getErrorText "$c")!\\n";;
  995. esac
  996. let n=n+1
  997. done
  998. fi
  999. }
  1000. stop_service() {
  1001. load_package_config
  1002. if [ -s "$outputFile" ]; then
  1003. output "Stopping $serviceName... "
  1004. cacheOps 'create'
  1005. if dnsOps 'on_stop'; then
  1006. led_off "$led"
  1007. output 0 "$__OK__\\n"; output_okn;
  1008. jsonOps set status "statusStopped"
  1009. jsonOps del message
  1010. else
  1011. output 0 "$__FAIL__\\n"; output_fail;
  1012. jsonOps set status "statusFail"
  1013. jsonOps add error "errorStopping"
  1014. output "$_ERROR_: $(getErrorText 'errorStopping')!\\n"
  1015. fi
  1016. fi
  1017. }
  1018. service_triggers() {
  1019. procd_open_trigger
  1020. procd_add_config_trigger 'config.change' "${packageName}" /etc/init.d/${packageName} reload
  1021. procd_add_reload_interface_trigger 'wan'
  1022. procd_add_interface_trigger 'interface.*.up' 'wan' /etc/init.d/${packageName} reload
  1023. procd_close_trigger
  1024. }
  1025. check() {
  1026. load_package_config
  1027. local c string="$1"
  1028. c="$(grep -c "$string" "$outputFile")"
  1029. if [ ! -s "$outputFile" ]; then
  1030. echo "No block-list ('$outputFile') found."
  1031. elif [ -z "$string" ]; then
  1032. echo "Usage: /etc/init.d/${packageName} check string"
  1033. elif [ "$c" -gt 0 ]; then
  1034. if [ "$c" -gt 1 ]; then
  1035. echo "Found $c matches for '$string' in '$outputFile':"
  1036. else
  1037. echo "Found 1 match for '$string' in '$outputFile':"
  1038. fi
  1039. case "$targetDNS" in
  1040. dnsmasq.addnhosts)
  1041. grep "$string" "$outputFile" | sed 's|^127.0.0.1 ||;s|^:: ||;';;
  1042. dnsmasq.conf)
  1043. grep "$string" "$outputFile" | sed 's|local=/||;s|/$||;';;
  1044. dnsmasq.ipset)
  1045. grep "$string" "$outputFile" | sed 's|ipset=/||;s|/adb$||;';;
  1046. dnsmasq.servers)
  1047. grep "$string" "$outputFile" | sed 's|server=/||;s|/$||;';;
  1048. unbound.adb_list)
  1049. grep "$string" "$outputFile" | sed 's|^local-zone: "||;s|" static$||;';;
  1050. esac
  1051. else
  1052. echo "The $string is not found in current block-list ('$outputFile')."
  1053. fi
  1054. }
  1055. sizes() {
  1056. local i
  1057. load_package_config
  1058. echo "# $(date)"
  1059. for i in $blocked_domains_urls; do
  1060. [ "${i//melmac}" != "$i" ] && continue
  1061. if $dl_command "$i" $dl_flag /tmp/sast 2>/dev/null && [ -s /tmp/sast ]; then
  1062. echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
  1063. if compare_values "$(du -sk /tmp/sast)" "500"; then
  1064. echo "# block-list too big for most routers"
  1065. elif compare_values "$(du -sk /tmp/sast)" "100"; then
  1066. echo "# block-list may be too big for some routers"
  1067. fi
  1068. rm -rf /tmp/sast
  1069. echo " list blocked_domains_url '$i'"
  1070. echo ""
  1071. else
  1072. echo "# site was down on last check"
  1073. echo "# list blocked_domains_url '$i'"
  1074. echo ""
  1075. fi
  1076. done
  1077. for i in $blocked_hosts_urls; do
  1078. if $dl_command "$i" $dl_flag /tmp/sast 2>/dev/null && [ -s /tmp/sast ]; then
  1079. echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
  1080. if compare_values "$(du -sk /tmp/sast)" "500"; then
  1081. echo "# block-list too big for most routers"
  1082. elif compare_values "$(du -sk /tmp/sast)" "100"; then
  1083. echo "# block-list may be too big for some routers"
  1084. fi
  1085. rm -rf /tmp/sast
  1086. echo " list blocked_hosts_url '$i'"
  1087. echo ""
  1088. else
  1089. echo "# site was down on last check"
  1090. echo "# list blocked_hosts_url '$i'"
  1091. echo ""
  1092. fi
  1093. done
  1094. }