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.

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