-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathupdate-mirrorlist
executable file
·78 lines (68 loc) · 2.42 KB
/
update-mirrorlist
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
#!/bin/bash
CodeToCountry() { # convert country code to country name
echo "$REFLECTOR_COUNTRIES" | grep -w "$1" | sed 's|^\(.*[a-z]\)[ ]*[A-Z][A-Z].*$|\1|'
}
CountryToCode() { # convert name to code; used for checking
echo "$REFLECTOR_COUNTRIES" | grep -w "$1" | awk '{print $(NF-1)}'
}
CCCheck() { # check validity of country code
case "$1" in
[A-Z][A-Z]) test -n "$(CodeToCountry "$1")" && return 0 ;;
esac
return 1 # fail
}
GetYourCountryCode() {
local IP code
IP="$(dig -4 TXT +short o-o.myaddr.l.google.com @ns1.google.com | tr -d '"')" # ipv4 address
code="$(geoiplookup "$IP" | sed 's|^.*: \([A-Z][A-Z]\),.*$|\1|')"
CCCheck "$code" && {
echo "$code" ; return
}
code="$(whois "$IP" | grep ^country: | awk '{print $NF}')"
CCCheck "$code" && {
echo "$code" ; return
}
IP="$(dig -6 TXT +short o-o.myaddr.l.google.com @ns1.google.com | tr -d '"')" # ipv6 address
code="$(geoiplookup6 "$IP" | sed 's|^.*: \([A-Z][A-Z]\),.*$|\1|')"
CCCheck "$code" && {
echo "$code" ; return
}
code="$(whois "$IP" | grep ^country: | awk '{print $NF}')"
CCCheck "$code" && {
echo "$code" ; return
}
code="$(curl ipinfo.io 2>/dev/null | grep '"country":' | sed 's|^.*: "\([A-Z][A-Z]\)",$|\1|')"
CCCheck "$code" && {
echo "$code" ; return
}
# net services failed, use local variables, but may be wrong
code="$(locale | grep ^LC_TIME | cut -d '"' -f 2 | sed 's|^.*_\([A-Z][A-Z]\)\..*$|\1|')"
CCCheck "$code" && {
echo "$code" ; return
}
}
GetYourCountry() {
local code="$(GetYourCountryCode)"
local country="$(test -n "$code" && CodeToCountry "$code")"
echo "$country"
}
Main() {
REFLECTOR_COUNTRIES="$(reflector --list-countries)"
local this_country="$(GetYourCountry)"
local tf=$(mktemp)
local mf=/etc/pacman.d/mirrorlist
local refopts="-phttps -f10 -l20 --sort age --save $tf" # common reflector options
test -n "$this_country" && {
echo "Detected country: $this_country" >&2
if [ "$1" = "-P" ] ; then # testing time option -P: do not add http with this country
reflector $refopts -a2 -c "$this_country"
else
reflector $refopts -a2 -phttp -c "$this_country" # a country may lack https mirrors
fi
} || {
reflector $refopts -a1
}
sudo bash -c "cp $tf $mf && chmod 0644 $mf"
rm -f $tf
}
Main "$@"