From d556e64b9e63f559c5c8398205a05620d71d0c8b Mon Sep 17 00:00:00 2001 From: Lukas Borutta Date: Wed, 14 Oct 2020 18:23:36 +0200 Subject: [PATCH] GO added join message disentangling Go sometimes reads the two join messages as one buffer and causes the script to misinterpret what the client sent. Attempt to disentangle these two messages added. --- go-version/minecraft-server-hibernation.go | 30 +++++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/go-version/minecraft-server-hibernation.go b/go-version/minecraft-server-hibernation.go index f50a987d..be83d5c8 100644 --- a/go-version/minecraft-server-hibernation.go +++ b/go-version/minecraft-server-hibernation.go @@ -260,10 +260,7 @@ func handleClientSocket(clientSocket net.Conn) { // answer to client with ping answerPingReq(clientSocket) - } - - // the client first message is {data, 2} --> the client is trying to join the server - if buffer[dataLen-1] == 2 { + } else if buffer[dataLen-1] == 2 { // the client first message is {data, 2} --> the client is trying to join the server // read second packet (contains the playerName) dataLen, err = clientSocket.Read(buffer) if err != nil { @@ -284,6 +281,31 @@ func handleClientSocket(clientSocket net.Conn) { // answer to client with text in the loadscreen clientSocket.Write(buildMessage("txt", fmt.Sprintf("Server is starting. Please wait... Time left: %d seconds", timeLeftUntilUp))) } + } else { + // Go probably entangled two packets again and player tried to join. Search for the two bytes 211 2 and split byte array after them. + // If no 211 2 in byte array, then ignore request and continue listening. + clientMessage := fmt.Sprintln(buffer[:dataLen]) + logger("Message incomprehensible: \n" + clientMessage) + if bytes.Contains(buffer, []byte{211, 2}) { + logger("Disentangling join message.") + splitBuffer := bytes.SplitAfter(buffer, []byte{211, 2}) + playerName := splitBuffer[1][3:dataLen] + + if serverStatus == "offline" { + // client is trying to join the server and serverStatus == "offline" --> issue startMinecraftServer() + startMinecraftServer() + log.Printf("*** %s tried to join from %s:%s to %s:%s\n", playerName, clientAddress, config.Advanced.ListenPort, config.Advanced.TargetHost, config.Advanced.TargetPort) + // answer to client with text in the loadscreen + clientSocket.Write(buildMessage("txt", fmt.Sprintf("Server start command issued. Please wait... Time left: %d seconds", timeLeftUntilUp))) + + } else if serverStatus == "starting" { + log.Printf("*** %s tried to join from %s:%s to %s:%s during server startup\n", playerName, clientAddress, config.Advanced.ListenPort, config.Advanced.TargetHost, config.Advanced.TargetPort) + // answer to client with text in the loadscreen + clientSocket.Write(buildMessage("txt", fmt.Sprintf("Server is starting. Please wait... Time left: %d seconds", timeLeftUntilUp))) + } + } else { + logger("Cannot interpret message. Will ignore and go on to listen for new messages.") + } } // since the server is still not online, close the client connection