trust-store-generators/get-hosts.sh

48 lines
1.7 KiB
Bash
Raw Normal View History

2021-04-28 09:20:18 +00:00
#!/bin/sh
2021-06-04 11:20:22 +00:00
# Download and merge lists of Gemini hosts from:
# gemini://geminispace.info/known-hosts
# gemini://gemini.bortzmeyer.org/software/lupa/lupa-capsules.txt
2021-04-28 09:20:18 +00:00
set -o errexit # (-e) exit immediately if any command has a non-zero exit status
set -o nounset # (-u) don't accept undefined variables
#set -o xtrace # for debugging
# Go where this script is.
cd "$(dirname "$0")" || exit
# If Agunua is installed, use it.
if command -v agunua >/dev/null; then
# Using Agunua is more secure, because it does certificate pinning.
2021-06-04 11:20:22 +00:00
# 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 \
2021-04-28 09:20:18 +00:00
| grep "gemini://" | cut -d ' ' -f 3)
2021-06-04 11:20:22 +00:00
hosts2=$(agunua --insecure --binary gemini://gemini.bortzmeyer.org/software/lupa/lupa-capsules.txt 2>/dev/null)
2021-04-28 09:20:18 +00:00
else
# If Agunua is not installed, pipe the request into OpenSSL s_client.
2021-06-04 11:20:22 +00:00
hosts1=$(printf "gemini://geminispace.info/known-hosts\r\n" \
2021-04-28 09:20:18 +00:00
| timeout 5 openssl s_client -quiet -connect "geminispace.info:1965" 2>/dev/null \
| grep "gemini://" | cut -d ' ' -f 3)
2021-06-04 11:20:22 +00:00
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)
2021-04-28 09:20:18 +00:00
fi
2021-06-04 11:20:22 +00:00
# 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)
2021-04-28 09:20:18 +00:00
if [ -z "$hosts" ]; then
2021-06-04 11:20:22 +00:00
>&2 echo "hosts file downloads failed."
2021-04-28 09:20:18 +00:00
exit 1
fi
# Save to file.
2021-06-04 11:20:22 +00:00
echo "$hosts" > hosts
2021-04-28 09:20:18 +00:00
echo OK