pup-advert-blocker 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/bin/sh
  2. #v0.3 created by sc0ttman, August 2010: http://murga-linux.com/puppy/viewtopic.php?t=59290
  3. #GPL license /usr/share/doc/legal/gpl-2.0.txt
  4. #100830 BK added GPL license, amended Exit msg, bug fixes.
  5. #120202 rodin.s: adding gettext
  6. #140212 zigbert: gui (gtkdialog) improvements.
  7. # advert blocker
  8. # downloads a list of known advert servers
  9. # then appends them to /etc/hosts so that
  10. # many online adverts are blocked from sight
  11. # make a hosts file if none found, or add a marker
  12. export TEXTDOMAIN=pup-advert-blocker
  13. export TEXTDOMAINDIR=/usr/share/locale
  14. export OUTPUT_CHARSET=UTF-8
  15. eval_gettext () {
  16. local myMESSAGE=$(gettext "$1")
  17. eval echo \"$myMESSAGE\"
  18. }
  19. export LANGORG=$LANG
  20. if [ ! -f /etc/hosts ];then
  21. echo "#host file
  22. 127.0.0.1 localhost puppypc
  23. " > /etc/hosts
  24. fi
  25. # set vars
  26. export appver='0.3'
  27. export title='Pup Advert Blocker'
  28. # the markers used to find the changes in /etc/hosts, which are made by this app
  29. export markerstart='# pup-advert-blocker IPs below'
  30. export markerend='# pup-advert-blocker IPs above'
  31. export mvps='false'
  32. export sysctl='false'
  33. export yoyo='false'
  34. # create functions
  35. set -a
  36. # cleanup all leftover files
  37. cleanup () {
  38. # remove all temp files
  39. rm -f /tmp/adlist1 /tmp/adlist2 /tmp/adlist3 /tmp/adlist-all /tmp/hosts-temp
  40. }
  41. # download the ads lists
  42. download_adlist () {
  43. # mvps
  44. if [ "$mvps" = true ]; then
  45. wget -c -4 -t 2 -O /tmp/adlist1 'http://winhelp2002.mvps.org/hosts.txt'
  46. fi
  47. # sysctl
  48. if [ "$sysctl" = true ]; then
  49. wget -c -4 -t 2 -O /tmp/adlist2 'http://sysctl.org/cameleon/hosts'
  50. fi
  51. # yoyo
  52. if [ "$yoyo" = true ]; then
  53. wget -c -4 -t 2 -O /tmp/adlist3 'http://pgl.yoyo.org/as/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext'
  54. fi
  55. #100830 BK bug fix: create if not exist...
  56. touch /tmp/adlist1
  57. touch /tmp/adlist2
  58. touch /tmp/adlist3
  59. # format of mvps list needs to be modified before combining with others
  60. sed -i '/^#/d' /tmp/adlist1
  61. sed -i '/localhost/d' /tmp/adlist1
  62. sed -i 's/0.0.0.0/127.0.0.1/' /tmp/adlist1
  63. sed -i 's/ #.*$//' /tmp/adlist1
  64. # combine the downloaded lists, then sort and remove duplicates
  65. cat /tmp/adlist1 /tmp/adlist2 /tmp/adlist3 |grep ^[0-9] |sed "s/\t//g"|sort |uniq > /tmp/adlist-all
  66. }
  67. # clean out everything but the list of IPs and servers
  68. clean_adlist () {
  69. sed -i '/^#/d' /tmp/adlist-all # remove all comments
  70. sed -i '/localhost/d' /tmp/adlist-all # remove the original links to localhost (we already have them)
  71. sed -i '/^$/d' /tmp/adlist-all # remove empty lines
  72. sed -i 's/\t/ /' /tmp/adlist-all # replace all tabs with spaces
  73. sed -i 's/ / /g' /tmp/adlist-all # remove double spaces
  74. dos2unix -u /tmp/adlist-all # change all carriage returns to UNIX format
  75. #cat /tmp/adlist-all |sort |uniq > /tmp/adlist-all # remove duplicates (again)
  76. ADLISTALL="`cat /tmp/adlist-all |sort | uniq`" #100830 BK
  77. echo "$ADLISTALL" > /tmp/adlist-all #100830 BK
  78. }
  79. # append the list to the /etc/hosts
  80. append_adlist () {
  81. # echo all but the stuff between the markers to a temp hosts file
  82. sed -e "/$markerstart/,/$markerend/d" /etc/hosts > /tmp/hosts-temp
  83. # remove the markers
  84. sed -i -e "/$markerstart/d" /tmp/hosts-temp
  85. sed -i -e "/$markerend/d" /tmp/hosts-temp
  86. # check the size of the final adlist
  87. if [ -s /tmp/adlist-all ];then
  88. # get contents of the downloaded adlist
  89. adlist=`cat /tmp/adlist-all`
  90. # add list contents into the hosts file, below a marker (for easier removal)
  91. echo "$markerstart" >> /tmp/hosts-temp
  92. echo "$adlist" >> /tmp/hosts-temp
  93. echo "$markerend" >> /tmp/hosts-temp
  94. else
  95. Xdialog --title "$title $appver" --msgbox "$(gettext 'No ad lists selected. Ad blocking will be disabled.')" 0 0
  96. fi
  97. # replace the original with the cleaned version
  98. mv "/tmp/hosts-temp" "/etc/hosts"
  99. }
  100. success () {
  101. # tell user
  102. /usr/lib/gtkdialog/box_ok "$title" complete "$(gettext '<b>Success - your settings have been changed.</b>
  103. Your hosts file has been updated.
  104. Restart your browser to see the changes.')"
  105. }
  106. msg_help () {
  107. echo "$title $(gettext "adds stuff to your '/etc/hosts' file, so that many advertising servers and websites will not be able to connect to this PC.
  108. Leave your cursor over a service to see a short description. You can choose one service or combine multiple services for more advert protection.
  109. Blocking ad servers protects your privacy, saves you bandwidth, greatly improves web-browsing speeds and makes the internet much less annoying in general.")" > /tmp/box_help
  110. /usr/lib/gtkdialog/box_help "$title" &
  111. }
  112. export -f msg_help
  113. export PupAdvertBlocker='<window title="'$title' '$appver'" icon-name="gtk-execute">
  114. <vbox space-expand="true" space-fill="true">
  115. '"`/usr/lib/gtkdialog/xml_info scale dialog_stop.svg 60 "<b>$title</b>" "$(gettext "Block online ads in all browsers with this simple tool")"`"'
  116. <frame '$(gettext 'Choose your preferred service(s) or select them all for maximum ad blocking')'>
  117. <vbox>
  118. <checkbox tooltip-text="'$(gettext 'Blocks many known malware sites and unsafe adult networks')'">
  119. <label>Mvps.org</label>
  120. <variable>mvps</variable>
  121. <default>false</default>
  122. </checkbox>
  123. <checkbox tooltip-text="'$(gettext 'A large, fairly comprehensive list of many known ad servers')'">
  124. <label>Sysctl.org</label>
  125. <variable>sysctl</variable>
  126. <default>false</default>
  127. </checkbox>
  128. <checkbox tooltip-text="'$(gettext 'A small and effective list of popular ad servers')'">
  129. <label>Yoyo.org</label>
  130. <variable>yoyo</variable>
  131. <default>false</default>
  132. </checkbox>
  133. <text><label>""</label></text>
  134. <hbox border-width="7" space-expand="true" space-fill="true">
  135. <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"'$(gettext "<b>Start blocker</b>
  136. Download and block the latest list of known advertising servers. Click with nothing selected to remove all blocking")'"</label></text>
  137. <vbox space-expand="false" space-fill="false">
  138. <button>
  139. '"`/usr/lib/gtkdialog/xml_button-icon execute.svg big`"'
  140. <action>download_adlist</action>
  141. <action>clean_adlist</action>
  142. <action>append_adlist</action>
  143. <action>cleanup</action>
  144. <action>success</action>
  145. </button>
  146. </vbox>
  147. </hbox>
  148. <hbox border-width="7" space-expand="true" space-fill="true">
  149. <text xalign="0" use-markup="true" space-expand="true" space-fill="true"><label>"'$(gettext "<b>Edit host list</b>
  150. Manually edit your hosts file in a text editor, adding or removing any entries you like.")'"</label></text>
  151. <vbox space-expand="false" space-fill="false">
  152. <button>
  153. '"`/usr/lib/gtkdialog/xml_button-icon edit.svg big`"'
  154. <action>defaulttexteditor /etc/hosts &</action>
  155. </button>
  156. </vbox>
  157. </hbox>
  158. </vbox>
  159. </frame>
  160. <hbox space-expand="false" space-fill="false">
  161. <hbox space-expand="false" space-fill="false">
  162. <button>
  163. <label>'$(gettext "Help")'</label>
  164. '"`/usr/lib/gtkdialog/xml_button-icon help`"'
  165. <action>msg_help</action>
  166. </button>
  167. </hbox>
  168. <text space-expand="true" space-fill="true"><label>""</label></text>
  169. <button space-expand="false" space-fill="false">
  170. <label>'$(gettext "Quit")'</label>
  171. '"`/usr/lib/gtkdialog/xml_button-icon quit`"'
  172. <action type="exit">EXIT_NOW</action>
  173. </button>
  174. </hbox>
  175. </vbox>
  176. </window>'
  177. # cleanup before start
  178. cleanup
  179. # run the program
  180. . /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
  181. gtkdialog -p PupAdvertBlocker