Skip to content

Instantly share code, notes, and snippets.

@AaronNGray
Created March 18, 2024 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AaronNGray/dcb482cd44b802064b6cb62d8f41f1a9 to your computer and use it in GitHub Desktop.
Save AaronNGray/dcb482cd44b802064b6cb62d8f41f1a9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
if [ ! `which jq` ]
then
echo Installing 'jq'
sudo apt-get install jq -y
fi
set -euo pipefail
verbose=
rel=
rawurlencode() {
local string="${1}"
local strlen=${#string}
local encoded=""
local pos c o
for (( pos=0 ; pos<strlen ; pos++ )); do
c=${string:$pos:1}
case "$c" in
[-_.~a-zA-Z0-9] ) o="${c}" ;;
* ) printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
echo "${encoded}" # You can either set a return variable (FASTER)
REPLY="${encoded}" #+or echo the result (EASIER)... or both... :p
}
usage() {
>&2 cat << EOF
Usage: $0
[ -v | --verbose ]
[ -r | --rel | --REL input ]
<user@domain> [<query>]
EOF
exit 1
}
args=$(getopt -a -o hvr: --long help,verbose,rel:,REL: -- "$@")
if [[ $? -gt 0 ]]; then
usage
fi
eval set -- ${args}
while :
do
case $1 in
-h | --help) usage; shift;;
-v | --verbose) verbose=true; shift;;
-r | --rel | --REL) rel=$2; shift 2;;
# -- means the end of the arguments; drop this, and break out of the while loop
--) shift; break ;;
*) >&2 echo Unsupported option: $1
usage ;;
esac
done
if [[ $# -eq 0 ]]; then
usage
fi
name=`echo $1 | sed 's/\(.*\)@\(.*\)$/\1/'`
domain=`echo $1 | sed 's/\(.*\)@\(.*\)$/\2/'`
server=$domain
address=${name}@${domain}
resource=acct:$(rawurlencode $address)
if [ ! -n "${rel}" ]; then
command="curl -s -H \"Accept: application/jrd+json\" https://${server}/.well-known/webfinger?resource=${resource}"
else
command="curl -s -H \"Accept: application/jrd+json\" https://${server}/.well-known/webfinger?resource=${resource}&rel=${rel}"
fi
if [ -n "${verbose}" ]
then
echo ${command}
echo
fi
result=`${command}`
type=`echo $result | jq -r '.links[] | select(.rel == "self") | .type'`
href=`echo $result | jq -r '.links[] | select(.rel == "self") | .href'`
header="Accept: ${type}"
if [ $# == "2" ]
then
query=$2
else
query=.
fi
curl -s -H "$header" $href | jq $query
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment