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.

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