@@ -4,8 +4,10 @@ package websocketproxy
4
4
import (
5
5
"io"
6
6
"log"
7
+ "net"
7
8
"net/http"
8
9
"net/url"
10
+ "strings"
9
11
10
12
"github.com/gorilla/websocket"
11
13
)
@@ -63,8 +65,6 @@ func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
63
65
dialer = DefaultDialer
64
66
}
65
67
66
- // TODO: Also consider adding x-fowarded-for and x-forwarded-proto headers.
67
-
68
68
// Pass headers from the incoming request to the dialer to forward them to
69
69
// the final destinations.
70
70
h := http.Header {}
@@ -78,6 +78,27 @@ func (w *WebsocketProxy) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
78
78
h .Add ("Cookie" , cookie )
79
79
}
80
80
81
+ // Pass X-Forwarded-For headers too, code below is a part of
82
+ // httputil.ReverseProxy. See http://en.wikipedia.org/wiki/X-Forwarded-For
83
+ // for more information
84
+ if clientIP , _ , err := net .SplitHostPort (req .RemoteAddr ); err == nil {
85
+ // If we aren't the first proxy retain prior
86
+ // X-Forwarded-For information as a comma+space
87
+ // separated list and fold multiple headers into one.
88
+ if prior , ok := req .Header ["X-Forwarded-For" ]; ok {
89
+ clientIP = strings .Join (prior , ", " ) + ", " + clientIP
90
+ }
91
+ h .Set ("X-Forwarded-For" , clientIP )
92
+ }
93
+
94
+ // Set the originating protol of the incoming HTTP request. The SSL might
95
+ // be terminated on our site and because we doing proxy adding this would
96
+ // be helpful for applications on the backedn.
97
+ h .Set ("X-Forwarded-Proto" , "http" )
98
+ if req .TLS != nil {
99
+ h .Set ("X-Forwarded-Proto" , "https" )
100
+ }
101
+
81
102
// Connect to the backend url, also pass the headers we prepared above.
82
103
connBackend , resp , err := dialer .Dial (backendURL .String (), h )
83
104
if err != nil {
0 commit comments