fix: ensure StreamWebsocketConn call N.NewDeadlineConn

This commit is contained in:
wwqgtxx 2023-04-17 00:23:12 +08:00
parent 8e5dbc7382
commit 4a0d097fe9
2 changed files with 15 additions and 3 deletions

View File

@ -79,3 +79,7 @@ func (c *BufferedConn) ReaderReplaceable() bool {
} }
return true return true
} }
func (c *BufferedConn) WriterReplaceable() bool {
return true
}

View File

@ -334,7 +334,10 @@ func streamWebsocketWithEarlyDataConn(conn net.Conn, c *WebsocketConfig) (net.Co
underlay: conn, underlay: conn,
config: c, config: c,
} }
return conn, nil // websocketWithEarlyDataConn can't correct handle Deadline
// it will not apply the already set Deadline after Dial()
// so call N.NewDeadlineConn to add a safe wrapper
return N.NewDeadlineConn(conn), nil
} }
func streamWebsocketConn(conn net.Conn, c *WebsocketConfig, earlyData *bytes.Buffer) (net.Conn, error) { func streamWebsocketConn(conn net.Conn, c *WebsocketConfig, earlyData *bytes.Buffer) (net.Conn, error) {
@ -402,11 +405,16 @@ func streamWebsocketConn(conn net.Conn, c *WebsocketConfig, earlyData *bytes.Buf
return nil, fmt.Errorf("dial %s error: %s", uri.Host, reason) return nil, fmt.Errorf("dial %s error: %s", uri.Host, reason)
} }
return &websocketConn{ conn = &websocketConn{
conn: wsConn, conn: wsConn,
rawWriter: N.NewExtendedWriter(wsConn.UnderlyingConn()), rawWriter: N.NewExtendedWriter(wsConn.UnderlyingConn()),
remoteAddr: conn.RemoteAddr(), remoteAddr: conn.RemoteAddr(),
}, nil }
// websocketConn can't correct handle ReadDeadline
// gorilla/websocket will cache the os.ErrDeadlineExceeded from conn.Read()
// it will cause read fail and event panic in *websocket.Conn.NextReader()
// so call N.NewDeadlineConn to add a safe wrapper
return N.NewDeadlineConn(conn), nil
} }
func StreamWebsocketConn(conn net.Conn, c *WebsocketConfig) (net.Conn, error) { func StreamWebsocketConn(conn net.Conn, c *WebsocketConfig) (net.Conn, error) {