Files
mc-connect/mc

182 lines
5.5 KiB
Bash
Executable File

#!/usr/bin/env bash
args=("$@")
CYAN="\033[1;96m"
RED="\033[0;91m"
GREEN="\033[0;92m"
RESET='\033[0m'
readonly base_url="https://api.my-mc.link/"
readonly headers=( -H "Accept: application/json" -H "Content-Type: application/json" -H "x-my-mc-auth: ${MY_MC_API_KEY}" )
# command format: '<endpoint> <method> [options]'
declare -rA commands=(
[hello]='hello GET'
[time]='time GET'
[stats]='stats GET'
[log]='log GET'
[start]='start GET'
[stop]='stop GET'
[restart]='restart GET'
[my-link]='my-link GET'
[my-geyser-link]='my-geyser-link GET'
[my-sftp]='my-sftp GET'
[my-hash]='my-hash GET'
[my-geyser-hash]='my-geyser-hash GET'
[my-hash-sftp]='my-hash-sftp GET'
[list-players]='list-players GET'
[website]='website GET'
[map]='map GET'
[status-minecraft]='status/Minecraft GET'
[status-bedrock]='status/Bedrock GET'
[status-sftp]='status/SFTP GET'
[ban]='ban POST {"username":"%s"}'
[unban]='unban POST {"username":"%s"}'
[say]='say POST {"message":"%s"}'
[tell]='tell POST {"username":"%s","message":"%s"}'
[console]='console POST {"command":"%s"}'
[give]='give POST {"username":"%s","item":"%s","amount":"%s"}'
[install]='install POST {"mod":"%s"}'
[uninstall]='uninstall POST {"mod":"%s"}'
[search]='search POST {"mod":"%s"}'
[mod-list]='mod-list GET'
[backup]='backup FUNC'
[connect]='connect FUNC'
[check-update]='check_update FUNC game_version'
[version]='version FUNC'
)
function version() {
local message="$(mc console version | jq -r '.message')"
if [[ $message =~ ([0-9]+\.[0-9]+\.[0-9]+) ]]; then
local version="${BASH_REMATCH[0]}"
fi
echo "$version"
}
function check_update() {
local version="${1:-$(version)}"
local url_base="$(mc website | jq -r '.message')"
local mods="$(curl -sS ${url_base}/mods.json | jq -c '.'))"
local -a result=("MOD\tID\tAVAILABLE\n")
local item id name latest available has_version available_versions
while read -r item; do
name="$(jq -r '.name' <<<"$item")"
id="$(jq -r '.id' <<<"$item")"
available_versions="$(curl -sS -X GET "https://api.modrinth.com/v2/project/$id" | jq -r '.game_versions')"
has_version="$(jq "contains([\"$version\"])" <<<"$available_versions")"
if [[ "$has_version" == "true" ]]; then
available="${GREEN}true${RESET}";
else
available="${RED}false${RESET}";
fi
result+=("$name\t$id\t$available\n")
done < <(jq -c '.[]' <<<"$mods")
echo -e "${result[*]}" | column -t -s $'\t'
}
function connect() {
if [[ ! $(which docker) ]]; then echo "docker is required"; exit 1; fi
docker run --rm \
-e MODE=my-mc \
-e PORT=25565 \
-e HOST=0.0.0.0 \
-e PUBLIC=false \
-e MY_MC_API_KEY=$MY_MC_API_KEY \
-p 25565:25565 anaxios/holesail:testing
echo ""
}
function backup() {
if [[ ! $(which lftp) ]]; then echo "lftp is required"; exit 1; fi
local sftp_credentials="$(mc my-sftp)"
if [[ "$(jq -r '.success' <<<"${sftp_credentials}" )" != "true" ]]; then
echo "ERROR: failed to get sftp login info";
exit 1;
fi
local host="$(jq -r '.hostname' <<<"${sftp_credentials}")"
local username="$(jq -r '.user' <<<"${sftp_credentials}")"
local port="$(jq -r '.port' <<<"${sftp_credentials}")"
local password="$(jq -r '.password' <<<"${sftp_credentials}")"
local localdir="${MY_MC_BACKUP_DIR:-${HOME}/my-mc_backup/}"
if [[ ! -d ${localdir} ]]; then mkdir -p ${localdir}; fi
pushd "${localdir}"
lftp -u "${username}","${password}" "${host}":"${port}" \
-e "set sftp:connect-program 'ssh -o StrictHostKeyChecking=no'; mirror -c --use-pget minecraft/ minecraft/; quit"
popd
}
function usage() {
printf "API key for my-mc must be exported using 'export MY_MC_API_KEY=<my-mc api key>'\n\n"
printf "Backup directory can be changed from default ~/mc-mc_backup/ by using 'export MY_MC_BACKUP_DIR=</path/to/dir/>'\n\n"
printf "Positional arguments will fill the JSON objects with values from left to right.\n\n"
printf "COMMANDS:\n"
local command
for command in "${!commands[@]}"; do
local endpoint method body
read -r endpoint method body <<<"${commands[${command}]}"
echo -e "\t ${command} ${body}" | sed 's/[":\{\}]//g' |sed 's/%s//g'| sed 's/[,]/ /g'
done
printf "\n"
}
function main() {
# remove command from arg list while preserving quoted strings.
args=("${args[@]:1}")
if [[ ! $(which curl) ]]; then echo "curl is required"; exit 1; fi
if [[ ! $(which jq) ]]; then echo "jq is required"; exit 1; fi
# Check if key does NOT exists in command array
if [[ ! -v commands[$1] ]]; then
printf "${0}: invalid option -- $1\n\n"
usage
exit 1
fi
local endpoint method body
read -r endpoint method body <<<"${commands[$1]}"
case "${method}" in
"POST")
curl -sS "${headers[@]}" -X "${method}" "${base_url}${endpoint}" \
-d "$(printf "${body}\n" "${args[@]}")"
echo ""
;;
"GET")
curl -sS "${headers[@]}" -X "${method}" "${base_url}${endpoint}"
echo ""
;;
"FUNC")
eval "${endpoint}" "${args[@]}"
;;
*)
echo "ERROR: Invalid state. Check for typo in command array."
;;
esac
exit 0
}
if [[ "$(basename "$0")" == "mc" ]]; then
main "${args[@]}"
fi