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.

1160 lines
37 KiB

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