|
| 1 | +#!/system/bin/sh |
| 2 | + |
| 3 | +# Function to add a host entry |
| 4 | +add_host() { |
| 5 | + if [ -z "$1" ]; then |
| 6 | + echo "Usage: $0 add <hostname>" |
| 7 | + exit 1 |
| 8 | + fi |
| 9 | + |
| 10 | + host="$1" |
| 11 | + hosts_file="/etc/hosts" |
| 12 | + |
| 13 | + if grep -qxF "0.0.0.0 $host" "$hosts_file"; then |
| 14 | + echo "Entry for $host already exists in $hosts_file" |
| 15 | + else |
| 16 | + if echo "0.0.0.0 $host" >> "$hosts_file"; then |
| 17 | + echo "Entry for $host added to $hosts_file" |
| 18 | + else |
| 19 | + echo "Error: Failed to add entry to $hosts_file" |
| 20 | + exit 1 |
| 21 | + fi |
| 22 | + fi |
| 23 | +} |
| 24 | + |
| 25 | +# Function to remove a host entry |
| 26 | +remove_host() { |
| 27 | + if [ -z "$1" ]; then |
| 28 | + echo "Usage: $0 remove <hostname>" |
| 29 | + exit 1 |
| 30 | + fi |
| 31 | + |
| 32 | + word="$1" |
| 33 | + hosts_file="/etc/hosts" |
| 34 | + temp_file="$(mktemp)" |
| 35 | + |
| 36 | + if grep -qF "$word" "$hosts_file"; then |
| 37 | + if grep -vF "$word" "$hosts_file" > "$temp_file"; then |
| 38 | + if mv "$temp_file" "$hosts_file"; then |
| 39 | + echo "Entry for $word removed from $hosts_file" |
| 40 | + else |
| 41 | + echo "Error: Failed to update $hosts_file" |
| 42 | + rm "$temp_file" |
| 43 | + exit 1 |
| 44 | + fi |
| 45 | + else |
| 46 | + echo "Error: Failed to create temporary file" |
| 47 | + rm "$temp_file" |
| 48 | + exit 1 |
| 49 | + fi |
| 50 | + else |
| 51 | + echo "No entry found for $word" |
| 52 | + fi |
| 53 | +} |
| 54 | + |
| 55 | +# Function to update the hosts file |
| 56 | +update_hosts() { |
| 57 | + PS3='Choose your preferred option: ' |
| 58 | + options=("Ultimate Hosts" "Daily Hosts" "Quit") |
| 59 | + |
| 60 | + update_hosts_file() { |
| 61 | + local url="$1" |
| 62 | + if curl -sf "$url" -o /etc/hosts; then |
| 63 | + echo "Successfully updated" |
| 64 | + else |
| 65 | + echo "Failed to update. Please check your internet connection or try again later." |
| 66 | + fi |
| 67 | + } |
| 68 | + |
| 69 | + select opt in "${options[@]}"; do |
| 70 | + case $opt in |
| 71 | + "Ultimate Hosts") |
| 72 | + echo "Unified hosts + fakenews + gambling + porn + social" |
| 73 | + update_hosts_file "https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-gambling-porn-social/hosts" |
| 74 | + break |
| 75 | + ;; |
| 76 | + "Daily Hosts") |
| 77 | + echo "Unified hosts = (adware + malware)" |
| 78 | + update_hosts_file "https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts" |
| 79 | + break |
| 80 | + ;; |
| 81 | + "Quit") |
| 82 | + echo "User requested exit" |
| 83 | + break |
| 84 | + ;; |
| 85 | + *) |
| 86 | + echo "Invalid option $REPLY" |
| 87 | + ;; |
| 88 | + esac |
| 89 | + done |
| 90 | +} |
| 91 | + |
| 92 | +# Main script |
| 93 | +if [ $# -lt 1 ]; then |
| 94 | + echo "Usage: $0 {add|remove|update} [hostname]" |
| 95 | + exit 1 |
| 96 | +fi |
| 97 | + |
| 98 | +case $1 in |
| 99 | + add) |
| 100 | + shift |
| 101 | + add_host "$@" |
| 102 | + ;; |
| 103 | + remove) |
| 104 | + shift |
| 105 | + remove_host "$@" |
| 106 | + ;; |
| 107 | + update) |
| 108 | + update_hosts |
| 109 | + ;; |
| 110 | + *) |
| 111 | + echo "Usage: $0 {add|remove|update} [hostname]" |
| 112 | + exit 1 |
| 113 | + ;; |
| 114 | +esac |
0 commit comments