package libp2pquic import ( "net" ic "github.com/libp2p/go-libp2p-crypto" peer "github.com/libp2p/go-libp2p-peer" tpt "github.com/libp2p/go-libp2p-transport" quic "github.com/lucas-clemente/quic-go" ma "github.com/multiformats/go-multiaddr" manet "github.com/multiformats/go-multiaddr-net" ) var quicListenAddr = quic.ListenAddr // A listener listens for QUIC connections. type listener struct { quicListener quic.Listener transport tpt.Transport acceptQueue chan tpt.Conn privKey ic.PrivKey localPeer peer.ID localMultiaddr ma.Multiaddr } var _ tpt.Listener = &listener{} func newListener(addr ma.Multiaddr, transport tpt.Transport, localPeer peer.ID, key ic.PrivKey) (tpt.Listener, error) { _, host, err := manet.DialArgs(addr) if err != nil { return nil, err } tlsConf, err := generateConfig(key) if err != nil { return nil, err } ln, err := quicListenAddr(host, tlsConf, &quic.Config{Versions: []quic.VersionNumber{101}}) if err != nil { return nil, err } localMultiaddr, err := quicMultiaddr(ln.Addr()) if err != nil { return nil, err } return &listener{ quicListener: ln, transport: transport, privKey: key, localPeer: localPeer, localMultiaddr: localMultiaddr, }, nil } // Accept accepts new connections. // TODO(#2): don't accept a connection if the client's peer verification fails func (l *listener) Accept() (tpt.Conn, error) { sess, err := l.quicListener.Accept() if err != nil { return nil, err } remotePubKey, err := getRemotePubKey(sess.ConnectionState().PeerCertificates) if err != nil { return nil, err } remotePeerID, err := peer.IDFromPublicKey(remotePubKey) if err != nil { return nil, err } remoteMultiaddr, err := quicMultiaddr(sess.RemoteAddr()) if err != nil { return nil, err } return &conn{ sess: sess, transport: l.transport, localPeer: l.localPeer, localMultiaddr: l.localMultiaddr, privKey: l.privKey, remoteMultiaddr: remoteMultiaddr, remotePeerID: remotePeerID, remotePubKey: remotePubKey, }, nil } // Close closes the listener. func (l *listener) Close() error { return l.quicListener.Close() } // Addr returns the address of this listener. func (l *listener) Addr() net.Addr { return l.quicListener.Addr() } // Multiaddr returns the multiaddress of this listener. func (l *listener) Multiaddr() ma.Multiaddr { return l.localMultiaddr }