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.

1132 lines
37 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='dev-test'
  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() { printf "%b" "$1" | sed -e "s/$(printf "%b" "$2")/$(printf "%b" "$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_hup() { killall -q -HUP dnsmasq; }
  76. dnsmasq_kill() { killall -q -KILL dnsmasq; }
  77. dnsmasq_restart() { /etc/init.d/dnsmasq restart >/dev/null 2>&1; }
  78. unbound_restart() { /etc/init.d/unbound restart >/dev/null 2>&1; }
  79. output() {
  80. # Can take a single parameter (text) to be output at any verbosity
  81. # Or target verbosity level and text to be output at specifc verbosity
  82. local msg memmsg
  83. if [ $# -ne 1 ]; then
  84. if [ $((verbosity & $1)) -gt 0 ] || [ "$verbosity" = "$1" ]; then shift; else return 0; fi
  85. fi
  86. [ -t 1 ] && printf "%b" "$1"
  87. msg="$(printf "%s" "$(str_replace "$1" "$serviceName " "service ")" | sed 's|\\033\[[0-9]\?;\?[0-9]\?[0-9]\?m||g')";
  88. if [ "$(printf "%b" "$msg" | wc -l)" -gt 0 ]; then
  89. [ -s "$sharedMemoryOutput" ] && memmsg="$(cat "$sharedMemoryOutput")"
  90. logger -t "${packageName:-service} [$$]" "$(printf "%b" "${memmsg}${msg}")"
  91. rm -f "$sharedMemoryOutput"
  92. else
  93. printf "%b" "$msg" >> "$sharedMemoryOutput"
  94. fi
  95. }
  96. export serviceEnabled forceDNS parallelDL debug allowIDN compressedCache
  97. export targetDNS bootDelay dlTimeout curlRetry verbosity led dnsInstance
  98. export whitelist_domains blacklist_domains
  99. export whitelist_domains_urls blacklist_domains_urls blacklist_hosts_urls
  100. export wan_if wan_gw wanphysdev dl_command serviceStatus dl_flag
  101. export outputFilter outputFilterIPv6 outputFile outputGzip outputCache ipv6Enabled
  102. load_package_config() {
  103. config_load "$packageName"
  104. config_get_bool serviceEnabled 'config' 'enabled' 1
  105. config_get_bool forceDNS 'config' 'force_dns' 1
  106. config_get_bool parallelDL 'config' 'parallel_downloads' 1
  107. config_get_bool debug 'config' 'debug' 0
  108. config_get_bool compressedCache 'config' 'compressed_cache' 0
  109. config_get_bool ipv6Enabled 'config' 'ipv6_enabled' 0
  110. config_get bootDelay 'config' 'boot_delay' '120'
  111. config_get dlTimeout 'config' 'download_timeout' '20'
  112. config_get curlRetry 'config' 'curl_retry' '3'
  113. config_get verbosity 'config' 'verbosity' '2'
  114. config_get led 'config' 'led'
  115. config_get targetDNS 'config' 'dns' 'dnsmasq.servers'
  116. config_get dnsInstance 'config' 'dns_instance' '0'
  117. config_get whitelist_domains 'config' 'whitelist_domain'
  118. config_get blacklist_domains 'config' 'blacklist_domain'
  119. config_get whitelist_domains_urls 'config' 'whitelist_domains_url'
  120. config_get blacklist_domains_urls 'config' 'blacklist_domains_url'
  121. config_get blacklist_hosts_urls 'config' 'blacklist_hosts_url'
  122. if [ "$targetDNS" != 'dnsmasq.addnhosts' ] && [ "$targetDNS" != 'dnsmasq.conf' ] && \
  123. [ "$targetDNS" != 'dnsmasq.servers' ] && [ "$targetDNS" != 'unbound.adb_list' ] && \
  124. [ "$targetDNS" != 'dnsmasq.ipset' ] ; then
  125. targetDNS='dnsmasq.servers'
  126. fi
  127. case "$targetDNS" in
  128. dnsmasq.addnhosts)
  129. outputFilter="$addnhostsOutputFilter"
  130. outputFile="$addnhostsFile"
  131. outputCache="$addnhostsCache"
  132. outputGzip="$addnhostsGzip"
  133. [ "$ipv6Enabled" -gt 0 ] && outputFilterIPv6="$addnhostsOutputFilterIPv6"
  134. rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
  135. rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
  136. rm -f "$serversFile" "$serversCache" "$serversGzip"
  137. rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
  138. ;;
  139. dnsmasq.conf)
  140. outputFilter="$dnsmasqOutputFilter"
  141. outputFile="$dnsmasqFile"
  142. outputCache="$dnsmasqCache"
  143. outputGzip="$dnsmasqGzip"
  144. rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
  145. rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
  146. rm -f "$serversFile" "$serversCache" "$serversGzip"
  147. rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
  148. ;;
  149. dnsmasq.ipset)
  150. outputFilter="$ipsetOutputFilter"
  151. outputFile="$ipsetFile"
  152. outputCache="$ipsetCache"
  153. outputGzip="$ipsetGzip"
  154. rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
  155. rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
  156. rm -f "$serversFile" "$serversCache" "$serversGzip"
  157. rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
  158. ;;
  159. dnsmasq.servers)
  160. outputFilter="$serversOutputFilter"
  161. outputFile="$serversFile"
  162. outputCache="$serversCache"
  163. outputGzip="$serversGzip"
  164. rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
  165. rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
  166. rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
  167. rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
  168. ;;
  169. unbound.adb_list)
  170. outputFilter="$unboundOutputFilter"
  171. outputFile="$unboundFile"
  172. outputCache="$unboundCache"
  173. outputGzip="$unboundGzip"
  174. rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
  175. rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
  176. rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
  177. rm -f "$serversFile" "$serversCache" "$serversGzip"
  178. ;;
  179. esac
  180. if [ -z "${verbosity##*[!0-9]*}" ] || [ "$verbosity" -lt 0 ] || [ "$verbosity" -gt 2 ]; then
  181. verbosity=1
  182. fi
  183. . /lib/functions/network.sh
  184. . /usr/share/libubox/jshn.sh
  185. # Prefer curl because it supports the file: scheme.
  186. if [ -x /usr/bin/curl ] ; then
  187. dl_command="curl --insecure --retry $curlRetry --connect-timeout $dlTimeout --silent"
  188. dl_flag="-o"
  189. else
  190. dl_command="wget --no-check-certificate --timeout $dlTimeout -q"
  191. dl_flag="-O"
  192. fi
  193. led="${led:+/sys/class/leds/$led}"
  194. }
  195. is_enabled() {
  196. load_package_config
  197. if [ "$debug" -ne 0 ]; then
  198. exec 1>>/tmp/simple-adblock.log
  199. exec 2>&1
  200. set -x
  201. fi
  202. if [ "$serviceEnabled" -eq 0 ]; then
  203. case "$1" in
  204. on_start)
  205. output "$packageName is currently disabled.\\n"
  206. output "Run the following commands before starting service again:\\n"
  207. output "uci set ${packageName}.config.enabled='1'; uci commit $packageName;\\n"
  208. ;;
  209. esac
  210. return 1
  211. fi
  212. case $targetDNS in
  213. dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers)
  214. if dnsmasq -v 2>/dev/null | grep -q 'no-IDN' || ! dnsmasq -v 2>/dev/null | grep -q -w 'IDN'; then
  215. allowIDN=0
  216. else
  217. allowIDN=1
  218. fi
  219. ;;
  220. unbound.adb_list)
  221. allowIDN=1;;
  222. esac
  223. case $targetDNS in
  224. dnsmasq.ipset)
  225. if dnsmasq -v 2>/dev/null | grep -q 'no-ipset' || ! dnsmasq -v 2>/dev/null | grep -q -w 'ipset'; then
  226. output "$_ERROR_: DNSMASQ ipset support is enabled in $packageName, but DNSMASQ is either not installed or installed DNSMASQ does not support ipsets!\\n"
  227. targetDNS='dnsmasq.servers'
  228. fi
  229. if ! ipset help hash:net >/dev/null 2>&1; then
  230. 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"
  231. targetDNS='dnsmasq.servers'
  232. fi
  233. ;;
  234. esac
  235. [ ! -d "${outputFile%/*}" ] && mkdir -p "${outputFile%/*}"
  236. [ ! -d "${outputCache%/*}" ] && mkdir -p "${outputFile%/*}"
  237. [ ! -d "${outputGzip%/*}" ] && mkdir -p "${outputFile%/*}"
  238. cacheOps 'testGzip' && return 0
  239. network_flush_cache; network_find_wan wan_if; network_get_gateway wan_gw "$wan_if";
  240. [ -n "$wan_gw" ] && return 0
  241. output "$_ERROR_: $serviceName failed to discover WAN gateway.\\n"; return 1;
  242. }
  243. dnsOps() {
  244. local param output_text
  245. case $1 in
  246. on_start)
  247. if [ ! -s "$outputFile" ]; then
  248. tmpfs set status "$statusFail"
  249. tmpfs add error "Error: Failed to create $outputFile file."
  250. output "$_ERROR_: $serviceName failed to create its data file!\\n"
  251. return 1
  252. fi
  253. case "$targetDNS" in
  254. dnsmasq.addnhosts)
  255. if [ "$(uci -q get dhcp.@dnsmasq["$dnsInstance"].serversfile)" = "$serversFile" ]; then
  256. uci -q del dhcp.@dnsmasq["$dnsInstance"].serversfile
  257. fi
  258. if ! uci -q get dhcp.@dnsmasq["$dnsInstance"].addnhosts | grep -q "$addnhostsFile"; then
  259. uci add_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
  260. fi
  261. param=dnsmasq_hup
  262. output_text='Reloading DNSMASQ'
  263. ;;
  264. dnsmasq.conf)
  265. uci -q del_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
  266. if [ "$(uci -q get dhcp.@dnsmasq["$dnsInstance"].serversfile)" = "$serversFile" ]; then
  267. uci -q del dhcp.@dnsmasq["$dnsInstance"].serversfile
  268. fi
  269. param=dnsmasq_restart
  270. output_text='Restarting DNSMASQ'
  271. ;;
  272. dnsmasq.ipset)
  273. uci -q del_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
  274. if [ "$(uci -q get dhcp.@dnsmasq["$dnsInstance"].serversfile)" = "$serversFile" ]; then
  275. uci -q del dhcp.@dnsmasq["$dnsInstance"].serversfile
  276. fi
  277. param=dnsmasq_restart
  278. output_text='Restarting DNSMASQ'
  279. ;;
  280. dnsmasq.servers)
  281. uci -q del_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
  282. if [ "$(uci -q get dhcp.@dnsmasq["$dnsInstance"].serversfile)" != "$serversFile" ]; then
  283. uci set dhcp.@dnsmasq["$dnsInstance"].serversfile="$serversFile"
  284. fi
  285. param=dnsmasq_hup
  286. output_text='Reloading DNSMASQ'
  287. ;;
  288. unbound.adb_list)
  289. uci -q del_list dhcp.@dnsmasq["$dnsInstance"].addnhosts="$addnhostsFile"
  290. if [ "$(uci -q get dhcp.@dnsmasq["$dnsInstance"].serversfile)" = "$serversFile" ]; then
  291. uci -q del dhcp.@dnsmasq["$dnsInstance"].serversfile
  292. fi
  293. param=unbound_restart
  294. output_text='Restarting Unbound'
  295. ;;
  296. esac
  297. if [ -n "$(uci changes dhcp)" ]; then
  298. uci commit dhcp
  299. if [ "$param" = 'unbound_restart' ]; then
  300. param='dnsmasq_restart; unbound_restart;'
  301. output_text='Restarting Unbound/DNSMASQ'
  302. else
  303. param=dnsmasq_restart
  304. output_text='Restarting DNSMASQ'
  305. fi
  306. fi
  307. output 1 "$output_text "
  308. output 2 "$output_text "
  309. tmpfs set message "$output_text"
  310. if eval "$param"; then
  311. tmpfs set status "$statusSuccess"
  312. led_on "$led"
  313. output_okn
  314. else
  315. output_fail
  316. tmpfs set status "$statusFail"
  317. tmpfs add error "Error: $output_text error."
  318. output "$_ERROR_: $serviceName $output_text error!\\n"
  319. return 1
  320. fi
  321. ;;
  322. on_stop)
  323. case "$targetDNS" in
  324. dnsmasq.addnhosts | dnsmasq.servers)
  325. param=dnsmasq_hup
  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. [ -s "$outputFile" ] && { mv -f "$outputFile" "$outputCache"; true > "$outputFile"; } >/dev/null 2>/dev/null
  461. return $?
  462. ;;
  463. restore|use)
  464. [ -s "$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 dnsOps '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. output 3 "$serviceName: another instance is starting up "; output_fail
  824. return 0
  825. fi
  826. status="$(tmpfs get status)"
  827. error="$(tmpfs get error)"
  828. message="$(tmpfs get message)"
  829. stats="$(tmpfs get stats)"
  830. action="$(tmpfs get triggers)"
  831. if [ "$action" = 'download' ] || [ "$1" = 'download' ] || [ -n "$error" ]; then
  832. action='download'
  833. elif [ ! -s "$outputFile" ] && ! cacheOps 'test' && ! cacheOps 'testGzip'; then
  834. action='download'
  835. elif [ ! -s "$outputFile" ] && cacheOps 'testGzip' || cacheOps 'test'; then
  836. action='restore'
  837. elif [ "$action" = 'restart' ] || [ "$1" = 'restart' ]; then
  838. action='restart'
  839. elif [ -s "$outputFile" ] && [ -n "$status" ] && [ -z "$error" ]; then
  840. if [ "$1" != 'hotplug' ]; then status; fi
  841. exit 0
  842. else
  843. action='download'
  844. fi
  845. tmpfs del all
  846. tmpfs set triggers
  847. if is_chaos_calmer || ! is_ipset_procd; then
  848. if [ "$forceDNS" -ne 0 ]; then
  849. fw3Ops 'insert' 'dns_redirect'
  850. else
  851. fw3Ops 'remove' 'dns_redirect'
  852. fi
  853. if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
  854. fw3Ops 'insert' 'ipset'
  855. else
  856. fw3Ops 'remove' 'ipset'
  857. fi
  858. procd_open_instance 'main'
  859. procd_set_param command /bin/true
  860. procd_set_param stdout 1
  861. procd_set_param stderr 1
  862. procd_close_instance
  863. else
  864. procd_open_instance 'main'
  865. procd_set_param command /bin/true
  866. procd_set_param stdout 1
  867. procd_set_param stderr 1
  868. procd_open_data
  869. json_add_array firewall
  870. if [ "$forceDNS" -ne 0 ]; then
  871. json_add_object ''
  872. json_add_string type redirect
  873. json_add_string name simple_adblock_dns_redirect
  874. json_add_string target DNAT
  875. json_add_string src lan
  876. json_add_string proto tcpudp
  877. json_add_string src_dport 53
  878. json_add_string dest_port 53
  879. json_add_string reflection 0
  880. json_close_object
  881. fi
  882. if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
  883. json_add_object ''
  884. json_add_string type ipset
  885. json_add_string name adb
  886. json_add_string match dest_net
  887. json_add_string storage hash
  888. json_add_string enabled 1
  889. json_close_object
  890. json_add_object ''
  891. json_add_string type rule
  892. json_add_string name simple_adblock_ipset_rule
  893. json_add_string ipset adb
  894. json_add_string src lan
  895. json_add_string dest '*'
  896. json_add_string proto tcpudp
  897. json_add_string target REJECT
  898. json_add_string enabled 1
  899. json_close_object
  900. fi
  901. json_close_array
  902. procd_close_data
  903. procd_close_instance
  904. fi
  905. if [ "$action" = 'restore' ]; then
  906. output 0 "Starting $serviceName... "
  907. output 3 "Starting $serviceName...\\n"
  908. tmpfs set status "$statusStarting"
  909. if cacheOps 'testGzip' && ! cacheOps 'test' && [ ! -s "$outputFile" ]; then
  910. output 3 'Found compressed cache file, unpacking it '
  911. tmpfs set message 'found compressed cache file, unpacking it.'
  912. if cacheOps 'unpackGzip'; then
  913. output_okn
  914. else
  915. output_fail
  916. output "$_ERROR_: $serviceName failed to unpack compressed cache!\\n"
  917. action='download'
  918. fi
  919. fi
  920. if cacheOps 'test' && [ ! -s "$outputFile" ]; then
  921. output 3 'Found cache file, reusing it '
  922. tmpfs set message 'found cache file, reusing it.'
  923. if cacheOps 'restore'; then
  924. output_okn
  925. dnsOps 'on_start'
  926. else
  927. output_fail
  928. output "$_ERROR_: $serviceName failed to move '$outputCache' to '$outputFile'!\\n"
  929. action='download'
  930. fi
  931. fi
  932. fi
  933. case "$action" in
  934. download)
  935. if [ -s "$outputFile" ] || cacheOps 'test' || cacheOps 'testGzip'; then
  936. output 0 "Force-reloading $serviceName... "
  937. output 3 "Force-reloading $serviceName...\\n"
  938. tmpfs set status "$statusForceReloading"
  939. else
  940. output 0 "Starting $serviceName... "
  941. output 3 "Starting $serviceName...\\n"
  942. tmpfs set status "$statusStarting"
  943. fi
  944. download_lists
  945. dnsOps 'on_start'
  946. ;;
  947. restart)
  948. output 0 "Restarting $serviceName... "
  949. output 3 "Restarting $serviceName...\\n"
  950. tmpfs set status "$statusRestarting"
  951. dnsOps 'on_start'
  952. ;;
  953. start)
  954. output 0 "Starting $serviceName... "
  955. output 3 "Starting $serviceName...\\n"
  956. tmpfs set status "$statusStarting"
  957. dnsOps 'on_start'
  958. ;;
  959. esac
  960. if [ -s "$outputFile" ] && [ "$(tmpfs get status)" != "$statusFail" ]; then
  961. output 0 "$__OK__\\n";
  962. c="$(wc -l < "$outputFile")"
  963. output 3 "$serviceName is blocking $c domains (with ${targetDNS}) "; output_okn
  964. tmpfs del message
  965. tmpfs set status "$statusSuccess: $c domains blocked (with ${targetDNS})."
  966. error="$(tmpfs get error)"
  967. if [ -n "$error" ]; then
  968. output "$(str_replace "$error" "Error:" "$_ERROR_:")\\n"
  969. fi
  970. else
  971. output 0 "$__FAIL__\\n";
  972. tmpfs set status "$statusFail"
  973. tmpfs add error 'Error: Failed to create blocklist or restart DNS resolver.'
  974. fi
  975. remove_lock
  976. }
  977. service_started() { is_ipset_procd && procd_set_config_changed firewall; }
  978. service_stopped() { is_ipset_procd && procd_set_config_changed firewall; }
  979. restart_service() { rc_procd start_service 'restart'; }
  980. reload_service() { restart_service; }
  981. restart() { restart_service; }
  982. reload() { restart_service; }
  983. dl() { rc_procd start_service 'download'; }
  984. killcache() {
  985. rm -f "$addnhostsCache" "$addnhostsGzip"
  986. rm -f "$dnsmasqCache" "$dnsmasqGzip"
  987. rm -f "$ipsetCache" "$ipsetGzip"
  988. rm -f "$serversCache" "$serversGzip"
  989. rm -f "$unboundCache" "$unboundGzip"
  990. return 0
  991. }
  992. status_service() {
  993. local status="$(tmpfs get status)" error="$(tmpfs get error)" message="$(tmpfs get message)"
  994. if [ -n "$status" ] && [ -n "$message" ]; then
  995. status="${status}: $message"
  996. fi
  997. [ -n "$status" ] && output "$serviceName $status\\n"
  998. [ -n "$error" ] && output "$error\\n"
  999. }
  1000. stop_service() {
  1001. load_package_config
  1002. fw3Ops 'remove' 'all'
  1003. if [ -s "$outputFile" ]; then
  1004. output "Stopping $serviceName... "
  1005. cacheOps 'create'
  1006. if dnsOps 'on_stop'; then
  1007. led_off "$led"
  1008. output 0 "$__OK__\\n"; output_okn
  1009. tmpfs set status "$statusStopped"
  1010. tmpfs del message
  1011. else
  1012. output 0 "$__FAIL__\\n"; output_fail
  1013. tmpfs set status "$statusFail"
  1014. tmpfs add error "Error: error stopping $serviceName."
  1015. output "$_ERROR_: error stopping $serviceName!\\n"
  1016. fi
  1017. fi
  1018. }
  1019. check() {
  1020. load_package_config
  1021. local string="$1"
  1022. local c="$(grep -c "$string" "$outputFile")"
  1023. if [ ! -s "$outputFile" ]; then
  1024. echo "No blacklist ('$outputFile') found."
  1025. elif [ -z "$string" ]; then
  1026. echo "Usage: /etc/init.d/${packageName} check string"
  1027. elif [ "$c" -gt 0 ]; then
  1028. if [ "$c" -gt 1 ]; then
  1029. echo "Found $c matches for '$string' in '$outputFile':"
  1030. else
  1031. echo "Found 1 match for '$string' in '$outputFile':"
  1032. fi
  1033. case "$targetDNS" in
  1034. dnsmasq.addnhosts)
  1035. grep "$string" "$outputFile" | sed 's|^127.0.0.1 ||;s|^:: ||;';;
  1036. dnsmasq.conf)
  1037. grep "$string" "$outputFile" | sed 's|local=/||;s|/$||;';;
  1038. dnsmasq.ipset)
  1039. grep "$string" "$outputFile" | sed 's|ipset=/||;s|/adb$||;';;
  1040. dnsmasq.servers)
  1041. grep "$string" "$outputFile" | sed 's|server=/||;s|/$||;';;
  1042. unbound.adb_list)
  1043. grep "$string" "$outputFile" | sed 's|^local-zone: "||;s|" static$||;';;
  1044. esac
  1045. else
  1046. echo "The $string is not found in current blacklist ('$outputFile')."
  1047. fi
  1048. }
  1049. sizes() {
  1050. local i
  1051. load_package_config
  1052. echo "# $(date)"
  1053. for i in $blacklist_domains_urls; do
  1054. [ "${i//melmac}" != "$i" ] && continue
  1055. if $dl_command "$i" $dl_flag /tmp/sast 2>/dev/null && [ -s /tmp/sast ]; then
  1056. echo "# File size: $(du -sh /tmp/sast | awk '{print $1}')"
  1057. if compare_versions "$(du -sk /tmp/sast)" "500"; then
  1058. echo "# blocklist too big for most routers"
  1059. elif compare_versions "$(du -sk /tmp/sast)" "100"; then
  1060. echo "# blocklist may be too big for some routers"
  1061. fi
  1062. rm -rf /tmp/sast
  1063. echo " list blacklist_domains_url '$i'"
  1064. echo ""
  1065. else
  1066. echo "# site was down on last check"
  1067. echo "# list blacklist_domains_url '$i'"
  1068. echo ""
  1069. fi
  1070. done
  1071. for i in $blacklist_hosts_urls; do
  1072. if $dl_command "$i" $dl_flag /tmp/sast 2>/dev/null && [ -s /tmp/sast ]; then
  1073. echo "# File size: $(du -sh /tmp/sast | awk '{print $1}')"
  1074. if compare_versions "$(du -sk /tmp/sast)" "500"; then
  1075. echo "# blocklist too big for most routers"
  1076. elif compare_versions "$(du -sk /tmp/sast)" "100"; then
  1077. echo "# blocklist may be too big for some routers"
  1078. fi
  1079. rm -rf /tmp/sast
  1080. echo " list blacklist_hosts_url '$i'"
  1081. echo ""
  1082. else
  1083. echo "# site was down on last check"
  1084. echo "# list blacklist_hosts_url '$i'"
  1085. echo ""
  1086. fi
  1087. done
  1088. }