From 607d2befa3bc0f6312e48cefb7bad5392780ad98 Mon Sep 17 00:00:00 2001 From: yurisasc Date: Fri, 3 Mar 2023 12:03:04 +1000 Subject: [PATCH] Allow watchdog to listen to bedrock port if using geyser --- cdk/.env.sample | 1 + cdk/lib/config.ts | 1 + cdk/lib/minecraft-stack.ts | 1 + cdk/lib/types.ts | 8 ++++++ minecraft-ecsfargate-watchdog/watchdog.sh | 31 ++++++++++++++--------- 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/cdk/.env.sample b/cdk/.env.sample index a924464..3e01599 100644 --- a/cdk/.env.sample +++ b/cdk/.env.sample @@ -25,4 +25,5 @@ TWILIO_AUTH_CODE = DEBUG = false CDK_NEW_BOOTSTRAP = 1 +USE_GEYSER = false diff --git a/cdk/lib/config.ts b/cdk/lib/config.ts index 58178a6..d88cb15 100644 --- a/cdk/lib/config.ts +++ b/cdk/lib/config.ts @@ -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, }); diff --git a/cdk/lib/minecraft-stack.ts b/cdk/lib/minecraft-stack.ts index 49a7816..483cf4b 100644 --- a/cdk/lib/minecraft-stack.ts +++ b/cdk/lib/minecraft-stack.ts @@ -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({ diff --git a/cdk/lib/types.ts b/cdk/lib/types.ts index 40506b3..7824ca1 100644 --- a/cdk/lib/types.ts +++ b/cdk/lib/types.ts @@ -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 { diff --git a/minecraft-ecsfargate-watchdog/watchdog.sh b/minecraft-ecsfargate-watchdog/watchdog.sh index 0970d78..2106c8c 100755 --- a/minecraft-ecsfargate-watchdog/watchdog.sh +++ b/minecraft-ecsfargate-watchdog/watchdog.sh @@ -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))