Browse Source

websocket: don't set a WSS multiaddr for accepted unencrypted conns (#2199)

host-no-duplicate-dials
Marten Seemann 2 years ago
committed by GitHub
parent
commit
eeb685f68f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      p2p/transport/websocket/listener.go
  2. 14
      p2p/transport/websocket/websocket_test.go

8
p2p/transport/websocket/listener.go

@ -20,6 +20,9 @@ import (
type listener struct {
nl net.Listener
server http.Server
// The Go standard library sets the http.Server.TLSConfig no matter if this is a WS or WSS,
// so we can't rely on checking if server.TLSConfig is set.
isWss bool
laddr ma.Multiaddr
@ -81,6 +84,7 @@ func newListener(a ma.Multiaddr, tlsConf *tls.Config) (*listener, error) {
}
ln.server = http.Server{Handler: ln}
if parsed.isWSS {
ln.isWss = true
ln.server.TLSConfig = tlsConf
}
return ln, nil
@ -88,7 +92,7 @@ func newListener(a ma.Multiaddr, tlsConf *tls.Config) (*listener, error) {
func (l *listener) serve() {
defer close(l.closed)
if l.server.TLSConfig == nil {
if !l.isWss {
l.server.Serve(l.nl)
} else {
l.server.ServeTLS(l.nl, "", "")
@ -97,7 +101,7 @@ func (l *listener) serve() {
func (l *listener) ServeHTTP(w http.ResponseWriter, r *http.Request) {
scheme := "ws"
if l.server.TLSConfig != nil {
if l.isWss {
scheme = "wss"
}

14
p2p/transport/websocket/websocket_test.go

@ -308,6 +308,16 @@ func TestWebsocketTransport(t *testing.T) {
ttransport.SubtestTransport(t, ta, tb, "/ip4/127.0.0.1/tcp/0/ws", "peerA")
}
func isWSS(addr ma.Multiaddr) bool {
if _, err := addr.ValueForProtocol(ma.P_WSS); err == nil {
return true
}
if _, err := addr.ValueForProtocol(ma.P_WS); err == nil {
return false
}
panic("not a WebSocket address")
}
func connectAndExchangeData(t *testing.T, laddr ma.Multiaddr, secure bool) {
var opts []Option
var tlsConf *tls.Config
@ -339,6 +349,8 @@ func connectAndExchangeData(t *testing.T, laddr ma.Multiaddr, secure bool) {
require.NoError(t, err)
c, err := tpt.Dial(context.Background(), l.Multiaddr(), server)
require.NoError(t, err)
require.Equal(t, secure, isWSS(c.LocalMultiaddr()))
require.Equal(t, secure, isWSS(c.RemoteMultiaddr()))
str, err := c.OpenStream(context.Background())
require.NoError(t, err)
defer str.Close()
@ -349,6 +361,8 @@ func connectAndExchangeData(t *testing.T, laddr ma.Multiaddr, secure bool) {
c, err := l.Accept()
require.NoError(t, err)
defer c.Close()
require.Equal(t, secure, isWSS(c.LocalMultiaddr()))
require.Equal(t, secure, isWSS(c.RemoteMultiaddr()))
str, err := c.AcceptStream()
require.NoError(t, err)
defer str.Close()

Loading…
Cancel
Save