Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. #!/bin/sh
  2. #Put in /etc/adblock.sh
  3.  
  4. #Block ads, malware, etc.
  5.  
  6. # Only block wireless ads? Y/N
  7. ONLY_WIRELESS="N"
  8.  
  9. # IPv6 support? Y/N
  10. IPV6="N"
  11.  
  12. # Try to transparently serve pixel response?
  13. # NOTE: Ideally, understand the consequences and mechanics of this setup
  14. TRANS="N"
  15.  
  16. # Redirect endpoint
  17. ENDPOINT_IP4="0.0.0.0"
  18. ENDPOINT_IP6="::"
  19.  
  20. #Change the cron command to what is comfortable, or leave as is
  21. CRON="0 4 * * 0,3 sh /etc/adblock.sh"
  22.  
  23. #Need iptables-mod-nat-extra installed
  24. if opkg list-installed | grep -q iptables-mod-nat-extra
  25. then
  26. echo 'iptables-mod-nat-extra is installed!'
  27. else
  28. echo 'Updating package list...'
  29. opkg update > /dev/null
  30. echo 'Installing iptables-mod-nat-extra...'
  31. opkg install iptables-mod-nat-extra > /dev/null
  32. fi
  33.  
  34. #Need wget for https websites
  35. if opkg list-installed wget | grep -q wget
  36. then
  37. if wget --version | grep -q +ssl
  38. then
  39. echo 'wget (with ssl) found'
  40. else
  41. # wget without ssl, need to reinstall full wget
  42. opkg update > /dev/null
  43. opkg install wget --force-reinstall > /dev/null
  44. fi
  45. else
  46. echo 'Updating package list...'
  47. opkg update > /dev/null
  48. echo 'Installing wget (with ssl)...'
  49. opkg install wget > /dev/null
  50. fi
  51.  
  52.  
  53. if [ "$ONLY_WIRELESS" == "Y" ]
  54. then
  55. echo 'Wireless only blocking!'
  56. FW1="iptables -t nat -I PREROUTING -i wlan+ -p tcp --dport 53 -j REDIRECT --to-ports 53"
  57. FW2="iptables -t nat -I PREROUTING -i wlan+ -p udp --dport 53 -j REDIRECT --to-ports 53"
  58. else
  59. FW1="iptables -t nat -I PREROUTING -p tcp --dport 53 -j REDIRECT --to-ports 53"
  60. FW2="iptables -t nat -I PREROUTING -p udp --dport 53 -j REDIRECT --to-ports 53"
  61. fi
  62.  
  63.  
  64. DNSMASQ_EDITED="1"
  65. FIREWALL_EDITED="1"
  66.  
  67. echo 'Updating config, if necessary...'
  68.  
  69. #Check proper DHCP config and, if necessary, update it
  70. uci get dhcp.@dnsmasq[0].addnhosts > /dev/null 2>&1 && DNSMASQ_EDITED="0" || uci add_list dhcp.@dnsmasq[0].addnhosts=/etc/block.hosts && uci commit
  71.  
  72. #Leave crontab alone, or add to it
  73. grep -q "/etc/adblock.sh" /etc/crontabs/root || echo "$CRON" >> /etc/crontabs/root
  74.  
  75. #Add firewall rules if necessary
  76. grep -q "$FW1" /etc/firewall.user && FIREWALL_EDITED="0" || echo "$FW1" >> /etc/firewall.user
  77. grep -q "$FW2" /etc/firewall.user && FIREWALL_EDITED="0" || echo "$FW2" >> /etc/firewall.user
  78.  
  79. #Delete the old block.hosts to make room for the updates
  80. rm -f /etc/block.hosts
  81.  
  82. # Determining uhttpd/httpd_gargoyle for transparent pixel support
  83. if [ "$TRANS" == "Y" ]
  84. then
  85. ENDPOINT_IP4=$(uci get network.lan.ipaddr)
  86. if [ "$IPV6" == "Y"]
  87. then
  88. ENDPOINT_IP6=$(uci get network.lan6.ipaddr)
  89. fi
  90. if [ ! -e "/www/1.gif" ]
  91. then
  92. /usr/bin/wget -O /www/1.gif http://upload.wikimedia.org/wikipedia/commons/c/ce/Transparent.gif > /dev/null
  93. fi
  94. if [ -s "/usr/sbin/httpd_gargoyle" ]
  95. then
  96. # Write without testing
  97. echo "httpd_gargoyle found..."
  98. echo "updating server error page to return transparent pixel..."
  99. uci set httpd_gargoyle.server.page_not_found_file="1.gif" && uci commit
  100. /etc/init.d/httpd_gargoyle restart
  101. elif [ -s "/usr/sbin/uhttpd" ]
  102. then
  103. #The default is none, so I don't want to check for it, so just write it
  104. echo "uhttpd found..."
  105. echo "updating server error page to return transparent pixel..."
  106. uci set uhttpd.main.error_page="/1.gif" && uci commit
  107. /etc/init.d/uhttpd restart
  108. else
  109. echo "Cannot find supported web server..."
  110. fi
  111. fi
  112.  
  113. echo 'Downloading hosts lists...'
  114.  
  115. #Download and process the files needed to make the lists (enable/add more, if you want)
  116. wget -qO- http://www.mvps.org/winhelp2002/hosts.txt| awk -v r="$ENDPOINT_IP4" '{sub(/^0.0.0.0/, r)} $0 ~ "^"r' > /tmp/block.build.list
  117. wget -qO- --no-check-certificate "https://adaway.org/hosts.txt"|awk -v r="$ENDPOINT_IP4" '{sub(/^127.0.0.1/, r)} $0 ~ "^"r' >> /tmp/block.build.list
  118. #wget -qO- http://www.malwaredomainlist.com/hostslist/hosts.txt|awk -v r="$ENDPOINT_IP4" '{sub(/^127.0.0.1/, r)} $0 ~ "^"r' >> /tmp/block.build.list
  119. #wget -qO- "http://hosts-file.net/.\ad_servers.txt"|awk -v r="$ENDPOINT_IP4" '{sub(/^127.0.0.1/, r)} $0 ~ "^"r' >> /tmp/block.build.list
  120.  
  121. #Add black list, if non-empty
  122. if [ -s "/etc/black.list" ]
  123. then
  124. echo 'Adding blacklist...'
  125. awk -v r="$ENDPOINT_IP4" '/^[^#]/ { print r,$1 }' /etc/black.list >> /tmp/block.build.list
  126. fi
  127.  
  128. echo 'Sorting lists...'
  129.  
  130. #Sort the download/black lists
  131. awk '{sub(/\r$/,"");print $1,$2}' /tmp/block.build.list|sort -u > /tmp/block.build.before
  132.  
  133. #Filter (if applicable)
  134. if [ -s "/etc/white.list" ]
  135. then
  136. #Filter the blacklist, supressing whitelist matches
  137. # This is relatively slow =-(
  138. echo 'Filtering white list...'
  139. egrep -v "^[[:space:]]*$" /etc/white.list | awk '/^[^#]/ {sub(/\r$/,"");print $1}' | grep -vf - /tmp/block.build.before > /etc/block.hosts
  140. else
  141. cat /tmp/block.build.before > /etc/block.hosts
  142. fi
  143.  
  144. safe_pattern=$(printf '%s\n' "$ENDPOINT_IP4" | sed 's/[[\.*^$(){}?+|/]/\\&/g')
  145. safe_addition=$(printf '%s\n' "$ENDPOINT_IP6" | sed 's/[\&/]/\\&/g')
  146.  
  147. if [ "$IPV6" == "Y" ]
  148. then
  149. echo 'Adding ipv6 support...'
  150. sed -i -re "s/^(${safe_pattern}) (.*)$/\1 \2\n${safe_addition} \2/g" /etc/block.hosts
  151. fi
  152.  
  153. echo 'Cleaning up...'
  154.  
  155. #Delete files used to build list to free up the limited space
  156. rm -f /tmp/block.build.list
  157. rm -f /tmp/block.build.before
  158.  
  159. if [ "$FIREWALL_EDITED" -ne "0" ]
  160. then
  161. echo 'Restarting firewall...'
  162. if [ -s "/usr/lib/gargoyle/restart_firewall.sh" ]
  163. then
  164. /usr/lib/gargoyle/restart_firewall.sh > /dev/null 2>&1
  165. else
  166. /etc/init.d/firewall restart > /dev/null 2>&1
  167. fi
  168. fi
  169.  
  170. echo 'Restarting dnsmasq...'
  171.  
  172. #Restart dnsmasq
  173. if [ "$DNSMASQ_EDITED" -eq "0" ]
  174. then
  175. killall -HUP dnsmasq
  176. else
  177. /etc/init.d/dnsmasq restart
  178. fi
  179.  
  180. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement