@@ -11,6 +11,7 @@ Geoblock can break the certificate renewal via \"Let's encrypt!\" if done too st
1111If you have problems with \" Let's encrypt!\" , please uninstall geoblock first to see if that fixes those issues!"
1212# shellcheck source=lib.sh
1313source /var/scripts/fetch_lib.sh
14+ # source <(curl -sL https://raw.githubusercontent.com/nextcloud/vm/geoblock-v2/lib.sh) # TODO, remove after testing
1415
1516# Check for errors + debug code and abort if something isn't right
1617# 1 = ON
@@ -22,41 +23,89 @@ debug_mode
2223root_check
2324
2425# Check if it is already configured
25- if ! grep -q " ^#Geoip-block " /etc/apache2/apache2.conf
26+ if [ ! -f " $GEOBLOCK_MOD_CONF " ] || [ ! -f " $GEOBLOCK_MOD " ]
2627then
2728 # Ask for installing
2829 install_popup " $SCRIPT_NAME "
2930else
3031 # Ask for removal or reinstallation
3132 reinstall_remove_menu " $SCRIPT_NAME "
32- # Removal
33+ # Remove Apache mod config
34+ rm -f " $GEOBLOCK_MOD_CONF "
35+ # Remove old database files
3336 find /var/scripts -type f -regex \
3437" $SCRIPTS /202[0-9]-[01][0-9]-Maxmind-Country-IPv[46]\.dat" -delete
38+ # Remove Apache2 mod
39+ if [ -f " $GEOBLOCK_MOD " ]
40+ then
41+ a2dismod maxminddb
42+ rm -f " $GEOBLOCK_MOD "
43+ rm -f /usr/lib/apache2/modules/mod_maxminddb.so
44+ fi
3545 if is_this_installed libapache2-mod-geoip
3646 then
3747 a2dismod geoip
3848 apt-get purge libapache2-mod-geoip -y
39- rm -rf /usr/share/GeoIP
4049 fi
41- apt-get autoremove -y
42- sed -i " /^#Geoip-block-start/,/^#Geoip-block-end/d" /etc/apache2/apache2.conf
43- check_command systemctl restart apache2
50+ # Remove PPA
51+ if grep ^ /etc/apt/sources.list /etc/apt/sources.list.d/* | grep maxmind-ubuntu-ppa
52+ then
53+ install_if_not ppa-purge
54+ yes | ppa-purge maxmind/ppa
55+ rm -f /etc/apt/sources.list.d/maxmind*
56+ fi
57+ # Remove Apache config
58+ if grep " Geoip-block-start" /etc/apache2/apache2.conf
59+ then
60+ sed -i " /^#Geoip-block-start/,/^#Geoip-block-end/d" /etc/apache2/apache2.conf
61+ fi
62+ if [ -f " $GEOBLOCK_MOD_CONF " ]
63+ then
64+ a2disconf geoblock
65+ rm -f " $GEOBLOCK_MOD_CONF "
66+ fi
4467 # Show successful uninstall if applicable
4568 removal_popup " $SCRIPT_NAME "
69+ # Make sure it's clean from unused packages and files
70+ apt purge libmaxminddb0* libmaxminddb-dev* mmdb-bin* apache2-dev* -y
71+ apt autoremove -y
72+ # rm -rf /usr/share/GeoIP keep these to save downloads...
73+ check_command systemctl restart apache2
4674fi
4775
48- # Install needed tools
49- install_if_not libapache2-mod-geoip
76+ # Download GeoIP Databases
77+ if ! download_geoip_mmdb
78+ then
79+ exit 1
80+ fi
5081
51- # Enable apache mod
52- check_command a2enmod geoip rewrite
53- check_command systemctl restart apache2
82+ # #### GeoIP script (Apache Setup)
83+ # Install requirements
84+ yes | add-apt-repository ppa:maxmind/ppa
85+ install_if_not libmaxminddb0
86+ install_if_not libmaxminddb-dev
87+ install_if_not mmdb-bin
88+ install_if_not apache2-dev
5489
55- # Download newest dat files
56- # IPv4
57- download_geoip_dat " 4" " v4"
58- # IPv6
59- download_geoip_dat " 6" " v6"
90+ # maxminddb_module https://github.com/maxmind/mod_maxminddb
91+ cd /tmp
92+ curl_to_dir https://github.com/maxmind/mod_maxminddb/releases/download/1.2.0/ mod_maxminddb-1.2.0.tar.gz /tmp
93+ tar -xzf mod_maxminddb-1.2.0.tar.gz
94+ cd mod_maxminddb-1.2.0
95+ if ./configure
96+ then
97+ make install
98+ if ! apachectl -M | grep -i " maxminddb"
99+ then
100+ msg_box " Couldn't install the Apache module for MaxMind. Please report this to $ISSUES "
101+ exit 1
102+ fi
103+ # Cleanup
104+ rm -rf mod_maxminddb-1.2.0 mod_maxminddb-1.2.0.tar.gz
105+ fi
106+
107+ check_command a2enmod rewrite remoteip maxminddb
108+ check_command systemctl restart apache2
60109
61110# Restrict to countries and/or continents
62111choice=$( whiptail --title " $TITLE " --checklist \
@@ -160,24 +209,35 @@ then
160209 mapfile -t choice <<< " $choice"
161210fi
162211
163- GEOIP_CONF=" #Geoip-block-start - Please don't remove or change this line
164- <IfModule mod_geoip.c>
165- GeoIPEnable On
166- GeoIPDBFile /usr/share/GeoIP/GeoIPv4.dat
167- GeoIPDBFile /usr/share/GeoIP/GeoIPv6.dat
212+ # Create conff
213+ cat << GEOBLOCKCONF_CREATE > "$GEOBLOCK_MOD_CONF "
214+ <IfModule mod_maxminddb.c>
215+ MaxMindDBEnable On
216+ MaxMindDBFile DB /usr/share/GeoIP/GeoLite2-Country.mmdb
217+
218+ MaxMindDBEnv MM_CONTINENT_CODE DB/continent/code
219+ MaxMindDBEnv MM_COUNTRY_CODE DB/country/iso_code
168220</IfModule>
169- <Location />\n"
221+
222+ # Geoblock rules
223+ GEOBLOCKCONF_CREATE
224+
225+ # Add <Location> parameters to maxmind conf
226+ echo " <Location />" >> " $GEOBLOCK_MOD_CONF "
170227for continent in " ${choice[@]} "
171228do
172- GEOIP_CONF+=" SetEnvIf GEOIP_CONTINENT_CODE $continent AllowCountryOrContinent\n"
173- GEOIP_CONF+=" SetEnvIf GEOIP_CONTINENT_CODE_V6 $continent AllowCountryOrContinent\n"
229+ echo " SetEnvIf MM_CONTINENT_CODE $continent AllowCountryOrContinent" >> " $GEOBLOCK_MOD_CONF "
174230done
175231for country in " ${selected_options[@]} "
176232do
177- GEOIP_CONF+=" SetEnvIf GEOIP_COUNTRY_CODE $country AllowCountryOrContinent\n"
178- GEOIP_CONF+=" SetEnvIf GEOIP_COUNTRY_CODE_V6 $country AllowCountryOrContinent\n"
233+ echo " SetEnvIf MM_COUNTRY_CODE $country AllowCountryOrContinent" >> " $GEOBLOCK_MOD_CONF "
179234done
180- GEOIP_CONF+=" Allow from env=AllowCountryOrContinent
235+ echo " Allow from env=AllowCountryOrContinent" >> " $GEOBLOCK_MOD_CONF "
236+
237+ # Add allow rules to maxmind conf
238+ cat << GEOBLOCKALLOW_CREATE >> "$GEOBLOCK_MOD_CONF "
239+
240+ # Specifically allow this
181241 Allow from 127.0.0.1/8
182242 Allow from 192.168.0.0/16
183243 Allow from 172.16.0.0/12
@@ -188,13 +248,18 @@ GEOIP_CONF+=" Allow from env=AllowCountryOrContinent
188248 Order Deny,Allow
189249 Deny from all
190250</Location>
191- #Geoip-block-end - Please don't remove or change this line"
192251
193- # Write everything to the file
194- echo -e " $GEOIP_CONF " >> /etc/apache2/apache2.conf
195-
196- check_command systemctl restart apache2
252+ # Logs
253+ LogLevel info
254+ CustomLog " $VMLOGS /geoblock_access.log" common
255+ GEOBLOCKALLOW_CREATE
197256
198- msg_box " GeoBlock was successfully configured"
257+ # Enable config
258+ check_command a2enconf geoblock
199259
200- exit
260+ if check_command systemctl restart apache2
261+ then
262+ msg_box " GeoBlock was successfully configured"
263+ else
264+ msg_box " Something went wrong, please check Apache error logs."
265+ fi
0 commit comments