├── LICENSE ├── README.md └── block-ip.sh /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Opiran 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### BLOCK OUTGOING TRAFFIC BY COUNTRY 2 | ### این اسکریپت تمام ترافیک خروجی به IP مقصد ایران china russia شامل اکثر دیتاسنترهای ایران را مسدود می کند. 3 | 4 | 5 | ❗ UPDATE: 6 | - ADDED CHINA 7 | - ADDED RUSSIA 8 | 9 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 10 | 11 | ❗ Requirements 12 | 13 | - run with root previllage 14 | - no any requirements, they will be install 15 | 16 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 17 | 18 | ❗❗ INSTALLATION 19 | ``` 20 | bash <(curl -s https://raw.githubusercontent.com/opiran-club/block-iran-ip/main/block-ip.sh --ipv4) 21 | ``` 22 | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 23 | 24 | #### Written by OPIran 25 | 26 | TELEGRAM CHANNEL: https://t.me/OPIranv2rayproxy 27 | 28 | TELEGRAM GROUP: https://t.me/OPIranCluB 29 | -------------------------------------------------------------------------------- /block-ip.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | CYAN="\e[96m" 4 | GREEN="\e[92m" 5 | YELLOW="\e[93m" 6 | RED="\e[91m" 7 | BLUE="\e[94m " 8 | MAGENTA="\e[95m" 9 | NC="\e[0m" 10 | BOLD=$(tput bold) 11 | 12 | logo=$(cat << "EOF" 13 | ______ _______ __ _______ __ _____ ___ 14 | / " \ | __ "\ |" \ /" \ /""\ (\" \|" \ 15 | // ____ \ (. |__) :)|| | |: | / \ |.\\ \ | 16 | / / ) :)|: ____/ |: | |_____/ ) /' /\ \ |: \. \\ | 17 | (: (____/ // (| / |. | // / // __' \ |. \ \. | 18 | \ / /|__/ \ /\ |\ |: __ \ / / \\ \| \ \ | 19 | \"_____/ (_______) (__\_|_)|__| \___)(___/ \___)\___|\____\) 20 | EOF 21 | ) 22 | 23 | logo() { 24 | echo -e "\033[1;34m$logo\033[0m" 25 | } 26 | 27 | # Function to block IPs from a specific country 28 | block_country_ips() { 29 | country_code="$1" 30 | echo -e "\e[33mBlocking IPs from $country_code\e[0m" 31 | curl -sSL "https://www.ipdeny.com/ipblocks/data/countries/$country_code.zone" | awk '{print "sudo ufw deny out from any to " $1}' | bash 32 | } 33 | 34 | block_incoming_country_ips() { 35 | country_code="$1" 36 | echo -e "\e[33mBlocking incoming traffic from $country_code\e[0m" 37 | curl -sSL "https://www.ipdeny.com/ipblocks/data/countries/$country_code.zone" | awk '{print "sudo ufw deny in from " $1 " to any"}' | bash 38 | } 39 | 40 | flush() { 41 | echo -e "\e[33mUninstalling and flushing UFW rules\e[0m" 42 | sudo ufw reset 43 | sudo ufw disable 44 | 45 | echo -e "\e[32mUFW rules have been uninstalled and flushed, and also ufw is disabled. \e[0m" 46 | } 47 | 48 | print_table() { 49 | tg_title="https://t.me/OPIranCluB" 50 | yt_title="youtube.com/@opiran-inistitute" 51 | clear 52 | logo 53 | echo -e "\e[93m╔═══════════════════════════════════════════════╗\e[0m" 54 | echo -e "\e[93m║ \e[95mBLOCK IP MENU \e[93m║\e[0m" 55 | echo -e "\e[93m╠═══════════════════════════════════════════════╣\e[0m" 56 | echo "" 57 | echo -e "${BLUE} ${tg_title} ${NC}" 58 | echo -e "${BLUE} ${yt_title} ${NC}" 59 | echo "" 60 | } 61 | 62 | # Install required packages 63 | apt update 64 | apt install ufw libapache2-mod-geoip geoip-database -y 65 | a2enmod geoip 66 | apt install geoip-bin -y 67 | 68 | # Open desired ports 69 | ufw allow ssh 70 | ufw allow http 71 | ufw allow https 72 | 73 | clear 74 | # Print the question and response table 75 | print_table 76 | 77 | # Ask the user which country IPs to block 78 | echo -e "\e[31m!!\e[0m\e[32m Its the common script to block outgoing traffic base on" 79 | echo -e "country with UFW so after that allow your required ports\e[0m" 80 | printf "\e[93m+-----------------------------------------------+\e[0m\n" 81 | 82 | echo -e "\e[33mWhich country IPs do you want to block?\e[0m" 83 | echo "" 84 | echo -e "\e[31m1)\e[0m \e[36mIran\e[0m" 85 | echo -e "\e[31m2)\e[0m \e[36mChina\e[0m" 86 | echo -e "\e[31m3)\e[0m \e[36mRussia\e[0m" 87 | echo "" 88 | echo -e "\e[31m4)\e[0m \e[36mBlock incoming traffic from specific locations\e[0m" 89 | echo "" 90 | echo -e "\e[31m5)\e[0m \e[36mUninstall and flush rules\e[0m" 91 | echo "" 92 | printf "\e[93m+-----------------------------------------------+\e[0m\n" 93 | 94 | read -p "Enter the number of your choice (1-5): " choice 95 | 96 | 97 | case "$choice" in 98 | 1) 99 | block_country_ips "ir" 100 | ;; 101 | 2) 102 | block_country_ips "cn" 103 | ;; 104 | 3) 105 | block_country_ips "ru" 106 | ;; 107 | 4) 108 | clear 109 | # Print the question and response table 110 | print_table 111 | 112 | # Ask the user which country IPs to block 113 | echo -e "\e[31m!!\e[0m\e[32m Its the common script to block outgoing traffic base on" 114 | echo -e "country with UFW so after that allow your required ports\e[0m" 115 | printf "\e[93m+-----------------------------------------------+\e[0m\n" 116 | echo "" 117 | echo -e "\e[33mWhich country IPs do you want to block for incoming traffic?\e[0m" 118 | echo "" 119 | echo -e "\e[31m1)\e[0m \e[36mIran\e[0m" 120 | echo -e "\e[31m2)\e[0m \e[36mChina\e[0m" 121 | echo -e "\e[31m3)\e[0m \e[36mRussia\e[0m" 122 | echo "" 123 | echo -e "\e[31m3)\e[0m \e[36mBack\e[0m" 124 | echo "" 125 | printf "\e[93m+-----------------------------------------------+\e[0m\n" 126 | 127 | read -p "Enter the number of your choice (1-3): " choice 128 | 129 | case "$choice" in 130 | 1) 131 | block_incoming_country_ips "ir" 132 | ;; 133 | 2) 134 | block_incoming_country_ips "cn" 135 | ;; 136 | 3) 137 | block_incoming_country_ips "ru" 138 | ;; 139 | 4) 140 | exit 1 141 | ;; 142 | *) 143 | echo "\e[31mInvalid choice. Exiting...\e[0m" 144 | exit 1 145 | ;; 146 | esac 147 | ;; 148 | 5) 149 | flush 150 | ;; 151 | *) 152 | echo "\e[31mInvalid choice. Exiting...\e[0m" 153 | exit 1 154 | ;; 155 | esac 156 | 157 | # Print the question and response table again 158 | print_table 159 | 160 | # Ask the user whether to enable the firewall or not 161 | echo -ne "\e[33mDo you want to enable the firewall\e[0m \e[31m(without enabling ufw the function won't work correctly)\e[0m? \e[33m(yes/no):\e[0m " 162 | read enable_firewall 163 | printf "\e[93m+-----------------------------------------------+\e[0m\n" 164 | 165 | if [[ "$enable_firewall" =~ ^(Y|y|YES|yes)$ ]]; then 166 | ufw enable 167 | else 168 | echo -e "\e[31mFirewall remains disabled.\e[0m" 169 | fi 170 | 171 | # Set up a cronjob to update the zone every 1 month 172 | cronjob_command="curl -sSL https://www.ipdeny.com/ipblocks/data/countries/$country_code.zone | awk '{print \"sudo ufw deny out from any to \" \$1}' | bash" 173 | (crontab -l ; echo "0 0 1 * * $cronjob_command") | crontab - 174 | --------------------------------------------------------------------------------