Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

used shellcheck, some fixes, added new countries, added new support for url query parameter #10

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
186 changes: 156 additions & 30 deletions fetchmirrors.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,116 @@ usage() {
echo "${Yellow} Number of servers to rank (default 6)"
echo "${Green} -v --verbose"
echo "${Yellow} Verbose output"
echo "${Green} -p --protocol"
echo "${Yellow} the protocol to use either http or https, default is empty (both)"
echo "${Green} -i --ip-version"
echo "${Yellow} the ip version to use either 4 or 6, default is empty (both)"
echo "${Green} -u --use-mirror-status"
echo "${Yellow} use mirror status"
echo
echo "${Yellow} Use${Green} $this ${Yellow}without any option to prompt for country code(s)${ColorOff}"
}

get_opts() {

this=${0##*/} # Set 'this', 'rank_int', 'confirm', 'countries', and color variables
this=${0##*/} # Set 'this', 'rank_int', 'confirm', 'countries', 'protocol', 'ip_version', 'use_mirror_status' and color variables
rank_int="6"
confirm=true
protocol=()
ip_version=()
use_mirror_status=""
err=false
Green=$'\e[0;32m';
Yellow=$'\e[0;33m';
Red=$'\e[0;31m';
Magenta=$'\e[0;35m';
ColorOff=$'\e[0m';
# frmt_countries=( "${Magenta}1) ${Green}AM ${Yellow}All-Mirrors - ${Magenta}2) ${Green}AS ${Yellow}All-Https - ${Magenta}3) ${Green}AT ${Yellow}Austria\n${Magenta}4) ${Green}AU ${Yellow}Australia - ${Magenta}5) ${Green}BE ${Yellow}Belgium - ${Magenta}6) ${Green}BG ${Yellow}Bulgaria\n${Magenta}7) ${Green}BR ${Yellow}Brazil - ${Magenta}8) ${Green}BY ${Yellow}Belarus - ${Magenta}9) ${Green}CA ${Yellow}Canada\n${Magenta}10) ${Green}CL ${Yellow}Chile - ${Magenta}11) ${Green}CN ${Yellow}China - ${Magenta}12) ${Green}CO ${Yellow}Columbia\n${Magenta}13) ${Green}CZ ${Yellow}Czech-Republic - ${Magenta}14) ${Green}DE ${Yellow}Germany - ${Magenta}15) ${Green}DK ${Yellow}Denmark\n${Magenta}16) ${Green}EE ${Yellow}Estonia - ${Magenta}17) ${Green}ES ${Yellow}Spain - ${Magenta}18) ${Green}FI ${Yellow}Finland\n${Magenta}19) ${Green}FR ${Yellow}France - ${Magenta}20) ${Green}GB ${Yellow}United-Kingdom - ${Magenta}21) ${Green}HU ${Yellow}Hungary\n${Magenta}22) ${Green}IE ${Yellow}Ireland - ${Magenta}23) ${Green}IL ${Yellow}Isreal - ${Magenta}24) ${Green}IN ${Yellow}India\n${Magenta}25) ${Green}IT ${Yellow}Italy - ${Magenta}26) ${Green}JP ${Yellow}Japan - ${Magenta}27) ${Green}KR ${Yellow}Korea\n${Magenta}28) ${Green}KZ ${Yellow}Kazakhstan - ${Magenta}29) ${Green}LK ${Yellow}Sri-Lanka - ${Magenta}30) ${Green}LU ${Yellow}Luxembourg\n${Magenta}31) ${Green}LV ${Yellow}Lativia - ${Magenta}32) ${Green}MK ${Yellow}Macedonia - ${Magenta}33) ${Green}NC ${Yellow}New-Caledonia\n${Magenta}34) ${Green}NL ${Yellow}Netherlands - ${Magenta}35) ${Green}NO ${Yellow}Norway - ${Magenta}36) ${Green}NZ ${Yellow}New-Zealand\n${Magenta}37) ${Green}PL ${Yellow}Poland - ${Magenta}38) ${Green}PT ${Yellow}Portugal - ${Magenta}39) ${Green}RO ${Yellow}Romania\n${Magenta}40) ${Green}RS ${Yellow}Serbia - ${Magenta}41) ${Green}RU ${Yellow}Russia - ${Magenta}42) ${Green}SE ${Yellow}Sweden\n${Magenta}43) ${Green}SG ${Yellow}Singapore - ${Magenta}44) ${Green}SK ${Yellow}Slovakia - ${Magenta}45) ${Green}TR ${Yellow}Turkey\n${Magenta}46) ${Green}TW ${Yellow}Taiwan - ${Magenta}47) ${Green}UA ${Yellow}Ukraine - ${Magenta}48) ${Green}US ${Yellow}United-States\n${Magenta}49) ${Green}UZ ${Yellow}Uzbekistan - ${Magenta}50) ${Green}VN ${Yellow}Viet-Nam - ${Magenta}51) ${Green}ZA ${Yellow}South-Africa" )
countries=( "1) AM All-Mirrors - 2) AS All-Https - 3) AT Austria\n4) AU Australia - 5) BE Belgium - 6) BG Bulgaria\n7) BR Brazil - 8) BY Belarus - 9) CA Canada\n10) CL Chile - 11) CN China - 12) CO Columbia\n13) CZ Czech-Republic - 14) DE Germany - 15) DK Denmark\n16) EE Estonia - 17) ES Spain - 18) FI Finland\n19) FR France - 20) GB United-Kingdom - 21) HU Hungary\n22) IE Ireland - 23) IL Isreal - 24) IN India\n25) IT Italy - 26) JP Japan - 27) KR Korea\n28) KZ Kazakhstan - 29) LK Sri-Lanka - 30) LU Luxembourg\n31) LV Lativia - 32) MK Macedonia - 33) NC New-Caledonia\n34) NL Netherlands - 35) NO Norway - 36) NZ New-Zealand\n37) PL Poland - 38) PT Portugal - 39) RO Romania\n40) RS Serbia - 41) RU Russia - 42) SE Sweden\n43) SG Singapore - 44) SK Slovakia - 45) TR Turkey\n46) TW Taiwan - 47) UA Ukraine - 48) US United-States\n49) UZ Uzbekistan - 50) VN Viet-Nam - 51) ZA South-Africa" )

#countries=( "1) AM All-Mirrors - 2) AS All-Https - 3) AT Austria\n4) AU Australia - 5) BE Belgium - 6) BG Bulgaria\n7) BR Brazil - 8) BY Belarus - 9) CA Canada\n10) CL Chile - 11) CN China - 12) CO Columbia\n13) CZ Czech-Republic - 14) DE Germany - 15) DK Denmark\n16) EE Estonia - 17) ES Spain - 18) FI Finland\n19) FR France - 20) GB United-Kingdom - 21) HU Hungary\n22) IE Ireland - 23) IL Isreal - 24) IN India\n25) IT Italy - 26) JP Japan - 27) KR Korea\n28) KZ Kazakhstan - 29) LK Sri-Lanka - 30) LU Luxembourg\n31) LV Lativia - 32) MK Macedonia - 33) NC New-Caledonia\n34) NL Netherlands - 35) NO Norway - 36) NZ New-Zealand\n37) PL Poland - 38) PT Portugal - 39) RO Romania\n40) RS Serbia - 41) RU Russia - 42) SE Sweden\n43) SG Singapore - 44) SK Slovakia - 45) TR Turkey\n46) TW Taiwan - 47) UA Ukraine - 48) US United-States\n49) UZ Uzbekistan - 50) VN Viet-Nam - 51) ZA South-Africa" )
countries+=("1) AM All-Mirrors")
countries+=("2) AS All-Https")
countries+=("3) AT Austria")
countries+=("\n")
countries+=("4) AU Australia")
countries+=("5) BA Bosnia-and-Herzegovina")
countries+=("6) BE Belgium")
countries+=("\n")
countries+=("7) BG Bulgaria")
countries+=("8) BR Brazil")
countries+=("9) BY Belarus")
countries+=("\n")
countries+=("10) CA Canada")
countries+=("11) CH Switzerland")
countries+=("12) CL Chile")
countries+=("\n")
countries+=("13) CN China")
countries+=("14) CO Colombia")
countries+=("15) CZ Czech-Republic")
countries+=("\n")
countries+=("16) DE Germany")
countries+=("17) DK Denmark")
countries+=("18) EC Ecuador")
countries+=("\n")
countries+=("19) ES Spain")
countries+=("20) FI Finland")
countries+=("21) FR France")
countries+=("\n")
countries+=("22) GB United-Kingdom")
countries+=("23) GR Greece")
countries+=("24) HK Hong-Kong")
countries+=("\n")
countries+=("25) HR Croatia")
countries+=("26) HU Hungary")
countries+=("27) ID Indonesia")
countries+=("\n")
countries+=("28) IE Ireland")
countries+=("29) IL Israel")
countries+=("30) IN India")
countries+=("\n")
countries+=("31) IR Iran")
countries+=("32) IS Iceland")
countries+=("33) IT Italy")
countries+=("\n")
countries+=("34) JP Japan")
countries+=("35) KR South-Korea")
countries+=("36) KZ Kazakhstan")
countries+=("\n")
countries+=("37) LT Lithuania")
countries+=("38) LU Luxembourg")
countries+=("39) LV Latvia")
countries+=("\n")
countries+=("40) MK Macedonia")
countries+=("41) MX Mexico")
countries+=("42) NC New-Caledonia")
countries+=("\n")
countries+=("43) NL Netherlands")
countries+=("44) NO Norway")
countries+=("45) NZ New-Zealand")
countries+=("\n")
countries+=("46) PH Philippines")
countries+=("47) PL Poland")
countries+=("48) PT Portugal")
countries+=("\n")
countries+=("49) QA Qatar")
countries+=("50) RO Romania")
countries+=("51) RS Serbia")
countries+=("\n")
countries+=("52) RU Russia")
countries+=("53) SE Sweden")
countries+=("54) SG Singapore")
countries+=("\n")
countries+=("55) SI Slovenia")
countries+=("56) SK Slovakia")
countries+=("57) TH Thailand")
countries+=("\n")
countries+=("58) TR Turkey")
countries+=("59) TW Taiwan")
countries+=("60) UA Ukraine")
countries+=("\n")
countries+=("61) US United-States")
countries+=("62) VN Vietnam")
countries+=("63) ZA South-Africa")
countries+=("\n")
trap ctrl_c INT

if [ -z "$1" ] && [ "$UID" -ne "0" ]; then
Expand Down Expand Up @@ -89,7 +181,7 @@ get_opts() {
;;
-l|--list) # Display a list of country codes to the user
echo "${Yellow}Country codes:${ColorOff}"
echo -e "$countries" | column -t
echo -e "${countries[@]}" | column -t
echo "${Yellow}Note: Use only the upercase two character code in your command ex:${Green} $this -c US"
echo "${Yellow}Or simply use:${Green} ${this}${ColorOff}"
break
Expand All @@ -102,9 +194,9 @@ get_opts() {
echo "${Yellow}[${this}]${Red} Error: ${Yellow}You must enter a country code."
else
shift
for i in $(echo "$@") ; do
if (echo "$countries" | grep -w "$i" &> /dev/null); then
query+="https://www.archlinux.org/mirrorlist/?country=${i} "
for i in "$@" ; do
if (echo "${countries[@]}" | grep -w "$i" &> /dev/null); then
query+=("https://www.archlinux.org/mirrorlist/?country=${i}")
else
echo "${Yellow}[${this}]${Red} Error: ${Yellow}country code: $2 not found."
echo "To view a list of country codes run:${Green} $this -l${ColorOff}"
Expand All @@ -115,6 +207,29 @@ get_opts() {
fi
break
;;
-p|--protocol) # Set protocol eg. http or https
if [ "$2" != "https" ] && [ "$2" != "http" ] ; then
echo "${Yellow}[${this}]${Red} Error: ${Yellow} invalid protocol use https or http or both ${ColorOff}"
exit 1
fi
protocol+=("$2");
# remove duplicates, in case of "-p https -p https --protocol https"
protocol=($(echo "${protocol[@]}" | tr ' ' '\n' | sort -u))
shift ; shift ;
;;
-i|--ip-version) # Set ip version eg. 4 or 6
if [ "$2" -ne "4" ] && [ "$2" -ne "6" ] ; then
echo "${Yellow}[${this}]${Red} Error: ${Yellow} invalid ip_version use 4 or 6 or both ${ColorOff}"
exit 1
fi
ip_version+=("$2");
# remove duplicates, in case of "-i 4 -i 4 --ip-version 4"
ip_version=($(echo "${ip_version[@]}" | tr ' ' '\n' | sort -u))
shift ; shift ;
;;
-u|--use-mirror-status) # use mirror status
use_mirror_status="&use_mirror_status=on" ; shift
;;
"") # Empty 1 parameter means search for country code
search ; break
;;
Expand All @@ -128,35 +243,34 @@ get_opts() {
}

search() {

while (true)
do
echo "${Green}Country codes:${ColorOff}"
echo -e "$countries" | column -t
echo -e "${countries[@]}" | column -t
echo -n "${Yellow}Enter the number(s) or code(s) corresponding to your country ${Green}[4 9 US]${Yellow}:${ColorOff} "
read code
read -ra code

for i in $(echo "$code") ; do
for i in "${code[@]}" ; do
case "$i" in
1) # Set query to all mirrors
1|AM) # Set query to all mirrors
country="All"
query="https://www.archlinux.org/mirrorlist/all/"
query=("https://www.archlinux.org/mirrorlist/all/")
break
;;
2) # Set query to all https mirrors
2|AS) # Set query to all https mirrors
country="All HTTPS"
query="https://www.archlinux.org/mirrorlist/all/https/"
query=("https://www.archlinux.org/mirrorlist/all/https/")
break
;;
[0-9]|[1-4][0-9]|[5][0-1])
country_code=$(<<<"$countries" grep -o "$i.*" | awk 'NR==1 {print $2}')
country+=$(<<<"$countries" grep -o "$i.*" | awk 'NR==1 {print $3" "}' | sed 's/\\n.*/ /')
query+="https://www.archlinux.org/mirrorlist/?country=${country_code} "
country_code=$(<<<"${countries[@]}" grep -o "$i.*" | awk 'NR==1 {print $2}')
country+=($(<<<"${countries[@]}" grep -o "$i.*" | awk 'NR==1 {print $3" "}' | sed 's/\\n.*/ /'))
query+=("https://www.archlinux.org/mirrorlist/?country=${country_code}")
;;
[A-Z][A-Z])
if (<<<"$countries" grep -o "$i" &>/dev/null); then
country+=$(<<<"$countries" grep -o "$i.*" | awk 'NR==1 {print $2}' | sed 's/\\n.*/ /')
query+="https://www.archlinux.org/mirrorlist/?country=${i} "
[[:upper:]][[:upper:]])
if (<<<"${countries[@]}" grep -o "$i" &>/dev/null); then
country+=($(<<<"${countries[@]}" grep -o "$i.*" | awk 'NR==1 {print $2}' | sed 's/\\n.*/ /'))
query+=("https://www.archlinux.org/mirrorlist/?country=${i}")
else
echo "${Yellow}[${this}]${Red} Error: ${Yellow}invalid input [ $i ], select a number or code from the list.${ColorOff}"
err=true
Expand All @@ -180,8 +294,8 @@ search() {
done

if "$confirm" ; then
echo -en "\n${Yellow}You have selected the countries:${Green} $country${Yellow}- is this correct ${Green}[Y/n]:${ColorOff} "
read input
echo -en "\n${Yellow}You have selected the countries:${Green} ${country[*]} ${Yellow}- is this correct ${Green}[Y/n]:${ColorOff} "
read -r input

case "$input" in
y|Y|yes|Yes|yY|Yy|yy|YY|"")
Expand All @@ -199,8 +313,21 @@ search() {

get_list() {

# build query string for protocol
protocolQueryString="";
for p in "${protocol[@]}";
do
protocolQueryString+="&protocol=$p";
done
# build query string for ip_version
ip_versionQueryString="";
for i in "${ip_version[@]}" ;
do
ip_versionQueryString+="&ip_version=$i";
done
echo
for list in $(echo "$query") ; do
for list in "${query[@]}" ; do
list+=${protocolQueryString}${ip_versionQueryString}${use_mirror_status}
if (curl -s "$list" | grep "Server" &>/dev/null); then
echo "${Yellow}Fetching new mirrorlist from:${Green} ${list}${ColorOff}"
curl -s "$list" | sed '1,4d' >> /tmp/mirrorlist
Expand All @@ -214,16 +341,15 @@ get_list() {
done

sed -i 's/#//' /tmp/mirrorlist
echo "${Yellow}Please wait while ranking${Green} $country${Yellow}mirrors...${ColorOff}"
rankmirrors -n "$rank_int" /tmp/mirrorlist > /tmp/mirrorlist.ranked
echo "${Yellow}Please wait while ranking${Green} ${country[*]} ${Yellow}mirrors...${ColorOff}"

if [ "$?" -gt "0" ]; then
if ! rankmirrors -n "$rank_int" /tmp/mirrorlist > /tmp/mirrorlist.ranked ; then
echo "${Yellow}[${this}]${Red} Error: ${Yellow}an error occured in ranking mirrorlist exiting..."
rm /tmp/{mirrorlist,mirrorlist.ranked} &> /dev/null
exit 1
elif "$confirm" ; then
echo -en "\n${Yellow}Would you like to view new mirrorlist? ${Green}[Y/n]: ${ColorOff}"
read input
read -r input

case "$input" in
y|Y|yes|Yes|yY|Yy|yy|YY|"")
Expand All @@ -232,7 +358,7 @@ get_list() {
esac

echo -en "\n${Yellow}Would you like to install the new mirrorlist backing up existing? ${Green}[Y/n]:${ColorOff} "
read input
read -r input
else
input=""
fi
Expand Down