2020-05-20 13:17:58 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2022-09-03 21:48:14 +00:00
|
|
|
. /auto/autopause-fcns.sh
|
2020-05-20 13:17:58 +00:00
|
|
|
|
2022-07-30 17:06:22 +00:00
|
|
|
# shellcheck source=../../scripts/start-utils
|
|
|
|
. "${SCRIPTS:-/}start-utils"
|
|
|
|
if isTrue "${DEBUG_AUTOPAUSE}"; then
|
|
|
|
set -x
|
|
|
|
fi
|
2021-01-02 17:27:03 +00:00
|
|
|
|
|
|
|
autopause_error_loop() {
|
|
|
|
logAutopause "Available interfaces within the docker container:"
|
2023-02-05 02:53:46 +00:00
|
|
|
logAutopause " $(mc-image-helper network-interfaces)"
|
2021-01-02 17:27:03 +00:00
|
|
|
logAutopause "Please set the environment variable AUTOPAUSE_KNOCK_INTERFACE to the interface that handles incoming connections."
|
|
|
|
logAutopause "If unsure which interface to choose, run the ifconfig command in the container."
|
|
|
|
logAutopause "Autopause failed to initialize. This log entry will be printed every 30 minutes."
|
2020-05-20 13:17:58 +00:00
|
|
|
while :
|
|
|
|
do
|
2021-01-12 22:05:12 +00:00
|
|
|
sleep 1800
|
|
|
|
logAutopause "Autopause failed to initialize."
|
2020-05-20 13:17:58 +00:00
|
|
|
done
|
2021-01-12 22:05:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# wait for java process to be started
|
|
|
|
while :
|
|
|
|
do
|
|
|
|
if java_process_exists ; then
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
sleep 0.1
|
|
|
|
done
|
|
|
|
|
|
|
|
# check for interface existence
|
|
|
|
if [[ -z "$AUTOPAUSE_KNOCK_INTERFACE" ]] ; then
|
|
|
|
logAutopause "AUTOPAUSE_KNOCK_INTERFACE variable must not be empty!"
|
|
|
|
autopause_error_loop
|
|
|
|
fi
|
2023-02-05 02:53:46 +00:00
|
|
|
if ! mc-image-helper network-interfaces --check="$AUTOPAUSE_KNOCK_INTERFACE" ; then
|
2021-01-12 22:05:12 +00:00
|
|
|
logAutopause "Selected interface \"$AUTOPAUSE_KNOCK_INTERFACE\" does not exist!"
|
|
|
|
autopause_error_loop
|
|
|
|
fi
|
|
|
|
|
2022-07-30 17:06:22 +00:00
|
|
|
knockdArgs=(-c /tmp/knockd-config.cfg -d -i "$AUTOPAUSE_KNOCK_INTERFACE")
|
|
|
|
if isTrue "${DEBUG_AUTOPAUSE}"; then
|
|
|
|
knockdArgs+=(-D)
|
|
|
|
fi
|
|
|
|
|
|
|
|
sudo /usr/sbin/knockd "${knockdArgs[@]}"
|
2021-01-12 22:05:12 +00:00
|
|
|
if [ $? -ne 0 ] ; then
|
2020-05-23 14:15:10 +00:00
|
|
|
logAutopause "Failed to start knockd daemon."
|
2021-01-12 22:05:12 +00:00
|
|
|
logAutopause "Probable cause: Unable to attach to interface \"$AUTOPAUSE_KNOCK_INTERFACE\"."
|
|
|
|
autopause_error_loop
|
2020-05-20 13:17:58 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
STATE=INIT
|
|
|
|
|
|
|
|
while :
|
|
|
|
do
|
2022-07-30 17:06:22 +00:00
|
|
|
isTrue "${DEBUG_AUTOPAUSE}" && log "DEBUG: autopause state = $STATE"
|
2020-05-20 13:17:58 +00:00
|
|
|
case X$STATE in
|
|
|
|
XINIT)
|
|
|
|
# Server startup
|
|
|
|
if mc_server_listening ; then
|
|
|
|
TIME_THRESH=$(($(current_uptime)+$AUTOPAUSE_TIMEOUT_INIT))
|
2022-07-30 17:06:22 +00:00
|
|
|
logAutopause "MC Server listening for connections - pausing in $AUTOPAUSE_TIMEOUT_INIT seconds"
|
2020-05-20 13:17:58 +00:00
|
|
|
STATE=K
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
XK)
|
|
|
|
# Knocked
|
|
|
|
if java_clients_connected ; then
|
2020-05-23 14:15:10 +00:00
|
|
|
logAutopause "Client connected - waiting for disconnect"
|
2020-05-20 13:17:58 +00:00
|
|
|
STATE=E
|
|
|
|
else
|
|
|
|
if [[ $(current_uptime) -ge $TIME_THRESH ]] ; then
|
2022-07-30 17:06:22 +00:00
|
|
|
logAutopause "No client connected since startup / knocked - pausing"
|
2022-09-03 21:48:14 +00:00
|
|
|
/auto/pause.sh
|
2020-05-20 13:17:58 +00:00
|
|
|
STATE=S
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
XE)
|
|
|
|
# Established
|
|
|
|
if ! java_clients_connected ; then
|
|
|
|
TIME_THRESH=$(($(current_uptime)+$AUTOPAUSE_TIMEOUT_EST))
|
2022-07-30 17:06:22 +00:00
|
|
|
logAutopause "All clients disconnected - pausing in $AUTOPAUSE_TIMEOUT_EST seconds"
|
2020-05-20 13:17:58 +00:00
|
|
|
STATE=I
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
XI)
|
|
|
|
# Idle
|
|
|
|
if java_clients_connected ; then
|
2020-05-23 14:15:10 +00:00
|
|
|
logAutopause "Client reconnected - waiting for disconnect"
|
2020-05-20 13:17:58 +00:00
|
|
|
STATE=E
|
|
|
|
else
|
|
|
|
if [[ $(current_uptime) -ge $TIME_THRESH ]] ; then
|
2022-07-30 17:06:22 +00:00
|
|
|
logAutopause "No client reconnected - pausing"
|
2022-09-03 21:48:14 +00:00
|
|
|
/auto/pause.sh
|
2020-05-20 13:17:58 +00:00
|
|
|
STATE=S
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
XS)
|
|
|
|
# Stopped
|
|
|
|
if rcon_client_exists ; then
|
2022-09-03 21:48:14 +00:00
|
|
|
/auto/resume.sh
|
2020-05-20 13:17:58 +00:00
|
|
|
fi
|
|
|
|
if java_running ; then
|
|
|
|
if java_clients_connected ; then
|
2020-05-23 14:15:10 +00:00
|
|
|
logAutopause "Client connected - waiting for disconnect"
|
2020-05-20 13:17:58 +00:00
|
|
|
STATE=E
|
|
|
|
else
|
|
|
|
TIME_THRESH=$(($(current_uptime)+$AUTOPAUSE_TIMEOUT_KN))
|
2022-09-03 21:48:14 +00:00
|
|
|
from=unknown
|
|
|
|
if [ -e /var/log/knocked-source ]; then
|
|
|
|
from=$(cat /var/log/knocked-source)
|
|
|
|
fi
|
|
|
|
logAutopause "Server was knocked from $from - waiting for clients or timeout"
|
2020-05-20 13:17:58 +00:00
|
|
|
STATE=K
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
*)
|
2020-05-23 14:15:10 +00:00
|
|
|
logAutopause "Error: invalid state: $STATE"
|
2020-05-20 13:17:58 +00:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
if [[ "$STATE" == "S" ]] ; then
|
|
|
|
# before rcon times out
|
2022-07-30 17:06:22 +00:00
|
|
|
sleep 5
|
2020-05-20 13:17:58 +00:00
|
|
|
else
|
2022-07-30 17:06:22 +00:00
|
|
|
sleep "$AUTOPAUSE_PERIOD"
|
2020-05-20 13:17:58 +00:00
|
|
|
fi
|
|
|
|
done
|