From 609d5f3833c9e40701a5b1164c7ce6fc14d58cd7 Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 13 Dec 2022 03:33:05 +0100 Subject: [PATCH] Protect play with hard-coded password. --- server/chess_move.go | 9 +++++++-- server/server.go | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/server/chess_move.go b/server/chess_move.go index 56b2f88..25d3361 100644 --- a/server/chess_move.go +++ b/server/chess_move.go @@ -46,8 +46,13 @@ func parseMove(received string) (*chessMove, error) { } func moveToString(move chessMove) string { - - str := fmt.Sprint(move.startSquare.col) + fmt.Sprint(move.startSquare.row) + + var str string + if !move.realMove { + str = "just checking: " + } else { + str = "move done: " + } + str = str + fmt.Sprint(move.startSquare.col) + fmt.Sprint(move.startSquare.row) + " " + fmt.Sprint(move.endSquare.col) + fmt.Sprint(move.endSquare.row) return str diff --git a/server/server.go b/server/server.go index fae0b42..fc97a3b 100644 --- a/server/server.go +++ b/server/server.go @@ -18,6 +18,20 @@ func SocketHandler(w http.ResponseWriter, r *http.Request) { return } defer conn.Close() + defer log.Println("SocketHandler exited") + + // Check if first message is the passphrase + msg_type, msg, err := conn.ReadMessage() + if err != nil { + return + } + + if string(msg) != "pw NC4EjHvRcsltY3ibWuYDH9OXbKgDXppfnHNCi1K4jktMspoeZjBnOPExxCpDms7zmxijoKCSaSlZ1fWclfWr7Q32DJnv2k87Z5uM" { + conn.WriteMessage(msg_type, []byte("Wrong password. Connection refused")) + return + } else { + conn.WriteMessage(msg_type, []byte("Password correct. Let's play")) + } var game = NewChessGame() game.handle(conn)