Skip to content

Commit

Permalink
Add reset, add mising boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
NHAS committed Jun 10, 2024
1 parent 47f85c0 commit 909852b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
18 changes: 18 additions & 0 deletions internal/router/session_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ func (ws *wsConnWrapper) Close() error {
return nil
}

ws.isClosed = true

close(ws.wait)
return ws.Conn.Close()
}
Expand Down Expand Up @@ -74,6 +76,9 @@ func (c *Challenger) Challenge(address string) error {
c.RLock()
defer c.RUnlock()

log.Println("challenging")
defer log.Println("end challenging")

conn, ok := c.connections[address]
if !ok {
return fmt.Errorf("no connection found for device: %s", address)
Expand Down Expand Up @@ -125,6 +130,18 @@ func (c *Challenger) Challenge(address string) error {
return nil
}

func (c *Challenger) Reset(address string) {
c.RLock()
defer c.RUnlock()

conn, ok := c.connections[address]
if !ok {
return
}

conn.WriteJSON("reset")
}

func (c *Challenger) WS(w http.ResponseWriter, r *http.Request) {
remoteAddress := utils.GetIPFromRequest(r)
user, err := users.GetUserFromAddress(remoteAddress)
Expand Down Expand Up @@ -166,6 +183,7 @@ func (c *Challenger) WS(w http.ResponseWriter, r *http.Request) {

err = c.Challenge(remoteAddress.String())
if err != nil {
c.Reset(remoteAddress.String())
log.Printf("client did not complete inital ws challenge: %s", err)
return
}
Expand Down
14 changes: 11 additions & 3 deletions internal/webserver/resources/static/js/challenge.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,17 @@ function connect() {

ws.onmessage = function (e) {
console.log('Message:', e.data);
ws.send(
JSON.stringify({challenge: challenge
}));
switch(e.data) {
case "challenge":
ws.send(
JSON.stringify({challenge: challenge
}));
return
case "reset":
window.location.href = '/'
return
}

};

ws.onclose = function (e) {
Expand Down

0 comments on commit 909852b

Please sign in to comment.