Skip to content

Commit

Permalink
[relay-server] Use X-Real-IP in case of reverse proxy (#2848)
Browse files Browse the repository at this point in the history
* Use X-Real-IP in case of reverse proxy

* Use sprintf
  • Loading branch information
pappz authored Nov 7, 2024
1 parent 509e184 commit cbf5000
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions relay/server/listener/ws/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ func (l *Listener) Shutdown(ctx context.Context) error {
}

func (l *Listener) onAccept(w http.ResponseWriter, r *http.Request) {
connRemoteAddr := remoteAddr(r)
wsConn, err := websocket.Accept(w, r, nil)
if err != nil {
log.Errorf("failed to accept ws connection from %s: %s", r.RemoteAddr, err)
log.Errorf("failed to accept ws connection from %s: %s", connRemoteAddr, err)
return
}

rAddr, err := net.ResolveTCPAddr("tcp", r.RemoteAddr)
rAddr, err := net.ResolveTCPAddr("tcp", connRemoteAddr)
if err != nil {
err = wsConn.Close(websocket.StatusInternalError, "internal error")
if err != nil {
Expand All @@ -90,3 +91,10 @@ func (l *Listener) onAccept(w http.ResponseWriter, r *http.Request) {
conn := NewConn(wsConn, lAddr, rAddr)
l.acceptFn(conn)
}

func remoteAddr(r *http.Request) string {
if r.Header.Get("X-Real-Ip") == "" || r.Header.Get("X-Real-Port") == "" {
return r.RemoteAddr
}
return fmt.Sprintf("%s:%s", r.Header.Get("X-Real-Ip"), r.Header.Get("X-Real-Port"))
}

0 comments on commit cbf5000

Please sign in to comment.