Protect play with hard-coded password.

This commit is contained in:
Marco 2022-12-13 03:33:05 +01:00
parent 38ac32e460
commit 609d5f3833
2 changed files with 21 additions and 2 deletions

View File

@ -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

View File

@ -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)