forked from thijsvanloef/palworld-server-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest_api.sh
93 lines (88 loc) · 2.64 KB
/
rest_api.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/bash
SCRIPT_DIR=${SCRIPT_DIR:-$(dirname "$(readlink -fn "${0}")")}
#shellcheck source=scripts/helper_functions.sh
source "${SCRIPT_DIR}/helper_functions.sh"
SCRIPT=$(basename "${0}")
help="-h|--help"
if [ $# -lt 1 ] || [[ ${1} =~ ${help} ]]; then
cat << EOF
Usage: ${SCRIPT} <api> [options]
api:
announce <json> ... announce message.
ban <json> ... ban player.
info ... show server informations.
kick <json> ... kick player.
metrics ... show server metrics.
players ... show online players.
save ... save the world.
settings ... show server settings.
shutdown <json> ... shutdown server.
stop ... force stop server.
unban <json> ... unban player.
options:
'{...}' ... json.
- ... json from stdin.
-h, --help ... help.
EOF
exit 1
fi
if [ ! "${REST_API_ENABLED,,}" = true ]; then
echo "ERROR: REST_API_ENABLED=False"
exit 1
fi
api="${1}"
json="${2}"
api_required_json="announce|ban|kick|shutdown|unban"
if [[ ${api} =~ ${api_required_json} ]]; then
if [ $# -lt 2 ]; then
echo "input json required."
exit 1
fi
if [ "${json}" = "-" ]; then
json="$(cat -)"
elif [[ ! ${json} =~ ^\{ ]]; then
usage="Usage: ${SCRIPT} ${api}"
case ${api} in
"announce")
if [[ ${json} =~ ${help} ]]; then
echo "${usage} <message>"
exit 1
fi
json="{\"message\":\"${2}\"}"
;;
"ban")
if [[ ${json} =~ ${help} ]]; then
echo "${usage} <steam_00000000000000000> [message]"
exit 1
fi
msg=${3:-You are banned.}
json="{\"userid\":\"${2}\",\"message\":\"${msg}\"}"
;;
"kick")
if [[ ${json} =~ ${help} ]]; then
echo "${usage} <steam_00000000000000000> [message]"
exit 1
fi
msg=${3:-You are kicked.}
json="{\"userid\":\"${2}\",\"message\":\"${msg}\"}"
;;
"shutdown")
if [[ ${json} =~ ${help} ]]; then
echo "${usage} <sec> [message]"
exit 1
fi
sec=${2}
msg=${3:-Server will shutdown in ${sec} sec.}
json="{\"waittime\":${sec},\"message\":\"${msg}\"}"
;;
"unban")
if [[ ${json} =~ ${help} ]]; then
echo "${usage} <steam_00000000000000000>"
exit 1
fi
json="{\"userid\":\"${2}\"}"
;;
esac
fi
fi
REST_API "${api}" "${json}" && echo ""