From a83cfa78acfa78d690aec7ca86922c8d9f58a01c Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 18 Jul 2019 18:15:46 +0200 Subject: [PATCH] instanceinfo.sh: Refactored code into functions. --- instanceinfo.sh | 255 ++++++++++++++++++++++++++---------------------- 1 file changed, 138 insertions(+), 117 deletions(-) diff --git a/instanceinfo.sh b/instanceinfo.sh index 7c3fac7..2fbbf7f 100755 --- a/instanceinfo.sh +++ b/instanceinfo.sh @@ -1,7 +1,7 @@ #!/bin/bash # Print some basic info about fediverse instances. -# Version: 2019-07-16_2 +# Version: 2019-07-18_1 if [[ -z "${1}" ]]; then echo "usage: ${0} DOMAIN" >&2 @@ -19,6 +19,140 @@ function striphtml() echo "${extract}" | sed 's/\\u003c//g' | sed -E 's/<[^>]+>//g' | sed 's/\\r\\n/ /g' | sed -E 's/(^"|"$)//g' } +function parse_nodeinfo() +{ + local instance="${1}" + local nodeinfo="${2}" + json=$(curl -s "${nodeinfo}") + + # shellcheck disable=SC2155 + local implementation=$(jq -r ".software.name" <<<"${json}") + # shellcheck disable=SC2155 + local version=$(jq -r ".software.version" <<<"${json}") + echo "${implementation} ${version} detected." + + # shellcheck disable=SC2155 + local description=$(jq -r ".metadata.nodeDescription" <<<"${json}") + if [[ "${description}" == "null" ]]; then + description=$(jq -r ".metadata.shortDescription" <<<"${json}") + fi + if [[ "${description}" == "null" ]]; then + description=$(jq -r ".metadata.nodeName" <<<"${json}") + fi + [[ "${description}" != "null" ]] && echo "Description: ${description}" + + # shellcheck disable=SC2155 + local admins=$(jq -r ".metadata.staffAccounts" <<<"${json}") + if [[ "${admins}" == "null" ]]; then + admins=$(jq -r ".metadata.adminAccount" <<<"${json}") + fi + [[ "${admins}" != "null" ]] && echo "Admins: ${admins}" + + echo -n "Number of users: " + jq -r ".usage.users.total" <<<"${json}" + + # shellcheck disable=SC2155 + local active=$(jq -r ".usage.users.activeMonth" <<<"${json}") + [[ "${active}" != "null" ]] && echo "Active user (month): ${active}" + + echo -n "Open registrations: " + jq -r ".openRegistrations" <<<"${json}" + + # shellcheck disable=SC2155 + local restrictions=$(jq -r ".metadata.federation.mrf_simple" <<<"${json}") + [[ "${restrictions}" != "null" ]] && echo "Federation restrictions: ${restrictions}" + + if [[ "${implementation}" == "pleroma" ]]; then + echo "Further info, maybe: https://${instance}/about" + elif [[ "${implementation}" == "funkwhale" ]]; then + echo -n "Artists / tracks: " + jq -rj ".metadata.library.artists.total" <<<"${json}" + echo -n " / " + jq -r ".metadata.library.tracks.total" <<<"${json}" + elif [[ "${implementation}" == "mastodon" ]]; then + echo "Further info, maybe: https://${instance}/about/more" + elif [[ "${implementation}" == "pixelfed" ]]; then + echo "Further info, maybe: https://${instance}/site/terms" + elif [[ "${implementation}" == "peertube" ]]; then + echo "Further info, maybe: https://${instance}/about/instance" + fi +} + +function parse_mastodon() +{ + local instance="${1}" + local mastoinfo="${2}" + json=$(curl -s "${mastoinfo}") + local implementation="" + + if grep -Eq '"is_(pro|verified)"' <<<"${json}"; then + implementation="Gab" + elif grep -Eq '"version":"[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\."' <<<"${json}"; then + implementation="Florence" + elif grep -Eq '"version":"[0-9]+\.[0-9]+\.[0-9]+"' <<<"${json}"; then + implementation="Mastodon" + elif grep -Eq '"version":"[0-9]+\.[0-9]+\.[0-9][\._\-]?[a-zA-Z]+"' <<<"${json}"; then + implementation="Mastodon fork" + fi + if [[ -n "${implementation}" ]]; then + # shellcheck disable=SC2155 + local version=$(jq -r ".version" <<<"${json}") + echo "${implementation} ${version} detected." + fi + + echo -n "Description: " + # shellcheck disable=SC2046 + striphtml $(jq ".description" <<<"${json}") + + # shellcheck disable=SC2155 + local admin=$(jq -r ".contact_account.acct" <<<"${json}") + [[ -n "${admin}" ]] && echo "Admin: https://${instance}/users/${admin}" + + # shellcheck disable=SC2155 + local email=$(jq -r ".email" <<<"${json}") + [[ -n "${email}" ]] && echo "E-Mail: ${email}" + + echo -n "Number of users: " + jq -r ".stats.user_count" <<<"${json}" + + echo -n "Open registrations: " + jq -r ".registrations" <<<"${json}" + + if [[ "${implementation}" == "Mastodon" + || "${implementation}" == "Mastodon fork" + || "${implementation}" == "Florence" ]]; then + echo "Further info, maybe: https://${instance}/about/more" + elif [[ "${implementation}" == "Gab" ]]; then + echo "Further info, maybe: https://${instance}/about" + fi +} + +function parse_misskey() +{ + local instance="${1}" + local misskeyinfo="${2}" + json=$(curl -s -X POST "${misskeyinfo}") + + # shellcheck disable=SC2155 + local version=$(jq -r ".version" <<<"${json}") + echo "Misskey ${version} detected." + + # shellcheck disable=SC2155 + local email=$(jq -r ".maintainerEmail" <<<"${json}") + [[ -n "${email}" ]] && echo "E-Mail: ${email}" + + # shellcheck disable=SC2155 + local users=$(curl -s -X POST "https://${instance}/api/stats" | jq -r ".usersCount") + [[ -n ${users} ]] && echo "Number of users: ${users}" + + echo -n "Open registrations: " + jq -r ".features.registration" <<<"${json}" + + # shellcheck disable=SC2155 + local tos=$(jq -r ".ToSUrl" <<<"${json}") + [[ -n "${tos}" ]] && echo "ToS: ${tos}" +} + function main() { local instance="${1}" @@ -29,126 +163,13 @@ function main() if curl -sILf -X POST "${misskeyinfo}" | grep -iq 'Content-Type: application/json'; then echo "Misskey API found." - json=$(curl -s -X POST "${misskeyinfo}") - - # shellcheck disable=SC2155 - local version=$(jq -r ".version" <<<"${json}") - echo "Misskey ${version} detected." - - # shellcheck disable=SC2155 - local email=$(jq -r ".maintainerEmail" <<<"${json}") - [[ -n "${email}" ]] && echo "E-Mail: ${email}" - - # shellcheck disable=SC2155 - local users=$(curl -s -X POST "https://${instance}/api/stats" | jq -r ".usersCount") - [[ -n ${users} ]] && echo "Number of users: ${users}" - - echo -n "Open registrations: " - jq -r ".features.registration" <<<"${json}" - - # shellcheck disable=SC2155 - local tos=$(jq -r ".ToSUrl" <<<"${json}") - [[ -n "${tos}" ]] && echo "ToS: ${tos}" + parse_misskey "${instance}" "${misskeyinfo}" elif curl -sILf "${nodeinfo}" | grep -iq 'Content-Type: application/json'; then echo "Nodeinfo found." - json=$(curl -s "${nodeinfo}") - - # shellcheck disable=SC2155 - local implementation=$(jq -r ".software.name" <<<"${json}") - # shellcheck disable=SC2155 - local version=$(jq -r ".software.version" <<<"${json}") - echo "${implementation} ${version} detected." - - # shellcheck disable=SC2155 - local description=$(jq -r ".metadata.nodeDescription" <<<"${json}") - if [[ "${description}" == "null" ]]; then - description=$(jq -r ".metadata.shortDescription" <<<"${json}") - fi - if [[ "${description}" == "null" ]]; then - description=$(jq -r ".metadata.nodeName" <<<"${json}") - fi - [[ "${description}" != "null" ]] && echo "Description: ${description}" - - # shellcheck disable=SC2155 - local admins=$(jq -r ".metadata.staffAccounts" <<<"${json}") - if [[ "${admins}" == "null" ]]; then - admins=$(jq -r ".metadata.adminAccount" <<<"${json}") - fi - [[ "${admins}" != "null" ]] && echo "Admins: ${admins}" - - echo -n "Number of users: " - jq -r ".usage.users.total" <<<"${json}" - - # shellcheck disable=SC2155 - local active=$(jq -r ".usage.users.activeMonth" <<<"${json}") - [[ "${active}" != "null" ]] && echo "Active user (month): ${active}" - - echo -n "Open registrations: " - jq -r ".openRegistrations" <<<"${json}" - - # shellcheck disable=SC2155 - local restrictions=$(jq -r ".metadata.federation.mrf_simple" <<<"${json}") - [[ "${restrictions}" != "null" ]] && echo "Federation restrictions: ${restrictions}" - - if [[ "${implementation}" == "pleroma" ]]; then - echo "Further info, maybe: https://${instance}/about" - elif [[ "${implementation}" == "funkwhale" ]]; then - echo -n "Artists / tracks: " - jq -rj ".metadata.library.artists.total" <<<"${json}" - echo -n " / " - jq -r ".metadata.library.tracks.total" <<<"${json}" - elif [[ "${implementation}" == "mastodon" ]]; then - echo "Further info, maybe: https://${instance}/about/more" - elif [[ "${implementation}" == "pixelfed" ]]; then - echo "Further info, maybe: https://${instance}/site/terms" - elif [[ "${implementation}" == "peertube" ]]; then - echo "Further info, maybe: https://${instance}/about/instance" - fi + parse_nodeinfo "${instance}" "${nodeinfo}" elif curl -sILf "${mastoinfo}" | grep -iq 'Content-Type: application/json'; then echo "Mastodon API found." - json=$(curl -s "${mastoinfo}") - local implementation="" - - if grep -Eq '"is_(pro|verified)"' <<<"${json}"; then - implementation="Gab" - elif grep -Eq '"version":"[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\."' <<<"${json}"; then - implementation="Florence" - elif grep -Eq '"version":"[0-9]+\.[0-9]+\.[0-9]+"' <<<"${json}"; then - implementation="Mastodon" - elif grep -Eq '"version":"[0-9]+\.[0-9]+\.[0-9][\._\-]?[a-zA-Z]+"' <<<"${json}"; then - implementation="Mastodon fork" - fi - if [[ -n "${implementation}" ]]; then - # shellcheck disable=SC2155 - local version=$(jq -r ".version" <<<"${json}") - echo "${implementation} ${version} detected." - fi - - echo -n "Description: " - # shellcheck disable=SC2046 - striphtml $(jq ".description" <<<"${json}") - - # shellcheck disable=SC2155 - local admin=$(jq -r ".contact_account.acct" <<<"${json}") - [[ -n "${admin}" ]] && echo "Admin: https://${instance}/users/${admin}" - - # shellcheck disable=SC2155 - local email=$(jq -r ".email" <<<"${json}") - [[ -n "${email}" ]] && echo "E-Mail: ${email}" - - echo -n "Number of users: " - jq -r ".stats.user_count" <<<"${json}" - - echo -n "Open registrations: " - jq -r ".registrations" <<<"${json}" - - if [[ "${implementation}" == "Mastodon" - || "${implementation}" == "Mastodon fork" - || "${implementation}" == "Florence" ]]; then - echo "Further info, maybe: https://${instance}/about/more" - elif [[ "${implementation}" == "Gab" ]]; then - echo "Further info, maybe: https://${instance}/about" - fi + parse_mastodon "${instance}" "${mastoinfo}" else echo "Could not detect type of server." curl -I "https://${instance}/"