From 48732e16dc554d2e9536d8562926ec93a6a20a20 Mon Sep 17 00:00:00 2001 From: Esa Jokinen Date: Sat, 28 Sep 2024 21:08:04 +0300 Subject: [PATCH] http-dns-round-robin.sh: support port in url Support https://example.com:443/ URLs. Default to 443 with https:, otherwise to 80. --- bin/http-dns-round-robin.sh | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/bin/http-dns-round-robin.sh b/bin/http-dns-round-robin.sh index abcb856..b2b592b 100755 --- a/bin/http-dns-round-robin.sh +++ b/bin/http-dns-round-robin.sh @@ -59,12 +59,24 @@ IPV6ADDR+="(${IPV6SEG}:){1,4}:${IPV4ADDR}" IPV6ADDR+=")" IPADDR="${IPV4ADDR}|${IPV6ADDR}" -HOSTNAME=$(echo "$URL" | awk -F/ '{print $3}') +PROTOCOL=$(echo "$URL" | awk -F/ '{print $1}') +HOSTPORT=$(echo "$URL" | awk -F/ '{print $3}') +if [[ $HOSTPORT =~ .*:[0-9]+ ]]; then + PORT="$(echo "$HOSTPORT" | awk -F: '{print $NF}')" + HOSTNAME="${HOSTPORT%":${PORT}"}" +else + HOSTNAME="$HOSTPORT" + if [[ $PROTOCOL == 'https:' ]]; then + PORT=443 + else + PORT=80 + fi +fi while read -r ip do - echo "[${ip}]" - curl --resolve "[${HOSTNAME}]:443:${ip}" --silent --head "$URL" + echo "[${ip}]:${PORT}" + curl --resolve "[${HOSTNAME}]:${PORT}:${ip}" --silent --head "$URL" done <<< "$( dig +short "$HOSTNAME" A | grep -E -o "$IPADDR" dig +short "$HOSTNAME" AAAA | grep -E -o "$IPADDR"