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.

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