forked from nextcloud/vm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeoblock.sh
198 lines (174 loc) Β· 5.69 KB
/
geoblock.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/bin/bash
# T&M Hansson IT AB Β© - 2022, https://www.hanssonit.se/
# Copyright Β© 2021 Simon Lindner (https://github.com/szaimen)
true
SCRIPT_NAME="GeoBlock"
SCRIPT_EXPLAINER="This script lets you restrict access to your webserver, only allowing the countries you choose.\n
Attention!
Geoblock can break the certificate renewal via \"Let's encrypt!\" if done too strict!
If you have problems with \"Let's encrypt!\", please uninstall geoblock first to see if that fixes those issues!"
# shellcheck source=lib.sh
source /var/scripts/fetch_lib.sh
# Check for errors + debug code and abort if something isn't right
# 1 = ON
# 0 = OFF
DEBUG=0
debug_mode
# Must be root
root_check
# Check if it is already configured
if ! grep -q "^#Geoip-block" /etc/apache2/apache2.conf
then
# Ask for installing
install_popup "$SCRIPT_NAME"
else
# Ask for removal or reinstallation
reinstall_remove_menu "$SCRIPT_NAME"
# Removal
find /var/scripts -type f -regex \
"$SCRIPTS/202[0-9]-[01][0-9]-Maxmind-Country-IPv[46]\.dat" -delete
if is_this_installed libapache2-mod-geoip
then
a2dismod geoip
apt-get purge libapache2-mod-geoip -y
fi
apt-get autoremove -y
sed -i "/^#Geoip-block-start/,/^#Geoip-block-end/d" /etc/apache2/apache2.conf
check_command systemctl restart apache2
# Show successful uninstall if applicable
removal_popup "$SCRIPT_NAME"
fi
# Install needed tools
install_if_not libapache2-mod-geoip
# Enable apache mod
check_command a2enmod geoip rewrite
check_command systemctl restart apache2
# Download newest dat files
find /var/scripts -type f -regex \
"$SCRIPTS/202[0-9]-[01][0-9]-Maxmind-Country-IPv[46]\.dat" -delete
get_newest_dat_files
# Restrict to countries and/or continents
choice=$(whiptail --title "$TITLE" --checklist \
"Do you want to restrict to countries and/or continents?
$CHECKLIST_GUIDE\n\n$RUN_LATER_GUIDE" "$WT_HEIGHT" "$WT_WIDTH" 4 \
"Countries" "" ON \
"Continents" "" ON 3>&1 1>&2 2>&3)
if [ -z "$choice" ]
then
exit 1
fi
# Countries
if [[ "$choice" = *"Countries"* ]]
then
# Download csv file
if ! curl_to_dir "https://dev.maxmind.com/csv-files/codes" "iso3166.csv" "$SCRIPTS"
then
msg_box "Could not download the iso3166.csv file.
Please report this to $ISSUES"
exit 1
fi
# Get country names
COUNTRY_NAMES=$(sed 's|.*,"||;s|"$||' "$SCRIPTS/iso3166.csv")
mapfile -t COUNTRY_NAMES <<< "$COUNTRY_NAMES"
# Get country codes
COUNTRY_CODES=$(sed 's|,.*||' "$SCRIPTS/iso3166.csv")
mapfile -t COUNTRY_CODES <<< "$COUNTRY_CODES"
# Remove the csv file since no longer needed
check_command rm "$SCRIPTS/iso3166.csv"
# Check if both arrays match
if [ "${#COUNTRY_NAMES[@]}" != "${#COUNTRY_CODES[@]}" ]
then
msg_box "Somethings is wrong. The names length is not equal to the codes length.
Please report this to $ISSUES"
exit 1
fi
# Create checklist
args=(whiptail --title "$TITLE - $SUBTITLE" --separate-output --checklist \
"Please select all countries that shall have access to your server.
All countries that aren't selected will *not* have access to your server. \
To allow them you have to choose the specific continent.
$CHECKLIST_GUIDE\n\n$RUN_LATER_GUIDE" "$WT_HEIGHT" "$WT_WIDTH" 4)
count=0
while [ "$count" -lt "${#COUNTRY_NAMES[@]}" ]
do
args+=("${COUNTRY_CODES[$count]}" "${COUNTRY_NAMES[$count]}" OFF)
((count++))
done
# Let the user choose the countries
selected_options=$("${args[@]}" 3>&1 1>&2 2>&3)
if [ -z "$selected_options" ]
then
unset selected_options
fi
fi
# Continents
if [[ "$choice" = *"Continents"* ]]
then
# Restrict to continents
choice=$(whiptail --title "$TITLE" --separate-output --checklist \
"Please choose all continents that shall have access to your server.
All countries on not selected continents will not have access to your server \
if you haven't explicitly chosen them in the countries menu before.
$CHECKLIST_GUIDE\n\n$RUN_LATER_GUIDE" "$WT_HEIGHT" "$WT_WIDTH" 4 \
"AF" "Africa" OFF \
"AN" "Antarctica" OFF \
"AS" "Asia" OFF \
"EU" "Europe" OFF \
"NA" "North America" OFF \
"OC" "Oceania" OFF \
"SA" "South America" OFF 3>&1 1>&2 2>&3)
if [ -z "$choice" ]
then
unset choice
fi
else
unset choice
fi
# Exit if nothing chosen
if [ -z "$selected_options" ] && [ -z "$choice" ]
then
exit 1
fi
# Convert to array
if [ -n "$selected_options" ]
then
mapfile -t selected_options <<< "$selected_options"
fi
if [ -n "$choice" ]
then
mapfile -t choice <<< "$choice"
fi
GEOIP_CONF="#Geoip-block-start - Please don't remove or change this line
<IfModule mod_geoip.c>
GeoIPEnable On
GeoIPDBFile /usr/share/GeoIP/GeoIP.dat
GeoIPDBFile /usr/share/GeoIP/GeoIPv6.dat
</IfModule>
<Location />\n"
for continent in "${choice[@]}"
do
GEOIP_CONF+=" SetEnvIf GEOIP_CONTINENT_CODE $continent AllowCountryOrContinent\n"
GEOIP_CONF+=" SetEnvIf GEOIP_CONTINENT_CODE_V6 $continent AllowCountryOrContinent\n"
done
for country in "${selected_options[@]}"
do
GEOIP_CONF+=" SetEnvIf GEOIP_COUNTRY_CODE $country AllowCountryOrContinent\n"
GEOIP_CONF+=" SetEnvIf GEOIP_COUNTRY_CODE_V6 $country AllowCountryOrContinent\n"
done
GEOIP_CONF+=" Allow from env=AllowCountryOrContinent
Allow from 127.0.0.1/8
Allow from 192.168.0.0/16
Allow from 172.16.0.0/12
Allow from 10.0.0.0/8
Allow from scan.nextcloud.com
# Allow scans from observatory.mozilla.org:
Allow from 63.245.208.0/24
Order Deny,Allow
Deny from all
</Location>
#Geoip-block-end - Please don't remove or change this line"
# Write everything to the file
echo -e "$GEOIP_CONF" >> /etc/apache2/apache2.conf
check_command systemctl restart apache2
msg_box "GeoBlock was successfully configured"
exit