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.

1217 lines
38 KiB

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