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.

173 lines
4.0 KiB

  1. #!/bin/sh /etc/rc.common
  2. #
  3. START=90
  4. EXTRA_COMMANDS="toggle stats cfgup envchk query"
  5. EXTRA_HELP=" toggle Toggle adblock 'on' or 'off'
  6. stats Update adblock statistics
  7. cfgup Update adblock configuration file
  8. envchk Check/Set adblock environment
  9. query <DOMAIN> Query active blocklists for specific domain"
  10. adb_debug=0
  11. adb_pid="${$}"
  12. adb_script="/usr/bin/adblock-update.sh"
  13. adb_helper="/usr/bin/adblock-helper.sh"
  14. adb_pidfile="/var/run/adblock.pid"
  15. bg_parm="&"
  16. if [ $((adb_debug)) -eq 0 ]
  17. then
  18. exec 2>/dev/null
  19. fi
  20. if [ -r "${adb_pidfile}" ]
  21. then
  22. logger -s -t "adblock[${adb_pid}] error" "adblock service already running ($(cat ${adb_pidfile}))" 2>&1
  23. exit 255
  24. fi
  25. . "${adb_helper}"
  26. f_envload
  27. if [ "${adb_restricted}" = "1" ]
  28. then
  29. adb_uci="$(which true)"
  30. fi
  31. boot()
  32. {
  33. return 0
  34. }
  35. start()
  36. {
  37. if [ -t 1 ]
  38. then
  39. unset bg_parm
  40. fi
  41. eval "${adb_script}" ${bg_parm}
  42. return 0
  43. }
  44. restart()
  45. {
  46. stop
  47. start
  48. }
  49. reload()
  50. {
  51. reload="true"
  52. stop
  53. start
  54. }
  55. stop()
  56. {
  57. f_rmdns
  58. f_rmuhttpd
  59. config_foreach f_rmconfig source
  60. if [ -z "${reload}" ]
  61. then
  62. f_rmfirewall
  63. fi
  64. if [ -n "${rm_dns}" ] || [ -n "${rm_uhttpd}" ] || [ -n "${rm_fw}" ] || [ -n "$(${adb_uci} -q changes adblock)" ]
  65. then
  66. "${adb_uci}" -q commit adblock
  67. f_log "all adblock related services stopped"
  68. fi
  69. return 0
  70. }
  71. toggle()
  72. {
  73. if [ -d "${adb_dnshidedir}" ]
  74. then
  75. list_dns="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
  76. list_dnshide="$(find "${adb_dnshidedir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
  77. if [ -n "${list_dns}" ]
  78. then
  79. source="${adb_dnsdir}/${adb_dnsprefix}"
  80. target="${adb_dnshidedir}"
  81. pos="off"
  82. elif [ -n "${list_dnshide}" ]
  83. then
  84. source="${adb_dnshidedir}/${adb_dnsprefix}"
  85. target="${adb_dnsdir}"
  86. pos="on"
  87. fi
  88. if [ -n "${list_dns}" ] || [ -n "${list_dnshide}" ]
  89. then
  90. mv -f "${source}"* "${target}"
  91. /etc/init.d/dnsmasq restart
  92. "${adb_uci}" -q set "adblock.global.adb_dnstoggle=${pos}"
  93. "${adb_uci}" -q commit "adblock"
  94. f_log "adblock toggle switched '${pos}'"
  95. fi
  96. fi
  97. return 0
  98. }
  99. stats()
  100. {
  101. f_statistics
  102. "${adb_uci}" -q commit "adblock"
  103. return 0
  104. }
  105. cfgup()
  106. {
  107. stop
  108. cp -pf "/etc/adblock/adblock.conf.default" "/etc/config/adblock"
  109. rc=$?
  110. if [ $((rc)) -eq 0 ]
  111. then
  112. f_log "default adblock configuration applied, please check the settings in '/etc/config/adblock'"
  113. else
  114. f_log "default adblock configuration not found, please re-install the package via 'opkg install adblock --force-maintainer'"
  115. fi
  116. return 0
  117. }
  118. query()
  119. {
  120. domain="${1}"
  121. tld="${domain#*.}"
  122. list_dns="$(find "${adb_dnsdir}" -maxdepth 1 -type f -name "${adb_dnsprefix}*" -print)"
  123. if [ -z "${list_dns}" ]
  124. then
  125. f_log "no active blocklists found, please start adblock first"
  126. elif [ -z "${domain}" ] || [ "${domain}" = "${tld}" ]
  127. then
  128. f_log "invalid domain query input, please submit a specific (sub-)domain, i.e. 'www.abc.xyz'"
  129. else
  130. while [ "${domain}" != "${tld}" ]
  131. do
  132. search="${domain//./\.}"
  133. result="$(grep -Hm 1 "[/\.]${search}/" "${adb_dnsdir}/adb_list"* | awk -F ':|/' '{print " "$4"\t: "$6}')"
  134. count="$(grep -hc "[/\.]${search}/" "${adb_dnsdir}/adb_list"* | awk '{sum += $1} END {printf sum}')"
  135. printf "%s\n" "=> distinct results for domain '${domain}' (overall ${count})"
  136. if [ -z "${result}" ]
  137. then
  138. printf "%s\n" " no matches in active blocklists"
  139. else
  140. printf "%s\n" "${result}"
  141. fi
  142. domain="${tld}"
  143. tld="${domain#*.}"
  144. done
  145. fi
  146. return 0
  147. }
  148. envchk()
  149. {
  150. adb_loglevel=0
  151. f_envcheck
  152. rm -f "${adb_tmpfile}"
  153. rm -rf "${adb_tmpdir}"
  154. adb_loglevel=1
  155. f_log "adblock environment check finished successfully"
  156. return 0
  157. }