Skip to content

Commit

Permalink
Merge pull request #116 from mattn/real-ip
Browse files Browse the repository at this point in the history
use X-Real-Ip if presented
  • Loading branch information
mattn authored Feb 25, 2024
2 parents baa75c6 + 5cc480c commit 80050dd
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,13 @@ func (s *Server) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
ticker := time.NewTicker(pingPeriod)
stop := make(chan struct{})

s.Log.Infof("connected from %s", conn.RemoteAddr().String())
ip := conn.RemoteAddr().String()
if realIP := r.Header.Get("X-Forwarded-For"); realIP != "" {
ip = realIP // possible to be multiple comma separated
} else if realIP := r.Header.Get("X-Real-Ip"); realIP != "" {
ip = realIP
}
s.Log.Infof("connected from %s", ip)

ws := challenge(conn)

Expand All @@ -372,7 +378,7 @@ func (s *Server) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
removeListener(ws)
}
s.clientsMu.Unlock()
s.Log.Infof("disconnected from %s", conn.RemoteAddr().String())
s.Log.Infof("disconnected from %s", ip)

ctx.Done()
}()
Expand Down Expand Up @@ -438,7 +444,7 @@ func (s *Server) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
s.Log.Errorf("error writing ping: %v; closing websocket", err)
return
}
s.Log.Infof("pinging for %s", conn.RemoteAddr().String())
s.Log.Infof("pinging for %s", ip)
case <-stop:
return
}
Expand Down

0 comments on commit 80050dd

Please sign in to comment.