feat(backend): websockets with player co-ordination and JWT auto-generation

This commit is contained in:
Muhammad Nauman Raza 2023-08-20 12:50:21 +01:00
parent 33f7ba8fcd
commit a57b68891b
4 changed files with 184 additions and 4 deletions

View file

@ -5,6 +5,7 @@ go 1.20
require ( require (
github.com/charmbracelet/log v0.2.3 github.com/charmbracelet/log v0.2.3
github.com/golang-jwt/jwt v3.2.2+incompatible github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/gorilla/websocket v1.5.0
github.com/joho/godotenv v1.5.1 github.com/joho/godotenv v1.5.1
github.com/mattn/go-sqlite3 v1.14.17 github.com/mattn/go-sqlite3 v1.14.17
golang.org/x/crypto v0.11.0 golang.org/x/crypto v0.11.0

View file

@ -9,6 +9,8 @@ github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi
github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=

View file

@ -1,6 +1,7 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
_ "github.com/joho/godotenv/autoload" _ "github.com/joho/godotenv/autoload"
"net/http" "net/http"
@ -8,20 +9,92 @@ import (
// Stylish stuff // Stylish stuff
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
// Websockets
"github.com/gorilla/websocket"
) )
type App struct { type App struct {
UserHandler *UserHandler UserHandler *UserHandler
clients map[*websocket.Conn]bool
broadcast chan []byte
upgrader websocket.Upgrader
} }
type Player struct {
X int `json:"x"`
Y int `json:"y"`
}
type GameState struct {
Players []Player `json:"players"`
}
func (s *App) handlePlayerMovement(conn *websocket.Conn) {
var player Player
player.X = 0
player.Y = 0
for {
// Read the next message from the client
_, message, err := conn.ReadMessage()
if err != nil {
log.Error("Failed to read message:", err)
break
}
// Update the player's position based on the received message
switch string(message) {
case "up":
player.Y--
case "down":
player.Y++
case "left":
player.X--
case "right":
player.X++
}
// Create a JSON representation of the game state
gameState := GameState{
Players: []Player{player},
}
gameStateData, err := json.Marshal(gameState)
if err != nil {
log.Error("Failed to marshal game state:", err)
break
}
// Broadcast the game state to all clients
s.broadcast <- gameStateData
}
}
func (s *App) handleBroadcasts() {
for {
// Read the next message from the broadcast channel
message := <-s.broadcast
// Broadcast the message to all clients
for client := range s.clients {
err := client.WriteMessage(websocket.TextMessage, message)
if err != nil {
log.Error("Failed to write message:", err)
}
}
}
}
var upgrader = websocket.Upgrader{}
// Define the serve function // Define the serve function
func (h *App) ServeHTTP(res http.ResponseWriter, req *http.Request) { func (s *App) ServeHTTP(res http.ResponseWriter, req *http.Request) {
var head string var head string
head, req.URL.Path = ShiftPath(req.URL.Path) head, req.URL.Path = ShiftPath(req.URL.Path)
switch head { switch head {
// Start the user handler should the requested user be found // Start the user handler should the requested user be found
case "user": case "user":
h.UserHandler.Handle(res, req) s.UserHandler.Handle(res, req)
// Return a `Not Found` if the user is not found // Return a `Not Found` if the user is not found
default: default:
http.Error(res, "Not Found", http.StatusNotFound) http.Error(res, "Not Found", http.StatusNotFound)
@ -29,7 +102,75 @@ func (h *App) ServeHTTP(res http.ResponseWriter, req *http.Request) {
} }
// Run the server // Run the server
func (s *App) Run() {
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
// Upgrade the connection to a WebSocket connection
conn, err := s.upgrader.Upgrade(w, r, nil)
if err != nil {
log.Error("Failed to upgrade connection:", err)
return
}
// Add the client connection to the clients map
s.clients[conn] = true
// Log when a client connects
log.Info("Client connected:", conn.RemoteAddr())
// Allow the server to handle player movement
go s.handlePlayerMovement(conn)
// Close the connection and remove it from the clients map
defer func() {
// Log when a client disconnects
log.Info("Client disconnected:", conn.RemoteAddr())
conn.Close()
delete(s.clients, conn)
}()
// Handle incoming messages
for {
_, message, err := conn.ReadMessage()
if err != nil {
log.Error("Failed to read message:", err)
break
}
// Broadcast the received message to all clients
s.broadcast <- message
}
})
go s.handleBroadcasts()
http.HandleFunc("/echo", func(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Fatal(err)
}
for {
msgType, msg, err := conn.ReadMessage()
if err != nil {
return
}
fmt.Printf("%s sent: %s\n", conn.RemoteAddr(), string(msg))
if err = conn.WriteMessage(msgType, msg); err != nil {
return
}
}
})
}
func main() { func main() {
// Make the jwt_secret file within the server configuration directory
makeSecret()
// Initialise the user handler // Initialise the user handler
user_handler, err := NewUserHandler() user_handler, err := NewUserHandler()
@ -49,4 +190,5 @@ func main() {
// Log that the program has successfully started listening to the port // Log that the program has successfully started listening to the port
log.Info(fmt.Sprintf("Ambition backend listening to port %v", port)) log.Info(fmt.Sprintf("Ambition backend listening to port %v", port))
http.ListenAndServe(":"+port, a) http.ListenAndServe(":"+port, a)
} }

View file

@ -7,8 +7,13 @@ import (
"io" "io"
"os" "os"
// A cute logging system
"github.com/charmbracelet/log"
// Encryption // Encryption
"crypto/rand"
"encoding/json" "encoding/json"
"math/big"
"net/http" "net/http"
"github.com/golang-jwt/jwt" "github.com/golang-jwt/jwt"
@ -19,6 +24,11 @@ import (
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
) )
// Define the configuration directories
var userConfigDirectory, err = os.UserConfigDir()
var serverConfigDirectory = fmt.Sprintf("%v/ambition/server", userConfigDirectory)
var jwtPath = fmt.Sprintf("%v/jwt_secret", serverConfigDirectory)
// Define the user request struct // Define the user request struct
type UserRequest struct { type UserRequest struct {
Name string `json:"name"` Name string `json:"name"`
@ -37,6 +47,30 @@ func (ur *UserRequest) Parse(req *http.Request) error {
return nil return nil
} }
// A function to write a randomly-generated cryptographically secure 24-character string to a file
func makeSecret() {
const characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-"
ret := make([]byte, 24)
for i := 0; i < 24; i++ {
num, err := rand.Int(rand.Reader, big.NewInt(int64(len(characters))))
if err != nil {
log.Fatal(err)
}
ret[i] = characters[num.Int64()]
}
// Check if the Ambition server config folder exists, otherwise make it
_, err2 := os.Stat(serverConfigDirectory)
if os.IsNotExist(err2) {
log.Info("Ambition backend config folder does not exist, creating...")
os.MkdirAll(serverConfigDirectory, 0755)
log.Info("Made Ambition backend config folder!")
}
// Write the secret to the file
os.WriteFile(jwtPath, ret, 0755)
}
// Define the user handler struct // Define the user handler struct
type UserHandler struct { type UserHandler struct {
db *sql.DB db *sql.DB
@ -51,8 +85,9 @@ func NewUserHandler() (*UserHandler, error) {
return nil, err return nil, err
} }
// Define the JSON web token // Get the JSON web token
jwt_secret_str := os.Getenv("JWT_SECRET") jwt_secret_bytes, err := os.ReadFile(jwtPath)
jwt_secret_str := string(jwt_secret_bytes)
// Return any errors // Return any errors
if jwt_secret_str == "" { if jwt_secret_str == "" {
return nil, errors.New("no JWT_SECRET provided in .env") return nil, errors.New("no JWT_SECRET provided in .env")