1
0

90-adblock-fast 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/bin/sh
  2. # Copyright 2023 MOSSDeF, Stan Grishin (stangri@melmac.ca)
  3. # shellcheck disable=SC2015,SC3043,SC3060
  4. readonly adbFunctionsFile='/etc/init.d/adblock-fast'
  5. if [ -s "$adbFunctionsFile" ]; then
  6. # shellcheck source=../../etc/init.d/adblock-fast
  7. . "$adbFunctionsFile"
  8. else
  9. printf "%b: adblock-fast init.d file (%s) not found! \n" '\033[0;31mERROR\033[0m' "$adbFunctionsFile"
  10. fi
  11. # Transition from simple-adblock
  12. _enable_url() {
  13. local cfg="$1" url="$2" action="$3"
  14. local u a
  15. config_get u "$cfg" 'url'
  16. config_get a "$cfg" 'action' 'block'
  17. if [ "$u" = "$url" ] && [ "$a" = "$action" ]; then
  18. uci del "${packageName}.${cfg}.enabled" && _found=1
  19. fi
  20. }
  21. enable_add_url() {
  22. local url="$1" action="$2" _found
  23. config_load "$packageName"
  24. config_foreach _enable_url 'file_url' "$url" "$action"
  25. if [ -z "$_found" ]; then
  26. uci add "${packageName}" 'file_url' >/dev/null 2>&1
  27. uci set "${packageName}.@file_url[-1].url=$url"
  28. uci set "${packageName}.@file_url[-1].size=$(get_url_filesize "$url")"
  29. uci set "${packageName}.@file_url[-1].action=$action"
  30. fi
  31. }
  32. if [ -s '/etc/config/simple-adblock' ] \
  33. && [ ! -s '/etc/config/adblock-fast-opkg' ] \
  34. && [ "$(uci get adblock-fast.config.enabled)" = '0' ]; then
  35. cp -f '/etc/config/adblock-fast' '/etc/config/adblock-fast-opkg'
  36. enabled="$(uci get simple-adblock.config.enabled)"
  37. if [ -x '/etc/init.d/simple-adblock' ]; then
  38. output "Stopping and disabling simple-adblock "
  39. if /etc/init.d/simple-adblock stop >/dev/null 2>&1 \
  40. && /etc/init.d/simple-adblock disable \
  41. && uci set simple-adblock.config.enabled=0 \
  42. && uci commit simple-adblock; then
  43. output_okn
  44. else
  45. output_failn
  46. fi
  47. else
  48. output "Disabling simple-adblock."
  49. if uci set simple-adblock.config.enabled=0 \
  50. && uci commit simple-adblock; then
  51. output_okn
  52. else
  53. output_failn
  54. fi
  55. fi
  56. output "Migrating simple-adblock config file "
  57. for i in allow_non_ascii canary_domains_icloud canary_domains_mozilla \
  58. compressed_cache compressed_cache_dir config_update_enabled \
  59. curl_additional_param curl_max_file_size curl_retry download_timeout \
  60. debug dns dns_instance dnsmasq_config_file_url force_dns led \
  61. parallel_downloads procd_trigger_wan6 procd_boot_wan_timeout verbosity; do
  62. j="$(uci -q get simple-adblock.config.${i})"
  63. [ -n "$j" ] && uci set "${packageName}.config.${i}=${j}"
  64. done
  65. [ -n "$enabled" ] && uci set "${packageName}.config.enabled=${enabled}"
  66. j="$(uci -q get simple-adblock.config.config_update_url)"
  67. if [ "${j//simple-adblock/}" = "$j" ]; then
  68. uci set "${packageName}.config.config_update_url=$j"
  69. fi
  70. ccd="$(uci get simple-adblock.config.compressed_cache_dir)"
  71. ccd="${ccd:-/etc}"
  72. for j in $(uci -q get simple-adblock.config.allowed_domain); do
  73. [ -n "$j" ] && uci add_list "${packageName}.config.allowed_domain=${j}"
  74. done
  75. for j in $(uci -q get simple-adblock.config.blocked_domain); do
  76. [ -n "$j" ] && uci add_list "${packageName}.config.blocked_domain=${j}"
  77. done
  78. for j in $(uci -q get simple-adblock.config.force_dns_port); do
  79. [ -n "$j" ] && uci add_list "${packageName}.config.force_dns_port=${j}"
  80. done
  81. output_okn
  82. for i in allowed_domains_url blocked_adblockplus_url blocked_domains_url \
  83. blocked_hosts_url; do
  84. output "Migrating simple-adblock ${i} "
  85. for j in $(uci -q get simple-adblock.config.${i}); do
  86. if [ "$i" = 'allowed_domains_url' ]; then
  87. enable_add_url "$j" 'allow'
  88. else
  89. enable_add_url "$j" 'block'
  90. fi
  91. done
  92. output_okn
  93. done
  94. uci commit "$packageName"
  95. output "Migrating simple-adblock cache file(s) "
  96. for i in '/var/run/simple-adblock/dnsmasq.addnhosts.cache' \
  97. '/var/run/simple-adblock/dnsmasq.conf.cache' \
  98. '/var/run/simple-adblock/dnsmasq.ipset.cache' \
  99. '/var/run/simple-adblock/dnsmasq.nftset.cache' \
  100. '/var/run/simple-adblock/dnsmasq.servers.cache' \
  101. '/var/run/simple-adblock/unbound.cache'; do
  102. if [ -s "$i" ]; then
  103. current_dir="$(dirname "$i")"
  104. mkdir -p "${current_dir//simple-adblock/adblock-fast}"
  105. mv -f "$i" "${i//simple-adblock/adblock-fast}" && output_okn || output_failn
  106. fi
  107. done
  108. for i in 'simple-adblock.dnsmasq.addnhosts.gz' \
  109. 'simple-adblock.dnsmasq.conf.gz' \
  110. 'simple-adblock.dnsmasq.ipset.gz' \
  111. 'simple-adblock.dnsmasq.nftset.gz' \
  112. 'simple-adblock.dnsmasq.servers.gz' \
  113. 'simple-adblock.unbound.gz'; do
  114. i="${ccd}/${i}"
  115. if [ -s "$i" ]; then
  116. mkdir -p "${ccd//simple-adblock/adblock-fast}"
  117. mv -f "$i" "${i//simple-adblock/adblock-fast}" && output_okn || output_failn
  118. fi
  119. done
  120. output_okn
  121. fi
  122. exit 0