@@ -2,6 +2,7 @@ package proxy
22
33import (
44 "context"
5+ "encoding/hex"
56 "encoding/json"
67 "errors"
78 "sync/atomic"
@@ -42,9 +43,19 @@ func newWebsocketPump(
4243
4344func (w * websocketPump ) run () error {
4445 failure := make (chan error , 2 )
45- done := make (chan struct {}, 1 )
46+ done := make (chan struct {}, 2 )
4647
47- go w .pump (w .backend , w .frontend , "b->f" , done , failure )
48+ w .frontend .SetPingHandler (w .pumpPings (w .frontend , w .backend , "f->b" ))
49+ w .backend .SetPongHandler (w .pumpPongs (w .backend , w .frontend , "b->f" ))
50+
51+ w .backend .SetPingHandler (w .pumpPings (w .backend , w .frontend , "b->f" ))
52+ w .frontend .SetPongHandler (w .pumpPongs (w .frontend , w .backend , "f->b" ))
53+
54+ w .frontend .SetCloseHandler (w .pumpCloseMessages (w .frontend , w .backend , "f->b" ))
55+ w .backend .SetCloseHandler (w .pumpCloseMessages (w .backend , w .frontend , "b->f" ))
56+
57+ go w .pumpMessages (w .backend , w .frontend , "b->f" , done , failure )
58+ go w .pumpMessages (w .frontend , w .backend , "f->b" , done , failure )
4859
4960 w .active .Store (true )
5061
@@ -86,7 +97,7 @@ func (w *websocketPump) stop() error {
8697 return utils .FlattenErrors (errs )
8798}
8899
89- func (w * websocketPump ) pump (
100+ func (w * websocketPump ) pumpMessages (
90101 from , to * websocket.Conn ,
91102 direction string ,
92103 done chan struct {},
@@ -105,7 +116,7 @@ loop:
105116 return
106117
107118 default :
108- if err := from .SetReadDeadline (time .Now ().Add (5 * time . Second )); err != nil {
119+ if err := from .SetReadDeadline (time .Now ().Add (w . cfg . proxy . Timeout )); err != nil {
109120 failure <- err
110121 continue loop
111122 }
@@ -117,7 +128,7 @@ loop:
117128
118129 ts := time .Now ()
119130
120- if err := to .SetWriteDeadline (time .Now ().Add (5 * time . Second )); err != nil {
131+ if err := to .SetWriteDeadline (time .Now ().Add (w . cfg . proxy . Timeout )); err != nil {
121132 failure <- err
122133 continue loop
123134 }
@@ -130,6 +141,7 @@ loop:
130141 loggedFields := make ([]zap.Field , 0 , 6 )
131142 loggedFields = append (loggedFields ,
132143 zap .Time ("ts_message_received" , ts ),
144+ zap .Int ("message_type" , msgType ),
133145 zap .Int ("message_size" , len (bytes )),
134146 )
135147
@@ -155,3 +167,61 @@ loop:
155167 }
156168 }
157169}
170+
171+ func (w * websocketPump ) pumpPings (
172+ from , to * websocket.Conn ,
173+ direction string ,
174+ ) func (message string ) error {
175+ l := w .logger .With (
176+ zap .String ("from_addr" , from .RemoteAddr ().String ()),
177+ zap .String ("to_addr" , to .RemoteAddr ().String ()),
178+ zap .String ("direction" , direction ),
179+ )
180+ return func (message string ) error {
181+ l .Debug ("Passing ping through..." ,
182+ zap .String ("message" , hex .EncodeToString ([]byte (message ))),
183+ )
184+ return to .WriteControl (
185+ websocket .PingMessage , []byte (message ), time .Now ().Add (w .cfg .proxy .Timeout ),
186+ )
187+ }
188+ }
189+
190+ func (w * websocketPump ) pumpPongs (
191+ from , to * websocket.Conn ,
192+ direction string ,
193+ ) func (message string ) error {
194+ l := w .logger .With (
195+ zap .String ("from_addr" , from .RemoteAddr ().String ()),
196+ zap .String ("to_addr" , to .RemoteAddr ().String ()),
197+ zap .String ("direction" , direction ),
198+ )
199+ return func (message string ) error {
200+ l .Debug ("Passing pong through..." ,
201+ zap .String ("message" , hex .EncodeToString ([]byte (message ))),
202+ )
203+ return to .WriteControl (
204+ websocket .PongMessage , []byte (message ), time .Now ().Add (w .cfg .proxy .Timeout ),
205+ )
206+ }
207+ }
208+
209+ func (w * websocketPump ) pumpCloseMessages (
210+ from , to * websocket.Conn ,
211+ direction string ,
212+ ) func (code int , message string ) error {
213+ l := w .logger .With (
214+ zap .String ("from_addr" , from .RemoteAddr ().String ()),
215+ zap .String ("to_addr" , to .RemoteAddr ().String ()),
216+ zap .String ("direction" , direction ),
217+ )
218+ return func (code int , message string ) error {
219+ l .Info ("Passing close message through..." ,
220+ zap .Int ("code" , code ),
221+ zap .String ("message" , hex .EncodeToString ([]byte (message ))),
222+ )
223+ return to .WriteControl (
224+ code , []byte (message ), time .Now ().Add (w .cfg .proxy .Timeout ),
225+ )
226+ }
227+ }
0 commit comments