improve failure handling in get-hosts.sh

This commit is contained in:
nervuri 2021-08-22 15:18:08 +00:00
parent 39ce27a9d8
commit a8bb016171
Signed by: nervuri
GPG Key ID: C4769EEA7BA61672
1 changed files with 22 additions and 4 deletions

View File

@ -16,25 +16,43 @@ if command -v agunua >/dev/null; then
# Using Agunua is more secure, because it does certificate pinning.
# The --insecure option just makes it accept certificates that are
# not signed by a (known) CA.
hosts1=$(agunua --insecure --binary gemini://geminispace.info/known-hosts 2>/dev/null \
hosts1=$(agunua --insecure --binary --maximum-time 5 \
gemini://geminispace.info/known-hosts 2>/dev/null \
| grep "gemini://" | cut -d ' ' -f 3)
hosts2=$(agunua --insecure --binary gemini://gemini.bortzmeyer.org/software/lupa/lupa-capsules.txt 2>/dev/null)
if [ -z "$hosts1" ]; then
>&2 echo "geminispace.info/known-hosts download failed."
exit 1
fi
hosts2=$(agunua --insecure --binary --maximum-time 5 \
gemini://gemini.bortzmeyer.org/software/lupa/lupa-capsules.txt 2>/dev/null)
if [ -z "$hosts2" ]; then
>&2 echo "lupa-capsules.txt download failed."
exit 1
fi
else
# If Agunua is not installed, pipe the request into OpenSSL s_client.
hosts1=$(printf "gemini://geminispace.info/known-hosts\r\n" \
| timeout 5 openssl s_client -quiet -connect "geminispace.info:1965" 2>/dev/null \
| grep "gemini://" | cut -d ' ' -f 3)
if [ -z "$hosts1" ]; then
>&2 echo "geminispace.info/known-hosts download failed."
exit 1
fi
hosts2=$(printf "gemini://gemini.bortzmeyer.org/software/lupa/lupa-capsules.txt\r\n" \
| timeout 5 openssl s_client -quiet -connect "gemini.bortzmeyer.org:1965" 2>/dev/null \
| tail -n +2)
if [ -z "$hosts2" ]; then
>&2 echo "lupa-capsules.txt download failed."
exit 1
fi
fi
# Concatenate the two files.
hosts="$hosts1
$hosts2"
# Convert punycode to unicode; sort entries; remove duplicates; remove empty lines.
hosts=$(echo "$hosts" | idn --allow-unassigned --idna-to-unicode | sort -fu | awk NF)
# Remove empty lines; convert punycode to unicode; sort entries; remove duplicates.
hosts=$(echo "$hosts" | awk NF | idn --allow-unassigned --idna-to-unicode | sort -fu)
# Remove hosts which contain neither "." nor ":", such as "localhost".
hosts=$(echo "$hosts" | grep '\.\|:')