Skip to content

Instantly share code, notes, and snippets.

@babolivier
Last active May 28, 2020 10:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save babolivier/d9009c11a934a0c4d3fdb0855196e966 to your computer and use it in GitHub Desktop.
Save babolivier/d9009c11a934a0c4d3fdb0855196e966 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Init.
if [ "$1" == "--silent-fail" ]; then
SILENT_FAIL=true
else
SILENT_FAIL=false
fi
ACCESS_TOKEN="[REDACTED]"
HS_FQDN="chat.abolivier.bzh"
ROOM_ID="!someroom:example.com"
declare -A versions
success_count=0
# Get the initial list of servers in the room.
res=`curl --silent https://$HS_FQDN/_matrix/client/r0/rooms/$ROOM_ID/joined_members?access_token=$ACCESS_TOKEN`
srvs=`echo $res | jq ".joined | keys[]" | sed 's/"//g' | cut -d':' -f2 | sort -u`
function resolve_srv() {
fed=`dig +short -t SRV _matrix._tcp.$1 | grep -oE "[^ ]+ [^ ]+$" | sed 's/\.$//'`
dom=`echo $fed | cut -d' ' -f2`
port=`echo $fed | cut -d' ' -f1`
echo "$dom:$port"
}
function resolve_well_known() {
code=`curl --connect-timeout 10 -L --silent --write-out %{http_code} https://$1/.well-known/matrix/server | tail -n1`
if [ "$code" != "200" ]; then echo ""; return; fi
echo `curl --connect-timeout 10 -L --silent https://$1/.well-known/matrix/server | jq '."m.server"' | sed -E 's/["]//g' | sed -E "s/[']//g"`
}
# Check and display version for each server.
for s in $srvs; do
# Try to resolve the host of the server using a well-known file
srv=0
fed=`resolve_well_known $s`
# If that failed, try to resolve using a SRV record
if [ -z "$fed" ]; then
srv=1
fed=`resolve_srv $s`
fi
if [ -z `echo $fed | grep ":"` ]; then
dom=$fed
port="8448"
else
dom=`echo $fed | cut -d':' -f1`
port=`echo $fed | cut -d':' -f2`
fi
# Get server version.
if [ "$srv" == "1" ]; then
ver=`curl --connect-to "$s:443:$dom:$port" --connect-timeout 10 --silent https://$s/_matrix/federation/v1/version | jq -r '.server | .version' 2> /dev/null`
else
ver=`curl --connect-timeout 10 --silent https://$dom:$port/_matrix/federation/v1/version | jq -r '.server | .version' 2> /dev/null`
fi
# If the request was successful, print the result and increment the counters. Otherwise print that it couldn't be reached (if not in silent failure mode).
if [ ! -z "$ver" ]; then
echo "$s: $ver"
shortver=`echo $ver | cut -d" " -f1`
# Update the counters.
count=${versions[$shortver]}
versions["$shortver"]=$(($count+1))
success_count=$(($success_count+1))
else
if [ $SILENT_FAIL == false ]; then
echo "Can't get version of homeserver $s"
fi
fi
done
# Display stats on the version of online servers in the room.
echo -e "\n\n----- STATS -----\n"
echo "Online servers in the room: $success_count"
# Iterate over the per-version counters.
vers=`echo "${!versions[@]}" | sed 's/ /\n/g' | sort -r`
for ver in $vers; do
echo "$ver: ${versions[$ver]}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment