adblock 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/bin/sh
  2. #
  3. # Load config
  4. #
  5. config=/opt/adblock/adblock.conf
  6. if [ ! -f $config ]; then
  7. echo "config file not found [ $config ]"
  8. exit 1;
  9. fi
  10. source $config;
  11. #
  12. # Check local db version
  13. #
  14. localVersion=0
  15. if [ -f "$dbFile".version ]; then
  16. localVersion=$( cat "$dbFile".version )
  17. echo "Local db version: $localVersion"
  18. fi
  19. #
  20. # Check remote db version
  21. #
  22. remoteVersion=$( wget -O - "$dbURL".version 2> /dev/null )
  23. echo "Remote db version: $remoteVersion"
  24. #
  25. # Check if db should be updated
  26. #
  27. if [ $localVersion -eq $remoteVersion ]; then
  28. echo "Database is up-to-date."
  29. else
  30. # Update stale db
  31. echo "Updating local database..."
  32. wget -O - $dbURL.version 2> /dev/null > $dbFile.version
  33. wget -O - $dbURL 2> /dev/null | grep [0-9].* |
  34. sed 's/[[:space:]]*#.*$//g;' |
  35. grep -v localhost | tr ' ' '\t' |
  36. tr -s '\t' | tr -d '\015' | sort -u > $dbFile
  37. fi
  38. #
  39. # Update dnsmasq if needed
  40. #
  41. grep "addn-hosts=$dbFile" $dnsmasq &> /dev/null
  42. if [ $? -eq 0 ]; then
  43. echo "dnsmasq up-to-date."
  44. else
  45. echo "Updating dnsmasq..."
  46. echo "" >> $dnsmasq
  47. echo "" >> $dnsmasq
  48. echo "#Adblock" >> $dnsmasq
  49. echo "addn-hosts=$dbFile" >> $dnsmasq
  50. fi
  51. #
  52. # Restart dnsmasq
  53. #
  54. echo "Restarting dnsmasq..."
  55. killall dnsmasq
  56. dnsmasq --conf-file=$dnsmasq
  57. #
  58. # Update firewall if needed
  59. #
  60. iptables -vnL FORWARD | grep $blackhole &> /dev/null
  61. if [ $? -eq 0 ]; then
  62. echo "Firewall FORWARD rule up-to-date."
  63. else
  64. echo "Applying FORWARD firewall rule..."
  65. iptables -I FORWARD -d $blackhole -j REJECT
  66. fi
  67. iptables -vnL OUTPUT | grep $blackhole &> /dev/null
  68. if [ $? -eq 0 ]; then
  69. echo "Firewall OUTPUT rule up-to-date."
  70. else
  71. echo "Applying OUTPUT firewall rule..."
  72. iptables -I OUTPUT -d $blackhole -j REJECT
  73. fi
  74. #
  75. # Done
  76. #
  77. echo "done"
  78. exit 0