Skip to content

Commit 8cbdd94

Browse files
authored
Refactor geoblock.sh for CSV file management
Updated the geoblock script to remove old CSV files and adjust country code handling. Fix #2807 Signed-off-by: enoch85 <mailto@danielhansson.nu>
1 parent b2b8331 commit 8cbdd94

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

network/geoblock.sh

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ else
3737
find "$GEOBLOCK_DIR" -type f -regex \
3838
"*.dat" -delete
3939
rm -f "$GEOBLOCK_DIR"/IPInfo-Country.mmdb
40+
# Remove old csv files
41+
rm -f "$SCRIPTS/iso3166.csv"
42+
rm -f "$SCRIPTS/country-codes.csv"
4043
# Remove Apache2 mod
4144
if [ -f "$GEOBLOCK_MOD" ]
4245
then
@@ -121,25 +124,25 @@ then
121124
msg_box "Failed to compile MaxMindDB module. Please report this to $ISSUES"
122125
exit 1
123126
fi
124-
127+
125128
# Delete conf made by module
126129
rm -f /etc/apache2/mods-enabled/maxminddb.conf
127-
130+
128131
# Check if module is enabled
129132
if ! apachectl -M | grep -i "maxminddb"
130133
then
131134
msg_box "Couldn't load the Apache module for MaxMind after installation. Please report this to $ISSUES"
132135
exit 1
133136
fi
134-
137+
135138
print_text_in_color "$IGreen" "MaxMindDB module loaded in Apache successfully!"
136-
139+
137140
# Cleanup
138141
cd /tmp
139142
rm -rf mod_maxminddb-1.2.0 mod_maxminddb-1.2.0.tar.gz
140143
else
141144
msg_box "Failed to configure MaxMindDB module compilation.
142-
145+
143146
This usually means apxs/apxs2 is not properly installed.
144147
Please report this issue to: $ISSUES"
145148
exit 1
@@ -166,23 +169,28 @@ fi
166169
if [[ "$choice" = *"Countries"* ]]
167170
then
168171
# Download csv file
169-
if ! curl_to_dir "https://dev.maxmind.com/static/csv/codes" "iso3166.csv" "$SCRIPTS"
172+
if ! curl_to_dir "https://raw.githubusercontent.com/datasets/country-codes/main/data" "country-codes.csv" "$SCRIPTS"
170173
then
171-
msg_box "Could not download the iso3166.csv file.
174+
msg_box "Could not download the country-codes.csv file.
172175
Please report this to $ISSUES"
173176
exit 1
174177
fi
175178

179+
# Extract country codes (column 10: ISO3166-1-Alpha-2) and
180+
# names (column 41: official_name_en) from the CSV.
181+
# Custom field parser handles quoted fields that contain commas.
182+
CSV_DATA=$(awk 'NR>1{n=split($0,c,"");q=0;f=1;v="";for(i=1;i<=n;i++){if(c[i]=="\"")q=!q;else if(c[i]==","&&!q){if(f==10)code=v;if(f==41)name=v;f++;v=""}else v=v c[i]}if(f==10)code=v;if(f==41)name=v;if(code!=""&&name!="")print code",\""name"\""}' "$SCRIPTS/country-codes.csv" | sort)
183+
176184
# Get country names
177-
COUNTRY_NAMES=$(sed 's|.*,"||;s|"$||' "$SCRIPTS/iso3166.csv")
185+
COUNTRY_NAMES=$(sed 's|.*,"||;s|"$||' <<< "$CSV_DATA")
178186
mapfile -t COUNTRY_NAMES <<< "$COUNTRY_NAMES"
179187

180188
# Get country codes
181-
COUNTRY_CODES=$(sed 's|,.*||' "$SCRIPTS/iso3166.csv")
189+
COUNTRY_CODES=$(sed 's|,.*||' <<< "$CSV_DATA")
182190
mapfile -t COUNTRY_CODES <<< "$COUNTRY_CODES"
183191

184192
# Remove the csv file since no longer needed
185-
check_command rm "$SCRIPTS/iso3166.csv"
193+
check_command rm "$SCRIPTS/country-codes.csv"
186194

187195
# Check if both arrays match
188196
if [ "${#COUNTRY_NAMES[@]}" != "${#COUNTRY_CODES[@]}" ]

0 commit comments

Comments
 (0)