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.

638 lines
20 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. fi
  197. # get lan ip addresses
  198. #
  199. network_get_ipaddr adb_ipv4 "${adb_lanif}"
  200. network_get_ipaddr6 adb_ipv6 "${adb_lanif}"
  201. if [ -z "${adb_ipv4}" ] && [ -z "${adb_ipv6}" ]
  202. then
  203. rc=130
  204. f_log "no valid IPv4/IPv6 configuration for given logical LAN interface found (${adb_lanif}), please set 'adb_lanif' manually" "${rc}"
  205. f_exit
  206. fi
  207. }
  208. #################################################
  209. # f_envcheck: check/set environment prerequisites
  210. #
  211. f_envcheck()
  212. {
  213. local check
  214. # check general package dependencies
  215. #
  216. f_depend "uhttpd"
  217. f_depend "wget"
  218. f_depend "iptables"
  219. f_depend "kmod-ipt-nat"
  220. # check ipv6 related package dependencies
  221. #
  222. if [ -n "${adb_wanif6}" ]
  223. then
  224. check="$(printf "${pkg_list}" | grep "^ip6tables -")"
  225. if [ -z "${check}" ]
  226. then
  227. f_log "package 'ip6tables' not found, IPv6 support will be disabled"
  228. unset adb_wanif6
  229. else
  230. check="$(printf "${pkg_list}" | grep "^kmod-ipt-nat6 -")"
  231. if [ -z "${check}" ]
  232. then
  233. f_log "package 'kmod-ipt-nat6' not found, IPv6 support will be disabled"
  234. unset adb_wanif6
  235. fi
  236. fi
  237. fi
  238. # check ca-certificates package and set wget parms accordingly
  239. #
  240. wget_parm="--no-config --quiet --tries=1 --no-cache --no-cookies --max-redirect=0 --dns-timeout=5 --connect-timeout=5 --read-timeout=5"
  241. check="$(printf "${pkg_list}" | grep "^ca-certificates -")"
  242. if [ -z "${check}" ]
  243. then
  244. wget_parm="${wget_parm} --no-check-certificate"
  245. fi
  246. # check adblock blacklist/whitelist configuration
  247. #
  248. if [ ! -r "${adb_blacklist}" ]
  249. then
  250. f_log "adblock blacklist not found, source will be disabled"
  251. fi
  252. if [ ! -r "${adb_whitelist}" ]
  253. then
  254. f_log "adblock whitelist not found, source will be disabled"
  255. fi
  256. # check adblock temp directory
  257. #
  258. if [ -n "${adb_tmpdir}" ] && [ -d "${adb_tmpdir}" ]
  259. then
  260. f_space "${adb_tmpdir}"
  261. if [ "${space_ok}" = "false" ]
  262. then
  263. if [ $((av_space)) -le 2000 ]
  264. then
  265. rc=135
  266. f_log "not enough free space in '${adb_tmpdir}' (avail. ${av_space} kb)" "${rc}"
  267. f_exit
  268. else
  269. f_log "not enough free space to handle all adblock list sources at once in '${adb_tmpdir}' (avail. ${av_space} kb)"
  270. fi
  271. fi
  272. else
  273. rc=135
  274. f_log "temp directory not found" "${rc}"
  275. f_exit
  276. fi
  277. # memory check
  278. #
  279. mem_total="$(awk '$1 ~ /^MemTotal/ {printf $2}' "/proc/meminfo")"
  280. mem_free="$(awk '$1 ~ /^MemFree/ {printf $2}' "/proc/meminfo")"
  281. mem_swap="$(awk '$1 ~ /^SwapTotal/ {printf $2}' "/proc/meminfo")"
  282. if [ $((mem_total)) -le 64000 ] && [ $((mem_swap)) -eq 0 ]
  283. then
  284. mem_ok="false"
  285. f_log "not enough free memory, overall sort processing will be disabled (total: ${mem_total}, free: ${mem_free}, swap: ${mem_swap})"
  286. else
  287. mem_ok="true"
  288. fi
  289. # check backup configuration
  290. #
  291. if [ -n "${adb_backupdir}" ] && [ -d "${adb_backupdir}" ]
  292. then
  293. f_space "${adb_backupdir}"
  294. if [ "${space_ok}" = "false" ]
  295. then
  296. f_log "not enough free space in '${adb_backupdir}'(avail. ${av_space} kb), backup/restore will be disabled"
  297. backup_ok="false"
  298. else
  299. f_log "backup/restore will be enabled"
  300. backup_ok="true"
  301. fi
  302. else
  303. backup_ok="false"
  304. f_log "backup/restore will be disabled"
  305. fi
  306. # check ipv4/iptables configuration
  307. #
  308. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wandev4}" ]
  309. then
  310. 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}"
  311. f_firewall "IPv4" "filter" "A" "${adb_fwdchain_ipv4}" "adb-forward" "! -i ${adb_wandev4} -d ${adb_nullipv4} -j REJECT --reject-with icmp-host-unreachable"
  312. f_firewall "IPv4" "filter" "A" "${adb_outchain_ipv4}" "adb-output" "! -i ${adb_wandev4} -d ${adb_nullipv4} -j REJECT --reject-with icmp-host-unreachable"
  313. if [ $((adb_forcedns)) -eq 1 ]
  314. then
  315. f_firewall "IPv4" "nat" "A" "${adb_prechain_ipv4}" "adb-dns" "! -i ${adb_wandev4} -p udp --dport 53 -j REDIRECT"
  316. f_firewall "IPv4" "nat" "A" "${adb_prechain_ipv4}" "adb-dns" "! -i ${adb_wandev4} -p tcp --dport 53 -j REDIRECT"
  317. fi
  318. if [ "${fw_done}" = "true" ]
  319. then
  320. f_log "created volatile IPv4 firewall ruleset"
  321. fw_done="false"
  322. fi
  323. fi
  324. # check ipv6/ip6tables configuration
  325. #
  326. if [ -n "${adb_wanif6}" ] && [ -n "${adb_wandev6}" ]
  327. then
  328. 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}"
  329. f_firewall "IPv6" "filter" "A" "${adb_fwdchain_ipv6}" "adb-forward" "! -i ${adb_wandev6} -d ${adb_nullipv6} -j REJECT --reject-with icmp6-addr-unreachable"
  330. f_firewall "IPv6" "filter" "A" "${adb_outchain_ipv6}" "adb-output" "! -i ${adb_wandev6} -d ${adb_nullipv6} -j REJECT --reject-with icmp6-addr-unreachable"
  331. if [ $((adb_forcedns)) -eq 1 ]
  332. then
  333. f_firewall "IPv6" "nat" "A" "${adb_prechain_ipv6}" "adb-dns" "! -i ${adb_wandev6} -p udp --dport 53 -j REDIRECT"
  334. f_firewall "IPv6" "nat" "A" "${adb_prechain_ipv6}" "adb-dns" "! -i ${adb_wandev6} -p tcp --dport 53 -j REDIRECT"
  335. fi
  336. if [ "${fw_done}" = "true" ]
  337. then
  338. f_log "created volatile IPv6 firewall ruleset"
  339. fw_done="false"
  340. fi
  341. fi
  342. # check volatile adblock uhttpd instance configuration
  343. #
  344. rc="$(ps | grep -q "[u]httpd.*\-h /www/adblock"; printf ${?})"
  345. if [ $((rc)) -ne 0 ]
  346. then
  347. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
  348. then
  349. 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}"
  350. rc=${?}
  351. elif [ -n "${adb_wanif4}" ]
  352. then
  353. uhttpd -h "/www/adblock" -k 5 -N 200 -t 0 -T 1 -D -S -E "/index.html" -p "${adb_ipv4}:${adb_port}"
  354. rc=${?}
  355. elif [ -n "${adb_wanif6}" ]
  356. then
  357. uhttpd -h "/www/adblock" -k 5 -N 200 -t 0 -T 1 -D -S -E "/index.html" -p "[${adb_ipv6}]:${adb_port}"
  358. rc=${?}
  359. fi
  360. if [ $((rc)) -eq 0 ]
  361. then
  362. f_log "created volatile uhttpd instance"
  363. else
  364. f_log "failed to initialize volatile uhttpd instance" "${rc}"
  365. f_restore
  366. fi
  367. fi
  368. # set dnsmasq defaults
  369. #
  370. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
  371. then
  372. adb_dnsformat="awk -v ipv4="${adb_nullipv4}" -v ipv6="${adb_nullipv6}" '{print \"address=/\"\$0\"/\"ipv4\"\n\"\"address=/\"\$0\"/\"ipv6}'"
  373. elif [ -n "${adb_wanif4}" ]
  374. then
  375. adb_dnsformat="awk -v ipv4="${adb_nullipv4}" '{print \"address=/\"\$0\"/\"ipv4}'"
  376. elif [ -n "${adb_wanif6}" ]
  377. then
  378. adb_dnsformat="awk -v ipv6="${adb_nullipv6}" '{print \"address=/\"\$0\"/\"ipv6}'"
  379. fi
  380. # remove no longer used opkg package list
  381. #
  382. unset pkg_list
  383. }
  384. ######################################
  385. # f_depend: check package dependencies
  386. #
  387. f_depend()
  388. {
  389. local check
  390. local package="${1}"
  391. check="$(printf "${pkg_list}" | grep "^${package} -")"
  392. if [ -z "${check}" ]
  393. then
  394. rc=140
  395. f_log "package '${package}' not found" "${rc}"
  396. f_exit
  397. fi
  398. }
  399. ##############################################
  400. # f_firewall: set iptables rules for ipv4/ipv6
  401. #
  402. f_firewall()
  403. {
  404. local ipt
  405. local iptv4="/usr/sbin/iptables"
  406. local iptv6="/usr/sbin/ip6tables"
  407. local proto="${1}"
  408. local table="${2}"
  409. local ctype="${3}"
  410. local chain="${4}"
  411. local notes="${5}"
  412. local rules="${6}"
  413. # select appropriate iptables executable
  414. #
  415. if [ "${proto}" = "IPv4" ]
  416. then
  417. ipt="${iptv4}"
  418. else
  419. ipt="${iptv6}"
  420. fi
  421. # check whether iptables rule already applied and proceed accordingly
  422. #
  423. rc="$("${ipt}" -w -t "${table}" -C "${chain}" -m comment --comment "${notes}" ${rules}; printf ${?})"
  424. if [ $((rc)) -ne 0 ]
  425. then
  426. "${ipt}" -w -t "${table}" -"${ctype}" "${chain}" -m comment --comment "${notes}" ${rules}
  427. rc=${?}
  428. if [ $((rc)) -eq 0 ]
  429. then
  430. fw_done="true"
  431. else
  432. f_log "failed to initialize volatile ${proto} firewall rule '${notes}'" "${rc}"
  433. f_restore
  434. fi
  435. fi
  436. }
  437. ##########################################
  438. # f_log: log messages to stdout and syslog
  439. #
  440. f_log()
  441. {
  442. local log_parm
  443. local log_msg="${1}"
  444. local log_rc="${2}"
  445. local class="info "
  446. # check for terminal session
  447. #
  448. if [ -t 1 ]
  449. then
  450. log_parm="-s"
  451. fi
  452. # log to different output devices and set log class accordingly
  453. #
  454. if [ -n "${log_msg}" ]
  455. then
  456. if [ $((log_rc)) -gt 0 ]
  457. then
  458. class="error"
  459. log_rc=", rc: ${log_rc}"
  460. log_msg="${log_msg}${log_rc}"
  461. fi
  462. /usr/bin/logger ${log_parm} -t "adblock[${adb_pid}] ${class}" "${log_msg}" 2>&1
  463. fi
  464. }
  465. ################################################
  466. # f_space: check mount points/space requirements
  467. #
  468. f_space()
  469. {
  470. local mp="${1}"
  471. if [ -d "${mp}" ]
  472. then
  473. av_space="$(df "${mp}" | tail -n1 | awk '{printf $4}')"
  474. if [ $((av_space)) -lt $((adb_minspace)) ]
  475. then
  476. space_ok="false"
  477. fi
  478. fi
  479. }
  480. ##################################################################
  481. # f_restore: restore last adblock list backups and restart dnsmasq
  482. #
  483. f_restore()
  484. {
  485. local rm_done
  486. local restore_done
  487. # remove bogus adblock lists
  488. #
  489. if [ -n "${adb_revsrclist}" ]
  490. then
  491. rm_done="$(find "${adb_dnsdir}" -maxdepth 1 -type f \( ${adb_revsrclist} \) -print -exec rm -f "{}" \;)"
  492. rc=${?}
  493. if [ $((rc)) -eq 0 ] && [ -n "${rm_done}" ]
  494. then
  495. f_log "all bogus adblock lists removed"
  496. elif [ $((rc)) -ne 0 ]
  497. then
  498. f_log "error during removal of bogus adblock lists" "${rc}"
  499. f_exit
  500. fi
  501. fi
  502. # restore backups
  503. #
  504. if [ "${backup_ok}" = "true" ] && [ "$(printf "${adb_backupdir}/${adb_dnsprefix}."*)" != "${adb_backupdir}/${adb_dnsprefix}.*" ]
  505. then
  506. restore_done="$(find "${adb_backupdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}.*" -print -exec cp -pf "{}" "${adb_dnsdir}" \;)"
  507. rc=${?}
  508. if [ $((rc)) -eq 0 ] && [ -n "${restore_done}" ]
  509. then
  510. f_log "all available backups restored"
  511. elif [ $((rc)) -ne 0 ]
  512. then
  513. f_log "error during restore of adblock lists" "${rc}"
  514. f_exit
  515. fi
  516. else
  517. f_log "no backups found, nothing to restore"
  518. fi
  519. # (re-)try dnsmasq restart without bogus adblock lists / with backups
  520. #
  521. if [ -n "${restore_done}" ] || [ -n "${rm_done}" ]
  522. then
  523. /etc/init.d/dnsmasq restart
  524. sleep 1
  525. rc="$(ps | grep -q "[d]nsmasq"; printf ${?})"
  526. if [ $((rc)) -eq 0 ]
  527. then
  528. rc=0
  529. adb_count="$(head -qn -3 "${adb_dnsdir}/${adb_dnsprefix}."* | wc -l)"
  530. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
  531. then
  532. adb_count="$((adb_count / 2))"
  533. fi
  534. f_log "adblock lists with overall ${adb_count} domains loaded"
  535. else
  536. rc=145
  537. f_log "dnsmasq restart failed, please check 'logread' output" "${rc}"
  538. fi
  539. fi
  540. f_exit
  541. }
  542. ###################################
  543. # f_exit: delete (temporary) files,
  544. # generate statistics and exit
  545. #
  546. f_exit()
  547. {
  548. local ipv4_prerouting=0
  549. local ipv4_forward=0
  550. local ipv4_output=0
  551. local ipv6_prerouting=0
  552. local ipv6_forward=0
  553. local ipv6_output=0
  554. local iptv4="/usr/sbin/iptables"
  555. local iptv6="/usr/sbin/ip6tables"
  556. # delete temporary files & directories
  557. #
  558. if [ -f "${adb_tmpfile}" ]
  559. then
  560. rm -f "${adb_tmpfile}"
  561. fi
  562. if [ -d "${adb_tmpdir}" ]
  563. then
  564. rm -rf "${adb_tmpdir}"
  565. fi
  566. # final log message and iptables statistics
  567. #
  568. if [ $((rc)) -eq 0 ]
  569. then
  570. if [ -n "${adb_wanif4}" ]
  571. then
  572. ipv4_prerouting="$(${iptv4} -t nat -vnL | awk '$11 ~ /^adb-prerouting$/ {sum += $1} END {printf sum}')"
  573. ipv4_forward="$(${iptv4} -vnL | awk '$11 ~ /^adb-forward$/ {sum += $1} END {printf sum}')"
  574. ipv4_output="$(${iptv4} -vnL | awk '$11 ~ /^adb-output$/ {sum += $1} END {printf sum}')"
  575. fi
  576. if [ -n "${adb_wanif6}" ]
  577. then
  578. ipv6_prerouting="$(${iptv6} -t nat -vnL | awk '$10 ~ /^adb-prerouting$/ {sum += $1} END {printf sum}')"
  579. ipv6_forward="$(${iptv6} -vnL | awk '$10 ~ /^adb-forward$/ {sum += $1} END {printf sum}')"
  580. ipv6_output="$(${iptv6} -vnL | awk '$10 ~ /^adb-output$/ {sum += $1} END {printf sum}')"
  581. fi
  582. f_log "adblock firewall statistics (IPv4/IPv6):"
  583. f_log "${ipv4_prerouting}/${ipv6_prerouting} packets redirected in PREROUTING chain"
  584. f_log "${ipv4_forward}/${ipv6_forward} packets rejected in FORWARD chain"
  585. f_log "${ipv4_output}/${ipv6_output} packets rejected in OUTPUT chain"
  586. f_log "domain adblock processing finished successfully (${adb_scriptver}, ${openwrt_version}, $(/bin/date "+%d.%m.%Y %H:%M:%S"))"
  587. elif [ $((rc)) -gt 0 ]
  588. then
  589. f_log "domain adblock processing failed (${adb_scriptver}, ${openwrt_version}, $(/bin/date "+%d.%m.%Y %H:%M:%S"))"
  590. else
  591. rc=0
  592. fi
  593. rm -f "${adb_pidfile}"
  594. exit ${rc}
  595. }