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.

1225 lines
38 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright 2017-2020 Stan Grishin (stangri@melmac.net)
  3. # shellcheck disable=SC2039,SC1091
  4. PKG_VERSION='dev-test'
  5. export START=94
  6. export USE_PROCD=1
  7. export LC_ALL=C
  8. export EXTRA_COMMANDS='check dl killcache sizes show'
  9. export EXTRA_HELP=' check Checks if specified domain is found in current blacklist
  10. dl Force-downloads all enabled block-list
  11. sizes Displays the file-sizes of enabled block-lists
  12. show Shows the service last-run status'
  13. readonly packageName='simple-adblock'
  14. readonly serviceName="$packageName $PKG_VERSION"
  15. readonly addnhostsFile="/var/run/${packageName}.addnhosts"
  16. readonly addnhostsCache="/var/run/${packageName}.addnhosts.cache"
  17. readonly addnhostsGzip="/etc/${packageName}.addnhosts.gz"
  18. readonly addnhostsOutputFilter='s|^|127.0.0.1 |;s|$||'
  19. readonly addnhostsOutputFilterIPv6='s|^|:: |;s|$||'
  20. readonly dnsmasqFile="/var/dnsmasq.d/${packageName}"
  21. readonly dnsmasqCache="/var/run/${packageName}.dnsmasq.cache"
  22. readonly dnsmasqGzip="/etc/${packageName}.dnsmasq.gz"
  23. readonly dnsmasqOutputFilter='s|^|local=/|;s|$|/|'
  24. readonly ipsetFile="/var/dnsmasq.d/${packageName}.ipset"
  25. readonly ipsetCache="/var/run/${packageName}.ipset.cache"
  26. readonly ipsetGzip="/etc/${packageName}.ipset.gz"
  27. readonly ipsetOutputFilter='s|^|ipset=/|;s|$|/adb|'
  28. readonly serversFile="/var/run/${packageName}.servers"
  29. readonly serversCache="/var/run/${packageName}.servers.cache"
  30. readonly serversGzip="/etc/${packageName}.servers.gz"
  31. readonly serversOutputFilter='s|^|server=/|;s|$|/|'
  32. readonly unboundFile="/var/lib/unbound/adb_list.${packageName}"
  33. readonly unboundCache="/var/run/${packageName}.unbound.cache"
  34. readonly unboundGzip="/etc/${packageName}.unbound.gz"
  35. readonly unboundOutputFilter='s|^|local-zone: "|;s|$|" static|'
  36. readonly A_TMP="/var/${packageName}.hosts.a.tmp"
  37. readonly B_TMP="/var/${packageName}.hosts.b.tmp"
  38. readonly PIDFile="/var/run/${packageName}.pid"
  39. readonly jsonFile="/var/run/${packageName}.json"
  40. readonly sharedMemoryError="/dev/shm/$packageName-error"
  41. readonly sharedMemoryOutput="/dev/shm/$packageName-output"
  42. 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;'
  43. readonly domainsFilter='/^#/d;s/[[:space:]]*#.*$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
  44. readonly checkmark='\xe2\x9c\x93'
  45. readonly xmark='\xe2\x9c\x97'
  46. readonly _OK_='\033[0;32m\xe2\x9c\x93\033[0m'
  47. readonly _FAIL_='\033[0;31m\xe2\x9c\x97\033[0m'
  48. readonly __OK__='\033[0;32m[\xe2\x9c\x93]\033[0m'
  49. readonly __FAIL__='\033[0;31m[\xe2\x9c\x97]\033[0m'
  50. readonly _ERROR_='\033[0;31mERROR\033[0m'
  51. readonly messageSuccess='Success'
  52. readonly messageFail='Fail'
  53. readonly messageDownloading='Downloading'
  54. readonly messageReloading='Reloading'
  55. readonly messageRestarting='Restarting'
  56. readonly messageStarting='Starting'
  57. readonly messageForceReloading='Force-Reloading'
  58. readonly messageProcessing='Processing'
  59. readonly messageStopped='Stopped'
  60. getStatusText() {
  61. local _ret
  62. case "$1" in
  63. statusNoInstall) _ret="$serviceName is not installed or not found";;
  64. statusStopped) _ret="Stopped";;
  65. statusStarting) _ret="Starting";;
  66. statusRestarting) _ret="Restarting";;
  67. statusForceReloading) _ret="Force Reloading";;
  68. statusDownloading) _ret="Downloading";;
  69. statusError) _ret="Error";;
  70. statusWarning) _ret="Warning";;
  71. statusFail) _ret="Fail";;
  72. statusSuccess) _ret="Success";;
  73. esac
  74. printf "%b" "$_ret"
  75. }
  76. getErrorText() {
  77. local _ret
  78. case "$1" in
  79. errorOutputFileCreate) _ret="failed to create $outputFile file";;
  80. errorFailDNSReload) _ret="failed to restart/reload DNS resolver";;
  81. errorSharedMemory) _ret="failed to access shared memory";;
  82. errorSorting) _ret="failed to sort data file";;
  83. errorOptimization) _ret="failed to optimize data file";;
  84. errorWhitelistProcessing) _ret="failed to process whitelist";;
  85. errorDataFileFormatting) _ret="failed to format data file";;
  86. errorMovingDataFile) _ret="failed to move data file '${A_TMP}' to '${outputFile}'";;
  87. errorCreatingCompressedCache) _ret="failed to create compressed cache";;
  88. errorRemovingTempFiles) _ret="failed to remove temporary files";;
  89. errorRestoreCompressedCache) _ret="failed to unpack compressed cache";;
  90. errorRestoreCache) _ret="failed to move '$outputCache' to '$outputFile'";;
  91. errorOhSnap) _ret="failed to create blocklist or restart DNS resolver";;
  92. errorStopping) _ret="failed to stop $serviceName";;
  93. errorDNSReload) _ret="failed to reload/restart DNS resolver";;
  94. errorDownloadingList) _ret="failed to download";;
  95. errorParsingList) _ret="failed to parse";;
  96. esac
  97. printf "%b" "$_ret"
  98. }
  99. create_lock() { [ -e "$PIDFile" ] && return 1; touch "$PIDFile"; }
  100. remove_lock() { [ -e "$PIDFile" ] && rm -f "$PIDFile"; }
  101. trap remove_lock EXIT
  102. output_ok() { output 1 "$_OK_"; output 2 "$__OK__\\n"; }
  103. output_okn() { output 1 "$_OK_\\n"; output 2 "$__OK__\\n"; }
  104. output_fail() { output 1 "$_FAIL_"; output 2 "$__FAIL__\\n"; }
  105. output_failn() { output 1 "$_FAIL_\\n"; output 2 "$__FAIL__\\n"; }
  106. # str_replace() { printf "%b" "$1" | sed -e "s/$(printf "%b" "$2")/$(printf "%b" "$3")/g"; }
  107. # str_contains() { test "$1" != "$(str_replace "$1" "$2" '')"; }
  108. compare_versions() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"; }
  109. is_chaos_calmer() { ubus -S call system board | grep -q 'Chaos Calmer'; }
  110. is_ipset_procd() { compare_versions "$(sed -ne 's/^Version: //p' /usr/lib/opkg/info/firewall.control)" "2019-09-18"; }
  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. output() {
  118. # Can take a single parameter (text) to be output at any verbosity
  119. # Or target verbosity level and text to be output at specifc verbosity
  120. local msg memmsg logmsg
  121. if [ $# -ne 1 ]; then
  122. if [ $((verbosity & $1)) -gt 0 ] || [ "$verbosity" = "$1" ]; then shift; else return 0; fi
  123. fi
  124. [ -t 1 ] && printf "%b" "$1"
  125. msg="${1//$serviceName /service }";
  126. if [ "$(printf "%b" "$msg" | wc -l)" -gt 0 ]; then
  127. [ -s "$sharedMemoryOutput" ] && memmsg="$(cat "$sharedMemoryOutput")"
  128. logmsg="$(printf "%b" "${memmsg}${msg}" | sed 's/\x1b\[[0-9;]*m//g')"
  129. logger -t "${packageName:-service} [$$]" "$(printf "%b" "$logmsg")"
  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 [ ! -s "$jsonFile" ]; then
  431. ret='on_boot'
  432. elif [ "$curReload" != "$readReload" ]; then
  433. ret='download'
  434. elif [ "$curRestart" != "$readRestart" ]; then
  435. ret='restart'
  436. fi
  437. printf "%b" "$ret"
  438. return;;
  439. esac
  440. ;;
  441. add)
  442. case "$instance" in
  443. status)
  444. [ -n "$status" ] && status="$status $value" || status="$value";;
  445. message)
  446. [ -n "$message" ] && message="$message $value" || message="$value";;
  447. error)
  448. [ -n "$error" ] && error="$error $value" || error="$value";;
  449. stats)
  450. [ -n "$stats" ] && stats="$stats $value" || stats="$value";;
  451. esac
  452. ;;
  453. del)
  454. case "$instance" in
  455. all)
  456. unset status;
  457. unset message;
  458. unset error;
  459. unset stats;
  460. ;;
  461. status)
  462. unset status;;
  463. message)
  464. unset message;;
  465. error)
  466. unset error;;
  467. stats)
  468. unset stats;;
  469. triggers)
  470. unset readReload; unset readRestart;;
  471. esac
  472. ;;
  473. set)
  474. case "$instance" in
  475. status)
  476. status="$value";;
  477. message)
  478. message="$value";;
  479. error)
  480. error="$value";;
  481. stats)
  482. stats="$value";;
  483. triggers)
  484. readReload="$parallelDL $debug $dlTimeout $whitelist_domains $blacklist_domains $whitelist_domains_urls $blacklist_domains_urls $blacklist_hosts_urls $targetDNS"
  485. readRestart="$compressedCache $forceDNS $led"
  486. ;;
  487. esac
  488. ;;
  489. esac
  490. json_init
  491. json_add_object 'data'
  492. json_add_string version "$PKG_VERSION"
  493. json_add_string status "$status"
  494. json_add_string message "$message"
  495. json_add_string error "$error"
  496. json_add_string stats "$stats"
  497. json_add_string reload "$readReload"
  498. json_add_string restart "$readRestart"
  499. json_close_object
  500. json_dump > "$jsonFile"
  501. sync
  502. }
  503. cacheOps() {
  504. local R_TMP
  505. case "$1" in
  506. create|backup)
  507. [ -s "$outputFile" ] && { mv -f "$outputFile" "$outputCache"; true > "$outputFile"; } >/dev/null 2>/dev/null
  508. return $?
  509. ;;
  510. restore|use)
  511. [ -s "$outputCache" ] && mv "$outputCache" "$outputFile" >/dev/null 2>/dev/null
  512. return $?
  513. ;;
  514. test)
  515. [ -s "$outputCache" ]
  516. return $?
  517. ;;
  518. testGzip)
  519. [ -s "$outputGzip" ] && gzip -t -c "$outputGzip"
  520. return $?
  521. ;;
  522. createGzip)
  523. R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
  524. if gzip < "$outputFile" > "$R_TMP"; then
  525. if mv "$R_TMP" "$outputGzip"; then
  526. rm -f "$R_TMP"
  527. return 0
  528. else
  529. rm -f "$R_TMP"
  530. return 1
  531. fi
  532. else
  533. return 1
  534. fi
  535. ;;
  536. expand|unpack|expandGzip|unpackGzip)
  537. [ -s "$outputGzip" ] && gzip -dc < "$outputGzip" > "$outputCache"
  538. return $?
  539. ;;
  540. esac
  541. }
  542. fw3Ops() {
  543. local action="$1" param="$2" _restart
  544. case "$action" in
  545. reload) /etc/init.d/firewall reload >/dev/null 2>&1;;
  546. restart) /etc/init.d/firewall restart >/dev/null 2>&1;;
  547. remove)
  548. case "$param" in
  549. dns_redirect) uci -q del firewall.simple_adblock_dns_redirect;;
  550. ipset) uci -q del firewall.simple_adblock_ipset
  551. uci -q del firewall.simple_adblock_ipset_rule;;
  552. *)
  553. uci -q del firewall.simple_adblock_dns_redirect
  554. uci -q del firewall.simple_adblock_ipset
  555. uci -q del firewall.simple_adblock_ipset_rule
  556. ;;
  557. esac
  558. ;;
  559. insert)
  560. case "$param" in
  561. dns_redirect)
  562. if ! uci -q get firewall.simple_adblock_dns_redirect >/dev/null; then
  563. uci -q set firewall.simple_adblock_dns_redirect=redirect
  564. uci -q set firewall.simple_adblock_dns_redirect.name=simple_adblock_dns_hijack
  565. uci -q set firewall.simple_adblock_dns_redirect.target=DNAT
  566. uci -q set firewall.simple_adblock_dns_redirect.src=lan
  567. uci -q set firewall.simple_adblock_dns_redirect.proto=tcpudp
  568. uci -q set firewall.simple_adblock_dns_redirect.src_dport=53
  569. uci -q set firewall.simple_adblock_dns_redirect.dest_port=53
  570. fi
  571. ;;
  572. ipset)
  573. if ! uci -q get firewall.simple_adblock_ipset >/dev/null; then
  574. uci -q set firewall.simple_adblock_ipset=ipset
  575. uci -q set firewall.simple_adblock_ipset.name=adb
  576. uci -q set firewall.simple_adblock_ipset.match=dest_net
  577. uci -q set firewall.simple_adblock_ipset.storage=hash
  578. uci -q set firewall.simple_adblock_ipset.enabled=1
  579. _restart=1
  580. fi
  581. if ! uci -q get firewall.simple_adblock_ipset_rule >/dev/null; then
  582. uci -q set firewall.simple_adblock_ipset_rule=rule
  583. uci -q set firewall.simple_adblock_ipset_rule.name=simple_adblock_ipset_rule
  584. uci -q set firewall.simple_adblock_ipset_rule.ipset=adb
  585. uci -q set firewall.simple_adblock_ipset_rule.src=lan
  586. uci -q set firewall.simple_adblock_ipset_rule.dest='*'
  587. uci -q set firewall.simple_adblock_ipset_rule.proto=tcpudp
  588. uci -q set firewall.simple_adblock_ipset_rule.target=REJECT
  589. uci -q set firewall.simple_adblock_ipset_rule.enabled=1
  590. fi
  591. ;;
  592. *)
  593. if ! uci -q get firewall.simple_adblock_dns_redirect >/dev/null; then
  594. uci -q set firewall.simple_adblock_dns_redirect=redirect
  595. uci -q set firewall.simple_adblock_dns_redirect.name=simple_adblock_dns_hijack
  596. uci -q set firewall.simple_adblock_dns_redirect.target=DNAT
  597. uci -q set firewall.simple_adblock_dns_redirect.src=lan
  598. uci -q set firewall.simple_adblock_dns_redirect.proto=tcpudp
  599. uci -q set firewall.simple_adblock_dns_redirect.src_dport=53
  600. uci -q set firewall.simple_adblock_dns_redirect.dest_port=53
  601. fi
  602. if ! uci -q get firewall.simple_adblock_ipset >/dev/null; then
  603. uci -q set firewall.simple_adblock_ipset=ipset
  604. uci -q set firewall.simple_adblock_ipset.name=adb
  605. uci -q set firewall.simple_adblock_ipset.match=dest_net
  606. uci -q set firewall.simple_adblock_ipset.storage=hash
  607. uci -q set firewall.simple_adblock_ipset.enabled=1
  608. _restart=1
  609. fi
  610. if ! uci -q get firewall.simple_adblock_ipset_rule >/dev/null; then
  611. uci -q set firewall.simple_adblock_ipset_rule=rule
  612. uci -q set firewall.simple_adblock_ipset_rule.name=simple_adblock_ipset_rule
  613. uci -q set firewall.simple_adblock_ipset_rule.ipset=adb
  614. uci -q set firewall.simple_adblock_ipset_rule.src=lan
  615. uci -q set firewall.simple_adblock_ipset_rule.dest='*'
  616. uci -q set firewall.simple_adblock_ipset_rule.proto=tcpudp
  617. uci -q set firewall.simple_adblock_ipset_rule.target=REJECT
  618. uci -q set firewall.simple_adblock_ipset_rule.enabled=1
  619. fi
  620. ;;
  621. esac
  622. esac
  623. if [ -n "$(uci changes firewall)" ]; then
  624. uci -q commit firewall
  625. if [ -z "$_restart" ]; then
  626. fw3Ops 'reload'
  627. else
  628. fw3Ops 'restart'
  629. fi
  630. fi
  631. }
  632. process_url() {
  633. local label type D_TMP R_TMP
  634. if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then return 1; fi
  635. label="${1##*//}"; label="${label%%/*}";
  636. if [ "$2" = 'hosts' ]; then
  637. label="Hosts: $label"; filter="$hostsFilter";
  638. else
  639. label="Domains: $label"; filter="$domainsFilter";
  640. fi
  641. if [ "$3" = 'blocked' ]; then
  642. type='Blocked'; D_TMP="$B_TMP";
  643. else
  644. type='Allowed'; D_TMP="$A_TMP";
  645. fi
  646. while [ -z "$R_TMP" ] || [ -e "$R_TMP" ]; do
  647. R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
  648. done
  649. if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev/null || [ ! -s "$R_TMP" ]; then
  650. output 1 "$_FAIL_"
  651. output 2 "[DL] $type $label $__FAIL__\\n"
  652. echo "errorDownloadingList=${1}" >> "$sharedMemoryError"
  653. else
  654. sed -i "$filter" "$R_TMP"
  655. if [ ! -s "$R_TMP" ]; then
  656. output 1 "$_FAIL_"
  657. output 2 "[DL] $type $label $__FAIL__\\n"
  658. echo "errorParsingList=${1}" >> "$sharedMemoryError"
  659. else
  660. cat "${R_TMP}" >> "$D_TMP"
  661. output 1 "$_OK_"
  662. output 2 "[DL] $type $label $__OK__\\n"
  663. fi
  664. fi
  665. rm -f "$R_TMP"
  666. return 0
  667. }
  668. download_lists() {
  669. local hf w_filter j=0 R_TMP
  670. tmpfs set message "${messageDownloading}..."
  671. tmpfs set status "statusDownloading"
  672. rm -f "$A_TMP" "$B_TMP" "$outputFile" "$outputCache" "$outputGzip"
  673. if [ "$(awk '/^MemFree/ {print int($2/1000)}' "/proc/meminfo")" -lt 32 ]; then
  674. output 3 'Low free memory, restarting resolver... '
  675. if dnsOps 'quiet'; then
  676. output_okn
  677. else
  678. output_fail
  679. fi
  680. fi
  681. touch $A_TMP; touch $B_TMP;
  682. output 1 'Downloading lists '
  683. rm -f "$sharedMemoryError"
  684. if [ -n "$blacklist_hosts_urls" ]; then
  685. for hf in ${blacklist_hosts_urls}; do
  686. if [ "$parallelDL" -gt 0 ]; then
  687. process_url "$hf" 'hosts' 'blocked' &
  688. else
  689. process_url "$hf" 'hosts' 'blocked'
  690. fi
  691. done
  692. fi
  693. if [ -n "$blacklist_domains_urls" ]; then
  694. for hf in ${blacklist_domains_urls}; do
  695. if [ "$parallelDL" -gt 0 ]; then
  696. process_url "$hf" 'domains' 'blocked' &
  697. else
  698. process_url "$hf" 'domains' 'blocked'
  699. fi
  700. done
  701. fi
  702. if [ -n "$whitelist_domains_urls" ]; then
  703. for hf in ${whitelist_domains_urls}; do
  704. if [ "$parallelDL" -gt 0 ]; then
  705. process_url "$hf" 'domains' 'allowed' &
  706. else
  707. process_url "$hf" 'domains' 'allowed'
  708. fi
  709. done
  710. fi
  711. wait
  712. output 1 '\n'
  713. if [ -s "$sharedMemoryError" ]; then
  714. while IFS= read -r line; do
  715. tmpfs add error "$line"
  716. done < "$sharedMemoryError"
  717. rm -f "$sharedMemoryError"
  718. fi
  719. [ -n "$blacklist_domains" ] && for hf in ${blacklist_domains}; do echo "$hf" | sed "$domainsFilter" >> $B_TMP; done
  720. whitelist_domains="${whitelist_domains}
  721. $(cat $A_TMP)"
  722. [ -n "$whitelist_domains" ] && for hf in ${whitelist_domains}; do hf="$(echo "$hf" | sed 's/\./\\./g')"; w_filter="$w_filter/^${hf}$/d;/\\.${hf}$/d;"; done
  723. [ ! -s "$B_TMP" ] && return 1
  724. output 1 'Processing downloads '
  725. output 2 'Sorting combined list '
  726. tmpfs set message "$messageProcessing: sorting combined list"
  727. if [ "$allowIDN" -gt 0 ]; then
  728. if sort -u "$B_TMP" > "$A_TMP"; then
  729. output_ok
  730. else
  731. output_failn
  732. tmpfs add error "errorSorting"
  733. fi
  734. else
  735. if sort -u "$B_TMP" | grep -E -v '[^a-zA-Z0-9=/.-]' > "$A_TMP"; then
  736. output_ok
  737. else
  738. output_failn
  739. tmpfs add error "errorSorting"
  740. fi
  741. fi
  742. if [ "$targetDNS" = 'dnsmasq.conf' ] || \
  743. [ "$targetDNS" = 'dnsmasq.ipset' ] || \
  744. [ "$targetDNS" = 'dnsmasq.servers' ] || \
  745. [ "$targetDNS" = 'unbound.adb_list' ]; then
  746. # TLD optimization written by Dirk Brenken (dev@brenken.org)
  747. output 2 'Optimizing combined list '
  748. tmpfs set message "$messageProcessing: optimizing combined list"
  749. # sed -E 'G;:t;s/(.*)(\.)(.*)(\n)(.*)/\1\4\5\2\3/;tt;s/(.*)\n(\.)(.*)/\3\2\1/' is actually slower than awk
  750. if awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$A_TMP" > "$B_TMP"; then
  751. if sort "$B_TMP" > "$A_TMP"; then
  752. if awk '{if(NR=1){tld=$NF};while(getline){if($NF!~tld"\\."){print tld;tld=$NF}}print tld}' "$A_TMP" > "$B_TMP"; then
  753. if awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$B_TMP" > "$A_TMP"; then
  754. if sort -u "$A_TMP" > "$B_TMP"; then
  755. output_ok
  756. else
  757. output_failn
  758. tmpfs add error "errorOptimization"
  759. mv "$A_TMP" "$B_TMP"
  760. fi
  761. else
  762. output_failn
  763. tmpfs add error "errorOptimization"
  764. fi
  765. else
  766. output_failn
  767. tmpfs add error "errorOptimization"
  768. mv "$A_TMP" "$B_TMP"
  769. fi
  770. else
  771. output_failn
  772. tmpfs add error "errorOptimization"
  773. fi
  774. else
  775. output_failn
  776. tmpfs add error "errorOptimization"
  777. mv "$A_TMP" "$B_TMP"
  778. fi
  779. else
  780. mv "$A_TMP" "$B_TMP"
  781. fi
  782. output 2 'Whitelisting domains '
  783. tmpfs set message "$messageProcessing: whitelisting domains"
  784. if sed -i "$w_filter" "$B_TMP"; then
  785. output_ok
  786. else
  787. output_failn
  788. tmpfs add error "errorWhitelistProcessing"
  789. fi
  790. output 2 'Formatting merged file '
  791. tmpfs set message "$messageProcessing: formatting merged file"
  792. if [ -z "$outputFilterIPv6" ]; then
  793. if sed "$outputFilter" "$B_TMP" > "$A_TMP"; then
  794. output_ok
  795. else
  796. output_failn
  797. tmpfs add error "errorDataFileFormatting"
  798. fi
  799. else
  800. case "$targetDNS" in
  801. dnsmasq.addnhosts)
  802. if sed "$outputFilter" "$B_TMP" > "$A_TMP" && \
  803. sed "$outputFilterIPv6" "$B_TMP" >> "$A_TMP"; then
  804. output_ok
  805. else
  806. output_failn
  807. tmpfs add error "errorDataFileFormatting"
  808. fi
  809. ;;
  810. esac
  811. fi
  812. case "$targetDNS" in
  813. dnsmasq.addnhosts)
  814. output 2 'Creating DNSMASQ addnhosts file '
  815. tmpfs set message "$messageProcessing: creating DNSMASQ addnhosts file"
  816. ;;
  817. dnsmasq.conf)
  818. output 2 'Creating DNSMASQ config file '
  819. tmpfs set message "$messageProcessing: creating DNSMASQ config file"
  820. ;;
  821. dnsmasq.ipset)
  822. output 2 'Creating DNSMASQ ipset file '
  823. tmpfs set message "$messageProcessing: creating DNSMASQ ipset file"
  824. ;;
  825. dnsmasq.servers)
  826. output 2 'Creating DNSMASQ servers file '
  827. tmpfs set message "$messageProcessing: creating DNSMASQ servers file"
  828. ;;
  829. unbound.adb_list)
  830. output 2 'Creating Unbound adb_list file '
  831. tmpfs set message "$messageProcessing: creating Unbound adb_list file"
  832. ;;
  833. esac
  834. if mv "$A_TMP" "$outputFile"; then
  835. output_ok
  836. else
  837. output_failn
  838. tmpfs add error "errorMovingDataFile"
  839. fi
  840. if [ "$compressedCache" -gt 0 ]; then
  841. output 2 'Creating compressed cache '
  842. tmpfs set message "$messageProcessing: creating compressed cache"
  843. if cacheOps 'createGzip'; then
  844. output_ok
  845. else
  846. output_failn
  847. tmpfs add error "errorCreatingCompressedCache"
  848. fi
  849. else
  850. rm -f "$outputGzip"
  851. fi
  852. output 2 'Removing temporary files '
  853. tmpfs set message "$messageProcessing: removing temporary files"
  854. rm -f "/tmp/${packageName}_tmp.*" "$A_TMP" "$B_TMP" "$outputCache" || j=1
  855. if [ $j -eq 0 ]; then
  856. output_ok
  857. else
  858. output_failn
  859. tmpfs add error "errorRemovingTempFiles"
  860. fi
  861. output 1 '\n'
  862. }
  863. boot() {
  864. load_package_config
  865. if create_lock; then
  866. sleep "$bootDelay"
  867. remove_lock
  868. rc_procd start_service 'on_boot' && rc_procd service_triggers
  869. fi
  870. }
  871. start_service() {
  872. is_enabled 'on_start' || return 1
  873. local action status error message stats c
  874. if ! create_lock; then
  875. output 3 "$serviceName: another instance is starting up "; output_fail
  876. return 0
  877. fi
  878. status="$(tmpfs get status)"
  879. error="$(tmpfs get error)"
  880. message="$(tmpfs get message)"
  881. stats="$(tmpfs get stats)"
  882. action="$(tmpfs get triggers)"
  883. if [ "$action" = 'on_boot' ] || [ "$1" = 'on_boot' ]; then
  884. if cacheOps 'testGzip' || cacheOps 'test'; then
  885. action='restore'
  886. fi
  887. elif [ "$action" = 'download' ] || [ "$1" = 'download' ] || [ -n "$error" ]; then
  888. action='download'
  889. elif [ ! -s "$outputFile" ]; then
  890. if cacheOps 'testGzip' || cacheOps 'test'; then
  891. action='restore'
  892. else
  893. action='download'
  894. fi
  895. elif [ "$action" = 'restart' ] || [ "$1" = 'restart' ]; then
  896. action='restart'
  897. elif [ -s "$outputFile" ] && [ "$status" = "statusSuccess" ] && [ -z "$error" ]; then
  898. [ "$1" != 'hotplug' ] && showstatus
  899. exit 0
  900. else
  901. action='download'
  902. fi
  903. tmpfs del all
  904. tmpfs set triggers
  905. if is_chaos_calmer || ! is_ipset_procd; then
  906. if [ "$forceDNS" -ne 0 ]; then
  907. fw3Ops 'insert' 'dns_redirect'
  908. else
  909. fw3Ops 'remove' 'dns_redirect'
  910. fi
  911. if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
  912. fw3Ops 'insert' 'ipset'
  913. else
  914. fw3Ops 'remove' 'ipset'
  915. fi
  916. procd_open_instance 'main'
  917. procd_set_param command /bin/true
  918. procd_set_param stdout 1
  919. procd_set_param stderr 1
  920. procd_close_instance
  921. else
  922. procd_open_instance 'main'
  923. procd_set_param command /bin/true
  924. procd_set_param stdout 1
  925. procd_set_param stderr 1
  926. procd_open_data
  927. json_add_array firewall
  928. if [ "$forceDNS" -ne 0 ]; then
  929. json_add_object ''
  930. json_add_string type redirect
  931. json_add_string name simple_adblock_dns_redirect
  932. json_add_string target DNAT
  933. json_add_string src lan
  934. json_add_string proto tcpudp
  935. json_add_string src_dport 53
  936. json_add_string dest_port 53
  937. json_add_string reflection 0
  938. json_close_object
  939. fi
  940. if [ "$targetDNS" = 'dnsmasq.ipset' ]; then
  941. json_add_object ''
  942. json_add_string type ipset
  943. json_add_string name adb
  944. json_add_string match dest_net
  945. json_add_string storage hash
  946. json_add_string enabled 1
  947. json_close_object
  948. json_add_object ''
  949. json_add_string type rule
  950. json_add_string name simple_adblock_ipset_rule
  951. json_add_string ipset adb
  952. json_add_string src lan
  953. json_add_string dest '*'
  954. json_add_string proto tcpudp
  955. json_add_string target REJECT
  956. json_add_string enabled 1
  957. json_close_object
  958. fi
  959. json_close_array
  960. procd_close_data
  961. procd_close_instance
  962. fi
  963. if [ "$action" = 'restore' ]; then
  964. output 0 "Starting $serviceName... "
  965. output 3 "Starting $serviceName...\\n"
  966. tmpfs set status "statusStarting"
  967. if cacheOps 'testGzip' && ! cacheOps 'test' && [ ! -s "$outputFile" ]; then
  968. output 3 'Found compressed cache file, unpacking it '
  969. tmpfs set message 'found compressed cache file, unpacking it.'
  970. if cacheOps 'unpackGzip'; then
  971. output_okn
  972. else
  973. output_fail
  974. tmpfs add error "errorRestoreCompressedCache"
  975. output "$_ERROR_: $(getErrorText 'errorRestoreCompressedCache')!\\n"
  976. action='download'
  977. fi
  978. fi
  979. if cacheOps 'test' && [ ! -s "$outputFile" ]; then
  980. output 3 'Found cache file, reusing it '
  981. tmpfs set message 'found cache file, reusing it.'
  982. if cacheOps 'restore'; then
  983. output_okn
  984. dnsOps 'on_start'
  985. else
  986. output_fail
  987. tmpfs add error "errorRestoreCache"
  988. output "$_ERROR_: $(getErrorText 'errorRestoreCache')!\\n"
  989. action='download'
  990. fi
  991. fi
  992. fi
  993. case "$action" in
  994. download)
  995. if [ -s "$outputFile" ] || cacheOps 'test' || cacheOps 'testGzip'; then
  996. output 0 "Force-reloading $serviceName... "
  997. output 3 "Force-reloading $serviceName...\\n"
  998. tmpfs set status "statusForceReloading"
  999. else
  1000. output 0 "Starting $serviceName... "
  1001. output 3 "Starting $serviceName...\\n"
  1002. tmpfs set status "statusStarting"
  1003. fi
  1004. download_lists
  1005. dnsOps 'on_start'
  1006. ;;
  1007. restart)
  1008. output 0 "Restarting $serviceName... "
  1009. output 3 "Restarting $serviceName...\\n"
  1010. tmpfs set status "statusRestarting"
  1011. dnsOps 'on_start'
  1012. ;;
  1013. start)
  1014. output 0 "Starting $serviceName... "
  1015. output 3 "Starting $serviceName...\\n"
  1016. tmpfs set status "statusStarting"
  1017. dnsOps 'on_start'
  1018. ;;
  1019. esac
  1020. if [ -s "$outputFile" ] && [ "$(tmpfs get status)" != "statusFail" ]; then
  1021. output 0 "$__OK__\\n";
  1022. tmpfs del message
  1023. tmpfs set status "statusSuccess"
  1024. c="$(wc -l < "$outputFile")"
  1025. tmpfs set stats "$serviceName is blocking $c domains (with ${targetDNS})"
  1026. showstatus
  1027. else
  1028. output 0 "$__FAIL__\\n";
  1029. tmpfs set status "statusFail"
  1030. tmpfs add error "errorOhSnap"
  1031. showstatus
  1032. fi
  1033. remove_lock
  1034. }
  1035. service_started() { is_ipset_procd && procd_set_config_changed firewall; }
  1036. service_stopped() { is_ipset_procd && procd_set_config_changed firewall; }
  1037. restart_service() { rc_procd start_service 'restart'; }
  1038. reload_service() { restart_service; }
  1039. restart() { restart_service; }
  1040. reload() { restart_service; }
  1041. dl() { rc_procd start_service 'download'; }
  1042. killcache() {
  1043. rm -f "$addnhostsCache" "$addnhostsGzip"
  1044. rm -f "$dnsmasqCache" "$dnsmasqGzip"
  1045. rm -f "$ipsetCache" "$ipsetGzip"
  1046. rm -f "$serversCache" "$serversGzip"
  1047. rm -f "$unboundCache" "$unboundGzip"
  1048. config_load 'dhcp'
  1049. config_foreach dnsmasqOps 'dnsmasq' 'cleanup'
  1050. uci -q commit 'dhcp'
  1051. return 0
  1052. }
  1053. show() { showstatus; }
  1054. status_service() { showstatus; }
  1055. showstatus() {
  1056. local status="$(tmpfs get status)"
  1057. local message="$(tmpfs get message)"
  1058. local error="$(tmpfs get error)"
  1059. local stats="$(tmpfs get stats)"
  1060. local c url
  1061. if [ "$status" = "statusSuccess" ]; then
  1062. output "$stats "; output_okn;
  1063. else
  1064. [ -n "$status" ] && status="$(getStatusText "$status")"
  1065. if [ -n "$status" ] && [ -n "$message" ]; then
  1066. status="${status}: $message"
  1067. fi
  1068. [ -n "$status" ] && output "$serviceName $status\\n"
  1069. fi
  1070. if [ -n "$error" ]; then
  1071. for c in $error; do
  1072. url="${c##*=}"
  1073. c="${c%=*}"
  1074. case "$c" in
  1075. errorDownloadingList|errorParsingList)
  1076. output "$_ERROR_: $(getErrorText "$c") $url!\\n";;
  1077. *)
  1078. output "$_ERROR_: $(getErrorText "$c")!\\n";;
  1079. esac
  1080. let n=n+1
  1081. done
  1082. fi
  1083. }
  1084. stop_service() {
  1085. load_package_config
  1086. fw3Ops 'remove' 'all'
  1087. if [ -s "$outputFile" ]; then
  1088. output "Stopping $serviceName... "
  1089. cacheOps 'create'
  1090. if dnsOps 'on_stop'; then
  1091. led_off "$led"
  1092. output 0 "$__OK__\\n"; output_okn;
  1093. tmpfs set status "statusStopped"
  1094. tmpfs del message
  1095. else
  1096. output 0 "$__FAIL__\\n"; output_fail;
  1097. tmpfs set status "statusFail"
  1098. tmpfs add error "errorStopping"
  1099. output "$_ERROR_: $(getErrorText 'errorStopping')!\\n"
  1100. fi
  1101. fi
  1102. }
  1103. service_triggers() {
  1104. procd_add_reload_trigger 'simple-adblock'
  1105. }
  1106. check() {
  1107. load_package_config
  1108. local string="$1"
  1109. local c="$(grep -c "$string" "$outputFile")"
  1110. if [ ! -s "$outputFile" ]; then
  1111. echo "No blacklist ('$outputFile') found."
  1112. elif [ -z "$string" ]; then
  1113. echo "Usage: /etc/init.d/${packageName} check string"
  1114. elif [ "$c" -gt 0 ]; then
  1115. if [ "$c" -gt 1 ]; then
  1116. echo "Found $c matches for '$string' in '$outputFile':"
  1117. else
  1118. echo "Found 1 match for '$string' in '$outputFile':"
  1119. fi
  1120. case "$targetDNS" in
  1121. dnsmasq.addnhosts)
  1122. grep "$string" "$outputFile" | sed 's|^127.0.0.1 ||;s|^:: ||;';;
  1123. dnsmasq.conf)
  1124. grep "$string" "$outputFile" | sed 's|local=/||;s|/$||;';;
  1125. dnsmasq.ipset)
  1126. grep "$string" "$outputFile" | sed 's|ipset=/||;s|/adb$||;';;
  1127. dnsmasq.servers)
  1128. grep "$string" "$outputFile" | sed 's|server=/||;s|/$||;';;
  1129. unbound.adb_list)
  1130. grep "$string" "$outputFile" | sed 's|^local-zone: "||;s|" static$||;';;
  1131. esac
  1132. else
  1133. echo "The $string is not found in current blacklist ('$outputFile')."
  1134. fi
  1135. }
  1136. sizes() {
  1137. local i
  1138. load_package_config
  1139. echo "# $(date)"
  1140. for i in $blacklist_domains_urls; do
  1141. [ "${i//melmac}" != "$i" ] && continue
  1142. if $dl_command "$i" $dl_flag /tmp/sast 2>/dev/null && [ -s /tmp/sast ]; then
  1143. echo "# File size: $(du -sh /tmp/sast | awk '{print $1}')"
  1144. if compare_versions "$(du -sk /tmp/sast)" "500"; then
  1145. echo "# blocklist too big for most routers"
  1146. elif compare_versions "$(du -sk /tmp/sast)" "100"; then
  1147. echo "# blocklist may be too big for some routers"
  1148. fi
  1149. rm -rf /tmp/sast
  1150. echo " list blacklist_domains_url '$i'"
  1151. echo ""
  1152. else
  1153. echo "# site was down on last check"
  1154. echo "# list blacklist_domains_url '$i'"
  1155. echo ""
  1156. fi
  1157. done
  1158. for i in $blacklist_hosts_urls; do
  1159. if $dl_command "$i" $dl_flag /tmp/sast 2>/dev/null && [ -s /tmp/sast ]; then
  1160. echo "# File size: $(du -sh /tmp/sast | awk '{print $1}')"
  1161. if compare_versions "$(du -sk /tmp/sast)" "500"; then
  1162. echo "# blocklist too big for most routers"
  1163. elif compare_versions "$(du -sk /tmp/sast)" "100"; then
  1164. echo "# blocklist may be too big for some routers"
  1165. fi
  1166. rm -rf /tmp/sast
  1167. echo " list blacklist_hosts_url '$i'"
  1168. echo ""
  1169. else
  1170. echo "# site was down on last check"
  1171. echo "# list blacklist_hosts_url '$i'"
  1172. echo ""
  1173. fi
  1174. done
  1175. }