feat: handle X-Forward-For for log output

- add Gorilla handlers package for enhanced HTTP handling
- refactor HTTP server to use a new ServeMux for routing
- update ListenAndServe to utilize ProxyHeaders for better proxy support
- add RealIP field to client model
  - use RealIP fild for connect/disconnect log output
This commit is contained in:
Sebastian Mark 2024-11-03 10:00:14 +01:00
parent dd9490bb3b
commit 44a64bfce4
6 changed files with 17 additions and 7 deletions

View file

@ -21,8 +21,8 @@ func handleClientCommands(c models.WebsocketClient) {
_, message, err := ws.ReadMessage()
if err != nil {
log.Info("Client disconnected:", "msg", err, "host", ws.RemoteAddr(), "clients", len(Clients)-1)
delete(Clients, ws.LocalAddr())
log.Info("Client disconnected:", "msg", err, "host", c.RealIP, "clients", len(Clients))
break
}

View file

@ -29,16 +29,17 @@ func HandleConnection(w http.ResponseWriter, r *http.Request) {
}
defer ws.Close()
log.Info("Client connected", "host", ws.RemoteAddr(), "clients", len(Clients)+1)
// Register the new client
client := models.WebsocketClient{
Conn: ws,
Conn: ws,
RealIP: r.RemoteAddr,
}
mu.Lock()
Clients[ws.LocalAddr()] = &client
mu.Unlock()
log.Info("Client connected", "host", client.RealIP, "clients", len(Clients))
// Listen for commands from the connected client
handleClientCommands(client)
}