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.

685 lines
20 KiB

  1. #!/bin/sh
  2. # function library used by adblock-update.sh
  3. # written by Dirk Brenken (dev@brenken.org)
  4. # f_envload: load adblock environment
  5. #
  6. f_envload()
  7. {
  8. # source in system function library
  9. #
  10. if [ -r "/lib/functions.sh" ]
  11. then
  12. . "/lib/functions.sh"
  13. else
  14. rc=-1
  15. f_log "system function library not found, please check your installation"
  16. f_exit
  17. fi
  18. # source in system network library
  19. #
  20. if [ -r "/lib/functions/network.sh" ]
  21. then
  22. . "/lib/functions/network.sh"
  23. else
  24. rc=-1
  25. f_log "system network library not found, please check your installation"
  26. f_exit
  27. fi
  28. # set initial defaults,
  29. # may be overwritten by setting appropriate adblock config options in global section of /etc/config/adblock
  30. #
  31. adb_lanif="lan"
  32. adb_nullport="65535"
  33. adb_nullipv4="192.0.2.1"
  34. adb_nullipv6="::ffff:c000:0201"
  35. adb_whitelist="/etc/adblock/adblock.whitelist"
  36. adb_whitelist_rset="\$1 ~/^([A-Za-z0-9_-]+\.){1,}[A-Za-z]+/{print tolower(\$1)}"
  37. adb_forcedns=1
  38. adb_fetchttl=5
  39. adb_restricted=0
  40. # function to parse global section by callback
  41. #
  42. config_cb()
  43. {
  44. local type="${1}"
  45. if [ "${type}" = "adblock" ]
  46. then
  47. option_cb()
  48. {
  49. local option="${1}"
  50. local value="${2}"
  51. eval "${option}=\"${value}\""
  52. }
  53. else
  54. reset_cb
  55. fi
  56. }
  57. # function to parse 'service' and 'source' sections
  58. #
  59. parse_config()
  60. {
  61. local value opt section="${1}" options="adb_dir adb_src adb_src_rset adb_src_cat"
  62. config_get switch "${section}" "enabled"
  63. if [ "${switch}" = "1" ]
  64. then
  65. if [ "${section}" != "backup" ]
  66. then
  67. eval "adb_sources=\"${adb_sources} ${section}\""
  68. fi
  69. for opt in ${options}
  70. do
  71. config_get value "${section}" "${opt}"
  72. if [ -n "${value}" ]
  73. then
  74. eval "${opt}_${section}=\"${value}\""
  75. fi
  76. done
  77. fi
  78. }
  79. # check opkg availability
  80. #
  81. if [ -r "/var/lock/opkg.lock" ]
  82. then
  83. rc=-1
  84. f_log "adblock installation finished successfully, 'opkg' currently locked by package installer"
  85. f_exit
  86. fi
  87. # get list with all installed packages
  88. #
  89. pkg_list="$(opkg list-installed)"
  90. if [ -z "${pkg_list}" ]
  91. then
  92. rc=-1
  93. f_log "empty package list, please check your installation"
  94. f_exit
  95. fi
  96. # load adblock config and start parsing functions
  97. #
  98. config_load adblock
  99. config_foreach parse_config service
  100. config_foreach parse_config source
  101. # set more script defaults (can't be overwritten by adblock config options)
  102. #
  103. adb_minspace=12000
  104. adb_tmpfile="$(mktemp -tu)"
  105. adb_tmpdir="$(mktemp -p /tmp -d)"
  106. adb_dnsdir="/tmp/dnsmasq.d"
  107. adb_dnshidedir="${adb_dnsdir}/.adb_hidden"
  108. adb_dnsprefix="adb_list"
  109. adb_iptv4="$(which iptables)"
  110. adb_iptv6="$(which ip6tables)"
  111. adb_fetch="$(which wget)"
  112. adb_uci="$(which uci)"
  113. adb_date="$(which date)"
  114. unset adb_srclist adb_revsrclist adb_errsrclist
  115. # check 'enabled' & 'version' config options
  116. #
  117. if [ -z "${adb_enabled}" ] || [ -z "${adb_cfgver}" ] || [ "${adb_cfgver%%.*}" != "${adb_mincfgver%%.*}" ]
  118. then
  119. rc=-1
  120. f_log "outdated adblock config (${adb_mincfgver} vs. ${adb_cfgver}), please run '/etc/init.d/adblock cfgup' to update your configuration"
  121. f_exit
  122. elif [ "${adb_cfgver#*.}" != "${adb_mincfgver#*.}" ]
  123. then
  124. outdate_ok="true"
  125. fi
  126. if [ $((adb_enabled)) -ne 1 ]
  127. then
  128. rc=-1
  129. f_log "adblock is currently disabled, please set adblock.global.adb_enabled=1' to use this service"
  130. f_exit
  131. fi
  132. # check running dnsmasq instance
  133. #
  134. check="$(pgrep -f "dnsmasq")"
  135. if [ -z "${check}" ]
  136. then
  137. rc=-1
  138. f_log "please enable the local dnsmasq instance to use adblock"
  139. f_exit
  140. fi
  141. # check running firewall
  142. #
  143. check="$(${adb_iptv4} -vnL | grep -F "DROP")"
  144. if [ -z "${check}" ]
  145. then
  146. rc=-1
  147. f_log "please enable the local firewall to use adblock"
  148. f_exit
  149. fi
  150. # get lan ip addresses
  151. #
  152. network_get_ipaddr adb_ipv4 "${adb_lanif}"
  153. network_get_ipaddr6 adb_ipv6 "${adb_lanif}"
  154. if [ -z "${adb_ipv4}" ] && [ -z "${adb_ipv6}" ]
  155. then
  156. rc=-1
  157. f_log "no valid IPv4/IPv6 configuration found (${adb_lanif}), please set 'adb_lanif' manually"
  158. f_exit
  159. else
  160. network_get_device adb_landev4 "${adb_lanif}"
  161. network_get_device adb_landev6 "${adb_lanif}"
  162. fi
  163. # check logical update interfaces (with default route)
  164. #
  165. network_find_wan adb_wanif4
  166. network_find_wan6 adb_wanif6
  167. if [ -z "${adb_wanif4}" ] && [ -z "${adb_wanif6}" ]
  168. then
  169. adb_wanif4="${adb_lanif}"
  170. fi
  171. # check AP mode
  172. #
  173. if [ "${adb_wanif4}" = "${adb_lanif}" ] || [ "${adb_wanif6}" = "${adb_lanif}" ]
  174. then
  175. adb_nullipv4="${adb_ipv4}"
  176. adb_nullipv6="${adb_ipv6}"
  177. if [ "$(${adb_uci} get uhttpd.main.listen_http | grep -Fo "80")" = "80" ] ||
  178. [ "$(${adb_uci} get uhttpd.main.listen_https | grep -Fo "443")" = "443" ]
  179. then
  180. rc=-1
  181. f_log "AP mode detected, set local LuCI instance to ports <> 80/443"
  182. f_exit
  183. else
  184. apmode_ok="true"
  185. fi
  186. fi
  187. # get system release level
  188. #
  189. adb_sysver="$(printf "${pkg_list}" | grep "^base-files -")"
  190. adb_sysver="${adb_sysver##*-}"
  191. }
  192. # f_envcheck: check/set environment prerequisites
  193. #
  194. f_envcheck()
  195. {
  196. local check
  197. # log partially outdated config
  198. #
  199. if [ "${outdate_ok}" = "true" ]
  200. then
  201. f_log "partially outdated adblock config (${adb_mincfgver} vs. ${adb_cfgver}), please run '/etc/init.d/adblock cfgup' to update your configuration"
  202. fi
  203. # log ap mode
  204. #
  205. if [ "${apmode_ok}" = "true" ]
  206. then
  207. f_log "AP mode enabled"
  208. fi
  209. # set & log restricted mode
  210. #
  211. if [ $((adb_restricted)) -eq 1 ]
  212. then
  213. adb_uci="$(which true)"
  214. f_log "Restricted mode enabled"
  215. fi
  216. # check general package dependencies
  217. #
  218. f_depend "busybox"
  219. f_depend "uci"
  220. f_depend "uhttpd"
  221. f_depend "wget"
  222. f_depend "iptables"
  223. f_depend "kmod-ipt-nat"
  224. # check ipv6 related package dependencies
  225. #
  226. if [ -n "${adb_wanif6}" ]
  227. then
  228. check="$(printf "${pkg_list}" | grep "^ip6tables -")"
  229. if [ -z "${check}" ]
  230. then
  231. f_log "package 'ip6tables' not found, IPv6 support will be disabled"
  232. unset adb_wanif6
  233. else
  234. check="$(printf "${pkg_list}" | grep "^kmod-ipt-nat6 -")"
  235. if [ -z "${check}" ]
  236. then
  237. f_log "package 'kmod-ipt-nat6' not found, IPv6 support will be disabled"
  238. unset adb_wanif6
  239. fi
  240. fi
  241. fi
  242. # check dns hideout directory
  243. #
  244. if [ -d "${adb_dnshidedir}" ]
  245. then
  246. mv_done="$(find "${adb_dnshidedir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print -exec mv -f "{}" "${adb_dnsdir}" \;)"
  247. else
  248. mkdir -p -m 660 "${adb_dnshidedir}"
  249. fi
  250. # check ca-certificates package and set fetch parms accordingly
  251. #
  252. fetch_parm="--no-config --quiet --tries=1 --no-cache --no-cookies --max-redirect=0 --dns-timeout=${adb_fetchttl} --connect-timeout=${adb_fetchttl} --read-timeout=${adb_fetchttl}"
  253. check="$(printf "${pkg_list}" | grep "^ca-certificates -")"
  254. if [ -z "${check}" ]
  255. then
  256. fetch_parm="${fetch_parm} --no-check-certificate"
  257. fi
  258. # check adblock temp directory
  259. #
  260. if [ -n "${adb_tmpdir}" ] && [ -d "${adb_tmpdir}" ]
  261. then
  262. f_space "${adb_tmpdir}"
  263. if [ "${space_ok}" = "false" ]
  264. then
  265. if [ $((av_space)) -le 2000 ]
  266. then
  267. rc=105
  268. f_log "not enough free space in '${adb_tmpdir}' (avail. ${av_space} kb)" "${rc}"
  269. f_exit
  270. else
  271. f_log "not enough free space to handle all adblock list sources at once in '${adb_tmpdir}' (avail. ${av_space} kb)"
  272. fi
  273. fi
  274. else
  275. rc=110
  276. f_log "temp directory not found" "${rc}"
  277. f_exit
  278. fi
  279. # check memory
  280. #
  281. mem_total="$(awk '$1 ~ /^MemTotal/ {printf $2}' "/proc/meminfo")"
  282. mem_free="$(awk '$1 ~ /^MemFree/ {printf $2}' "/proc/meminfo")"
  283. mem_swap="$(awk '$1 ~ /^SwapTotal/ {printf $2}' "/proc/meminfo")"
  284. if [ $((mem_total)) -le 64000 ] && [ $((mem_swap)) -eq 0 ]
  285. then
  286. mem_ok="false"
  287. f_log "not enough free memory, overall sort processing will be disabled (total: ${mem_total}, free: ${mem_free}, swap: ${mem_swap})"
  288. else
  289. mem_ok="true"
  290. fi
  291. # check backup configuration
  292. #
  293. if [ -n "${adb_dir_backup}" ] && [ -d "${adb_dir_backup}" ]
  294. then
  295. f_space "${adb_dir_backup}"
  296. if [ "${space_ok}" = "false" ]
  297. then
  298. f_log "not enough free space in '${adb_dir_backup}'(avail. ${av_space} kb), backup/restore will be disabled"
  299. backup_ok="false"
  300. else
  301. f_log "backup/restore will be enabled"
  302. backup_ok="true"
  303. fi
  304. else
  305. backup_ok="false"
  306. f_log "backup/restore will be disabled"
  307. fi
  308. # set dnsmasq defaults
  309. #
  310. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
  311. then
  312. adb_dnsformat="awk -v ipv4="${adb_nullipv4}" -v ipv6="${adb_nullipv6}" '{print \"address=/\"\$0\"/\"ipv4\"\n\"\"address=/\"\$0\"/\"ipv6}'"
  313. elif [ -n "${adb_wanif4}" ]
  314. then
  315. adb_dnsformat="awk -v ipv4="${adb_nullipv4}" '{print \"address=/\"\$0\"/\"ipv4}'"
  316. else
  317. adb_dnsformat="awk -v ipv6="${adb_nullipv6}" '{print \"address=/\"\$0\"/\"ipv6}'"
  318. fi
  319. # check ipv4/iptables configuration
  320. #
  321. if [ -n "${adb_wanif4}" ]
  322. then
  323. f_firewall "IPv4" "nat" "A" "prerouting_rule" "adb-nat" "-p tcp -d ${adb_nullipv4} -m multiport --dports 80,443 -j DNAT --to-destination ${adb_ipv4}:${adb_nullport}"
  324. f_firewall "IPv4" "filter" "A" "forwarding_rule" "adb-fwd" "-p tcp -d ${adb_nullipv4} -j REJECT --reject-with tcp-reset"
  325. f_firewall "IPv4" "filter" "A" "forwarding_rule" "adb-fwd" "-d ${adb_nullipv4} -j REJECT --reject-with icmp-host-unreachable"
  326. f_firewall "IPv4" "filter" "A" "output_rule" "adb-out" "-p tcp -d ${adb_nullipv4} -j REJECT --reject-with tcp-reset"
  327. f_firewall "IPv4" "filter" "A" "output_rule" "adb-out" "-d ${adb_nullipv4} -j REJECT --reject-with icmp-host-unreachable"
  328. if [ $((adb_forcedns)) -eq 1 ] && [ -n "${adb_landev4}" ]
  329. then
  330. f_firewall "IPv4" "nat" "A" "prerouting_rule" "adb-dns" "-i ${adb_landev4} -p udp --dport 53 -j DNAT --to-destination ${adb_ipv4}:53"
  331. f_firewall "IPv4" "nat" "A" "prerouting_rule" "adb-dns" "-i ${adb_landev4} -p tcp --dport 53 -j DNAT --to-destination ${adb_ipv4}:53"
  332. fi
  333. if [ "${fw_done}" = "true" ]
  334. then
  335. f_log "created volatile IPv4 firewall ruleset"
  336. fw_done="false"
  337. fi
  338. fi
  339. # check ipv6/ip6tables configuration
  340. #
  341. if [ -n "${adb_wanif6}" ]
  342. then
  343. f_firewall "IPv6" "nat" "A" "PREROUTING" "adb-nat" "-p tcp -d ${adb_nullipv6} -m multiport --dports 80,443 -j DNAT --to-destination [${adb_ipv6}]:${adb_nullport}"
  344. f_firewall "IPv6" "filter" "A" "forwarding_rule" "adb-fwd" "-p tcp -d ${adb_nullipv6} -j REJECT --reject-with tcp-reset"
  345. f_firewall "IPv6" "filter" "A" "forwarding_rule" "adb-fwd" "-d ${adb_nullipv6} -j REJECT --reject-with icmp6-addr-unreachable"
  346. f_firewall "IPv6" "filter" "A" "output_rule" "adb-out" "-p tcp -d ${adb_nullipv6} -j REJECT --reject-with tcp-reset"
  347. f_firewall "IPv6" "filter" "A" "output_rule" "adb-out" "-d ${adb_nullipv6} -j REJECT --reject-with icmp6-addr-unreachable"
  348. if [ $((adb_forcedns)) -eq 1 ] && [ -n "${adb_landev6}" ]
  349. then
  350. f_firewall "IPv6" "nat" "A" "PREROUTING" "adb-dns" "-i ${adb_landev6} -p udp --dport 53 -j DNAT --to-destination [${adb_ipv6}]:53"
  351. f_firewall "IPv6" "nat" "A" "PREROUTING" "adb-dns" "-i ${adb_landev6} -p tcp --dport 53 -j DNAT --to-destination [${adb_ipv6}]:53"
  352. fi
  353. if [ "${fw_done}" = "true" ]
  354. then
  355. f_log "created volatile IPv6 firewall ruleset"
  356. fw_done="false"
  357. fi
  358. fi
  359. # check volatile adblock uhttpd instance configuration
  360. #
  361. check="$(pgrep -f "uhttpd -h /www/adblock")"
  362. if [ -z "${check}" ]
  363. then
  364. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
  365. then
  366. uhttpd -h "/www/adblock" -k 0 -N 100 -t 0 -T 1 -D -S -E "/index.html" -p "${adb_ipv4}:${adb_nullport}" -p "[${adb_ipv6}]:${adb_nullport}"
  367. rc=${?}
  368. elif [ -n "${adb_wanif4}" ]
  369. then
  370. uhttpd -h "/www/adblock" -k 0 -N 100 -t 0 -T 1 -D -S -E "/index.html" -p "${adb_ipv4}:${adb_nullport}"
  371. rc=${?}
  372. else
  373. uhttpd -h "/www/adblock" -k 0 -N 100 -t 0 -T 1 -D -S -E "/index.html" -p "[${adb_ipv6}]:${adb_nullport}"
  374. rc=${?}
  375. fi
  376. if [ $((rc)) -eq 0 ]
  377. then
  378. f_log "created volatile uhttpd instance"
  379. else
  380. f_log "failed to initialize volatile uhttpd instance" "${rc}"
  381. f_restore
  382. fi
  383. fi
  384. # check whitelist entries
  385. #
  386. if [ -s "${adb_whitelist}" ]
  387. then
  388. awk "${adb_whitelist_rset}" "${adb_whitelist}" > "${adb_tmpdir}/tmp.whitelist"
  389. fi
  390. # remove no longer used opkg package list
  391. #
  392. unset pkg_list
  393. }
  394. # f_depend: check package dependencies
  395. #
  396. f_depend()
  397. {
  398. local check
  399. local package="${1}"
  400. check="$(printf "${pkg_list}" | grep "^${package} -")"
  401. if [ -z "${check}" ]
  402. then
  403. rc=115
  404. f_log "package '${package}' not found" "${rc}"
  405. f_exit
  406. fi
  407. }
  408. # f_firewall: set iptables rules for ipv4/ipv6
  409. #
  410. f_firewall()
  411. {
  412. local ipt="${adb_iptv4}"
  413. local proto="${1}"
  414. local table="${2}"
  415. local ctype="${3}"
  416. local chain="${4}"
  417. local notes="${5}"
  418. local rules="${6}"
  419. # select appropriate iptables executable for IPv6
  420. #
  421. if [ "${proto}" = "IPv6" ]
  422. then
  423. ipt="${adb_iptv6}"
  424. fi
  425. # check whether iptables rule already applied and proceed accordingly
  426. #
  427. rc="$("${ipt}" -w -t "${table}" -C "${chain}" -m comment --comment "${notes}" ${rules}; printf ${?})"
  428. if [ $((rc)) -ne 0 ]
  429. then
  430. "${ipt}" -w -t "${table}" -"${ctype}" "${chain}" -m comment --comment "${notes}" ${rules}
  431. rc=${?}
  432. if [ $((rc)) -eq 0 ]
  433. then
  434. fw_done="true"
  435. else
  436. f_log "failed to initialize volatile ${proto} firewall rule '${notes}'" "${rc}"
  437. f_exit
  438. fi
  439. fi
  440. }
  441. # f_log: log messages to stdout and syslog
  442. #
  443. f_log()
  444. {
  445. local log_parm
  446. local log_msg="${1}"
  447. local log_rc="${2}"
  448. local class="info "
  449. # check for terminal session
  450. #
  451. if [ -t 1 ]
  452. then
  453. log_parm="-s"
  454. fi
  455. # log to different output devices and set log class accordingly
  456. #
  457. if [ -n "${log_msg}" ]
  458. then
  459. if [ $((log_rc)) -gt 0 ]
  460. then
  461. class="error"
  462. log_rc=", rc: ${log_rc}"
  463. log_msg="${log_msg}${log_rc}"
  464. fi
  465. "${adb_log}" ${log_parm} -t "adblock[${adb_pid}] ${class}" "${log_msg}" 2>&1
  466. fi
  467. }
  468. ################################################
  469. # f_space: check mount points/space requirements
  470. #
  471. f_space()
  472. {
  473. local mp="${1}"
  474. if [ -d "${mp}" ]
  475. then
  476. av_space="$(df "${mp}" | tail -n1 | awk '{printf $4}')"
  477. if [ $((av_space)) -lt $((adb_minspace)) ]
  478. then
  479. space_ok="false"
  480. fi
  481. fi
  482. }
  483. # f_cntconfig: calculate counters in config
  484. #
  485. f_cntconfig()
  486. {
  487. local src_name
  488. local count=0
  489. local count_sum=0
  490. for src_name in $(ls -ASr "${adb_dnsdir}/${adb_dnsprefix}"*)
  491. do
  492. count="$(wc -l < "${src_name}")"
  493. src_name="${src_name##*.}"
  494. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
  495. then
  496. count=$((count / 2))
  497. fi
  498. "${adb_uci}" -q set "adblock.${src_name}.adb_src_count=${count}"
  499. count_sum=$((count_sum + count))
  500. done
  501. "${adb_uci}" -q set "adblock.global.adb_overall_count=${count_sum}"
  502. }
  503. # f_rmconfig: remove counters & timestamps in given config sections
  504. #
  505. f_rmconfig()
  506. {
  507. local src_name
  508. local rm_done="${1}"
  509. local restore_done="${2:-false}"
  510. for src_name in ${rm_done}
  511. do
  512. src_name="${src_name#*.}"
  513. if [ "${restore_done}" = "true" ]
  514. then
  515. src_name="${src_name%.*}"
  516. "${adb_uci}" -q set "adblock.${src_name}.adb_src_timestamp=list restored"
  517. else
  518. "${adb_uci}" -q delete "adblock.${src_name}.adb_src_count"
  519. "${adb_uci}" -q delete "adblock.${src_name}.adb_src_timestamp"
  520. fi
  521. done
  522. }
  523. # f_restore: restore last adblock list backups and restart dnsmasq
  524. #
  525. f_restore()
  526. {
  527. local rm_done
  528. local restore_done
  529. # remove bogus adblock lists
  530. #
  531. if [ -n "${adb_revsrclist}" ]
  532. then
  533. rm_done="$(find "${adb_dnsdir}" -maxdepth 1 -type f \( ${adb_revsrclist} \) -print -exec rm -f "{}" \;)"
  534. rc=${?}
  535. if [ $((rc)) -eq 0 ] && [ -n "${rm_done}" ]
  536. then
  537. f_rmconfig "${rm_done}"
  538. f_log "all bogus adblock lists removed"
  539. elif [ $((rc)) -ne 0 ]
  540. then
  541. f_log "error during removal of bogus adblock lists" "${rc}"
  542. f_exit
  543. fi
  544. fi
  545. # restore backups
  546. #
  547. if [ "${backup_ok}" = "true" ]
  548. then
  549. restore_done="$(find "${adb_dir_backup}" -maxdepth 1 -type f -name "${adb_dnsprefix}*.gz" -print -exec cp -pf "{}" "${adb_dnsdir}" \;)"
  550. rc=${?}
  551. if [ $((rc)) -eq 0 ] && [ -n "${restore_done}" ]
  552. then
  553. find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*.gz" -exec gunzip -f "{}" \;
  554. f_log "all available backups restored"
  555. elif [ $((rc)) -ne 0 ] && [ -n "${restore_done}" ]
  556. then
  557. f_log "error during restore of adblock lists" "${rc}"
  558. f_exit
  559. fi
  560. else
  561. f_log "backup service disabled, nothing to restore"
  562. fi
  563. # (re-)try dnsmasq restart without bogus adblock lists / with backups
  564. #
  565. if [ -n "${restore_done}" ] || [ -n "${rm_done}" ]
  566. then
  567. /etc/init.d/dnsmasq restart
  568. sleep 1
  569. check="$(pgrep -f "dnsmasq")"
  570. if [ -n "${check}" ]
  571. then
  572. rc=0
  573. f_cntconfig
  574. f_log "adblock lists with overall ${adb_count} domains loaded"
  575. else
  576. rc=120
  577. f_log "dnsmasq restart failed, please check 'logread' output" "${rc}"
  578. fi
  579. fi
  580. f_exit
  581. }
  582. # f_exit: delete (temporary) files, generate statistics and exit
  583. #
  584. f_exit()
  585. {
  586. local ipv4_adblock=0
  587. local ipv6_adblock=0
  588. local lastrun="$(${adb_date} "+%d.%m.%Y %H:%M:%S")"
  589. # delete temporary files & directories
  590. #
  591. if [ -f "${adb_tmpfile}" ]
  592. then
  593. rm -f "${adb_tmpfile}"
  594. fi
  595. if [ -d "${adb_tmpdir}" ]
  596. then
  597. rm -rf "${adb_tmpdir}"
  598. fi
  599. # final log message and iptables statistics
  600. #
  601. if [ $((rc)) -eq 0 ]
  602. then
  603. if [ -n "${adb_wanif4}" ]
  604. then
  605. ipv4_adblock="$(${adb_iptv4} -t nat -vnL | awk '$11 ~ /^adb-nat$/ {sum += $1} END {printf sum}')"
  606. ipv4_adblock="$((${ipv4_adblock} + $(${adb_iptv4} -vnL | awk '$11 ~ /^adb-(fwd|out)$/ {sum += $1} END {printf sum}')))"
  607. fi
  608. if [ -n "${adb_wanif6}" ]
  609. then
  610. ipv6_adblock="$(${adb_iptv6} -t nat -vnL | awk '$10 ~ /^adb-nat$/ {sum += $1} END {printf sum}')"
  611. ipv6_adblock="$((${ipv6_adblock} + $(${adb_iptv6} -vnL | awk '$10 ~ /^adb-(fwd|out)$/ {sum += $1} END {printf sum}')))"
  612. fi
  613. if [ -n "$(${adb_uci} -q changes adblock)" ]
  614. then
  615. "${adb_uci}" -q commit "adblock"
  616. fi
  617. f_log "firewall statistics (IPv4/IPv6): ${ipv4_adblock}/${ipv6_adblock} ad related packets blocked"
  618. f_log "domain adblock processing finished successfully (${adb_scriptver}, ${adb_sysver}, ${lastrun})"
  619. elif [ $((rc)) -gt 0 ]
  620. then
  621. if [ -n "$(${adb_uci} -q changes adblock)" ]
  622. then
  623. "${adb_uci}" -q revert "adblock"
  624. fi
  625. f_log "domain adblock processing failed (${adb_scriptver}, ${adb_sysver}, ${lastrun})"
  626. else
  627. rc=0
  628. fi
  629. rm -f "${adb_pidfile}"
  630. exit ${rc}
  631. }