Get real IP of webscoket connection

This commit is contained in:
Martin Boehm 2021-05-09 18:56:13 +02:00
parent af0711dc12
commit 1482b51503

View File

@ -118,6 +118,14 @@ func checkOrigin(r *http.Request) bool {
return true
}
func getIP(r *http.Request) string {
ip := r.Header.Get("X-Real-Ip")
if ip != "" {
return ip
}
return r.RemoteAddr
}
// ServeHTTP sets up handler of websocket channel
func (s *WebsocketServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
@ -133,7 +141,7 @@ func (s *WebsocketServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
id: atomic.AddUint64(&connectionCounter, 1),
conn: conn,
out: make(chan *websocketRes, outChannelSize),
ip: r.RemoteAddr,
ip: getIP(r),
requestHeader: r.Header,
alive: true,
}