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.

680 lines
21 KiB

  1. #!/bin/sh /etc/rc.common
  2. PKG_VERSION=
  3. export START=94
  4. export USE_PROCD=1
  5. export LC_ALL=C
  6. export EXTRA_COMMANDS="check dl killcache status"
  7. export EXTRA_HELP=" check Checks if specified domain is found in current blacklist
  8. dl Force-redownloads all the lists, even if the last download was successful and no config changes were made
  9. status Shows the service last-run status"
  10. readonly packageName="simple-adblock"
  11. readonly serviceName="$packageName $PKG_VERSION"
  12. readonly dnsmasqFile="/var/dnsmasq.d/${packageName}"
  13. readonly compressedCacheFile="/etc/${packageName}.gz"
  14. readonly A_TMP="/var/simple-adblock.hosts.a.tmp"
  15. readonly B_TMP="/var/simple-adblock.hosts.b.tmp"
  16. readonly cacheFile="/var/run/${packageName}.cache"
  17. readonly PIDFile="/var/run/${packageName}.pid"
  18. readonly JsonFile="/var/run/${packageName}.json"
  19. readonly h_filter='/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;'
  20. readonly d_filter='/^#/d;s/[[:space:]]*#.*$//;s/[[:space:]]*$//;s/[[:cntrl:]]$//;/[[:space:]]/d;/[`~!@#\$%\^&\*()=+;:"'\'',<>?/\|[{}]/d;/]/d;/\./!d;/^$/d;/[^[:alnum:]_.-]/d;'
  21. readonly f_filter='s|^|local=/|;s|$|/|'
  22. readonly checkmark='\xe2\x9c\x93'
  23. readonly xmark='\xe2\x9c\x97'
  24. readonly _OK_='\033[0;32m\xe2\x9c\x93\033[0m'
  25. readonly _FAIL_='\033[0;31m\xe2\x9c\x97\033[0m'
  26. readonly __OK__='\033[0;32m[\xe2\x9c\x93]\033[0m'
  27. readonly __FAIL__='\033[0;31m[\xe2\x9c\x97]\033[0m'
  28. readonly _ERROR_='\033[0;31mERROR\033[0m'
  29. readonly statusSuccess='Success'
  30. readonly statusFail='Fail'
  31. readonly statusDownloading='Downloading'
  32. readonly statusReloading='Reloading'
  33. readonly statusRestarting='Restarting'
  34. readonly statusStarting='Starting'
  35. readonly statusForceReloading='Force-Reloading'
  36. readonly statusProcessing='Processing'
  37. readonly statusStopped='Stopped'
  38. create_lock() { [ -e "$PIDFile" ] && return 1; touch "$PIDFile"; }
  39. remove_lock() { [ -e "$PIDFile" ] && rm -f "$PIDFile"; } # rm -f /var/simple-adblock_tmp_* >/dev/null 2>&1; }
  40. trap remove_lock EXIT
  41. output_ok() { case $verbosity in 1) output 1 "$_OK_";; 2) output 2 "$__OK__\\n";; esac; }
  42. output_okn() { case $verbosity in 1) output 1 "$_OK_\\n";; 2) output 2 "$__OK__\\n";; esac; }
  43. output_fail() { case $verbosity in 1) output 1 "$_FAIL_";; 2) output 2 "$__FAIL__\\n";; esac; }
  44. output_failn() { case $verbosity in 1) output 1 "$_FAIL_\\n";; 2) output 2 "$__FAIL__\\n";; esac; }
  45. export logmsg
  46. output() {
  47. # Can take a single parameter (text) to be output at any verbosity
  48. # Or target verbosity level and text to be output at specifc verbosity
  49. if [ $# -ne 1 ]; then
  50. if [ ! $((verbosity & $1)) -gt 0 ]; then return 0; else shift; fi
  51. fi
  52. [ -t 1 ] && echo -e -n "$1"
  53. local msg=$(echo -n "${1/$serviceName /service }" | sed 's|\\033\[[0-9]\?;\?[0-9]\?[0-9]\?m||g');
  54. if [ "$(echo -e -n "$msg" | wc -l)" -gt 0 ]; then
  55. logger -t "${packageName:-service} [$$]" "$(echo -e -n "${logmsg}${msg}")"
  56. logmsg=""
  57. else
  58. logmsg="${logmsg}${msg}"
  59. fi
  60. }
  61. led_on(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo "default-on" > "${1}/trigger" 2>&1; fi; }
  62. led_off(){ if [ -n "${1}" ] && [ -e "${1}/trigger" ]; then echo "none" > "${1}/trigger" 2>&1; fi; }
  63. testCompressedCache(){ [ "$compressedCache" -gt 0 ] && gzip -t -c $compressedCacheFile; }
  64. boot() { load_package_config; ( sleep "$bootDelay" && rc_procd start_service && rc_procd service_triggers | cat & ); }
  65. export serviceEnabled
  66. export forceDNS
  67. export parallelDL
  68. export debug
  69. export allowNonAscii
  70. export compressedCache
  71. export bootDelay
  72. export dlTimeout
  73. export verbosity
  74. export led
  75. export whitelist_domains
  76. export blacklist_domains
  77. export whitelist_domains_urls
  78. export blacklist_domains_urls
  79. export blacklist_hosts_urls
  80. export wan_if wan_gw wanphysdev dl_command serviceStatus dl_flag
  81. export dlStatus
  82. load_package_config() {
  83. config_load "$packageName"
  84. config_get_bool serviceEnabled "config" "enabled" 1
  85. config_get_bool forceDNS "config" "force_dns" 1
  86. config_get_bool parallelDL "config" "parallel_downloads" 1
  87. config_get_bool debug "config" "debug" 0
  88. config_get_bool allowNonAscii "config" "allow_non_ascii" 0
  89. config_get_bool compressedCache "config" "compressed_cache" 0
  90. config_get bootDelay "config" "boot_delay" "120"
  91. config_get dlTimeout "config" "download_timeout" "20"
  92. config_get verbosity "config" "verbosity" "2"
  93. config_get led "config" "led"
  94. config_get whitelist_domains "config" "whitelist_domain"
  95. config_get blacklist_domains "config" "blacklist_domain"
  96. config_get whitelist_domains_urls "config" "whitelist_domains_url"
  97. config_get blacklist_domains_urls "config" "blacklist_domains_url"
  98. config_get blacklist_hosts_urls "config" "blacklist_hosts_url"
  99. if [ -z "${verbosity##*[!0-9]*}" ] || [ "$verbosity" -lt 0 ] || [ "$verbosity" -gt 2 ]; then
  100. verbosity=1
  101. fi
  102. . /lib/functions/network.sh
  103. . /usr/share/libubox/jshn.sh
  104. # Prefer curl because it supports the file: scheme.
  105. if [ -x /usr/bin/curl ] ; then
  106. dl_command="curl --insecure --connect-timeout $dlTimeout --silent"
  107. dl_flag="-o"
  108. else
  109. dl_command="wget --no-check-certificate --timeout $dlTimeout -q"
  110. dl_flag="-O"
  111. fi
  112. led="${led:+/sys/class/leds/$led}"
  113. }
  114. is_enabled() {
  115. load_package_config
  116. if [ "$debug" -ne 0 ]; then
  117. exec 1>>/tmp/simple-adblock.log
  118. exec 2>&1
  119. set -x
  120. fi
  121. if [ "$serviceEnabled" -eq 0 ]; then
  122. if [ "$1" = "on_start" ]; then
  123. output "$packageName is currently disabled.\\n"
  124. output "Run the following commands before starting service again:\\n"
  125. output "uci set $packageName.config.enabled='1'; uci commit;\\n"
  126. fi
  127. return 1
  128. fi
  129. [ ! -d ${dnsmasqFile%/*} ] && mkdir -p ${dnsmasqFile%/*}
  130. testCompressedCache && return 0
  131. network_flush_cache; network_find_wan wan_if; network_get_gateway wan_gw "$wan_if";
  132. [ -n "$wan_gw" ] && return 0
  133. output "$_ERROR_: $serviceName failed to discover WAN gateway.\\n"; return 1;
  134. }
  135. dnsmasq_kill() { killall -q -HUP dnsmasq; }
  136. dnsmasq_restart() { /etc/init.d/dnsmasq restart >/dev/null 2>&1; }
  137. reload_dnsmasq() {
  138. case $1 in
  139. on_start)
  140. if [ -s "$dnsmasqFile" ]; then
  141. output 3 "Restarting DNSMASQ "
  142. tmpfs set message "restarting DNSMASQ"
  143. if dnsmasq_restart; then
  144. tmpfs set status "$statusSuccess"
  145. led_on "$led"
  146. output_okn
  147. else
  148. output_failn
  149. tmpfs set status "$statusFail"
  150. tmpfs add error "DNSMASQ restart error"
  151. output "$_ERROR_: $serviceName failed to restart DNSMASQ!\\n"
  152. return 1
  153. fi
  154. else
  155. tmpfs set status "$statusFail"
  156. tmpfs add error "Failed to create $dnsmasqFile file."
  157. output "$_ERROR_: $serviceName failed to create its data file!\\n"
  158. return 1
  159. fi
  160. ;;
  161. on_stop)
  162. [ -f $dnsmasqFile ] && mv $dnsmasqFile $cacheFile
  163. output 3 "Restarting dnsmasq "
  164. if dnsmasq_restart; then
  165. led_off "$led"
  166. output_okn
  167. output "$serviceName stopped.\\n"
  168. tmpfs set status "$statusStopped"
  169. tmpfs del message
  170. tmpfs del error
  171. return 0
  172. else
  173. output_failn;
  174. tmpfs set status "$statusFail"
  175. tmpfs add error "DNSMASQ restart error on stop"
  176. output "$_ERROR_: $serviceName failed to restart DNSMASQ on stop !\\n"
  177. return 1
  178. fi
  179. ;;
  180. quiet | *)
  181. dnsmasq_restart && return 0 || return 1
  182. ;;
  183. esac
  184. }
  185. tmpfs(){
  186. local action="$1" instance="$2" value="$3"
  187. local status message error stats
  188. local readReload readRestart curReload curRestart ret
  189. if [ -s "$JsonFile" ]; then
  190. status="$(jsonfilter -i $JsonFile -l1 -e "@['data']['status']")"
  191. message="$(jsonfilter -i $JsonFile -l1 -e "@['data']['message']")"
  192. error="$(jsonfilter -i $JsonFile -l1 -e "@['data']['error']")"
  193. stats="$(jsonfilter -i $JsonFile -l1 -e "@['data']['stats']")"
  194. readReload="$(jsonfilter -i $JsonFile -l1 -e "@['data']['reload']")"
  195. readRestart="$(jsonfilter -i $JsonFile -l1 -e "@['data']['restart']")"
  196. fi
  197. case "$action" in
  198. get)
  199. case "$instance" in
  200. status)
  201. echo "$status"; return;;
  202. message)
  203. echo "$message"; return;;
  204. error)
  205. echo "$error"; return;;
  206. stats)
  207. echo "$stats"; return;;
  208. triggers)
  209. curReload="$allowNonAscii $parallelDL $debug $dlTimeout $whitelist_domains $blacklist_domains $whitelist_domains_urls $blacklist_domains_urls $blacklist_hosts_urls"
  210. curRestart="$compressedCache $forceDNS $led"
  211. if [ "$curReload" != "$readReload" ]; then
  212. ret="download"
  213. elif [ "$curRestart" != "$readRestart" ]; then
  214. ret="restart"
  215. fi
  216. echo "$ret"
  217. return;;
  218. esac
  219. ;;
  220. add)
  221. case "$instance" in
  222. status)
  223. [ -n "$status" ] && status="$status $value" || status="$value";;
  224. message)
  225. [ -n "$message" ] && message="${message} ${value}" || message="$value";;
  226. error)
  227. [ -n "$error" ] && error="$error $value" || error="$value";;
  228. stats)
  229. [ -n "$stats" ] && stats="$stats $value" || stats="$value";;
  230. esac
  231. ;;
  232. del)
  233. case "$instance" in
  234. status)
  235. unset status;;
  236. message)
  237. unset message;;
  238. error)
  239. unset error;;
  240. stats)
  241. unset stats;;
  242. triggers)
  243. unset readReload; unset readRestart;;
  244. esac
  245. ;;
  246. set)
  247. case "$instance" in
  248. status)
  249. status="$value";;
  250. message)
  251. message="$value";;
  252. error)
  253. error="$value";;
  254. stats)
  255. stats="$value";;
  256. triggers)
  257. readReload="$allowNonAscii $parallelDL $debug $dlTimeout $whitelist_domains $blacklist_domains $whitelist_domains_urls $blacklist_domains_urls $blacklist_hosts_urls"
  258. readRestart="$compressedCache $forceDNS $led"
  259. ;;
  260. esac
  261. ;;
  262. esac
  263. json_init
  264. json_add_object "data"
  265. json_add_string version "$PKG_VERSION"
  266. json_add_string status "$status"
  267. json_add_string message "$message"
  268. json_add_string error "$error"
  269. json_add_string stats "$stats"
  270. json_add_string reload "$readReload"
  271. json_add_string restart "$readRestart"
  272. json_close_object
  273. json_dump > "$JsonFile"
  274. sync
  275. }
  276. is_chaos_calmer() { ubus -S call system board | grep -q "Chaos Calmer"; }
  277. remove_fw3_redirect() {
  278. local name
  279. config_get name "$1" "name"
  280. if [ -n "$name" ] && [ "$name" != "${name//simple_adblock}" ]; then
  281. uci -q del "firewall.$1"
  282. fi
  283. }
  284. fw3_setup() {
  285. config_load "firewall"
  286. config_foreach remove_fw3_redirect "redirect"
  287. if [ "$1" = "start" ]; then
  288. uci -q add firewall redirect >/dev/null 2>&1
  289. uci -q set firewall.@redirect[-1].name="simple_adblock_dns_hijack"
  290. uci -q set firewall.@redirect[-1].target="DNAT"
  291. uci -q set firewall.@redirect[-1].src="lan"
  292. uci -q set firewall.@redirect[-1].proto="tcpudp"
  293. uci -q set firewall.@redirect[-1].src_dport="53"
  294. uci -q set firewall.@redirect[-1].dest_port="53"
  295. uci -q set firewall.@redirect[-1].dest_ip="$ip"
  296. uci -q set firewall.@redirect[-1].reflection="0"
  297. fi
  298. if [ -n "$(uci changes firewall)" ]; then
  299. uci -q commit firewall
  300. /etc/init.d/firewall restart >/dev/null 2>&1
  301. fi
  302. }
  303. process_url() {
  304. local label type D_TMP R_TMP
  305. if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ]; then return 1; fi
  306. label="${1##*//}"; label="${label%%/*}";
  307. if [ "$2" = "hosts" ]; then
  308. label="Hosts: $label"; filter="$h_filter";
  309. else
  310. label="Domains: $label"; filter="$d_filter";
  311. fi
  312. if [ "$3" = "blocked" ]; then
  313. type="Blocked"; D_TMP="$B_TMP";
  314. else
  315. type="Allowed"; D_TMP="$A_TMP";
  316. fi
  317. while [ -z "$R_TMP" ] || [ -e "$R_TMP" ]; do
  318. R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
  319. done
  320. if ! $dl_command "$1" $dl_flag "$R_TMP" 2>/dev/null || [ ! -s "$R_TMP" ]; then
  321. output 2 "[DL] $type $label $__FAIL__\\n"
  322. output 1 "$_FAIL_"
  323. # tmpfs add message "-"
  324. dlStatus="${dlStatus}-"
  325. tmpfs add error "Error downloading ${1}."
  326. return 0
  327. fi
  328. sed -i "$filter" "$R_TMP"
  329. cat "${R_TMP}" >> "$D_TMP"
  330. rm -f "${R_TMP}" >/dev/null 2>/dev/null
  331. output 2 "[DL] $type $label $__OK__\\n"
  332. output 1 "$_OK_"
  333. # tmpfs add message "+"
  334. dlStatus="${dlStatus}+"
  335. return 0
  336. }
  337. download_lists() {
  338. local i hf w_filter j=0 R_TMP
  339. tmpfs set message "${statusDownloading}..."
  340. for i in $A_TMP $B_TMP $cacheFile $dnsmasqFile; do [ -f $i ] && rm -f $i; done
  341. if [ "$(awk '/^MemFree/ {print int($2/1000)}' "/proc/meminfo")" -lt 32 ]; then
  342. output 3 "Low free memory, restarting dnsmasq..."
  343. if reload_dnsmasq "quiet"; then output_okn; else output_failn; fi
  344. fi
  345. touch $A_TMP; touch $B_TMP;
  346. output 1 "Downloading lists "
  347. unset dlStatus
  348. if [ -n "$blacklist_hosts_urls" ]; then
  349. for hf in ${blacklist_hosts_urls}; do
  350. if [ "$parallelDL" -gt 0 ]; then
  351. process_url "$hf" "hosts" "blocked" &
  352. else
  353. process_url "$hf" "hosts" "blocked"
  354. fi
  355. done
  356. fi
  357. if [ -n "$blacklist_domains_urls" ]; then
  358. for hf in ${blacklist_domains_urls}; do
  359. if [ "$parallelDL" -gt 0 ]; then
  360. process_url "$hf" "domains" "blocked" &
  361. else
  362. process_url "$hf" "domains" "blocked"
  363. fi
  364. done
  365. fi
  366. if [ -n "$whitelist_domains_urls" ]; then
  367. for hf in ${whitelist_domains_urls}; do
  368. if [ "$parallelDL" -gt 0 ]; then
  369. process_url "$hf" "domains" "allowed" &
  370. else
  371. process_url "$hf" "domains" "allowed"
  372. fi
  373. done
  374. fi
  375. wait
  376. dlStatus="${dlStatus//-/$xmark}"
  377. dlStatus="${dlStatus//+/$checkmark}"
  378. [ "$verbosity" = "1" ] && logmsg="${logmsg}${dlStatus}"
  379. output 1 "\\n"
  380. [ -n "$blacklist_domains" ] && for hf in ${blacklist_domains}; do echo "$hf" | sed "$d_filter" >> $B_TMP; done
  381. whitelist_domains="${whitelist_domains}
  382. $(cat $A_TMP)"
  383. [ -n "$whitelist_domains" ] && for hf in ${whitelist_domains}; do hf=$(echo "$hf" | sed 's/\./\\./g'); w_filter="$w_filter/^${hf}$/d;/\\.${hf}$/d;"; done
  384. if [ -s $B_TMP ]; then
  385. output 1 "Processing downloads "
  386. output 2 "Sorting combined list "
  387. tmpfs set message "$statusProcessing: sorting combined list"
  388. if sort $B_TMP | uniq > $A_TMP; then
  389. output_ok
  390. else
  391. output_fail
  392. tmpfs add error "Sorting error."
  393. fi
  394. # TLD optimization written by Dirk Brenken (dev@brenken.org)
  395. output 2 "Optimizing combined list "
  396. tmpfs set message "$statusProcessing: optimizing combined list"
  397. if awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$A_TMP" > "$B_TMP"; then
  398. if sort "$B_TMP" > "$A_TMP"; then
  399. if awk '{if(NR==1){tld=$NF};while(getline){if($NF!~tld"\\."){print tld;tld=$NF}}print tld}' "$A_TMP" > "$B_TMP"; then
  400. if awk -F "." '{for(f=NF;f>1;f--)printf "%s.",$f;print $1}' "$B_TMP" > "$A_TMP"; then
  401. if sort "$A_TMP" | uniq > "$B_TMP"; then
  402. output_ok
  403. else
  404. output_fail
  405. tmpfs add error "Data file optimization error."
  406. mv $A_TMP $B_TMP
  407. fi
  408. else
  409. output_fail
  410. tmpfs add error "Data file optimization error."
  411. fi
  412. else
  413. output_fail
  414. tmpfs add error "Data file optimization error."
  415. mv $A_TMP $B_TMP
  416. fi
  417. else
  418. output_fail
  419. tmpfs add error "Data file optimization error."
  420. fi
  421. else
  422. output_fail
  423. tmpfs add error "Data file optimization error."
  424. mv $A_TMP $B_TMP
  425. fi
  426. output 2 "Whitelisting domains "
  427. tmpfs set message "$statusProcessing: whitelisting domains"
  428. if sed -i "$w_filter" $B_TMP; then
  429. output_ok
  430. else
  431. output_fail
  432. tmpfs add error "Whitelist processing error."
  433. fi
  434. output 2 "Formatting merged file "
  435. tmpfs set message "$statusProcessing: formatting merged file"
  436. if [ "$allowNonAscii" -gt 0 ]; then
  437. if sed "$f_filter" $B_TMP > $A_TMP; then
  438. output_ok
  439. else
  440. output_fail
  441. tmpfs add error "Data file formatting error."
  442. fi
  443. else
  444. if sed "$f_filter" $B_TMP | grep -E -v '[^a-zA-Z0-9=/.-]' > $A_TMP; then
  445. output_ok
  446. else
  447. output_fail
  448. tmpfs add error "Data file formatting error."
  449. fi
  450. fi
  451. output 2 "Creating DNSMASQ config "
  452. tmpfs set message "$statusProcessing: creating DNSMASQ blocklist"
  453. if mv $A_TMP $dnsmasqFile; then
  454. output_ok
  455. else
  456. output_fail
  457. tmpfs add error "Error moving data file ${A_TMP} to ${dnsmasqFile}."
  458. fi
  459. if [ "$compressedCache" -gt 0 ]; then
  460. output 2 "Creating compressed cache "
  461. tmpfs set message "$statusProcessing: creating compressed cache"
  462. R_TMP="$(mktemp -u -q -t ${packageName}_tmp.XXXXXXXX)"
  463. if gzip < "$dnsmasqFile" > "$R_TMP"; then
  464. mv "$R_TMP" "$compressedCacheFile"
  465. output_ok
  466. else
  467. output_fail
  468. rm -f "$R_TMP"
  469. tmpfs add error "Error creating compressed cache."
  470. fi
  471. else
  472. rm -f "$compressedCacheFile" >/dev/null 2>&1
  473. fi
  474. output 2 "Removing temporary files "
  475. tmpfs set message "$statusProcessing: removing temporary files"
  476. rm -f "/tmp/${packageName}_tmp.*" >/dev/null 2>&1
  477. for i in $A_TMP $B_TMP $cacheFile; do if [ -s $i ]; then rm -f $i || j=1; fi; done
  478. if [ $j -eq 0 ]; then
  479. output_ok
  480. else
  481. output_fail
  482. tmpfs add error "Error removing temporary files."
  483. fi
  484. output 1 "\\n"
  485. fi
  486. }
  487. start_service() {
  488. is_enabled "on_start" || return 1
  489. local ip status error action="$(tmpfs get triggers)"
  490. if create_lock; then
  491. tmpfs set triggers
  492. procd_open_instance "main"
  493. procd_set_param command /bin/true
  494. procd_set_param stdout 1
  495. procd_set_param stderr 1
  496. network_get_ipaddr ip "lan"
  497. if [ "$forceDNS" -ne 0 ] && [ -n "$ip" ]; then
  498. if is_chaos_calmer; then
  499. fw3_setup "start"
  500. else
  501. procd_open_data
  502. json_add_array firewall
  503. json_add_object ""
  504. json_add_string type redirect
  505. json_add_string target "DNAT"
  506. json_add_string src "lan"
  507. json_add_string dest "lan"
  508. json_add_string proto "tcpudp"
  509. json_add_string src_dport "53"
  510. json_add_string dest_port "53"
  511. json_add_string dest_ip "$ip"
  512. json_add_string name "simple-adblock-dns-hijack"
  513. json_add_string reflection "0"
  514. json_close_object
  515. json_close_array
  516. procd_close_data
  517. fi
  518. fi
  519. procd_close_instance
  520. status="$(tmpfs get status)"
  521. error="$(tmpfs get error)"
  522. tmpfs del status
  523. tmpfs del message
  524. tmpfs del error
  525. tmpfs del stats
  526. case "$1" in
  527. download) action="download";;
  528. restart|*)
  529. if [ ! -s "$dnsmasqFile" ] && [ ! -s "$cacheFile" ] && ! testCompressedCache; then
  530. action="download"
  531. fi
  532. if [ -n "$error" ]; then
  533. action="download"
  534. fi
  535. action="${action:-$1}"
  536. ;;
  537. esac
  538. case $action in
  539. download)
  540. if [ -s "$dnsmasqFile" ] || [ -s "$cacheFile" ] || testCompressedCache; then
  541. output "Force-reloading $serviceName...\\n"
  542. tmpfs set status "$statusForceReloading"
  543. else
  544. output "Starting $serviceName...\\n"
  545. tmpfs set status "$statusStarting"
  546. fi
  547. download_lists
  548. reload_dnsmasq "on_start"
  549. ;;
  550. restart|*)
  551. if [ "$action" == "restart" ]; then
  552. output "Retarting $serviceName...\\n"
  553. tmpfs set status "$statusRestarting"
  554. else
  555. output "Starting $serviceName...\\n"
  556. tmpfs set status "$statusStarting"
  557. fi
  558. if testCompressedCache && [ ! -s "$cacheFile" ] && [ ! -s "$dnsmasqFile" ]; then
  559. output 3 "Found compressed cache file, unpacking it "
  560. tmpfs set message "found compressed cache file, unpacking it."
  561. if gzip -dc < "${compressedCacheFile}" > "$cacheFile"; then
  562. output_okn
  563. else
  564. output_failn
  565. output "$_ERROR_: $serviceName failed to unpack compressed cache!\\n"
  566. tmpfs add error "failed to unpack compressed cache!"
  567. return 1
  568. fi
  569. fi
  570. if [ -s "$cacheFile" ] && [ ! -s "$dnsmasqFile" ]; then
  571. output 3 "Found cache file, reusing it "
  572. tmpfs set message "found cache file, reusing it."
  573. if mv "$cacheFile" "$dnsmasqFile"; then
  574. output_okn
  575. else
  576. output_failn
  577. tmpfs add error "Error moving $cacheFile to $dnsmasqFile."
  578. fi
  579. fi
  580. reload_dnsmasq "on_start"
  581. ;;
  582. esac
  583. if [ -s "$dnsmasqFile" ] && [ "$(tmpfs get status)" != "$statusFail" ]; then
  584. output "$serviceName is blocking $(wc -l < $dnsmasqFile) domains "; output_okn;
  585. tmpfs del message
  586. tmpfs set status "$statusSuccess: $(wc -l < $dnsmasqFile) domains blocked."
  587. if [ -n "$(tmpfs get error)" ]; then
  588. output "$_ERROR_: $(tmpfs get error)\\n"
  589. fi
  590. else
  591. tmpfs set status "$statusFail"
  592. tmpfs add error "Failed to create DNSMASQ blocklist."
  593. fi
  594. remove_lock
  595. else
  596. output "$serviceName: another instance is starting up "; output_failn;
  597. return 1
  598. fi
  599. }
  600. service_started() { procd_set_config_changed firewall; }
  601. service_stopped() { procd_set_config_changed firewall; }
  602. restart_service() { rc_procd stop_service "restart"; rc_procd start_service "restart"; }
  603. reload_service() { restart_service; }
  604. restart() { restart_service; }
  605. reload() { restart_service; }
  606. dl() { rc_procd stop_service "restart"; rc_procd start_service "download"; }
  607. killcache() { [ -s $cacheFile ] && rm -f $cacheFile >/dev/null 2>/dev/null; [ -s $compressedCacheFile ] && rm -f $compressedCacheFile >/dev/null 2>/dev/null; }
  608. status() {
  609. local status
  610. if [ -n "$(tmpfs get status)" ]; then
  611. status="$(tmpfs get status)"
  612. fi
  613. if [ -n "$status" ] && [ -n "$(tmpfs get message)" ]; then
  614. status="${status}: $(tmpfs get message)"
  615. fi
  616. [ -n "$status" ] && output "$serviceName $status\\n"
  617. [ -n "$(tmpfs get error)" ] && output "$_ERROR_: $(tmpfs get error)\\n"
  618. }
  619. stop_service() {
  620. load_package_config
  621. if [ "$serviceEnabled" -gt 0 ]; then
  622. if [ -z "$1" ] && [ -s "$dnsmasqFile" ]; then
  623. [ -s "$dnsmasqFile" ] && mv "$dnsmasqFile" "$cacheFile" >/dev/null 2>/dev/null
  624. output "Stopping $serviceName...\\n"
  625. tmpfs del triggers
  626. reload_dnsmasq "on_stop"
  627. fi
  628. else
  629. [ -s "$dnsmasqFile" ] && mv "$dnsmasqFile" "$cacheFile" >/dev/null 2>/dev/null
  630. reload_dnsmasq "quiet"
  631. fi
  632. if is_chaos_calmer; then
  633. fw3_setup "stop"
  634. fi
  635. }
  636. check() {
  637. load_package_config
  638. local string="$1"
  639. if [ ! -f "$dnsmasqFile" ]; then
  640. echo "No local blacklist ($dnsmasqFile) found."
  641. elif [ -z "$string" ]; then
  642. echo "Usage: /etc/init.d/${packageName} check domain"
  643. elif grep -m1 -q "$string" "$dnsmasqFile"; then
  644. echo "Found $(grep -c "$string" "$dnsmasqFile") matches for $string in $dnsmasqFile:"
  645. grep "$string" "$dnsmasqFile" | sed 's|local=/||;s|/$||;'
  646. else
  647. echo "The $string is not found in current blacklist."
  648. fi
  649. }