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.

1130 lines
36 KiB

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