Skip to content

Commit

Permalink
Merge pull request #111 from libp2p/accept-psk
Browse files Browse the repository at this point in the history
accept a PSK in the transport constructor (and reject it)
  • Loading branch information
Stebalien authored Mar 7, 2020
2 parents b8f591a + eea1dd7 commit c21973c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
22 changes: 11 additions & 11 deletions p2p/transport/quic/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ var _ = Describe("Connection", func() {
})

It("handshakes on IPv4", func() {
serverTransport, err := NewTransport(serverKey)
serverTransport, err := NewTransport(serverKey, nil)
Expect(err).ToNot(HaveOccurred())
ln := runServer(serverTransport, "/ip4/127.0.0.1/udp/0/quic")
defer ln.Close()

clientTransport, err := NewTransport(clientKey)
clientTransport, err := NewTransport(clientKey, nil)
Expect(err).ToNot(HaveOccurred())
conn, err := clientTransport.Dial(context.Background(), ln.Multiaddr(), serverID)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -86,12 +86,12 @@ var _ = Describe("Connection", func() {
})

It("handshakes on IPv6", func() {
serverTransport, err := NewTransport(serverKey)
serverTransport, err := NewTransport(serverKey, nil)
Expect(err).ToNot(HaveOccurred())
ln := runServer(serverTransport, "/ip6/::1/udp/0/quic")
defer ln.Close()

clientTransport, err := NewTransport(clientKey)
clientTransport, err := NewTransport(clientKey, nil)
Expect(err).ToNot(HaveOccurred())
conn, err := clientTransport.Dial(context.Background(), ln.Multiaddr(), serverID)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -110,12 +110,12 @@ var _ = Describe("Connection", func() {
})

It("opens and accepts streams", func() {
serverTransport, err := NewTransport(serverKey)
serverTransport, err := NewTransport(serverKey, nil)
Expect(err).ToNot(HaveOccurred())
ln := runServer(serverTransport, "/ip4/127.0.0.1/udp/0/quic")
defer ln.Close()

clientTransport, err := NewTransport(clientKey)
clientTransport, err := NewTransport(clientKey, nil)
Expect(err).ToNot(HaveOccurred())
conn, err := clientTransport.Dial(context.Background(), ln.Multiaddr(), serverID)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -139,11 +139,11 @@ var _ = Describe("Connection", func() {
It("fails if the peer ID doesn't match", func() {
thirdPartyID, _ := createPeer()

serverTransport, err := NewTransport(serverKey)
serverTransport, err := NewTransport(serverKey, nil)
Expect(err).ToNot(HaveOccurred())
ln := runServer(serverTransport, "/ip4/127.0.0.1/udp/0/quic")

clientTransport, err := NewTransport(clientKey)
clientTransport, err := NewTransport(clientKey, nil)
Expect(err).ToNot(HaveOccurred())
// dial, but expect the wrong peer ID
_, err = clientTransport.Dial(context.Background(), ln.Multiaddr(), thirdPartyID)
Expand All @@ -164,10 +164,10 @@ var _ = Describe("Connection", func() {
It("dials to two servers at the same time", func() {
serverID2, serverKey2 := createPeer()

serverTransport, err := NewTransport(serverKey)
serverTransport, err := NewTransport(serverKey, nil)
Expect(err).ToNot(HaveOccurred())
ln1 := runServer(serverTransport, "/ip4/127.0.0.1/udp/0/quic")
serverTransport2, err := NewTransport(serverKey2)
serverTransport2, err := NewTransport(serverKey2, nil)
defer ln1.Close()
Expect(err).ToNot(HaveOccurred())
ln2 := runServer(serverTransport2, "/ip4/127.0.0.1/udp/0/quic")
Expand All @@ -194,7 +194,7 @@ var _ = Describe("Connection", func() {
}
}()

clientTransport, err := NewTransport(clientKey)
clientTransport, err := NewTransport(clientKey, nil)
Expect(err).ToNot(HaveOccurred())
c1, err := clientTransport.Dial(context.Background(), ln1.Multiaddr(), serverID)
Expect(err).ToNot(HaveOccurred())
Expand Down
2 changes: 1 addition & 1 deletion p2p/transport/quic/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var _ = Describe("Listener", func() {
Expect(err).ToNot(HaveOccurred())
key, err := ic.UnmarshalRsaPrivateKey(x509.MarshalPKCS1PrivateKey(rsaKey))
Expect(err).ToNot(HaveOccurred())
t, err = NewTransport(key)
t, err = NewTransport(key, nil)
Expect(err).ToNot(HaveOccurred())
})

Expand Down
11 changes: 9 additions & 2 deletions p2p/transport/quic/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ import (
"errors"
"net"

logging "github.com/ipfs/go-log"
ic "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/pnet"
tpt "github.com/libp2p/go-libp2p-core/transport"
p2ptls "github.com/libp2p/go-libp2p-tls"

quic "github.com/lucas-clemente/quic-go"
ma "github.com/multiformats/go-multiaddr"
mafmt "github.com/multiformats/go-multiaddr-fmt"
manet "github.com/multiformats/go-multiaddr-net"
)

var log = logging.Logger("quic-transport")

var quicConfig = &quic.Config{
MaxIncomingStreams: 1000,
MaxIncomingUniStreams: -1, // disable unidirectional streams
Expand Down Expand Up @@ -86,7 +89,11 @@ type transport struct {
var _ tpt.Transport = &transport{}

// NewTransport creates a new QUIC transport
func NewTransport(key ic.PrivKey) (tpt.Transport, error) {
func NewTransport(key ic.PrivKey, psk pnet.PSK) (tpt.Transport, error) {
if len(psk) > 0 {
log.Error("QUIC doesn't support private networks yet.")
return nil, errors.New("QUIC doesn't support private networks yet")
}
localPeer, err := peer.IDFromPrivateKey(key)
if err != nil {
return nil, err
Expand Down

0 comments on commit c21973c

Please sign in to comment.