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
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 2>/dev/null)"
  13. cfg_enabled="$(/sbin/uci -q get adblock.global.adb_enabled 2>/dev/null)"
  14. rc=$?
  15. if [ $((rc)) -ne 0 ] || [ "${cfg_version}" != "${adb_scriptver%.*}" ]
  16. then
  17. cp -pf "/etc/adblock/adblock.conf.default" "/etc/config/adblock" >/dev/null 2>&1
  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" 2>/dev/null
  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" 2>/dev/null
  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 2>/dev/null)"
  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 2>/dev/null)"
  148. adb_tmpdir="$(mktemp -p /tmp -d 2>/dev/null)"
  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[ ]+${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[ ]+${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_whocares="awk '\$0 ~/^127\.0\.0\.1[ ]+${rset_core}/{print tolower(\$2)}'"
  175. rset_winhelp="awk '\$0 ~/^0\.0\.0\.0[ ]+${rset_core}/{print tolower(\$2)}'"
  176. rset_yoyo="awk '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
  177. rset_zeus="awk '\$1 ~/^${rset_core}/{print tolower(\$1)}'"
  178. # get logical wan update interfaces (with default route) and their device names
  179. #
  180. network_find_wan adb_wanif4 2>/dev/null
  181. network_find_wan6 adb_wanif6 2>/dev/null
  182. if [ -z "${adb_wanif4}" ] && [ -z "${adb_wanif6}" ]
  183. then
  184. rc=125
  185. f_log "no valid IPv4/IPv6 wan update interface found" "${rc}"
  186. f_exit
  187. elif [ "${adb_wanif4}" = "${adb_lanif}" ] || [ "${adb_wanif6}" = "${adb_lanif}" ]
  188. then
  189. rc=125
  190. f_log "LAN only (${adb_lanif}) network, no valid IPv4/IPv6 wan update interface found" "${rc}"
  191. f_exit
  192. else
  193. network_get_device adb_wandev4 "${adb_wanif4}" 2>/dev/null
  194. network_get_device adb_wandev6 "${adb_wanif6}" 2>/dev/null
  195. break
  196. fi
  197. # get lan ip addresses
  198. #
  199. network_get_ipaddr adb_ipv4 "${adb_lanif}" 2>/dev/null
  200. network_get_ipaddr6 adb_ipv6 "${adb_lanif}" 2>/dev/null
  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 -" 2>/dev/null)"
  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 -" 2>/dev/null)"
  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 -" 2>/dev/null)"
  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" 2>/dev/null)"
  280. mem_free="$(awk '$1 ~ /^MemFree/ {printf $2}' "/proc/meminfo" 2>/dev/null)"
  281. mem_swap="$(awk '$1 ~ /^SwapTotal/ {printf $2}' "/proc/meminfo" 2>/dev/null)"
  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. fi
  287. # check backup configuration
  288. #
  289. if [ -n "${adb_backupdir}" ] && [ -d "${adb_backupdir}" ]
  290. then
  291. f_space "${adb_backupdir}"
  292. if [ "${space_ok}" = "false" ]
  293. then
  294. f_log "not enough free space in '${adb_backupdir}'(avail. ${av_space} kb), backup/restore will be disabled"
  295. backup_ok="false"
  296. else
  297. f_log "backup/restore will be enabled"
  298. backup_ok="true"
  299. fi
  300. else
  301. backup_ok="false"
  302. f_log "backup/restore will be disabled"
  303. fi
  304. # check ipv4/iptables configuration
  305. #
  306. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wandev4}" ]
  307. then
  308. 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}"
  309. f_firewall "IPv4" "filter" "A" "${adb_fwdchain_ipv4}" "adb-forward" "! -i ${adb_wandev4} -d ${adb_nullipv4} -j REJECT --reject-with icmp-host-unreachable"
  310. f_firewall "IPv4" "filter" "A" "${adb_outchain_ipv4}" "adb-output" "! -i ${adb_wandev4} -d ${adb_nullipv4} -j REJECT --reject-with icmp-host-unreachable"
  311. if [ $((adb_forcedns)) -eq 1 ]
  312. then
  313. f_firewall "IPv4" "nat" "A" "${adb_prechain_ipv4}" "adb-dns" "! -i ${adb_wandev4} -p udp --dport 53 -j REDIRECT"
  314. f_firewall "IPv4" "nat" "A" "${adb_prechain_ipv4}" "adb-dns" "! -i ${adb_wandev4} -p tcp --dport 53 -j REDIRECT"
  315. fi
  316. if [ "${fw_done}" = "true" ]
  317. then
  318. f_log "created volatile IPv4 firewall ruleset"
  319. fw_done="false"
  320. fi
  321. fi
  322. # check ipv6/ip6tables configuration
  323. #
  324. if [ -n "${adb_wanif6}" ] && [ -n "${adb_wandev6}" ]
  325. then
  326. 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}"
  327. f_firewall "IPv6" "filter" "A" "${adb_fwdchain_ipv6}" "adb-forward" "! -i ${adb_wandev6} -d ${adb_nullipv6} -j REJECT --reject-with icmp6-addr-unreachable"
  328. f_firewall "IPv6" "filter" "A" "${adb_outchain_ipv6}" "adb-output" "! -i ${adb_wandev6} -d ${adb_nullipv6} -j REJECT --reject-with icmp6-addr-unreachable"
  329. if [ $((adb_forcedns)) -eq 1 ]
  330. then
  331. f_firewall "IPv6" "nat" "A" "${adb_prechain_ipv6}" "adb-dns" "! -i ${adb_wandev6} -p udp --dport 53 -j REDIRECT"
  332. f_firewall "IPv6" "nat" "A" "${adb_prechain_ipv6}" "adb-dns" "! -i ${adb_wandev6} -p tcp --dport 53 -j REDIRECT"
  333. fi
  334. if [ "${fw_done}" = "true" ]
  335. then
  336. f_log "created volatile IPv6 firewall ruleset"
  337. fw_done="false"
  338. fi
  339. fi
  340. # check volatile adblock uhttpd instance configuration
  341. #
  342. rc="$(ps | grep "[u]httpd.*\-h /www/adblock" >/dev/null 2>&1; printf ${?})"
  343. if [ $((rc)) -ne 0 ]
  344. then
  345. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
  346. then
  347. 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}">/dev/null 2>&1
  348. rc=${?}
  349. elif [ -n "${adb_wanif4}" ]
  350. then
  351. uhttpd -h "/www/adblock" -k 5 -N 200 -t 0 -T 1 -D -S -E "/index.html" -p "${adb_ipv4}:${adb_port}" >/dev/null 2>&1
  352. rc=${?}
  353. elif [ -n "${adb_wanif6}" ]
  354. then
  355. uhttpd -h "/www/adblock" -k 5 -N 200 -t 0 -T 1 -D -S -E "/index.html" -p "[${adb_ipv6}]:${adb_port}" >/dev/null 2>&1
  356. rc=${?}
  357. fi
  358. if [ $((rc)) -eq 0 ]
  359. then
  360. f_log "created volatile uhttpd instance"
  361. else
  362. f_log "failed to initialize volatile uhttpd instance" "${rc}"
  363. f_restore
  364. fi
  365. fi
  366. # set dnsmasq defaults
  367. #
  368. if [ -n "${adb_wanif4}" ] && [ -n "${adb_wanif6}" ]
  369. then
  370. adb_dnsformat="awk -v ipv4="${adb_nullipv4}" -v ipv6="${adb_nullipv6}" '{print \"address=/\"\$0\"/\"ipv4\"\n\"\"address=/\"\$0\"/\"ipv6}'"
  371. elif [ -n "${adb_wanif4}" ]
  372. then
  373. adb_dnsformat="awk -v ipv4="${adb_nullipv4}" '{print \"address=/\"\$0\"/\"ipv4}'"
  374. elif [ -n "${adb_wanif6}" ]
  375. then
  376. adb_dnsformat="awk -v ipv6="${adb_nullipv6}" '{print \"address=/\"\$0\"/\"ipv6}'"
  377. fi
  378. # remove no longer used opkg package list
  379. #
  380. unset pkg_list
  381. }
  382. ######################################
  383. # f_depend: check package dependencies
  384. #
  385. f_depend()
  386. {
  387. local check
  388. local package="${1}"
  389. check="$(printf "${pkg_list}" | grep "^${package} -" 2>/dev/null)"
  390. if [ -z "${check}" ]
  391. then
  392. rc=140
  393. f_log "package '${package}' not found" "${rc}"
  394. f_exit
  395. fi
  396. }
  397. ##############################################
  398. # f_firewall: set iptables rules for ipv4/ipv6
  399. #
  400. f_firewall()
  401. {
  402. local ipt
  403. local iptv4="/usr/sbin/iptables"
  404. local iptv6="/usr/sbin/ip6tables"
  405. local proto="${1}"
  406. local table="${2}"
  407. local ctype="${3}"
  408. local chain="${4}"
  409. local notes="${5}"
  410. local rules="${6}"
  411. # select appropriate iptables executable
  412. #
  413. if [ "${proto}" = "IPv4" ]
  414. then
  415. ipt="${iptv4}"
  416. else
  417. ipt="${iptv6}"
  418. fi
  419. # check whether iptables rule already applied and proceed accordingly
  420. #
  421. rc="$("${ipt}" -w -t "${table}" -C "${chain}" -m comment --comment "${notes}" ${rules} >/dev/null 2>&1; printf ${?})"
  422. if [ $((rc)) -ne 0 ]
  423. then
  424. "${ipt}" -w -t "${table}" -"${ctype}" "${chain}" -m comment --comment "${notes}" ${rules} >/dev/null 2>&1
  425. rc=${?}
  426. if [ $((rc)) -eq 0 ]
  427. then
  428. fw_done="true"
  429. else
  430. f_log "failed to initialize volatile ${proto} firewall rule '${notes}'" "${rc}"
  431. f_restore
  432. fi
  433. fi
  434. }
  435. ##########################################
  436. # f_log: log messages to stdout and syslog
  437. #
  438. f_log()
  439. {
  440. local log_parm
  441. local log_msg="${1}"
  442. local log_rc="${2}"
  443. local class="info "
  444. # check for terminal session
  445. #
  446. if [ -t 1 ]
  447. then
  448. log_parm="-s"
  449. fi
  450. # log to different output devices and set log class accordingly
  451. #
  452. if [ -n "${log_msg}" ]
  453. then
  454. if [ $((log_rc)) -gt 0 ]
  455. then
  456. class="error"
  457. log_rc=", rc: ${log_rc}"
  458. log_msg="${log_msg}${log_rc}"
  459. fi
  460. /usr/bin/logger ${log_parm} -t "adblock[${adb_pid}] ${class}" "${log_msg}"
  461. fi
  462. }
  463. ################################################
  464. # f_space: check mount points/space requirements
  465. #
  466. f_space()
  467. {
  468. local mp="${1}"
  469. # check relevant mount points in a subshell
  470. #
  471. if [ -d "${mp}" ]
  472. then
  473. av_space="$(df "${mp}" 2>/dev/null | tail -n1 2>/dev/null | 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 "{}" \; 2>/dev/null)"
  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}" \; 2>/dev/null)"
  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 >/dev/null 2>&1
  524. sleep 1
  525. dns_status="$(ps 2>/dev/null | grep "[d]nsmasq" 2>/dev/null)"
  526. if [ -n "${dns_status}" ]
  527. then
  528. rc=0
  529. adb_count="$(head -qn -3 "${adb_dnsdir}/${adb_dnsprefix}."* 2>/dev/null | 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}" >/dev/null 2>&1
  561. fi
  562. if [ -d "${adb_tmpdir}" ]
  563. then
  564. rm -rf "${adb_tmpdir}" >/dev/null 2>&1
  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}" >/dev/null 2>&1
  594. exit ${rc}
  595. }