2022-10-22 18:01:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-05-28 15:43:05 +00:00
|
|
|
"context"
|
2023-06-02 19:14:02 +00:00
|
|
|
"encoding/json"
|
2023-09-01 13:29:26 +00:00
|
|
|
"flag"
|
2023-06-02 19:14:02 +00:00
|
|
|
"fmt"
|
2023-06-29 20:20:41 +00:00
|
|
|
"log"
|
2023-06-25 14:11:29 +00:00
|
|
|
"mchess_server/api"
|
2024-05-09 20:29:48 +00:00
|
|
|
"mchess_server/handler"
|
2023-06-25 14:11:29 +00:00
|
|
|
lobbies "mchess_server/lobby_registry"
|
2023-04-18 20:12:05 +00:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2023-04-22 19:41:24 +00:00
|
|
|
"nhooyr.io/websocket"
|
2022-10-22 18:01:55 +00:00
|
|
|
)
|
|
|
|
|
2022-12-18 01:49:10 +00:00
|
|
|
var cert_path = "/etc/letsencrypt/live/chess.sw-gross.de/"
|
|
|
|
var cert_file = cert_path + "fullchain.pem"
|
|
|
|
var key_file = cert_path + "privkey.pem"
|
|
|
|
|
2023-04-18 20:12:05 +00:00
|
|
|
func main() {
|
2023-09-01 13:29:26 +00:00
|
|
|
var debugMode bool
|
|
|
|
|
|
|
|
debugModeLong := flag.Bool("debug", false, "activates debug mode")
|
|
|
|
debugModeShort := flag.Bool("d", false, "activates debug mode")
|
|
|
|
flag.Parse()
|
|
|
|
if *debugModeShort || *debugModeLong {
|
|
|
|
debugMode = true
|
2022-12-18 14:43:27 +00:00
|
|
|
}
|
|
|
|
|
2023-04-18 20:12:05 +00:00
|
|
|
router := gin.Default()
|
2024-05-09 20:29:48 +00:00
|
|
|
router.GET("/api/random", handler.RegisterForRandomGame)
|
|
|
|
router.GET("/api/hostPrivate", handler.HostPrivateGameHandler)
|
|
|
|
router.POST("/api/joinPrivate", handler.JoinPrivateGame)
|
2023-05-28 15:43:05 +00:00
|
|
|
router.GET("/api/ws", registerWebSocketConnection)
|
2024-05-09 20:29:48 +00:00
|
|
|
router.GET("/api/getLobbyForPassphrase/:phrase", handler.GetLobbyForPassphraseHandler)
|
2022-11-19 10:41:10 +00:00
|
|
|
|
2023-09-01 13:29:26 +00:00
|
|
|
if debugMode {
|
2023-04-18 20:12:05 +00:00
|
|
|
log.Println("Starting service WITHOUT TLS")
|
2023-09-23 23:15:52 +00:00
|
|
|
log.Fatal(router.Run(":8080"))
|
2022-12-18 14:56:06 +00:00
|
|
|
} else {
|
2023-06-06 20:58:33 +00:00
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
log.Println("Starting in release mode")
|
|
|
|
log.Println("Starting service with TLS")
|
2023-06-06 21:23:28 +00:00
|
|
|
log.Fatal(router.RunTLS("chess.sw-gross.de:9999", cert_file, key_file))
|
2022-11-19 10:41:10 +00:00
|
|
|
}
|
2023-04-18 20:12:05 +00:00
|
|
|
}
|
2022-11-19 10:41:10 +00:00
|
|
|
|
2023-04-22 19:41:24 +00:00
|
|
|
func registerWebSocketConnection(c *gin.Context) {
|
2023-06-08 18:20:37 +00:00
|
|
|
webSocketConn, err := websocket.Accept(c.Writer, c.Request, &websocket.AcceptOptions{OriginPatterns: []string{"chess.sw-gross.de", "localhost:*"}})
|
2023-05-28 15:43:05 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2023-06-06 20:58:33 +00:00
|
|
|
go waitForAndHandlePlayerID(c, webSocketConn)
|
2023-05-28 15:43:05 +00:00
|
|
|
}
|
2023-04-18 20:12:05 +00:00
|
|
|
|
2023-06-06 20:58:33 +00:00
|
|
|
func waitForAndHandlePlayerID(ctx context.Context, conn *websocket.Conn) {
|
2023-06-02 19:14:02 +00:00
|
|
|
msgType, msg, err := conn.Read(ctx)
|
|
|
|
if err != nil {
|
|
|
|
errorMessage := fmt.Sprintf("Reading from websocket connection did not work: %s", err)
|
|
|
|
log.Println(errorMessage)
|
|
|
|
conn.Close(websocket.StatusCode(400), errorMessage)
|
|
|
|
return
|
|
|
|
}
|
2023-05-28 15:43:05 +00:00
|
|
|
|
2023-11-20 00:25:27 +00:00
|
|
|
log.Println("read from websocket endpoint: ", msgType, string(msg), err)
|
2023-05-28 15:43:05 +00:00
|
|
|
|
2023-06-02 21:41:43 +00:00
|
|
|
var info api.PlayerInfo
|
2023-06-02 19:14:02 +00:00
|
|
|
err = json.Unmarshal(msg, &info)
|
2023-04-22 19:41:24 +00:00
|
|
|
if err != nil {
|
2023-06-02 19:14:02 +00:00
|
|
|
errorMessage := fmt.Sprintf("Unmarshaling message did not work: %s", err)
|
|
|
|
log.Println(errorMessage)
|
|
|
|
conn.Close(websocket.StatusCode(400), errorMessage)
|
2023-05-28 15:43:05 +00:00
|
|
|
return
|
2023-04-22 19:41:24 +00:00
|
|
|
}
|
2023-04-18 20:12:05 +00:00
|
|
|
|
2023-06-29 23:50:04 +00:00
|
|
|
lobby := lobbies.GetLobbyRegistry().GetLobbyByUUID(*info.LobbyID)
|
2023-11-20 00:25:27 +00:00
|
|
|
if lobby == nil {
|
|
|
|
conn.Close(websocket.StatusCode(400), "lobby not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-29 23:50:04 +00:00
|
|
|
player, found := lobby.GetPlayerByUUID(*info.PlayerID)
|
2023-05-28 15:43:05 +00:00
|
|
|
if !found {
|
|
|
|
conn.Close(websocket.StatusCode(400), "player not found")
|
|
|
|
return
|
|
|
|
}
|
2023-11-20 00:25:27 +00:00
|
|
|
if player.Conn.HasWebsocketConnection() {
|
|
|
|
player.Conn.Close("closing existing connection")
|
2023-04-18 20:12:05 +00:00
|
|
|
}
|
2023-11-20 00:25:27 +00:00
|
|
|
lobby.Game.SetWebsocketConnectionFor(ctx, player, conn)
|
2023-05-30 20:01:20 +00:00
|
|
|
log.Println("player after setting connection: ", player)
|
2022-12-14 21:19:47 +00:00
|
|
|
}
|