From: Skullheadx Date: Mon, 8 Jun 2026 20:48:33 +0000 (-0400) Subject: login check X-Git-Url: http://git.skullheadx.com/nixos/static/gitweb.js?a=commitdiff_plain;h=f3a498de478ef4ba39cabd235d367709960482d5;p=monopoly-web.git login check --- diff --git a/game/game.go b/game/game.go index 37fb726..bf6719d 100644 --- a/game/game.go +++ b/game/game.go @@ -3,6 +3,7 @@ package game import ( "context" "errors" + "fmt" "github.com/coder/websocket" "github.com/google/uuid" "golang.org/x/time/rate" @@ -26,25 +27,27 @@ type MonopolyServer struct { subscribersMu sync.Mutex subscribers map[*subscriber]string + // uuid to username + users map[string]string + gameCtxMu sync.Mutex gameCtx *Context randSeed *rand.PCG } -// uuid to username -var Users map[string]string - func NewMonopolyServer() *MonopolyServer { ms := &MonopolyServer{ subscriberMessageBuffer: 16, logf: log.Printf, subscribers: make(map[*subscriber]string), + users: make(map[string]string), publishLimiter: rate.NewLimiter(rate.Every(time.Millisecond*100), 8), gameCtx: nil, randSeed: rand.NewPCG(20, 26), } - ms.serveMux.Handle("/", http.FileServer(http.Dir("../public/"))) + ms.serveMux.Handle("/", http.FileServer(http.Dir("public/"))) ms.serveMux.HandleFunc("/login", ms.loginHandler) + ms.serveMux.HandleFunc("/loggedin", ms.loggedInHandler) ms.serveMux.HandleFunc("/subscribe", ms.subscribeHandler) ms.serveMux.HandleFunc("/start", ms.startHandler) ms.serveMux.HandleFunc("/roll", ms.rollHandler) @@ -77,6 +80,34 @@ func (ms *MonopolyServer) subscribeHandler(w http.ResponseWriter, r *http.Reques } } +func (ms *MonopolyServer) loggedInHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) + return + } + + cookie, err := r.Cookie("user") + if err != nil { + if err == http.ErrNoCookie { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + + http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) + return + } + + userUUID := cookie.Value + + _, ok := ms.users[userUUID] + if !ok { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) + return + } + + w.WriteHeader(http.StatusOK) +} + func (ms *MonopolyServer) loginHandler(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed) @@ -91,7 +122,7 @@ func (ms *MonopolyServer) loginHandler(w http.ResponseWriter, r *http.Request) { } userUUID := uuid.NewString() - Users[userUUID] = string(username) + ms.users[userUUID] = string(username) http.SetCookie(w, &http.Cookie{ Name: "user", @@ -112,6 +143,24 @@ func (ms *MonopolyServer) startHandler(w http.ResponseWriter, r *http.Request) { return } + cookie, err := r.Cookie("user") + if err != nil { + if err == http.ErrNoCookie { + http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) + return + } + + http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) + return + } + + userUUID := cookie.Value + _, ok := ms.users[userUUID] + if !ok { + http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) + return + } + ms.start() w.WriteHeader(http.StatusAccepted) @@ -127,25 +176,37 @@ func (ms *MonopolyServer) rollHandler(w http.ResponseWriter, r *http.Request) { if err != nil { if err == http.ErrNoCookie { http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) + return } http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) + return } userUUID := cookie.Value + _, ok := ms.users[userUUID] + if !ok { + http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) + return + } + if ms.gameCtx == nil { http.Error(w, "Game has not started yet", http.StatusConflict) + return } if !ms.gameCtx.ValidateCanRoll(userUUID) { http.Error(w, "Not your turn", http.StatusForbidden) + return } ms.roll() w.WriteHeader(http.StatusOK) } func (ms *MonopolyServer) roll() { + fmt.Printf("%#v\n", ms.gameCtx) ms.gameCtx.RollDice() + fmt.Printf("%#v\n", ms.gameCtx) ms.gameCtx.ProcessMovement() } @@ -154,9 +215,11 @@ func (ms *MonopolyServer) subscribe(w http.ResponseWriter, r *http.Request) erro if err != nil { if err == http.ErrNoCookie { http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized) + return err } http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest) + return err } userUUID := cookie.Value diff --git a/go.mod b/go.mod index f178e3e..c9366fc 100644 --- a/go.mod +++ b/go.mod @@ -7,4 +7,4 @@ require ( golang.org/x/time v0.15.0 ) -require github.com/google/uuid v1.6.0 // indirect +require github.com/google/uuid v1.6.0 diff --git a/main.go b/main.go index f51c0dd..8543845 100644 --- a/main.go +++ b/main.go @@ -28,9 +28,9 @@ func run() error { } log.Printf("listening on ws://%v", l.Addr()) - cs := game.NewMonopolyServer() + ms := game.NewMonopolyServer() s := &http.Server{ - Handler: cs, + Handler: ms, ReadTimeout: time.Second * 10, WriteTimeout: time.Second * 10, } diff --git a/monopoly-web b/monopoly-web new file mode 100755 index 0000000..0ae8eef Binary files /dev/null and b/monopoly-web differ diff --git a/public/index.js b/public/index.js index c6d759b..e965ea7 100644 --- a/public/index.js +++ b/public/index.js @@ -1,6 +1,5 @@ ; (() => { let connected = false - function dial() { const conn = new WebSocket(`ws://${location.host}/subscribe`) @@ -25,7 +24,20 @@ appendGameLog(ev.data) }) } - // dial() + async function loggedIn() { + try { + const resp = await fetch('/loggedin', { + method: 'GET' + }) + if (resp.status !== 200) { + throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText}`) + } + dial() + } catch (err) { + console.error(`Login check failed: ${err.message}`) + } + } + loggedIn() const gameLog = document.getElementById('log') const loginForm = document.getElementById('login-form') @@ -57,7 +69,7 @@ body: msg, }) if (resp.status !== 200) { - throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText}`) + throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText} ${resp.message}`) } dial() @@ -72,7 +84,7 @@ method: 'POST', }) if (resp.status !== 202) { - throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText}`) + throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText} ${resp.message}`) } } catch (err) { console.error(`Start failed: ${err.message}`) @@ -85,7 +97,7 @@ method: 'POST', }) if (resp.status !== 200) { - throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText}`) + throw new Error(`Unexpected HTTP Status ${resp.status} ${resp.statusText} ${resp.message}`) } } catch (err) { console.error(`Start failed: ${err.message}`)