diff --git a/core/network/conn.go b/core/network/conn.go index 7908d4198b..6d16d298cf 100644 --- a/core/network/conn.go +++ b/core/network/conn.go @@ -36,10 +36,12 @@ type Conn interface { // ConnectionState holds information about the connection. type ConnectionState struct { - // The stream multiplexer used on this connection (if any). + // The stream multiplexer used on this connection (if any). For example: /yamux/1.0.0 StreamMultiplexer string - // The security protocol used on this connection (if any). + // The security protocol used on this connection (if any). For example: /tls/1.0.0 Security string + // the transport used on this connection. For example: tcp + Transport string } // ConnSecurity is the interface that one can mix into a connection interface to diff --git a/p2p/net/upgrader/conn.go b/p2p/net/upgrader/conn.go index 4fdbd05fa8..7a079b29fe 100644 --- a/p2p/net/upgrader/conn.go +++ b/p2p/net/upgrader/conn.go @@ -58,5 +58,6 @@ func (t *transportConn) ConnState() network.ConnectionState { return network.ConnectionState{ StreamMultiplexer: string(t.muxer), Security: string(t.security), + Transport: "tcp", } } diff --git a/p2p/transport/quic/conn.go b/p2p/transport/quic/conn.go index 1af53f2abc..cc74b68cb2 100644 --- a/p2p/transport/quic/conn.go +++ b/p2p/transport/quic/conn.go @@ -71,45 +71,32 @@ func (c *conn) AcceptStream() (network.MuxedStream, error) { } // LocalPeer returns our peer ID -func (c *conn) LocalPeer() peer.ID { - return c.localPeer -} +func (c *conn) LocalPeer() peer.ID { return c.localPeer } // LocalPrivateKey returns our private key -func (c *conn) LocalPrivateKey() ic.PrivKey { - return c.privKey -} +func (c *conn) LocalPrivateKey() ic.PrivKey { return c.privKey } // RemotePeer returns the peer ID of the remote peer. -func (c *conn) RemotePeer() peer.ID { - return c.remotePeerID -} +func (c *conn) RemotePeer() peer.ID { return c.remotePeerID } // RemotePublicKey returns the public key of the remote peer. -func (c *conn) RemotePublicKey() ic.PubKey { - return c.remotePubKey -} - -// ConnState is the state of security connection. -// It is empty if not supported. -func (c *conn) ConnState() network.ConnectionState { - return network.ConnectionState{} -} +func (c *conn) RemotePublicKey() ic.PubKey { return c.remotePubKey } // LocalMultiaddr returns the local Multiaddr associated -func (c *conn) LocalMultiaddr() ma.Multiaddr { - return c.localMultiaddr -} +func (c *conn) LocalMultiaddr() ma.Multiaddr { return c.localMultiaddr } // RemoteMultiaddr returns the remote Multiaddr associated -func (c *conn) RemoteMultiaddr() ma.Multiaddr { - return c.remoteMultiaddr -} +func (c *conn) RemoteMultiaddr() ma.Multiaddr { return c.remoteMultiaddr } -func (c *conn) Transport() tpt.Transport { - return c.transport -} +func (c *conn) Transport() tpt.Transport { return c.transport } -func (c *conn) Scope() network.ConnScope { - return c.scope +func (c *conn) Scope() network.ConnScope { return c.scope } + +// ConnState is the state of security connection. +func (c *conn) ConnState() network.ConnectionState { + t := "quic-v1" + if _, err := c.LocalMultiaddr().ValueForProtocol(ma.P_QUIC); err == nil { + t = "quic" + } + return network.ConnectionState{Transport: t} } diff --git a/p2p/transport/websocket/conn.go b/p2p/transport/websocket/conn.go index 6f2e0a7667..30b70055d0 100644 --- a/p2p/transport/websocket/conn.go +++ b/p2p/transport/websocket/conn.go @@ -6,6 +6,9 @@ import ( "sync" "time" + "github.com/libp2p/go-libp2p/core/network" + "github.com/libp2p/go-libp2p/core/transport" + ws "github.com/gorilla/websocket" ) @@ -149,3 +152,13 @@ func (c *Conn) SetWriteDeadline(t time.Time) error { return c.Conn.SetWriteDeadline(t) } + +type capableConn struct { + transport.CapableConn +} + +func (c *capableConn) ConnState() network.ConnectionState { + cs := c.CapableConn.ConnState() + cs.Transport = "websocket" + return cs +} diff --git a/p2p/transport/websocket/listener.go b/p2p/transport/websocket/listener.go index ee6810807b..c9991d71b1 100644 --- a/p2p/transport/websocket/listener.go +++ b/p2p/transport/websocket/listener.go @@ -6,6 +6,8 @@ import ( "net" "net/http" + "github.com/libp2p/go-libp2p/core/transport" + ma "github.com/multiformats/go-multiaddr" manet "github.com/multiformats/go-multiaddr/net" ) @@ -140,3 +142,15 @@ func (l *listener) Multiaddr() ma.Multiaddr { func (l *listener) Multiaddrs() []ma.Multiaddr { return []ma.Multiaddr{l.laddr} } + +type transportListener struct { + transport.Listener +} + +func (l *transportListener) Accept() (transport.CapableConn, error) { + conn, err := l.Listener.Accept() + if err != nil { + return nil, err + } + return &capableConn{CapableConn: conn}, nil +} diff --git a/p2p/transport/websocket/websocket.go b/p2p/transport/websocket/websocket.go index b693ef4330..03941013a1 100644 --- a/p2p/transport/websocket/websocket.go +++ b/p2p/transport/websocket/websocket.go @@ -166,7 +166,11 @@ func (t *WebsocketTransport) Dial(ctx context.Context, raddr ma.Multiaddr, p pee connScope.Done() return nil, err } - return t.upgrader.Upgrade(ctx, t, macon, network.DirOutbound, p, connScope) + conn, err := t.upgrader.Upgrade(ctx, t, macon, network.DirOutbound, p, connScope) + if err != nil { + return nil, err + } + return &capableConn{CapableConn: conn}, nil } func (t *WebsocketTransport) maDial(ctx context.Context, raddr ma.Multiaddr) (manet.Conn, error) { @@ -230,5 +234,5 @@ func (t *WebsocketTransport) Listen(a ma.Multiaddr) (transport.Listener, error) if err != nil { return nil, err } - return t.upgrader.UpgradeListener(t, malist), nil + return &transportListener{Listener: t.upgrader.UpgradeListener(t, malist)}, nil } diff --git a/p2p/transport/webtransport/conn.go b/p2p/transport/webtransport/conn.go index d8a366a0eb..a6774c7d4a 100644 --- a/p2p/transport/webtransport/conn.go +++ b/p2p/transport/webtransport/conn.go @@ -75,3 +75,7 @@ func (c *conn) Close() error { func (c *conn) IsClosed() bool { return c.session.Context().Err() != nil } func (c *conn) Scope() network.ConnScope { return c.scope } func (c *conn) Transport() tpt.Transport { return c.transport } + +func (c *conn) ConnState() network.ConnectionState { + return network.ConnectionState{Transport: "webtransport"} +}