Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # VARIABLES
- FILE_LENGTH=12000
- FILE_EXTENSION=".editable"
- TMP_FILE="/tmp/hosts.txt"
- # 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):
- REMOTE_HOSTS="https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"
- NATIVE_HOSTS="/etc/hosts"
- ANDROID_DIR_1="/system/etc"
- ANDROID_HOSTS_1="/system/etc/hosts"
- ANDROID_DIR_2="/opt/alien/system/etc"
- ANDROID_HOSTS_2="/opt/alien/system/etc/hosts"
- # FUNCTIONS
- # 127.0.0.1 localhost
- function recreate_file {
- if [[ $1 == *"$ANDROID_HOSTS_1"* ]]; then
- echo "127.0.0.1 localhost" > $1
- else
- echo "127.0.0.1 localhost.localdomain localhost" > $1
- echo "::1 localhost6.localdomain6 localhost6" >> $1
- fi
- }
- function check_mvps {
- if grep -q "MVPS" $1; then
- file_lines=`wc -l $1 | cut -f1 -d' '`
- if [ $file_lines -gt $FILE_LENGTH ]; then
- return 0
- else
- return 1
- fi
- else
- return 1
- fi
- }
- function update_file {
- # Check if the new file exists. If not, download it.
- if [ ! -f $TMP_FILE ]; then
- # Download hosts file
- curl -o $TMP_FILE $REMOTE_HOSTS
- fi
- # Check if the /etc/hosts file exists - just in case a removal of other packages deleted it.
- # If it does not exist, recreate it.
- if [ ! -f $1 ]; then
- recreate_file $1
- fi
- # Check if our editable/backup file exists. If not
- if [ ! -f $1$FILE_EXTENSION ]; then
- # Check if it is not the MVPS hosts file. If it is, recreate hosts from scratch.
- cp $1 $1$FILE_EXTENSION
- fi
- # Check if the hosts file is really the one from mvps.org. If not (Jolla update), treat it as the default one.
- if ! check_mvps $1; then
- cp -f $1 $1$FILE_EXTENSION
- fi
- # Check if we have not copied the MVPS file into the /etc/hosts.editable file
- # If so, recreate the file from scratch.
- if check_mvps $1$FILE_EXTENSION; then
- recreate_file $1$FILE_EXTENSION
- fi
- # Check if the downloaded file is the MVPS file and do not act if not.
- if check_mvps $TMP_FILE; then
- cp -f $1$FILE_EXTENSION $1
- # Add divider to the file
- echo "" >> $1
- echo "# Autogenerated by the sailfishos-hosts-adblock app with the \"unified\" hosts blocking list from <https://github.com/StevenBlack/hosts>" >> $1
- echo "" >> $1
- # Sanitize and add to hosts
- grep -e '^0.0.0.0\|^#' $TMP_FILE >> $1
- fi
- }
- # Native hosts
- echo "Updating native hosts 1/2"
- update_file $NATIVE_HOSTS
- # Android hosts (only if the folder exists)
- # 1st file
- if [ -d $ANDROID_DIR_1 ]; then
- echo "Updating Android hosts 1/2"
- update_file $ANDROID_HOSTS_1
- fi
- # 2nd file
- if [ -d $ANDROID_DIR_2 ]; then
- echo "Updating Android hosts 2/2"
- update_file $ANDROID_HOSTS_2
- fi
- rm -f $TMP_FILE
Add Comment
Please, Sign In to add comment