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.

641 lines
21 KiB

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