├── UNLICENSE ├── README.md └── block-all-google.sh /UNLICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # block-all-google.sh 2 | 3 | ## Overview 4 | This BASH (Linux) script simplifies the process of cutting Google out of your digital life. 5 | 6 | This will block all Google services - GMail, YouTube, Google Play, ads, tracking and so on, both good and bad. 7 | It's worth noting that any sites that depend on Google to serve their content most likely will break, or at a minimum not function properly. 8 | 9 | It works by adding all the downloaded Google IP ranges to the routing table, which get routed to localhost and thus stays local to your machine and doesn't reach any Google servers. 10 | 11 | Tested on Ubuntu 18.04 only. 12 | 13 | Contacts third-party network services to retrieve proxies and IP ranges. 14 | 15 | If you don't agree to their respective terms of service (ToS), don't use the script or modify it to suit your needs. 16 | 17 | ## Using the script 18 | Using the terminal, navigate to the directory where the script is; e.g; 19 | ``` 20 | cd $HOME/Downloads 21 | ``` 22 | 23 | Make the script executable 24 | ``` 25 | chmod +x block-all-google.sh 26 | ``` 27 | 28 | The script needs to be run as root to be able to modify the routing table, and takes an argument to either block or unblock Google. 29 | To avoid issues where Google has already been blocked, it will attempt to download the IP ranges through proxies when unreachable. 30 | 31 | To block Google; 32 | ``` 33 | sudo ./block-all-google.sh -b 34 | ``` 35 | 36 | To unblock Google; 37 | ``` 38 | sudo ./block-all-google.sh -u 39 | ``` 40 | 41 | ## Scheduling 42 | Regular updating of the Google IP range list can be scheduled through crontab. 43 | 44 | Open the root crontab; 45 | ``` 46 | sudo crontab -e 47 | ``` 48 | 49 | Add the crontab, e.g; 50 | ``` 51 | # block-all-google 52 | 00 18 * * 7 "$HOME/Scripts/block-all-google.sh -b" 53 | ``` 54 | 55 | This example will run the script every Sunday at 6:00pm. 56 | 57 | To add logging; 58 | ``` 59 | # block-all-google 60 | 00 18 * * 7 "$HOME/Scripts/block-all-google.sh -b" >> /var/log/block-all-google.cron.log 2>&1 61 | ``` 62 | Save and exit. 63 | 64 | ## Improvements 65 | To contribute, or to report issues please use the [Issue Tracker](https://github.com/wesaphzt/block-all-google/issues/). 66 | 67 | ## Unlicense 68 | Unlicensed. 69 | 70 | See [UNLICENSE](https://github.com/wesaphzt/block-all-google/UNLICENSE) for more information. 71 | -------------------------------------------------------------------------------- /block-all-google.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #======================================== 3 | # Author: wesaphzt 4 | # Route all Google IP ranges to localhost 5 | # IPv4 only 6 | #======================================== 7 | 8 | ASN=AS15169 9 | ROUTETYPE='' 10 | HOST=ipinfo.io 11 | HOST_RANGE=https://$HOST/${ASN}\#blocks 12 | SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd)" 13 | RANGE_FILE=$SCRIPTDIR/$ASN 14 | HOSTPING=0 15 | PORT=8080 16 | PROXYSITE=https://api.getproxylist.com 17 | PROXYIP='' 18 | 19 | #-------------------------------------------------> 20 | function cmdStatus { 21 | "$@" 22 | local status=$? 23 | if [ $status -ne 0 ]; then 24 | echo "error" >&2 25 | fi 26 | return $status 27 | } 28 | #-------------------------------------------------> 29 | function randomProxy { 30 | # random proxy 31 | PROXYIP=$(curl $PROXYSITE/proxy?port[]=$PORT | grep '"ip":' | cut -f 4 -d '"') 32 | } 33 | #-------------------------------------------------> 34 | #======================================== 35 | # checks 36 | #======================================== 37 | #---------------------------------------- 38 | # arguments 39 | #---------------------------------------- 40 | usage() { echo "Usage: sudo $0 [-b] (block) [-u] (unblock)" 1>&2; exit 1; } 41 | 42 | while getopts "bu" option; do 43 | case "${option}" in 44 | b) 45 | ROUTETYPE="add" 46 | ;; 47 | u) 48 | ROUTETYPE="del" 49 | ;; 50 | *) 51 | usage 52 | ;; 53 | esac 54 | done 55 | shift $((OPTIND-1)) 56 | 57 | if [[ "$ROUTETYPE" == "add" ]]; then echo "script set to block"; elif [[ "$ROUTETYPE" == "del" ]]; then echo "script set to unblock"; else echo "error"; exit 1; fi 58 | 59 | #---------------------------------------- 60 | # check root 61 | #---------------------------------------- 62 | if (( $(id -u) != 0 )); then 63 | echo "not running as root, exit"; 64 | exit 1 65 | else 66 | echo "running as root"; 67 | fi 68 | #---------------------------------------- 69 | # test network conection 70 | #---------------------------------------- 71 | echo "check network interface connectivity" 72 | for intf in $(ls /sys/class/net/ | grep -v lo); 73 | do 74 | if [[ $(cat /sys/class/net/$intf/carrier) = 1 ]] &>/dev/null; then online=1; echo "interface $intf online"; fi 75 | done 76 | if ! [ $online ]; then echo "interface $intf offline, exit" >/dev/stderr; exit 1; fi 77 | #---------------------------------------- 78 | # test target host connection 79 | #---------------------------------------- 80 | echo "pinging $HOST" 81 | if ! ping -c3 $HOST &>/dev/null; then echo "$HOST ping fail, assume google blocked"; HOSTPING=1; else echo "$HOST ping success"; fi 82 | #---------------------------------------- 83 | # test google connection 84 | #---------------------------------------- 85 | echo "pinging google" 86 | if ! ping -c3 google.com &>/dev/null; then echo "google ping fail, assume google blocked"; HOSTPING=1; else echo "google ping success"; fi 87 | 88 | #======================================== 89 | # main 90 | #======================================== 91 | #---------------------------------------- 92 | # download ip ranges 93 | #---------------------------------------- 94 | echo "downloading ip ranges to $RANGE_FILE" 95 | 96 | # backup if file exists 97 | if [ -f $RANGE_FILE ]; then cp $RANGE_FILE $RANGE_FILE.bak; echo "file exists, backup created at $RANGE_FILE.bak"; fi 98 | 99 | # curl ip ranges 100 | if [[ $HOSTPING -eq 0 ]]; then 101 | cmdStatus curl -SsL $HOST_RANGE | grep "${ASN}/" | cut -f 2 -d '"' | cut -f 3-4 -d '/' | sed '/::/d' > $RANGE_FILE || exit; 102 | elif [[ $HOSTPING -eq 1 ]]; then 103 | # curl ip ranges through proxy 104 | echo "google offline, dl using proxy" 105 | COUNT=0 106 | until randomProxy && cmdStatus curl -SsLx $PROXYIP:$PORT $HOST_RANGE | grep "${ASN}/" | cut -f 2 -d '"' | cut -f 3-4 -d '/' | sed '/::/d' > $RANGE_FILE; do 107 | if [ $COUNT -eq 10 ]; then echo "$COUNT proxies failed, exit"; exit 10; fi 108 | sleep 1; ((COUNT++)) 109 | done 110 | else 111 | echo "error"; exit 1; 112 | fi 113 | #---------------------------------------- 114 | # change routing table 115 | #---------------------------------------- 116 | echo "making changes to routing table" 117 | COUNT=0 118 | while read line; do 119 | cmdStatus route $ROUTETYPE -net $line gw 127.0.0.1 lo &>/dev/null; 120 | # counter 121 | if [ $? -eq 0 ]; then ((COUNT++)); fi 122 | done < $RANGE_FILE 123 | echo "$COUNT ip ranges processed" 124 | 125 | #======================================== 126 | # finish 127 | #======================================== 128 | #---------------------------------------- 129 | # test google connection 130 | #---------------------------------------- 131 | echo "ping test google again" 132 | if ! ping -c3 google.com &>/dev/null; then HOSTPING=1; else HOSTPING=0; fi 133 | 134 | if [ "$ROUTETYPE" == "add" ]; then 135 | if [ $HOSTPING -eq 1 ]; then echo "success (google ping failed)"; fi 136 | if [ $HOSTPING -eq 0 ]; then echo "fail (google ping successful)"; fi 137 | elif [ "$ROUTETYPE" == "del" ]; then 138 | if [ $HOSTPING -eq 1 ]; then echo "fail (google ping failed)"; fi 139 | if [ $HOSTPING -eq 0 ]; then echo "success (google ping successful)"; fi 140 | else 141 | echo "error" 142 | fi 143 | 144 | echo "//done" 145 | --------------------------------------------------------------------------------