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.

693 lines
20 KiB

  1. #!/bin/sh
  2. # dns based ad/abuse domain blocking
  3. # written by Dirk Brenken (dev@brenken.org)
  4. # This is free software, licensed under the GNU General Public License v3.
  5. # You should have received a copy of the GNU General Public License
  6. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  7. # set initial defaults
  8. #
  9. LC_ALL=C
  10. PATH="/usr/sbin:/usr/bin:/sbin:/bin"
  11. adb_ver="3.0.3"
  12. adb_sysver="unknown"
  13. adb_enabled=0
  14. adb_debug=0
  15. adb_manmode=0
  16. adb_forcesrt=0
  17. adb_forcedns=0
  18. adb_triggerdelay=0
  19. adb_backup=0
  20. adb_backupdir="/mnt"
  21. adb_fetch="/usr/bin/wget"
  22. adb_fetchparm="--quiet --no-cache --no-cookies --max-redirect=0 --timeout=10 --no-check-certificate -O"
  23. adb_dns="dnsmasq"
  24. adb_dnsprefix="adb_list"
  25. adb_dnsfile="${adb_dnsprefix}.overall"
  26. adb_rtfile="/tmp/adb_runtime.json"
  27. adb_action="${1:-"start"}"
  28. adb_cnt=0
  29. adb_rc=0
  30. # f_envload: load adblock environment
  31. #
  32. f_envload()
  33. {
  34. local dns_up sys_call sys_desc sys_model sys_ver cnt=0
  35. # get system information
  36. #
  37. sys_call="$(ubus -S call system board 2>/dev/null)"
  38. if [ -n "${sys_call}" ]
  39. then
  40. sys_desc="$(printf '%s' "${sys_call}" | jsonfilter -e '@.release.description')"
  41. sys_model="$(printf '%s' "${sys_call}" | jsonfilter -e '@.model')"
  42. sys_ver="$(cat /etc/turris-version 2>/dev/null)"
  43. if [ -n "${sys_ver}" ]
  44. then
  45. sys_desc="${sys_desc}/${sys_ver}"
  46. fi
  47. adb_sysver="${sys_model}, ${sys_desc}"
  48. fi
  49. # source in system libraries
  50. #
  51. if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
  52. then
  53. . "/lib/functions.sh"
  54. . "/usr/share/libubox/jshn.sh"
  55. else
  56. f_log "error" "system libraries not found"
  57. fi
  58. # parse global section by callback
  59. #
  60. config_cb()
  61. {
  62. local type="${1}"
  63. if [ "${type}" = "adblock" ]
  64. then
  65. option_cb()
  66. {
  67. local option="${1}"
  68. local value="${2}"
  69. eval "${option}=\"${value}\""
  70. }
  71. else
  72. reset_cb
  73. fi
  74. }
  75. # parse 'source' sections
  76. #
  77. parse_config()
  78. {
  79. local value opt section="${1}" options="enabled adb_src adb_src_rset adb_src_cat"
  80. eval "adb_sources=\"${adb_sources} ${section}\""
  81. for opt in ${options}
  82. do
  83. config_get value "${section}" "${opt}"
  84. if [ -n "${value}" ]
  85. then
  86. eval "${opt}_${section}=\"${value}\""
  87. fi
  88. done
  89. }
  90. # load adblock config
  91. #
  92. config_load adblock
  93. config_foreach parse_config source
  94. # set/check dns backend environment
  95. #
  96. case "${adb_dns}" in
  97. dnsmasq)
  98. adb_dnsuser="${adb_dns}"
  99. adb_dnsdir="${adb_dnsdir:-"/tmp/dnsmasq.d"}"
  100. adb_dnsformat="awk '{print \"local=/\"\$0\"/\"}'"
  101. ;;
  102. unbound)
  103. adb_dnsuser="${adb_dns}"
  104. adb_dnsdir="${adb_dnsdir:-"/var/lib/unbound"}"
  105. adb_dnsformat="awk '{print \"local-zone: \042\"\$0\"\042 static\"}'"
  106. ;;
  107. named)
  108. adb_dnsuser="bind"
  109. adb_dnsdir="${adb_dnsdir:-"/var/lib/bind"}"
  110. adb_dnsformat="awk '{print \"\"\$0\" IN CNAME .\n*.\"\$0\" IN CNAME .\"}'"
  111. ;;
  112. kresd)
  113. adb_dnsuser="root"
  114. adb_dnsdir="${adb_dnsdir:-"/etc/kresd"}"
  115. adb_dnsformat="awk '{print \"\"\$0\" CNAME .\n*.\"\$0\" CNAME .\"}'"
  116. adb_dnsheader="\$TTL 2h"$'\n'"@ IN SOA localhost. root.localhost. (2 6h 1h 1w 2h)"$'\n'" IN NS localhost."
  117. ;;
  118. dnscrypt-proxy)
  119. adb_dnsuser="nobody"
  120. adb_dnsdir="${adb_dnsdir:-"/tmp"}"
  121. adb_dnsformat="awk '{print \$0}'"
  122. ;;
  123. esac
  124. if [ -d "${adb_dnsdir}" ] && [ ! -f "${adb_dnsdir}/${adb_dnsfile}" ]
  125. then
  126. > "${adb_dnsdir}/${adb_dnsfile}"
  127. fi
  128. case "${adb_action}" in
  129. start|restart|reload)
  130. > "${adb_rtfile}"
  131. if [ "${adb_action}" = "start" ] && [ "${adb_trigger}" = "timed" ]
  132. then
  133. sleep ${adb_triggerdelay}
  134. fi
  135. ;;
  136. esac
  137. while [ ${cnt} -le 30 ]
  138. do
  139. dns_up="$(ubus -S call service list "{\"name\":\"${adb_dns}\"}" 2>/dev/null | jsonfilter -l1 -e "@[\"${adb_dns}\"].instances.*.running" 2>/dev/null)"
  140. if [ "${dns_up}" = "true" ]
  141. then
  142. break
  143. fi
  144. sleep 1
  145. cnt=$((cnt+1))
  146. done
  147. if [ -z "${adb_dns}" ] || [ -z "${adb_dnsformat}" ] || [ ! -x "$(command -v ${adb_dns})" ] || [ ! -d "${adb_dnsdir}" ]
  148. then
  149. f_log "error" "'${adb_dns}' not running, DNS backend not found"
  150. fi
  151. # force dns to local resolver
  152. #
  153. if [ ${adb_forcedns} -eq 1 ] && [ -z "$(uci -q get firewall.adblock_dns)" ]
  154. then
  155. uci -q set firewall.adblock_dns="redirect"
  156. uci -q set firewall.adblock_dns.name="Adblock DNS"
  157. uci -q set firewall.adblock_dns.src="lan"
  158. uci -q set firewall.adblock_dns.proto="tcp udp"
  159. uci -q set firewall.adblock_dns.src_dport="53"
  160. uci -q set firewall.adblock_dns.dest_port="53"
  161. uci -q set firewall.adblock_dns.target="DNAT"
  162. elif [ ${adb_forcedns} -eq 0 ] && [ -n "$(uci -q get firewall.adblock_dns)" ]
  163. then
  164. uci -q delete firewall.adblock_dns
  165. fi
  166. if [ -n "$(uci -q changes firewall)" ]
  167. then
  168. uci -q commit firewall
  169. if [ $(/etc/init.d/firewall enabled; printf "%u" ${?}) -eq 0 ]
  170. then
  171. /etc/init.d/firewall reload >/dev/null 2>&1
  172. fi
  173. fi
  174. }
  175. # f_envcheck: check/set environment prerequisites
  176. #
  177. f_envcheck()
  178. {
  179. local ssl_lib
  180. # check 'enabled' option
  181. #
  182. if [ ${adb_enabled} -ne 1 ]
  183. then
  184. if [ -s "${adb_dnsdir}/${adb_dnsfile}" ]
  185. then
  186. f_rmdns
  187. f_dnsrestart
  188. fi
  189. f_jsnupdate
  190. f_log "info " "adblock is currently disabled, please set adb_enabled to '1' to use this service"
  191. exit 0
  192. fi
  193. # check fetch utility
  194. #
  195. ssl_lib="-"
  196. if [ -x "${adb_fetch}" ]
  197. then
  198. if [ "$(readlink -fn "${adb_fetch}")" = "/usr/bin/wget-nossl" ]
  199. then
  200. adb_fetchparm="--quiet --no-cache --no-cookies --max-redirect=0 --timeout=10 -O"
  201. elif [ "$(readlink -fn "${adb_fetch}")" = "/bin/busybox" ] ||
  202. ([ "$(readlink -fn "/bin/wget")" = "/bin/busybox" ] && [ "$(readlink -fn "${adb_fetch}")" != "/usr/bin/wget" ])
  203. then
  204. adb_fetch="/bin/busybox"
  205. adb_fetchparm="-q -O"
  206. else
  207. ssl_lib="built-in"
  208. fi
  209. fi
  210. if [ ! -x "${adb_fetch}" ] && [ "$(readlink -fn "/bin/wget")" = "/bin/uclient-fetch" ]
  211. then
  212. adb_fetch="/bin/uclient-fetch"
  213. if [ -f "/lib/libustream-ssl.so" ]
  214. then
  215. adb_fetchparm="-q --timeout=10 --no-check-certificate -O"
  216. ssl_lib="libustream-ssl"
  217. else
  218. adb_fetchparm="-q --timeout=10 -O"
  219. fi
  220. fi
  221. if [ ! -x "${adb_fetch}" ] || [ -z "${adb_fetch}" ] || [ -z "${adb_fetchparm}" ]
  222. then
  223. f_log "error" "no download utility found, please install 'uclient-fetch' with 'libustream-mbedtls' or the full 'wget' package"
  224. fi
  225. adb_fetchinfo="${adb_fetch##*/} (${ssl_lib})"
  226. # create adblock temp file/directory
  227. #
  228. adb_tmpload="$(mktemp -tu)"
  229. adb_tmpfile="$(mktemp -tu)"
  230. adb_tmpdir="$(mktemp -p /tmp -d)"
  231. # prepare whitelist entries
  232. #
  233. if [ -s "${adb_whitelist}" ] && [ -n "${adb_whitelist_rset}" ]
  234. then
  235. awk "${adb_whitelist_rset}" "${adb_whitelist}" > "${adb_tmpdir}/tmp.whitelist"
  236. fi
  237. }
  238. # f_rmtemp: remove temporary files & directories
  239. #
  240. f_rmtemp()
  241. {
  242. if [ -d "${adb_tmpdir}" ]
  243. then
  244. rm -f "${adb_tmpload}"
  245. rm -f "${adb_tmpfile}"
  246. rm -rf "${adb_tmpdir}"
  247. fi
  248. }
  249. # f_rmdns: remove dns related files & directories
  250. #
  251. f_rmdns()
  252. {
  253. if [ -n "${adb_dns}" ]
  254. then
  255. > "${adb_dnsdir}/${adb_dnsfile}"
  256. > "${adb_rtfile}"
  257. rm -f "${adb_dnsdir}/.${adb_dnsfile}"
  258. rm -f "${adb_backupdir}/${adb_dnsprefix}"*.gz
  259. fi
  260. }
  261. # f_dnsrestart: restart the dns backend
  262. #
  263. f_dnsrestart()
  264. {
  265. local dns_up cnt=0
  266. "/etc/init.d/${adb_dns}" restart >/dev/null 2>&1
  267. while [ ${cnt} -le 10 ]
  268. do
  269. dns_up="$(ubus -S call service list "{\"name\":\"${adb_dns}\"}" | jsonfilter -l1 -e "@[\"${adb_dns}\"].instances.*.running")"
  270. if [ "${dns_up}" = "true" ]
  271. then
  272. return 0
  273. fi
  274. cnt=$((cnt+1))
  275. sleep 1
  276. done
  277. return 1
  278. }
  279. # f_list: backup/restore/remove block lists
  280. #
  281. f_list()
  282. {
  283. local mode="${1}" in_rc="${adb_rc}" cnt=0
  284. case "${mode}" in
  285. backup)
  286. cnt="$(wc -l < "${adb_tmpfile}")"
  287. if [ ${adb_backup} -eq 1 ] && [ -d "${adb_backupdir}" ]
  288. then
  289. gzip -cf "${adb_tmpfile}" > "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz"
  290. adb_rc=${?}
  291. fi
  292. ;;
  293. restore)
  294. if [ ${adb_backup} -eq 1 ] && [ -d "${adb_backupdir}" ] &&
  295. [ -f "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" ]
  296. then
  297. gunzip -cf "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz" > "${adb_tmpfile}"
  298. adb_rc=${?}
  299. fi
  300. ;;
  301. remove)
  302. if [ -d "${adb_backupdir}" ]
  303. then
  304. rm -f "${adb_backupdir}/${adb_dnsprefix}.${src_name}.gz"
  305. fi
  306. adb_rc=${?}
  307. ;;
  308. merge)
  309. if [ -s "${adb_tmpfile}" ]
  310. then
  311. cat "${adb_tmpfile}" >> "${adb_tmpdir}/${adb_dnsfile}"
  312. adb_rc=${?}
  313. fi
  314. ;;
  315. format)
  316. if [ -s "${adb_tmpdir}/tmp.whitelist" ]
  317. then
  318. grep -vf "${adb_tmpdir}/tmp.whitelist" "${adb_tmpdir}/${adb_dnsfile}" | eval "${adb_dnsformat}" > "${adb_dnsdir}/${adb_dnsfile}"
  319. else
  320. eval "${adb_dnsformat}" "${adb_tmpdir}/${adb_dnsfile}" > "${adb_dnsdir}/${adb_dnsfile}"
  321. fi
  322. if [ -n "${adb_dnsheader}" ]
  323. then
  324. printf '%s\n' "${adb_dnsheader}" | cat - "${adb_dnsdir}/${adb_dnsfile}" > "${adb_tmpdir}/${adb_dnsfile}"
  325. cat "${adb_tmpdir}/${adb_dnsfile}" > "${adb_dnsdir}/${adb_dnsfile}"
  326. fi
  327. adb_rc=${?}
  328. ;;
  329. esac
  330. f_log "debug" "name: ${src_name}, mode: ${mode}, count: ${cnt}, in_rc: ${in_rc}, out_rc: ${adb_rc}"
  331. }
  332. # f_tldcompression: top level domain compression
  333. #
  334. f_tldcompression()
  335. {
  336. local source="${1}" temp="${adb_tmpload}"
  337. awk -F "." '{for(f=NF;f > 1;f--) printf "%s.", $f;print $1}' "${source}" 2>/dev/null | sort -u > "${temp}"
  338. awk '{if(NR==1){tld=$NF};while(getline){if($NF !~ tld"\\."){print tld;tld=$NF}}print tld}' "${temp}" 2>/dev/null > "${source}"
  339. awk -F "." '{for(f=NF;f > 1;f--) printf "%s.", $f;print $1}' "${source}" 2>/dev/null > "${temp}"
  340. sort -u "${temp}" > "${source}"
  341. }
  342. # f_switch: suspend/resume adblock processing
  343. #
  344. f_switch()
  345. {
  346. local source target status mode="${1}"
  347. if [ -s "${adb_dnsdir}/${adb_dnsfile}" ] && [ "${mode}" = "suspend" ]
  348. then
  349. source="${adb_dnsdir}/${adb_dnsfile}"
  350. target="${adb_dnsdir}/.${adb_dnsfile}"
  351. status="suspended"
  352. elif [ -s "${adb_dnsdir}/.${adb_dnsfile}" ] && [ "${mode}" = "resume" ]
  353. then
  354. source="${adb_dnsdir}/.${adb_dnsfile}"
  355. target="${adb_dnsdir}/${adb_dnsfile}"
  356. status="resumed"
  357. fi
  358. if [ -n "${status}" ]
  359. then
  360. cat "${source}" > "${target}"
  361. > "${source}"
  362. f_dnsrestart
  363. f_jsnupdate
  364. f_log "info " "adblock processing ${status}"
  365. fi
  366. }
  367. # f_query: query block list for certain (sub-)domains
  368. #
  369. f_query()
  370. {
  371. local search result cnt
  372. local domain="${1}"
  373. local tld="${domain#*.}"
  374. if [ ! -s "${adb_dnsdir}/${adb_dnsfile}" ]
  375. then
  376. printf "%s\n" "::: no active block list found, please start / resume adblock first"
  377. elif [ -z "${domain}" ] || [ "${domain}" = "${tld}" ]
  378. then
  379. printf "%s\n" "::: invalid domain input, please submit a single domain, e.g. 'doubleclick.net'"
  380. else
  381. cd "${adb_dnsdir}"
  382. while [ "${domain}" != "${tld}" ]
  383. do
  384. search="${domain//./\.}"
  385. if [ "${adb_dns}" = "dnsmasq" ] || [ "${adb_dns}" = "unbound" ]
  386. then
  387. result="$(awk -F '/|\"' "/[\/\"\.]${search}/{i++;{printf(\" + %s\n\",\$2)};if(i>9){exit}}" "${adb_dnsfile}")"
  388. else
  389. result="$(awk "/(^[^\*][a-z]*[\.]+${search}|^${search})/{i++;{printf(\" + %s\n\",\$1)};if(i>9){exit}}" "${adb_dnsfile}")"
  390. fi
  391. printf "%s\n" "::: max. ten results for domain '${domain}'"
  392. printf "%s\n" "${result:-" - no match"}"
  393. domain="${tld}"
  394. tld="${domain#*.}"
  395. done
  396. fi
  397. }
  398. # f_jsnupdate: update runtime information
  399. #
  400. f_jsnupdate()
  401. {
  402. local status rundate="$(/bin/date "+%d.%m.%Y %H:%M:%S")"
  403. if [ ${adb_rc} -gt 0 ]
  404. then
  405. status="error"
  406. elif [ ${adb_enabled} -ne 1 ]
  407. then
  408. status="disabled"
  409. elif [ -s "${adb_dnsdir}/.${adb_dnsfile}" ]
  410. then
  411. status="paused"
  412. else
  413. status="enabled"
  414. if [ -s "${adb_dnsdir}/${adb_dnsfile}" ]
  415. then
  416. if [ "${adb_dns}" = "named" ] || [ "${adb_dns}" = "kresd" ]
  417. then
  418. adb_cnt="$(( ( $(wc -l < "${adb_dnsdir}/${adb_dnsfile}") - $(printf "%s" "${adb_dnsheader}" | grep -c "^") ) / 2 ))"
  419. else
  420. adb_cnt="$(( $(wc -l < "${adb_dnsdir}/${adb_dnsfile}") - $(printf "%s" "${adb_dnsheader}" | grep -c "^") ))"
  421. fi
  422. fi
  423. fi
  424. if [ -z "${adb_fetchinfo}" ] && [ -s "${adb_rtfile}" ]
  425. then
  426. json_load "$(cat "${adb_rtfile}" 2>/dev/null)"
  427. json_select data
  428. json_get_var adb_fetchinfo "fetch_utility"
  429. fi
  430. json_init
  431. json_add_object "data"
  432. json_add_string "adblock_status" "${status}"
  433. json_add_string "adblock_version" "${adb_ver}"
  434. json_add_string "blocked_domains" "${adb_cnt}"
  435. json_add_string "fetch_utility" "${adb_fetchinfo}"
  436. json_add_string "dns_backend" "${adb_dns} (${adb_dnsdir})"
  437. json_add_string "last_rundate" "${rundate}"
  438. json_add_string "system_release" "${adb_sysver}"
  439. json_close_object
  440. json_dump > "${adb_rtfile}"
  441. }
  442. # f_status: output runtime information
  443. #
  444. f_status()
  445. {
  446. local key keylist value
  447. if [ -s "${adb_rtfile}" ]
  448. then
  449. printf "%s\n" "::: adblock runtime information"
  450. json_load "$(cat "${adb_rtfile}" 2>/dev/null)"
  451. json_select data
  452. json_get_keys keylist
  453. for key in ${keylist}
  454. do
  455. json_get_var value "${key}"
  456. printf " + %-15s : %s\n" "${key}" "${value}"
  457. done
  458. fi
  459. }
  460. # f_log: write to syslog, exit on error
  461. #
  462. f_log()
  463. {
  464. local class="${1}" log_msg="${2}"
  465. if [ -n "${log_msg}" ] && ([ "${class}" != "debug" ] || [ ${adb_debug} -eq 1 ])
  466. then
  467. logger -t "adblock-[${adb_ver}] ${class}" "${log_msg}"
  468. if [ "${class}" = "error" ]
  469. then
  470. logger -t "adblock-[${adb_ver}] ${class}" "Please check 'https://github.com/openwrt/packages/blob/master/net/adblock/files/README.md' (${adb_sysver})"
  471. f_rmtemp
  472. if [ -s "${adb_dnsdir}/${adb_dnsfile}" ]
  473. then
  474. f_rmdns
  475. f_dnsrestart
  476. fi
  477. adb_rc=1
  478. f_jsnupdate
  479. exit 1
  480. fi
  481. fi
  482. }
  483. # main function for block list processing
  484. #
  485. f_main()
  486. {
  487. local src_name src_rset shalla_archive enabled url hash_old hash_new
  488. local mem_total="$(awk '/^MemTotal/ {print int($2/1000)}' "/proc/meminfo")"
  489. f_log "info " "start adblock processing ..."
  490. f_log "debug" "action: ${adb_action}, manual_mode:${adb_manmode}, backup: ${adb_backup}, dns: ${adb_dns}, fetch: ${adb_fetchinfo}, mem_total: ${mem_total}, force_srt/_dns: ${adb_forcesrt}/${adb_forcedns}"
  491. > "${adb_rtfile}"
  492. for src_name in ${adb_sources}
  493. do
  494. eval "enabled=\"\${enabled_${src_name}}\""
  495. eval "url=\"\${adb_src_${src_name}}\""
  496. eval "src_rset=\"\${adb_src_rset_${src_name}}\""
  497. > "${adb_tmpload}"
  498. > "${adb_tmpfile}"
  499. adb_rc=4
  500. # basic pre-checks
  501. #
  502. f_log "debug" "name: ${src_name}, enabled: ${enabled}, url: ${url}, rset: ${src_rset}"
  503. if [ "${enabled}" != "1" ] || [ -z "${url}" ] || [ -z "${src_rset}" ]
  504. then
  505. f_list remove
  506. continue
  507. fi
  508. # manual / backup mode
  509. #
  510. if [ ${adb_manmode} -eq 1 ] && [ "${adb_action}" = "start" ] && [ "${src_name}" != "blacklist" ]
  511. then
  512. f_list restore
  513. if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpfile}" ]
  514. then
  515. f_list merge
  516. continue
  517. fi
  518. fi
  519. # download block list
  520. #
  521. if [ "${src_name}" = "blacklist" ] && [ -s "${url}" ]
  522. then
  523. cat "${url}" > "${adb_tmpload}"
  524. adb_rc=${?}
  525. elif [ "${src_name}" = "shalla" ]
  526. then
  527. shalla_archive="${adb_tmpdir}/shallalist.tar.gz"
  528. "${adb_fetch}" ${adb_fetchparm} "${shalla_archive}" "${url}" 2>/dev/null
  529. adb_rc=${?}
  530. if [ ${adb_rc} -eq 0 ]
  531. then
  532. for category in ${adb_src_cat_shalla}
  533. do
  534. tar -xOzf "${shalla_archive}" "BL/${category}/domains" >> "${adb_tmpload}"
  535. adb_rc=${?}
  536. if [ ${adb_rc} -ne 0 ]
  537. then
  538. break
  539. fi
  540. done
  541. fi
  542. rm -f "${shalla_archive}"
  543. rm -rf "${adb_tmpdir}/BL"
  544. else
  545. "${adb_fetch}" ${adb_fetchparm} "${adb_tmpload}" "${url}" 2>/dev/null
  546. adb_rc=${?}
  547. fi
  548. # check download result and prepare list output (incl. tld compression, list backup & restore)
  549. #
  550. if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpload}" ]
  551. then
  552. awk "${src_rset}" "${adb_tmpload}" 2>/dev/null > "${adb_tmpfile}"
  553. if [ -s "${adb_tmpfile}" ]
  554. then
  555. f_tldcompression "${adb_tmpfile}"
  556. if [ "${src_name}" != "blacklist" ]
  557. then
  558. f_list backup
  559. fi
  560. else
  561. f_list restore
  562. fi
  563. else
  564. f_list restore
  565. fi
  566. # list merge
  567. #
  568. if [ ${adb_rc} -eq 0 ] && [ -s "${adb_tmpfile}" ]
  569. then
  570. f_list merge
  571. if [ ${adb_rc} -ne 0 ]
  572. then
  573. f_list remove
  574. fi
  575. else
  576. f_list remove
  577. fi
  578. done
  579. # hash preparation, whitelist removal and overall sort
  580. #
  581. if [ -f "${adb_dnsdir}/${adb_dnsfile}" ]
  582. then
  583. hash_old="$(sha256sum "${adb_dnsdir}/${adb_dnsfile}" 2>/dev/null | awk '{print $1}')"
  584. fi
  585. if [ -s "${adb_tmpdir}/${adb_dnsfile}" ]
  586. then
  587. if [ ${mem_total} -ge 64 ] || [ ${adb_forcesrt} -eq 1 ]
  588. then
  589. f_tldcompression "${adb_tmpdir}/${adb_dnsfile}"
  590. fi
  591. f_list format
  592. else
  593. > "${adb_dnsdir}/${adb_dnsfile}"
  594. fi
  595. chown "${adb_dnsuser}" "${adb_dnsdir}/${adb_dnsfile}" 2>/dev/null
  596. f_rmtemp
  597. # conditional restart of the dns backend and runtime information export
  598. #
  599. hash_new="$(sha256sum "${adb_dnsdir}/${adb_dnsfile}" 2>/dev/null | awk '{print $1}')"
  600. if [ -z "${hash_old}" ] || [ -z "${hash_new}" ] || [ "${hash_old}" != "${hash_new}" ]
  601. then
  602. f_dnsrestart
  603. fi
  604. if [ ${?} -eq 0 ]
  605. then
  606. f_jsnupdate "${adb_cnt}"
  607. f_log "info " "block list with overall ${adb_cnt} domains loaded successfully (${adb_sysver})"
  608. else
  609. f_log "error" "dns backend restart with active block list failed"
  610. fi
  611. }
  612. # handle different adblock actions
  613. #
  614. f_envload
  615. case "${adb_action}" in
  616. stop)
  617. f_rmtemp
  618. f_rmdns
  619. f_dnsrestart
  620. ;;
  621. restart)
  622. f_rmtemp
  623. f_rmdns
  624. f_envcheck
  625. f_main
  626. ;;
  627. suspend)
  628. f_switch suspend
  629. ;;
  630. resume)
  631. f_switch resume
  632. ;;
  633. query)
  634. f_query "${2}"
  635. ;;
  636. status)
  637. f_status
  638. ;;
  639. *)
  640. f_envcheck
  641. f_main
  642. ;;
  643. esac
  644. exit 0