Guest User

update_hosts_adblock.patched

a guest
Nov 6th, 2016
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # VARIABLES
  4. FILE_LENGTH=12000
  5. FILE_EXTENSION=".editable"
  6. TMP_FILE="/tmp/hosts.txt"
  7. # Selecting the "unified" hosts blocking list from <https://github.com/StevenBlack/hosts> (picking the basic list, as the extended ones tend to block too eagerly):
  8. REMOTE_HOSTS="https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"
  9. NATIVE_HOSTS="/etc/hosts"
  10. ANDROID_DIR_1="/system/etc"
  11. ANDROID_HOSTS_1="/system/etc/hosts"
  12. ANDROID_DIR_2="/opt/alien/system/etc"
  13. ANDROID_HOSTS_2="/opt/alien/system/etc/hosts"
  14.  
  15. # FUNCTIONS
  16. # 127.0.0.1 localhost
  17.  
  18. function recreate_file {
  19. if [[ $1 == *"$ANDROID_HOSTS_1"* ]]; then
  20. echo "127.0.0.1 localhost" > $1
  21. else
  22. echo "127.0.0.1 localhost.localdomain localhost" > $1
  23. echo "::1 localhost6.localdomain6 localhost6" >> $1
  24. fi
  25. }
  26.  
  27. function check_mvps {
  28. if grep -q "MVPS" $1; then
  29. file_lines=`wc -l $1 | cut -f1 -d' '`
  30. if [ $file_lines -gt $FILE_LENGTH ]; then
  31. return 0
  32. else
  33. return 1
  34. fi
  35. else
  36. return 1
  37. fi
  38. }
  39.  
  40.  
  41. function update_file {
  42. # Check if the new file exists. If not, download it.
  43. if [ ! -f $TMP_FILE ]; then
  44. # Download hosts file
  45. curl -o $TMP_FILE $REMOTE_HOSTS
  46. fi
  47.  
  48. # Check if the /etc/hosts file exists - just in case a removal of other packages deleted it.
  49. # If it does not exist, recreate it.
  50. if [ ! -f $1 ]; then
  51. recreate_file $1
  52. fi
  53.  
  54. # Check if our editable/backup file exists. If not
  55. if [ ! -f $1$FILE_EXTENSION ]; then
  56. # Check if it is not the MVPS hosts file. If it is, recreate hosts from scratch.
  57. cp $1 $1$FILE_EXTENSION
  58. fi
  59.  
  60. # Check if the hosts file is really the one from mvps.org. If not (Jolla update), treat it as the default one.
  61. if ! check_mvps $1; then
  62. cp -f $1 $1$FILE_EXTENSION
  63. fi
  64.  
  65. # Check if we have not copied the MVPS file into the /etc/hosts.editable file
  66. # If so, recreate the file from scratch.
  67. if check_mvps $1$FILE_EXTENSION; then
  68. recreate_file $1$FILE_EXTENSION
  69. fi
  70.  
  71.  
  72.  
  73. # Check if the downloaded file is the MVPS file and do not act if not.
  74. if check_mvps $TMP_FILE; then
  75. cp -f $1$FILE_EXTENSION $1
  76.  
  77. # Add divider to the file
  78. echo "" >> $1
  79. echo "# Autogenerated by the sailfishos-hosts-adblock app with the \"unified\" hosts blocking list from <https://github.com/StevenBlack/hosts>" >> $1
  80. echo "" >> $1
  81.  
  82. # Sanitize and add to hosts
  83. grep -e '^0.0.0.0\|^#' $TMP_FILE >> $1
  84. fi
  85. }
  86.  
  87. # Native hosts
  88. echo "Updating native hosts 1/2"
  89. update_file $NATIVE_HOSTS
  90.  
  91. # Android hosts (only if the folder exists)
  92. # 1st file
  93. if [ -d $ANDROID_DIR_1 ]; then
  94. echo "Updating Android hosts 1/2"
  95. update_file $ANDROID_HOSTS_1
  96. fi
  97. # 2nd file
  98. if [ -d $ANDROID_DIR_2 ]; then
  99. echo "Updating Android hosts 2/2"
  100. update_file $ANDROID_HOSTS_2
  101. fi
  102.  
  103. rm -f $TMP_FILE
Add Comment
Please, Sign In to add comment