autohosts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/bin/bash
  2. VERSION=1.3.0
  3. # get the non-root user, so we can add the custom filters for them
  4. REGUSER=$(last | grep "logged in" | grep -o '^\S*')
  5. HOME_USER="/home/$REGUSER/autohosts/"
  6. # initialize reusable stuff, to make the terminal results prettier
  7. function croncheck() {
  8. crontab -l | grep -q 'autohosts' && EXIST=1 || EXIST=0
  9. }
  10. function green() {
  11. echo -e "\e[32m$1\e[0m"
  12. }
  13. function red() {
  14. echo -e "\e[0;31m$1\e[0m"
  15. }
  16. function brown() {
  17. echo -e "\e[0;33m$1\e[0m"
  18. }
  19. function purple() {
  20. echo -e "\e[0;35m$1\e[0m"
  21. }
  22. if [[ $EUID -ne 0 ]]; then
  23. red "This script must be ran as root, or with sudo privileges."
  24. exit 1
  25. fi
  26. ### setup ###
  27. # if this is the first run, move to appropriate directory and set a config file
  28. if [ ! -e /etc/autohosts.conf ];
  29. then
  30. # move the script to /usr/local/bin so it can be executed as an alias
  31. green "Creating an alias for autohosts.."
  32. cp autohosts /usr/local/bin/ && chmod +x /usr/local/bin/autohosts
  33. # migrate custom filters file to the user's home directory
  34. green "Creating custom filters directory at $REGUSER"
  35. mkdir "$HOME_USER" && cp custom_filters "$HOME_USER" && chown "$REGUSER:$REGUSER" -R "$HOME_USER"
  36. # double-check to ensure we don't have a conflicting cron
  37. croncheck
  38. if [ "$EXIST" == 0 ];
  39. then
  40. # if no cronjob exists, create one
  41. crontab -l | { cat; echo "22 19 * * 0 /usr/local/bin/autohosts > /dev/null 2>&1"; } | crontab -
  42. croncheck
  43. if [ "$EXIST" == 1 ];
  44. then
  45. green "Cronjob created..."
  46. else
  47. red "There was a problem creating the cronjob, you might have to create it manually!"
  48. fi
  49. fi
  50. # set a var that is later used to see if the application has been configured already
  51. echo FIRSTRUN=0 > /etc/autohosts.conf
  52. echo VERSION=$VERSION >> /etc/autohosts.conf
  53. purple "Setup is complete!"
  54. fi
  55. ### end setup ###
  56. source /etc/autohosts.conf
  57. purple "== UPDATING =="
  58. green "Obtaining hosts file from Steven Black's repo and LocalFigurez Firefox Telemetry filter..."
  59. # check os type
  60. if [[ "$OSTYPE" != "darwin"* ]];
  61. then
  62. # not a mac, use wget
  63. wget https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts -O /etc/hosts
  64. # include LocalFigurez Firefox snooping list
  65. wget https://raw.githubusercontent.com/angela-d/autohosts/master/firefox-includes -O ->> /etc/hosts
  66. else
  67. # mac.. use curl, instead
  68. curl -O https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts > /etc/hosts
  69. # append LocalFigurez Firefox snooping list
  70. curl https://raw.githubusercontent.com/angela-d/autohosts/master/firefox-includes >> /etc/hosts
  71. fi
  72. # check for and include any custom filters
  73. if [ -e "$HOME_USER"custom_filters ];
  74. then
  75. green "Processing custom filters..."
  76. cat "$HOME_USER"'custom_filters' >> /etc/hosts
  77. else
  78. brown "No custom filters found at home_user, skipping..."
  79. fi
  80. # if this is a mac, refresh the dns cache
  81. if [[ "$OSTYPE" == "darwin"* ]];
  82. then
  83. killall -HUP mDNSResponder
  84. fi
  85. # devdom: https://notabug.org/angela/devdom
  86. green "Checking if devdom is installed (will execute devdom hosts update, if so)"
  87. if [ -e /usr/bin/devdom ];
  88. then
  89. devdom hosts
  90. green "devdom hosts have been appended..."
  91. else
  92. brown "Devdom is not installed; skipping."
  93. fi
  94. purple "Hosts update complete!"