Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow watchdog to listen to bedrock port if using geyser
Browse files Browse the repository at this point in the history
yurisasc committed Mar 3, 2023
1 parent c12ab62 commit 607d2be
Showing 5 changed files with 30 additions and 12 deletions.
1 change: 1 addition & 0 deletions cdk/.env.sample
Original file line number Diff line number Diff line change
@@ -25,4 +25,5 @@ TWILIO_AUTH_CODE =

DEBUG = false
CDK_NEW_BOOTSTRAP = 1
USE_GEYSER = false

1 change: 1 addition & 0 deletions cdk/lib/config.ts
Original file line number Diff line number Diff line change
@@ -44,4 +44,5 @@ export const resolveConfig = (): StackConfig => ({
authCode: process.env.TWILIO_AUTH_CODE || '',
},
debug: stringAsBoolean(process.env.DEBUG) || false,
useGeyser: stringAsBoolean(process.env.USE_GEYSER) || false,
});
1 change: 1 addition & 0 deletions cdk/lib/minecraft-stack.ts
Original file line number Diff line number Diff line change
@@ -244,6 +244,7 @@ export class MinecraftStack extends Stack {
TWILIOAUTH: config.twilio.authCode,
STARTUPMIN: config.startupMinutes,
SHUTDOWNMIN: config.shutdownMinutes,
GEYSER: config.useGeyser.toString(),
},
logging: config.debug
? new ecs.AwsLogDriver({
8 changes: 8 additions & 0 deletions cdk/lib/types.ts
Original file line number Diff line number Diff line change
@@ -145,6 +145,14 @@ export interface StackConfig {
* - CloudWatch Logs for the `minecraft-ecsfargate-watchdog` ECS Container
*/
debug: boolean;
/**
* Setting to `true` will allow the watchdog to listen for geyser connections.
* Make sure to set `BEDROCK_SERVER_PORT` to your geyser port if it's not the default (`19132`).
*
* @default false
* @example true
*/
useGeyser: boolean;
}

export interface MinecraftEditionConfig {
31 changes: 19 additions & 12 deletions minecraft-ecsfargate-watchdog/watchdog.sh
Original file line number Diff line number Diff line change
@@ -8,6 +8,11 @@
[ -n "$DNSZONE" ] || { echo "DNSZONE env variable must be set to the Route53 Hosted Zone ID" ; exit 1; }
[ -n "$STARTUPMIN" ] || { echo "STARTUPMIN env variable not set, defaulting to a 10 minute startup wait" ; STARTUPMIN=10; }
[ -n "$SHUTDOWNMIN" ] || { echo "SHUTDOWNMIN env variable not set, defaulting to a 20 minute shutdown wait" ; SHUTDOWNMIN=20; }
[ -n "$JAVAPORT" ] || { echo "JAVA_PORT env variable not set, defaulting to 25565" ; JAVAPORT=25565; }
[ -n "$RCONPORT" ] || { echo "RCON_PORT env variable not set, defaulting to 25575" ; RCONPORT=25575; }
[ -n "$BEDROCKPORT" ] || { echo "BEDROCK_PORT env variable not set, defaulting to 19132" ; BEDROCKPORT=19132; }
[ -n "$GEYSER" ] || { echo "GEYSER env variable not set, defaulting to false" ; GEYSER=false; }


function send_notification ()
{
@@ -84,8 +89,8 @@ echo "If we are stuck here, the minecraft container probably failed to start. W
COUNTER=0
while true
do
netstat -atn | grep :25565 | grep LISTEN && EDITION="java" && break
netstat -aun | grep :19132 && EDITION="bedrock" && break
netstat -atn | grep :$JAVAPORT | grep LISTEN && EDITION="java" && break
netstat -aun | grep :$BEDROCKPORT && EDITION="bedrock" && break
sleep 1
COUNTER=$(($COUNTER + 1))
if [ $COUNTER -gt 600 ] ## server has not been detected as starting within 10 minutes
@@ -102,7 +107,7 @@ then
STARTED=0
while [ $STARTED -lt 1 ]
do
CONNECTIONS=$(netstat -atn | grep :25575 | grep LISTEN | wc -l)
CONNECTIONS=$(netstat -atn | grep :"$RCONPORT" | grep LISTEN | wc -l)
STARTED=$(($STARTED + $CONNECTIONS))
if [ $STARTED -gt 0 ] ## minecraft actively listening, break out of loop
then
@@ -113,7 +118,7 @@ then
done
fi

if [ "$EDITION" == "bedrock" ]
if [ "$EDITION" == "bedrock" ] || [ "$Geyser" == "true" ]
then
PINGA="\x01" ## uncommitted ping
PINGB="\x00\x00\x00\x00\x00\x00\x4e\x20" ## time since start in ms. 20 seconds sounds good
@@ -132,10 +137,11 @@ CONNECTED=0
while [ $CONNECTED -lt 1 ]
do
echo Waiting for connection, minute $COUNTER out of $STARTUPMIN...
[ "$EDITION" == "java" ] && CONNECTIONS=$(netstat -atn | grep :25565 | grep ESTABLISHED | wc -l)
[ "$EDITION" == "bedrock" ] && CONNECTIONS=$((echo -en "$BEDROCKPING" && sleep 1) | ncat -w 1 -u 127.0.0.1 19132 | cut -c34- | awk -F\; '{ print $5 }')
[ -n "$CONNECTIONS" ] || CONNECTIONS=0
CONNECTED=$(($CONNECTED + $CONNECTIONS))
[ "$EDITION" == "java" ] && CONNECTIONS_JAVA=$(netstat -atn | grep :"$JAVAPORT" | grep ESTABLISHED | wc -l)
[ "$EDITION" == "bedrock" ] || [ "$Geyser" == "true" ] && CONNECTIONS_BEDROCK=$((echo -en "$BEDROCKPING" && sleep 1) | ncat -w 1 -u 127.0.0.1 "$BEDROCKPORT" | cut -c34- | awk -F\; '{ print $5 }')
[ -n "$CONNECTIONS_JAVA" ] || CONNECTIONS_JAVA=0
[ -n "$CONNECTIONS_BEDROCK" ] || CONNECTIONS_BEDROCK=0
CONNECTED=$(($CONNECTED + $CONNECTIONS_JAVA + $CONNECTIONS_BEDROCK))
COUNTER=$(($COUNTER + 1))
if [ $CONNECTED -gt 0 ] ## at least one active connection detected, break out of loop
then
@@ -154,10 +160,11 @@ echo "We believe a connection has been made, switching to shutdown watcher."
COUNTER=0
while [ $COUNTER -le $SHUTDOWNMIN ]
do
[ "$EDITION" == "java" ] && CONNECTIONS=$(netstat -atn | grep :25565 | grep ESTABLISHED | wc -l)
[ "$EDITION" == "bedrock" ] && CONNECTIONS=$((echo -en "$BEDROCKPING" && sleep 1) | ncat -w 1 -u 127.0.0.1 19132 | cut -c34- | awk -F\; '{ print $5 }')
[ -n "$CONNECTIONS" ] || CONNECTIONS=0
if [ $CONNECTIONS -lt 1 ]
[ "$EDITION" == "java" ] && CONNECTIONS_JAVA=$(netstat -atn | grep :"$JAVAPORT" | grep ESTABLISHED | wc -l)
[ "$EDITION" == "bedrock" ] || [ "$Geyser" == "true" ] && CONNECTIONS_BEDROCK=$((echo -en "$BEDROCKPING" && sleep 1) | ncat -w 1 -u 127.0.0.1 "$BEDROCKPORT" | cut -c34- | awk -F\; '{ print $5 }')
[ -n "$CONNECTIONS_JAVA" ] || CONNECTIONS_JAVA=0
[ -n "$CONNECTIONS_BEDROCK" ] || CONNECTIONS_BEDROCK=0
if [ $CONNECTIONS_JAVA -lt 1 ] && [ $CONNECTIONS_BEDROCK -lt 1 ]
then
echo "No active connections detected, $COUNTER out of $SHUTDOWNMIN minutes..."
COUNTER=$(($COUNTER + 1))

0 comments on commit 607d2be

Please sign in to comment.