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.

1148 lines
36 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright 2017-2020 Stan Grishin (stangri@melmac.net)
  3. # shellcheck disable=SC2039,SC1091,SC2016
  4. PKG_VERSION='dev-test'
  5. # shellcheck disable=SC2034
  6. START=94
  7. # shellcheck disable=SC2034
  8. USE_PROCD=1
  9. LC_ALL=C
  10. extra_command 'check' 'Checks if specified domain is found in current block-list'
  11. extra_command 'dl' 'Force-downloads all enabled block-list'
  12. extra_command 'killcache' 'Delete all cached files'
  13. extra_command 'sizes' 'Displays the file-sizes of enabled block-listo'
  14. extra_command 'show' 'Shows the service last-run status'
  15. extra_command 'version' 'Show version information'
  16. readonly packageName='simple-adblock'
  17. readonly serviceName="$packageName $PKG_VERSION"
  18. readonly addnhostsFile="/var/run/${packageName}.addnhosts"
  19. readonly addnhostsCache="/var/run/${packageName}.addnhosts.cache"
  20. readonly addnhostsGzip="/etc/${packageName}.addnhosts.gz"
  21. readonly addnhostsOutputFilter='s|^|127.0.0.1 |;s|$||'
  22. readonly addnhostsOutputFilterIPv6='s|^|:: |;s|$||'
  23. readonly dnsmasqFile="/var/dnsmasq.d/${packageName}"
  24. readonly dnsmasqCache="/var/run/${packageName}.dnsmasq.cache"
  25. readonly dnsmasqGzip="/etc/${packageName}.dnsmasq.gz"
  26. readonly dnsmasqOutputFilter='s|^|local=/|;s|$|/|'
  27. readonly ipsetFile="/var/dnsmasq.d/${packageName}.ipset"
  28. readonly ipsetCache="/var/run/${packageName}.ipset.cache"
  29. readonly ipsetGzip="/etc/${packageName}.ipset.gz"
  30. readonly ipsetOutputFilter='s|^|ipset=/|;s|$|/adb|'
  31. readonly serversFile="/var/run/${packageName}.servers"
  32. readonly serversCache="/var/run/${packageName}.servers.cache"
  33. readonly serversGzip="/etc/${packageName}.servers.gz"
  34. readonly serversOutputFilter='s|^|server=/|;s|$|/|'
  35. readonly unboundFile="/var/lib/unbound/adb_list.${packageName}"
  36. readonly unboundCache="/var/run/${packageName}.unbound.cache"
  37. readonly unboundGzip="/etc/${packageName}.unbound.gz"
  38. readonly unboundOutputFilter='s|^|local-zone: "|;s|$|" static|'
  39. readonly A_TMP="/var/${packageName}.hosts.a.tmp"
  40. readonly B_TMP="/var/${packageName}.hosts.b.tmp"
  41. readonly jsonFile="/var/run/${packageName}.json"
  42. readonly sharedMemoryError="/dev/shm/$packageName-error"
  43. readonly sharedMemoryOutput="/dev/shm/$packageName-output"
  44. readonly hostsFilter='/localhost/d;/^#/d;/^[^0-9]/d;s/^0\.0\.0\.0.//;s/^127\.0\.0\.1.//;s/[[:space:]]*#.*$//;s/[[:cntrl:]]$//;s/[[:space:]]//g;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
  45. readonly domainsFilter='/^#/d;s/[[:space:]]*#.*$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
  46. readonly _OK_='\033[0;32m\xe2\x9c\x93\033[0m'
  47. readonly _FAIL_='\033[0;31m\xe2\x9c\x97\033[0m'
  48. readonly __OK__='\033[0;32m[\xe2\x9c\x93]\033[0m'
  49. readonly __FAIL__='\033[0;31m[\xe2\x9c\x97]\033[0m'
  50. readonly _ERROR_='\033[0;31mERROR\033[0m'
  51. readonly _WARNING_='\033[0;33mWARNING\033[0m'
  52. version() { echo "$PKG_VERSION"; }
  53. getStatusText() {
  54. local _ret
  55. case "$1" in
  56. statusNoInstall) _ret="$serviceName is not installed or not found";;
  57. statusStopped) _ret="Stopped";;
  58. statusStarting) _ret="Starting";;
  59. statusRestarting) _ret="Restarting";;
  60. statusForceReloading) _ret="Force Reloading";;
  61. statusDownloading) _ret="Downloading";;
  62. statusProcessing) _ret="Processing";;
  63. statusError) _ret="Error";;
  64. statusWarning) _ret="Warning";;
  65. statusFail) _ret="Fail";;
  66. statusSuccess) _ret="Success";;
  67. esac
  68. printf "%b" "$_ret"
  69. }
  70. getErrorText() {
  71. local _ret
  72. case "$1" in
  73. errorOutputFileCreate) _ret="failed to create $outputFile file";;
  74. errorFailDNSReload) _ret="failed to restart/reload DNS resolver";;
  75. errorSharedMemory) _ret="failed to access shared memory";;
  76. errorSorting) _ret="failed to sort data file";;
  77. errorOptimization) _ret="failed to optimize data file";;
  78. errorAllowListProcessing) _ret="failed to process allow-list";;
  79. errorDataFileFormatting) _ret="failed to format data file";;
  80. errorMovingDataFile) _ret="failed to move data file '${A_TMP}' to '${outputFile}'";;
  81. errorCreatingCompressedCache) _ret="failed to create compressed cache";;
  82. errorRemovingTempFiles) _ret="failed to remove temporary files";;
  83. errorRestoreCompressedCache) _ret="failed to unpack compressed cache";;
  84. errorRestoreCache) _ret="failed to move '$outputCache' to '$outputFile'";;
  85. errorOhSnap) _ret="failed to create block-list or restart DNS resolver";;
  86. errorStopping) _ret="failed to stop $serviceName";;
  87. errorDNSReload) _ret="failed to reload/restart DNS resolver";;
  88. errorDownloadingConfigUpdate) _ret="failed to download Config Update file";;
  89. errorDownloadingList) _ret="failed to download";;
  90. errorParsingList) _ret="failed to parse";;
  91. errorParsingConfigUpdate) _ret="failed to parse Config Update file";;
  92. esac
  93. printf "%b" "$_ret"
  94. }
  95. output_ok() { output 1 "$_OK_"; output 2 "$__OK__\\n"; }
  96. output_okn() { output 1 "$_OK_\\n"; output 2 "$__OK__\\n"; }
  97. output_fail() { output 1 "$_FAIL_"; output 2 "$__FAIL__\\n"; }
  98. output_failn() { output 1 "$_FAIL_\\n"; output 2 "$__FAIL__\\n"; }
  99. str_replace() { printf "%b" "$1" | sed -e "s/$(printf "%b" "$2")/$(printf "%b" "$3")/g"; }
  100. str_contains() { test "$1" != "$(str_replace "$1" "$2" '')"; }
  101. compare_values() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; }
  102. is_chaos_calmer() { ubus -S call system board | grep -q 'Chaos Calmer'; }
  103. led_on(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'default-on' > "${1}/trigger" 2>&1; fi; }
  104. led_off(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo 'none' > "${1}/trigger" 2>&1; fi; }
  105. dnsmasq_hup() { killall -q -HUP dnsmasq; }
  106. dnsmasq_kill() { killall -q -KILL dnsmasq; }
  107. dnsmasq_restart() { /etc/init.d/dnsmasq restart >/dev/null 2>&1; }
  108. unbound_restart() { /etc/init.d/unbound restart >/dev/null 2>&1; }
  109. is_force_dns_active() { iptables-save | grep -q -w -- '--dport 53'; }
  110. is_present() { command -v "$1" >/dev/null 2>&1; }
  111. output() {
  112. # Can take a single parameter (text) to be output at any verbosity
  113. # Or target verbosity level and text to be output at specifc verbosity
  114. local msg memmsg logmsg
  115. if [ $# -ne 1 ]; then
  116. if [ $((verbosity & $1)) -gt 0 ] || [ "$verbosity" = "$1" ]; then shift; else return 0; fi
  117. fi
  118. [ -t 1 ] && printf "%b" "$1"
  119. msg="${1//$serviceName /service }";
  120. if [ "$(printf "%b" "$msg" | wc -l)" -gt 0 ]; then
  121. [ -s "$sharedMemoryOutput" ] && memmsg="$(cat "$sharedMemoryOutput")"
  122. logmsg="$(printf "%b" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
  123. logger -t "${packageName:-service} [$$]" "$(printf "%b" "$logmsg")"
  124. rm -f "$sharedMemoryOutput"
  125. else
  126. printf "%b" "$msg" >> "$sharedMemoryOutput"
  127. fi
  128. }
  129. serviceEnabled=''; forceDNS=''; forceDNSPorts=''; parallelDL=''; debug='';
  130. compressedCache=''; ipv6Enabled=''; configUpdateEnabled=''; configUpdateURL='';
  131. bootDelay=''; dlTimeout=''; curlRetry=''; verbosity=''; led='';
  132. targetDNS=''; dnsInstance='';
  133. allowed_domains=''; allowed_domains_urls='';
  134. blocked_domains=''; blocked_domains_urls=''; blocked_hosts_urls='';
  135. dl_command=''; dl_flag=''; isSSLSupported=''; allowIDN='';
  136. outputFilter=''; outputFilterIPv6=''; outputFile=''; outputGzip=''; outputCache='';
  137. awk='awk';
  138. load_package_config() {
  139. config_load "$packageName"
  140. config_get_bool serviceEnabled 'config' 'enabled' 1
  141. config_get_bool forceDNS 'config' 'force_dns' 1
  142. config_get_bool parallelDL 'config' 'parallel_downloads' 1
  143. config_get_bool debug 'config' 'debug' 0
  144. config_get_bool compressedCache 'config' 'compressed_cache' 0
  145. config_get_bool ipv6Enabled 'config' 'ipv6_enabled' 0
  146. config_get_bool configUpdateEnabled 'config' 'config_update_enabled' 0
  147. config_get forceDNSPorts 'config' 'force_dns_port' '53 853'
  148. config_get bootDelay 'config' 'boot_delay' '120'
  149. config_get dlTimeout 'config' 'download_timeout' '20'
  150. config_get curlRetry 'config' 'curl_retry' '3'
  151. config_get verbosity 'config' 'verbosity' '2'
  152. config_get led 'config' 'led'
  153. config_get targetDNS 'config' 'dns' 'dnsmasq.servers'
  154. config_get dnsInstance 'config' 'dns_instance' '0'
  155. config_get allowed_domains 'config' 'allowed_domain'
  156. config_get allowed_domains_urls 'config' 'allowed_domains_url'
  157. config_get blocked_domains 'config' 'blocked_domain'
  158. config_get blocked_domains_urls 'config' 'blocked_domains_url'
  159. config_get blocked_hosts_urls 'config' 'blocked_hosts_url'
  160. config_get configUpdateURL 'config' 'config_update_url' 'https://cdn.jsdelivr.net/gh/openwrt/packages/net/simple-adblock/files/simple-adblock.conf.update'
  161. if [ "$targetDNS" != 'dnsmasq.addnhosts' ] && [ "$targetDNS" != 'dnsmasq.conf' ] && \
  162. [ "$targetDNS" != 'dnsmasq.servers' ] && [ "$targetDNS" != 'unbound.adb_list' ] && \
  163. [ "$targetDNS" != 'dnsmasq.ipset' ] ; then
  164. targetDNS='dnsmasq.servers'
  165. fi
  166. case "$targetDNS" in
  167. dnsmasq.addnhosts)
  168. outputFilter="$addnhostsOutputFilter"
  169. outputFile="$addnhostsFile"
  170. outputCache="$addnhostsCache"
  171. outputGzip="$addnhostsGzip"
  172. [ "$ipv6Enabled" -gt 0 ] && outputFilterIPv6="$addnhostsOutputFilterIPv6"
  173. rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
  174. rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
  175. rm -f "$serversFile" "$serversCache" "$serversGzip"
  176. rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
  177. ;;
  178. dnsmasq.conf)
  179. outputFilter="$dnsmasqOutputFilter"
  180. outputFile="$dnsmasqFile"
  181. outputCache="$dnsmasqCache"
  182. outputGzip="$dnsmasqGzip"
  183. rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
  184. rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
  185. rm -f "$serversFile" "$serversCache" "$serversGzip"
  186. rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
  187. ;;
  188. dnsmasq.ipset)
  189. outputFilter="$ipsetOutputFilter"
  190. outputFile="$ipsetFile"
  191. outputCache="$ipsetCache"
  192. outputGzip="$ipsetGzip"
  193. rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
  194. rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
  195. rm -f "$serversFile" "$serversCache" "$serversGzip"
  196. rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
  197. ;;
  198. dnsmasq.servers)
  199. outputFilter="$serversOutputFilter"
  200. outputFile="$serversFile"
  201. outputCache="$serversCache"
  202. outputGzip="$serversGzip"
  203. rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
  204. rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
  205. rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
  206. rm -f "$unboundFile" "$unboundCache" "$unboundGzip"
  207. ;;
  208. unbound.adb_list)
  209. outputFilter="$unboundOutputFilter"
  210. outputFile="$unboundFile"
  211. outputCache="$unboundCache"
  212. outputGzip="$unboundGzip"
  213. rm -f "$addnhostsFile" "$addnhostsCache" "$addnhostsGzip"
  214. rm -f "$dnsmasqFile" "$dnsmasqCache" "$dnsmasqGzip"
  215. rm -f "$ipsetFile" "$ipsetCache" "$ipsetGzip"
  216. rm -f "$serversFile" "$serversCache" "$serversGzip"
  217. ;;
  218. esac
  219. if [ -z "${verbosity##*[!0-9]*}" ] || [ "$verbosity" -lt 0 ] || [ "$verbosity" -gt 2 ]; then
  220. verbosity=1
  221. fi
  222. . /lib/functions/network.sh
  223. . /usr/share/libubox/jshn.sh
  224. is_present 'gawk' && awk='gawk'
  225. if ! is_present '/usr/libexec/grep-gnu' || ! is_present '/usr/libexec/sed-gnu' || \
  226. ! is_present '/usr/libexec/sort-coreutils' || ! is_present 'gawk'; then
  227. local s="opkg update; opkg --force-overwrite install"
  228. is_present 'gawk' || s="$s gawk"
  229. is_present '/usr/libexec/grep-gnu' || s="$s grep"
  230. is_present '/usr/libexec/sed-gnu' || s="$s sed"
  231. is_present '/usr/libexec/sort-coreutils' || s="$s coreutils-sort"
  232. output "$_WARNING_: Some recommended packages are missing, install them by running:\\n"
  233. output "$s;\\n"
  234. fi
  235. # Prefer curl because it supports the file:// scheme.
  236. if is_present 'curl'; then
  237. dl_command="curl --insecure --retry $curlRetry --connect-timeout $dlTimeout --silent"
  238. dl_flag="-o"
  239. elif is_present '/usr/libexec/wget-ssl'; then
  240. dl_command="/usr/libexec/wget-ssl --no-check-certificate --timeout $dlTimeout -q"
  241. dl_flag="-O"
  242. elif is_present wget && wget --version 2>/dev/null | grep -q "+https"; then
  243. dl_command="wget --no-check-certificate --timeout $dlTimeout -q"
  244. dl_flag="-O"
  245. else
  246. dl_command="uclient-fetch --no-check-certificate --timeout $dlTimeout -q"
  247. dl_flag="-O"
  248. fi
  249. led="${led:+/sys/class/leds/$led}"
  250. if curl --version 2>/dev/null | grep -q "https" \
  251. || wget --version 2>/dev/null | grep -q "+https" \
  252. || grep -q "libustream-mbedtls" /usr/lib/opkg/status \
  253. || grep -q "libustream-openssl" /usr/lib/opkg/status \
  254. || grep -q "libustream-wolfssl" /usr/lib/opkg/status; then
  255. isSSLSupported=1
  256. else
  257. unset isSSLSupported
  258. fi
  259. }
  260. is_enabled() {
  261. wan_if=''; wan_gw='';
  262. load_package_config
  263. if [ "$debug" -ne 0 ]; then
  264. exec 1>>/tmp/simple-adblock.log
  265. exec 2>&1
  266. set -x
  267. fi
  268. if [ "$serviceEnabled" -eq 0 ]; then
  269. case "$1" in
  270. on_start)
  271. output "$packageName is currently disabled.\\n"
  272. output "Run the following commands before starting service again:\\n"
  273. output "uci set ${packageName}.config.enabled='1'; uci commit $packageName;\\n"
  274. ;;
  275. esac
  276. return 1
  277. fi
  278. case $targetDNS in
  279. dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers)
  280. if dnsmasq -v 2>/dev/null | grep -q 'no-IDN' || ! dnsmasq -v 2>/dev/null | grep -q -w 'IDN'; then
  281. allowIDN=0
  282. else
  283. allowIDN=1
  284. fi
  285. ;;
  286. unbound.adb_list)
  287. allowIDN=1;;
  288. esac
  289. case $targetDNS in
  290. dnsmasq.ipset)
  291. if dnsmasq -v 2>/dev/null | grep -q 'no-ipset' || ! dnsmasq -v 2>/dev/null | grep -q -w 'ipset'; then
  292. output "$_ERROR_: DNSMASQ ipset support is enabled in $packageName, but DNSMASQ is either not installed or installed DNSMASQ does not support ipsets!\\n"
  293. targetDNS='dnsmasq.servers'
  294. fi
  295. if ! ipset help hash:net >/dev/null 2>&1; then
  296. output "$_ERROR_: DNSMASQ ipset support is enabled in $packageName, but ipset is either not installed or installed ipset does not support 'hash:net' type!\\n"
  297. targetDNS='dnsmasq.servers'
  298. fi
  299. ;;
  300. esac
  301. [ ! -d "${outputFile%/*}" ] && mkdir -p "${outputFile%/*}"
  302. [ ! -d "${outputCache%/*}" ] && mkdir -p "${outputFile%/*}"
  303. [ ! -d "${outputGzip%/*}" ] && mkdir -p "${outputFile%/*}"
  304. cacheOps 'testGzip' && return 0
  305. network_flush_cache; network_find_wan wan_if; network_get_gateway wan_gw "$wan_if";
  306. [ -n "$wan_gw" ] && return 0
  307. output "$_ERROR_: $serviceName failed to discover WAN gateway.\\n"; return 1;
  308. }
  309. dnsmasqOps() {
  310. local cfg="$1" param="$2"
  311. case "$param" in
  312. dnsmasq.addnhosts)
  313. if [ "$(uci -q get dhcp."$cfg".serversfile)" = "$serversFile" ]; then
  314. uci -q del dhcp."$cfg".serversfile
  315. fi
  316. if ! uci -q get dhcp."$cfg".addnhosts | grep -q "$addnhostsFile"; then
  317. uci add_list dhcp."$cfg".addnhosts="$addnhostsFile"
  318. fi
  319. ;;
  320. dnsmasq.conf|dnsmasq.ipset|unbound.adb_list|cleanup)
  321. uci -q del_list dhcp."$cfg".addnhosts="$addnhostsFile"
  322. if [ "$(uci -q get dhcp."$cfg".serversfile)" = "$serversFile" ]; then
  323. uci -q del dhcp."$cfg".serversfile
  324. fi
  325. ;;
  326. dnsmasq.servers)
  327. uci -q del_list dhcp."$cfg".addnhosts="$addnhostsFile"
  328. if [ "$(uci -q get dhcp."$cfg".serversfile)" != "$serversFile" ]; then
  329. uci set dhcp."$cfg".serversfile="$serversFile"
  330. fi
  331. ;;
  332. esac
  333. }
  334. dnsOps() {
  335. local param output_text i
  336. case $1 in
  337. on_start)
  338. if [ ! -s "$outputFile" ]; then
  339. jsonOps set status "statusFail"
  340. jsonOps add error "errorOutputFileCreate"
  341. output "$_ERROR_: $(getErrorText 'errorOutputFileCreate')!\\n"
  342. return 1
  343. fi
  344. config_load 'dhcp'
  345. if [ "$dnsInstance" = "*" ]; then
  346. config_foreach dnsmasqOps 'dnsmasq' "$targetDNS"
  347. elif [ -n "$dnsInstance" ]; then
  348. for i in $dnsInstance; do
  349. dnsmasqOps "@dnsmasq[$i]" "$targetDNS"
  350. done
  351. fi
  352. case "$targetDNS" in
  353. dnsmasq.addnhosts|dnsmasq.servers)
  354. param=dnsmasq_hup
  355. output_text='Reloading DNSMASQ'
  356. ;;
  357. dnsmasq.conf|dnsmasq.ipset)
  358. param=dnsmasq_restart
  359. output_text='Restarting DNSMASQ'
  360. ;;
  361. unbound.adb_list)
  362. param=unbound_restart
  363. output_text='Restarting Unbound'
  364. ;;
  365. esac
  366. if [ -n "$(uci changes dhcp)" ]; then
  367. uci commit dhcp
  368. if [ "$param" = 'unbound_restart' ]; then
  369. param='dnsmasq_restart; unbound_restart;'
  370. output_text='Restarting Unbound/DNSMASQ'
  371. else
  372. param=dnsmasq_restart
  373. output_text='Restarting DNSMASQ'
  374. fi
  375. fi
  376. output 1 "$output_text "
  377. output 2 "$output_text "
  378. jsonOps set message "$output_text"
  379. if eval "$param"; then
  380. jsonOps set status "statusSuccess"
  381. led_on "$led"
  382. output_okn
  383. else
  384. output_fail
  385. jsonOps set status "statusFail"
  386. jsonOps add error "errorDNSReload"
  387. output "$_ERROR_: $(getErrorText 'errorDNSReload')!\\n"
  388. return 1
  389. fi
  390. ;;
  391. on_stop)
  392. case "$targetDNS" in
  393. dnsmasq.addnhosts | dnsmasq.servers)
  394. param=dnsmasq_hup
  395. ;;
  396. dnsmasq.conf | dnsmasq.ipset)
  397. param=dnsmasq_restart
  398. ;;
  399. unbound.adb_list)
  400. param=unbound_restart
  401. ;;
  402. esac
  403. if [ -n "$(uci changes dhcp)" ]; then
  404. uci -q commit dhcp
  405. if [ "$param" = 'unbound_restart' ]; then
  406. param='dnsmasq_restart; unbound_restart;'
  407. else
  408. param=dnsmasq_restart
  409. fi
  410. fi
  411. eval "$param"
  412. return $?
  413. ;;
  414. quiet)
  415. case "$targetDNS" in
  416. dnsmasq.addnhosts | dnsmasq.conf | dnsmasq.ipset | dnsmasq.servers)
  417. param=dnsmasq_restart
  418. ;;
  419. unbound.adb_list)
  420. param=unbound_restart
  421. ;;
  422. esac
  423. eval "$param"
  424. return $?
  425. ;;
  426. esac
  427. }
  428. jsonOps() {
  429. # shellcheck disable=SC2034
  430. local action="$1" param="$2" value="$3"
  431. local status message error stats
  432. local reload restart curReload curRestart ret i
  433. if [ -s "$jsonFile" ]; then
  434. json_load_file "$jsonFile" 2>/dev/null
  435. json_select 'data' 2>/dev/null
  436. for i in status message error stats reload restart; do
  437. json_get_var $i "$i" 2>/dev/null
  438. done
  439. fi
  440. case "$action" in
  441. get)
  442. case "$param" in
  443. triggers)
  444. curReload="$parallelDL $debug $dlTimeout $allowed_domains $blocked_domains $allowed_domains_urls $blocked_domains_urls $blocked_hosts_urls $targetDNS $configUpdateEnabled $configUpdateURL"
  445. curRestart="$compressedCache $forceDNS $led $forceDNSPorts"
  446. if [ ! -s "$jsonFile" ]; then
  447. ret='on_boot'
  448. elif [ "$curReload" != "$reload" ]; then
  449. ret='download'
  450. elif [ "$curRestart" != "$restart" ]; then
  451. ret='restart'
  452. fi
  453. printf "%b" "$ret"
  454. return;;
  455. *)
  456. printf "%b" "$(eval echo "\$$param")"; return;;
  457. esac
  458. ;;
  459. add)
  460. if [ -n "$(eval echo "\$$param")" ]; then
  461. value="$(eval echo "\$$param") ${value}"
  462. fi
  463. eval "$param"='${value}'
  464. ;;
  465. del)
  466. case "$param" in
  467. all)
  468. unset status message error stats;;
  469. triggers)
  470. unset reload restart;;
  471. *)
  472. unset "$param";;
  473. esac
  474. ;;
  475. set)
  476. case "$param" in
  477. triggers)
  478. reload="$parallelDL $debug $dlTimeout $allowed_domains $blocked_domains $allowed_domains_urls $blocked_domains_urls $blocked_hosts_urls $targetDNS $configUpdateEnabled $configUpdateURL"
  479. restart="$compressedCache $forceDNS $led $forceDNSPorts"
  480. ;;
  481. *)
  482. eval "$param"='$value';;
  483. esac
  484. ;;
  485. esac
  486. json_init
  487. json_add_object 'data'
  488. json_add_string version "$PKG_VERSION"
  489. json_add_string status "$status"
  490. json_add_string message "$message"
  491. json_add_string error "$error"
  492. json_add_string stats "$stats"
  493. json_add_string reload "$reload"
  494. json_add_string restart "$restart"
  495. json_close_object
  496. json_dump > "$jsonFile"
  497. sync
  498. }
  499. cacheOps() {
  500. local R_TMP
  501. case "$1" in
  502. create|backup)
  503. [ -s "$outputFile" ] && { mv -f "$outputFile" "$outputCache"; true > "$outputFile"; } >/dev/null 2>/dev/null
  504. return $?
  505. ;;
  506. restore|use)
  507. [ -s "$outputCache" ] && mv "$outputCache" "$outputFile" >/dev/null 2>/dev/null
  508. return $?
  509. ;;
  510. test)
  511. [ -s "$outputCache" ]
  512. return $?
  513. ;;
  514. testGzip)
  515. [ -s "$outputGzip" ] && gzip -t -c "$outputGzip"
  516. return $?
  517. ;;
  518. createGzip)
  519. R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
  520. if gzip < "$outputFile" > "$R_TMP"; then
  521. if mv "$R_TMP" "$outputGzip"; then
  522. rm -f "$R_TMP"
  523. return 0
  524. else
  525. rm -f "$R_TMP"
  526. return 1
  527. fi
  528. else
  529. return 1
  530. fi
  531. ;;
  532. expand|unpack|expandGzip|unpackGzip)
  533. [ -s "$outputGzip" ] && gzip -dc < "$outputGzip" > "$outputCache"
  534. return $?
  535. ;;
  536. esac
  537. }
  538. process_url() {
  539. local label type D_TMP R_TMP
  540. if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then return 1; fi
  541. label="${1##*//}"; label="${label%%/*}";
  542. if [ "$2" = 'hosts' ]; then
  543. label="Hosts: $label"; filter="$hostsFilter";
  544. else
  545. label="Domains: $label"; filter="$domainsFilter";
  546. fi
  547. if [ "$3" = 'blocked' ]; then
  548. type='Blocked'; D_TMP="$B_TMP";
  549. else
  550. type='Allowed'; D_TMP="$A_TMP";
  551. fi
  552. if [ "${1:0:5}" == "https" ] && [ -z "$isSSLSupported" ]; then
  553. output 1 "$_FAIL_"
  554. output 2 "[DL] $type $label $__FAIL__\\n"
  555. echo "errorNoSSLSupport|${1}" >> "$sharedMemoryError"
  556. return 0
  557. fi
  558. while [ -z "$R_TMP" ] || [ -e "$R_TMP" ]; do
  559. R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
  560. done
  561. if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev/null || [ ! -s "$R_TMP" ]; then
  562. output 1 "$_FAIL_"
  563. output 2 "[DL] $type $label $__FAIL__\\n"
  564. echo "errorDownloadingList|${1}" >> "$sharedMemoryError"
  565. else
  566. sed -i "$filter" "$R_TMP"
  567. if [ ! -s "$R_TMP" ]; then
  568. output 1 "$_FAIL_"
  569. output 2 "[DL] $type $label $__FAIL__\\n"
  570. echo "errorParsingList|${1}" >> "$sharedMemoryError"
  571. else
  572. cat "${R_TMP}" >> "$D_TMP"
  573. output 1 "$_OK_"
  574. output 2 "[DL] $type $label $__OK__\\n"
  575. fi
  576. fi
  577. rm -f "$R_TMP"
  578. return 0
  579. }
  580. process_config_update() {
  581. local label R_TMP
  582. [ "$configUpdateEnabled" -eq 0 ] && return 0
  583. label="${1##*//}"; label="${label%%/*}";
  584. while [ -z "$R_TMP" ] || [ -e "$R_TMP" ]; do
  585. R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
  586. done
  587. if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev/null || [ ! -s "$R_TMP" ]; then
  588. output 1 "$_FAIL_"
  589. output 2 "[DL] Config Update: $label $__FAIL__\\n"
  590. jsonOps add error "errorDownloadingConfigUpdate"
  591. else
  592. if [ -s "$R_TMP" ] && sed -f "$R_TMP" -i /etc/config/simple-adblock 2>/dev/null; then
  593. output 1 "$_OK_"
  594. output 2 "[DL] Config Update: $label $__OK__\\n"
  595. else
  596. output 1 "$_FAIL_"
  597. output 2 "[DL] Config Update: $label $__FAIL__\\n"
  598. jsonOps add error "errorParsingConfigUpdate"
  599. fi
  600. fi
  601. rm -f "$R_TMP"
  602. return 0
  603. }
  604. download_lists() {
  605. local hf w_filter j=0 R_TMP
  606. jsonOps set message "$(getStatusText "statusDownloading")..."
  607. jsonOps set status "statusDownloading"
  608. rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
  609. if [ "$($awk '/^MemFree/ {print int($2/1000)}' "/proc/meminfo")" -lt 32 ]; then
  610. output 3 'Low free memory, restarting resolver... '
  611. if dnsOps 'quiet'; then
  612. output_okn
  613. else
  614. output_fail
  615. fi
  616. fi
  617. touch $A_TMP; touch $B_TMP;
  618. output 1 'Downloading lists '
  619. process_config_update "$configUpdateURL"
  620. rm -f "$sharedMemoryError"
  621. if [ -n "$blocked_hosts_urls" ]; then
  622. for hf in ${blocked_hosts_urls}; do
  623. if [ "$parallelDL" -gt 0 ]; then
  624. process_url "$hf" 'hosts' 'blocked' &
  625. else
  626. process_url "$hf" 'hosts' 'blocked'
  627. fi
  628. done
  629. fi
  630. if [ -n "$blocked_domains_urls" ]; then
  631. for hf in ${blocked_domains_urls}; do
  632. if [ "$parallelDL" -gt 0 ]; then
  633. process_url "$hf" 'domains' 'blocked' &
  634. else
  635. process_url "$hf" 'domains' 'blocked'
  636. fi
  637. done
  638. fi
  639. if [ -n "$allowed_domains_urls" ]; then
  640. for hf in ${allowed_domains_urls}; do
  641. if [ "$parallelDL" -gt 0 ]; then
  642. process_url "$hf" 'domains' 'allowed' &
  643. else
  644. process_url "$hf" 'domains' 'allowed'
  645. fi
  646. done
  647. fi
  648. wait
  649. output 1 '\n'
  650. if [ -s "$sharedMemoryError" ]; then
  651. while IFS= read -r line; do
  652. jsonOps add error "$line"
  653. done < "$sharedMemoryError"
  654. rm -f "$sharedMemoryError"
  655. fi
  656. [ -n "$blocked_domains" ] && for hf in ${blocked_domains}; do echo "$hf" | sed "$domainsFilter" >> $B_TMP; done
  657. allowed_domains="${allowed_domains}
  658. $(cat $A_TMP)"
  659. [ -n "$allowed_domains" ] && for hf in ${allowed_domains}; do hf="$(echo "$hf" | sed 's/\./\\./g')"; w_filter="$w_filter/${hf}$/d;"; done
  660. [ ! -s "$B_TMP" ] && return 1
  661. output 1 'Processing downloads '
  662. output 2 'Sorting combined list '
  663. jsonOps set message "$(getStatusText "statusProcessing"): sorting combined list"
  664. if [ "$allowIDN" -gt 0 ]; then
  665. if sort -u "$B_TMP" > "$A_TMP"; then
  666. output_ok
  667. else
  668. output_failn
  669. jsonOps add error "errorSorting"
  670. fi
  671. else
  672. if sort -u "$B_TMP" | grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
  673. output_ok
  674. else
  675. output_failn
  676. jsonOps add error "errorSorting"
  677. fi
  678. fi
  679. if [ "$targetDNS" = 'dnsmasq.conf' ] || \
  680. [ "$targetDNS" = 'dnsmasq.ipset' ] || \
  681. [ "$targetDNS" = 'dnsmasq.servers' ] || \
  682. [ "$targetDNS" = 'unbound.adb_list' ]; then
  683. # TLD optimization written by Dirk Brenken (dev@brenken.org)
  684. output 2 'Optimizing combined list '
  685. jsonOps set message "$(getStatusText "statusProcessing"): optimizing combined list"
  686. # sed -E 'G;:t;s/(.*)(\.)(.*)(\n)(.*)/\1\4\5\2\3/;tt;s/(.*)\n(\.)(.*)/\3\2\1/' is actually slower than command below
  687. if $awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$A_TMP" > "$B_TMP"; then
  688. if sort "$B_TMP" > "$A_TMP"; then
  689. if $awk '{if(NR=1){tld=$NF};while(getline){if($NF!~tld"\\."){print tld;tld=$NF}}print tld}' "$A_TMP" > "$B_TMP"; then
  690. if $awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$B_TMP" > "$A_TMP"; then
  691. if sort -u "$A_TMP" > "$B_TMP"; then
  692. output_ok
  693. else
  694. output_failn
  695. jsonOps add error "errorOptimization"
  696. mv "$A_TMP" "$B_TMP"
  697. fi
  698. else
  699. output_failn
  700. jsonOps add error "errorOptimization"
  701. fi
  702. else
  703. output_failn
  704. jsonOps add error "errorOptimization"
  705. mv "$A_TMP" "$B_TMP"
  706. fi
  707. else
  708. output_failn
  709. jsonOps add error "errorOptimization"
  710. fi
  711. else
  712. output_failn
  713. jsonOps add error "errorOptimization"
  714. mv "$A_TMP" "$B_TMP"
  715. fi
  716. else
  717. mv "$A_TMP" "$B_TMP"
  718. fi
  719. output 2 'Allowing domains '
  720. jsonOps set message "$(getStatusText "statusProcessing"): allowing domains"
  721. if sed -i "$w_filter" "$B_TMP"; then
  722. output_ok
  723. else
  724. output_failn
  725. jsonOps add error "errorAllowListProcessing"
  726. fi
  727. output 2 'Formatting merged file '
  728. jsonOps set message "$(getStatusText "statusProcessing"): formatting merged file"
  729. if [ -z "$outputFilterIPv6" ]; then
  730. if sed "$outputFilter" "$B_TMP" > "$A_TMP"; then
  731. output_ok
  732. else
  733. output_failn
  734. jsonOps add error "errorDataFileFormatting"
  735. fi
  736. else
  737. case "$targetDNS" in
  738. dnsmasq.addnhosts)
  739. if sed "$outputFilter" "$B_TMP" > "$A_TMP" && \
  740. sed "$outputFilterIPv6" "$B_TMP" >> "$A_TMP"; then
  741. output_ok
  742. else
  743. output_failn
  744. jsonOps add error "errorDataFileFormatting"
  745. fi
  746. ;;
  747. esac
  748. fi
  749. case "$targetDNS" in
  750. dnsmasq.addnhosts)
  751. output 2 'Creating DNSMASQ addnhosts file '
  752. jsonOps set message "$(getStatusText "statusProcessing"): creating DNSMASQ addnhosts file"
  753. ;;
  754. dnsmasq.conf)
  755. output 2 'Creating DNSMASQ config file '
  756. jsonOps set message "$(getStatusText "statusProcessing"): creating DNSMASQ config file"
  757. ;;
  758. dnsmasq.ipset)
  759. output 2 'Creating DNSMASQ ipset file '
  760. jsonOps set message "$(getStatusText "statusProcessing"): creating DNSMASQ ipset file"
  761. ;;
  762. dnsmasq.servers)
  763. output 2 'Creating DNSMASQ servers file '
  764. jsonOps set message "$(getStatusText "statusProcessing"): creating DNSMASQ servers file"
  765. ;;
  766. unbound.adb_list)
  767. output 2 'Creating Unbound adb_list file '
  768. jsonOps set message "$(getStatusText "statusProcessing"): creating Unbound adb_list file"
  769. ;;
  770. esac
  771. if mv "$A_TMP" "$outputFile"; then
  772. output_ok
  773. else
  774. output_failn
  775. jsonOps add error "errorMovingDataFile"
  776. fi
  777. if [ "$compressedCache" -gt 0 ]; then
  778. output 2 'Creating compressed cache '
  779. jsonOps set message "$(getStatusText "statusProcessing"): creating compressed cache"
  780. if cacheOps 'createGzip'; then
  781. output_ok
  782. else
  783. output_failn
  784. jsonOps add error "errorCreatingCompressedCache"
  785. fi
  786. else
  787. rm -f "$outputGzip"
  788. fi
  789. output 2 'Removing temporary files '
  790. jsonOps set message "$(getStatusText "statusProcessing"): removing temporary files"
  791. rm -f "/tmp/${packageName}_tmp.*" "$A_TMP" "$B_TMP" "$outputCache" || j=1
  792. if [ $j -eq 0 ]; then
  793. output_ok
  794. else
  795. output_failn
  796. jsonOps add error "errorRemovingTempFiles"
  797. fi
  798. output 1 '\n'
  799. }
  800. boot() {
  801. load_package_config
  802. sleep "$bootDelay"
  803. rc_procd start_service 'on_boot' && rc_procd service_triggers
  804. }
  805. start_service() {
  806. is_enabled 'on_start' || return 1
  807. local action status error message stats c
  808. status="$(jsonOps get status)"
  809. error="$(jsonOps get error)"
  810. message="$(jsonOps get message)"
  811. stats="$(jsonOps get stats)"
  812. action="$(jsonOps get triggers)"
  813. if [ "$action" = 'on_boot' ] || [ "$1" = 'on_boot' ]; then
  814. if cacheOps 'testGzip' || cacheOps 'test'; then
  815. action='restore'
  816. else
  817. action='download'
  818. fi
  819. elif [ "$action" = 'download' ] || [ "$1" = 'download' ] || [ -n "$error" ]; then
  820. action='download'
  821. elif [ ! -s "$outputFile" ]; then
  822. if cacheOps 'testGzip' || cacheOps 'test'; then
  823. action='restore'
  824. else
  825. action='download'
  826. fi
  827. elif [ "$action" = 'restart' ] || [ "$1" = 'restart' ]; then
  828. action='restart'
  829. elif [ -s "$outputFile" ] && [ "$status" = "statusSuccess" ] && [ -z "$error" ]; then
  830. showstatus
  831. exit 0
  832. else
  833. action='download'
  834. fi
  835. jsonOps del all
  836. jsonOps set triggers
  837. procd_open_instance 'main'
  838. procd_set_param command /bin/true
  839. procd_set_param stdout 1
  840. procd_set_param stderr 1
  841. procd_open_data
  842. json_add_array firewall
  843. if [ "$forceDNS" -ne 0 ]; then
  844. for c in $forceDNSPorts; do
  845. if netstat -tuln | grep LISTEN | grep ":${c}" >/dev/null 2>&1; then
  846. json_add_object ""
  847. json_add_string type redirect
  848. json_add_string target DNAT
  849. json_add_string src lan
  850. json_add_string proto "tcp udp"
  851. json_add_string src_dport "$c"
  852. json_add_string dest_port "$c"
  853. json_add_boolean reflection 0
  854. json_close_object
  855. else
  856. json_add_object ""
  857. json_add_string type rule
  858. json_add_string src lan
  859. json_add_string dest "*"
  860. json_add_string proto "tcp udp"
  861. json_add_string dest_port "$c"
  862. json_add_string target REJECT
  863. json_close_object
  864. fi
  865. done
  866. fi
  867. if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
  868. json_add_object ""
  869. json_add_string type ipset
  870. json_add_string name adb
  871. json_add_string match dest_net
  872. json_add_string storage hash
  873. json_close_object
  874. json_add_object ""
  875. json_add_string type rule
  876. json_add_string ipset adb
  877. json_add_string src lan
  878. json_add_string dest "*"
  879. json_add_string proto "tcp udp"
  880. json_add_string target REJECT
  881. json_close_object
  882. fi
  883. json_close_array
  884. procd_close_data
  885. procd_close_instance
  886. if [ "$action" = 'restore' ]; then
  887. output 0 "Starting $serviceName... "
  888. output 3 "Starting $serviceName...\\n"
  889. jsonOps set status "statusStarting"
  890. if cacheOps 'testGzip' && ! cacheOps 'test' && [ ! -s "$outputFile" ]; then
  891. output 3 'Found compressed cache file, unpacking it '
  892. jsonOps set message 'found compressed cache file, unpacking it.'
  893. if cacheOps 'unpackGzip'; then
  894. output_okn
  895. else
  896. output_fail
  897. jsonOps add error "errorRestoreCompressedCache"
  898. output "$_ERROR_: $(getErrorText 'errorRestoreCompressedCache')!\\n"
  899. action='download'
  900. fi
  901. fi
  902. if cacheOps 'test' && [ ! -s "$outputFile" ]; then
  903. output 3 'Found cache file, reusing it '
  904. jsonOps set message 'found cache file, reusing it.'
  905. if cacheOps 'restore'; then
  906. output_okn
  907. dnsOps 'on_start'
  908. else
  909. output_fail
  910. jsonOps add error "errorRestoreCache"
  911. output "$_ERROR_: $(getErrorText 'errorRestoreCache')!\\n"
  912. action='download'
  913. fi
  914. fi
  915. fi
  916. if [ "$action" = 'download' ]; then
  917. if [ -s "$outputFile" ] || cacheOps 'test' || cacheOps 'testGzip'; then
  918. output 0 "Force-reloading $serviceName... "
  919. output 3 "Force-reloading $serviceName...\\n"
  920. jsonOps set status "statusForceReloading"
  921. else
  922. output 0 "Starting $serviceName... "
  923. output 3 "Starting $serviceName...\\n"
  924. jsonOps set status "statusStarting"
  925. fi
  926. download_lists
  927. dnsOps 'on_start'
  928. fi
  929. if [ "$action" = 'restart' ]; then
  930. output 0 "Restarting $serviceName... "
  931. output 3 "Restarting $serviceName...\\n"
  932. jsonOps set status "statusRestarting"
  933. dnsOps 'on_start'
  934. fi
  935. if [ "$action" = 'start' ]; then
  936. output 0 "Starting $serviceName... "
  937. output 3 "Starting $serviceName...\\n"
  938. jsonOps set status "statusStarting"
  939. dnsOps 'on_start'
  940. fi
  941. if [ -s "$outputFile" ] && [ "$(jsonOps get status)" != "statusFail" ]; then
  942. output 0 "$__OK__\\n";
  943. jsonOps del message
  944. jsonOps set status "statusSuccess"
  945. jsonOps set stats "$serviceName is blocking $(wc -l < "$outputFile") domains (with ${targetDNS})"
  946. showstatus
  947. else
  948. output 0 "$__FAIL__\\n";
  949. jsonOps set status "statusFail"
  950. jsonOps add error "errorOhSnap"
  951. showstatus
  952. fi
  953. }
  954. service_started() { procd_set_config_changed firewall; }
  955. service_stopped() { procd_set_config_changed firewall; }
  956. restart_service() { rc_procd start_service 'restart'; }
  957. reload_service() { rc_procd start_service 'restart'; }
  958. dl() { rc_procd start_service 'download'; }
  959. killcache() {
  960. rm -f "$addnhostsCache" "$addnhostsGzip"
  961. rm -f "$dnsmasqCache" "$dnsmasqGzip"
  962. rm -f "$ipsetCache" "$ipsetGzip"
  963. rm -f "$serversCache" "$serversGzip"
  964. rm -f "$unboundCache" "$unboundGzip"
  965. config_load 'dhcp'
  966. config_foreach dnsmasqOps 'dnsmasq' 'cleanup'
  967. uci -q commit 'dhcp'
  968. return 0
  969. }
  970. show() { showstatus; }
  971. status_service() { showstatus; }
  972. showstatus() {
  973. local c url status message error stats
  974. status="$(jsonOps get status)"
  975. message="$(jsonOps get message)"
  976. error="$(jsonOps get error)"
  977. stats="$(jsonOps get stats)"
  978. if [ "$status" = "statusSuccess" ]; then
  979. output "$stats "; output_okn;
  980. else
  981. [ -n "$status" ] && status="$(getStatusText "$status")"
  982. if [ -n "$status" ] && [ -n "$message" ]; then
  983. status="${status}: $message"
  984. fi
  985. [ -n "$status" ] && output "$serviceName $status\\n"
  986. fi
  987. if [ -n "$error" ]; then
  988. for c in $error; do
  989. url="${c##*|}"
  990. c="${c%|*}"
  991. case "$c" in
  992. errorDownloadingList|errorParsingList)
  993. output "$_ERROR_: $(getErrorText "$c") $url!\\n";;
  994. *)
  995. output "$_ERROR_: $(getErrorText "$c")!\\n";;
  996. esac
  997. let n=n+1
  998. done
  999. fi
  1000. }
  1001. stop_service() {
  1002. load_package_config
  1003. if [ -s "$outputFile" ]; then
  1004. output "Stopping $serviceName... "
  1005. cacheOps 'create'
  1006. if dnsOps 'on_stop'; then
  1007. led_off "$led"
  1008. output 0 "$__OK__\\n"; output_okn;
  1009. jsonOps set status "statusStopped"
  1010. jsonOps del message
  1011. else
  1012. output 0 "$__FAIL__\\n"; output_fail;
  1013. jsonOps set status "statusFail"
  1014. jsonOps add error "errorStopping"
  1015. output "$_ERROR_: $(getErrorText 'errorStopping')!\\n"
  1016. fi
  1017. fi
  1018. }
  1019. service_triggers() {
  1020. procd_open_trigger
  1021. procd_add_config_trigger 'config.change' "${packageName}" /etc/init.d/${packageName} reload
  1022. procd_add_reload_interface_trigger 'wan'
  1023. procd_add_interface_trigger 'interface.*.up' 'wan' /etc/init.d/${packageName} reload
  1024. procd_close_trigger
  1025. }
  1026. check() {
  1027. load_package_config
  1028. local c string="$1"
  1029. c="$(grep -c "$string" "$outputFile")"
  1030. if [ ! -s "$outputFile" ]; then
  1031. echo "No block-list ('$outputFile') found."
  1032. elif [ -z "$string" ]; then
  1033. echo "Usage: /etc/init.d/${packageName} check string"
  1034. elif [ "$c" -gt 0 ]; then
  1035. if [ "$c" -gt 1 ]; then
  1036. echo "Found $c matches for '$string' in '$outputFile':"
  1037. else
  1038. echo "Found 1 match for '$string' in '$outputFile':"
  1039. fi
  1040. case "$targetDNS" in
  1041. dnsmasq.addnhosts)
  1042. grep "$string" "$outputFile" | sed 's|^127.0.0.1 ||;s|^:: ||;';;
  1043. dnsmasq.conf)
  1044. grep "$string" "$outputFile" | sed 's|local=/||;s|/$||;';;
  1045. dnsmasq.ipset)
  1046. grep "$string" "$outputFile" | sed 's|ipset=/||;s|/adb$||;';;
  1047. dnsmasq.servers)
  1048. grep "$string" "$outputFile" | sed 's|server=/||;s|/$||;';;
  1049. unbound.adb_list)
  1050. grep "$string" "$outputFile" | sed 's|^local-zone: "||;s|" static$||;';;
  1051. esac
  1052. else
  1053. echo "The $string is not found in current block-list ('$outputFile')."
  1054. fi
  1055. }
  1056. sizes() {
  1057. local i
  1058. load_package_config
  1059. echo "# $(date)"
  1060. for i in $blocked_domains_urls; do
  1061. [ "${i//melmac}" != "$i" ] && continue
  1062. if $dl_command "$i" $dl_flag /tmp/sast 2>/dev/null && [ -s /tmp/sast ]; then
  1063. echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
  1064. if compare_values "$(du -sk /tmp/sast)" "500"; then
  1065. echo "# block-list too big for most routers"
  1066. elif compare_values "$(du -sk /tmp/sast)" "100"; then
  1067. echo "# block-list may be too big for some routers"
  1068. fi
  1069. rm -rf /tmp/sast
  1070. echo " list blocked_domains_url '$i'"
  1071. echo ""
  1072. else
  1073. echo "# site was down on last check"
  1074. echo "# list blocked_domains_url '$i'"
  1075. echo ""
  1076. fi
  1077. done
  1078. for i in $blocked_hosts_urls; do
  1079. if $dl_command "$i" $dl_flag /tmp/sast 2>/dev/null && [ -s /tmp/sast ]; then
  1080. echo "# File size: $(du -sh /tmp/sast | $awk '{print $1}')"
  1081. if compare_values "$(du -sk /tmp/sast)" "500"; then
  1082. echo "# block-list too big for most routers"
  1083. elif compare_values "$(du -sk /tmp/sast)" "100"; then
  1084. echo "# block-list may be too big for some routers"
  1085. fi
  1086. rm -rf /tmp/sast
  1087. echo " list blocked_hosts_url '$i'"
  1088. echo ""
  1089. else
  1090. echo "# site was down on last check"
  1091. echo "# list blocked_hosts_url '$i'"
  1092. echo ""
  1093. fi
  1094. done
  1095. }