transport.go 2.32 KB
Newer Older
Marten Seemann's avatar
Marten Seemann committed
1
2
3
package libp2pquic

import (
4
5
	"context"
	"errors"
Marten Seemann's avatar
Marten Seemann committed
6

7
8
	ic "github.com/libp2p/go-libp2p-crypto"
	peer "github.com/libp2p/go-libp2p-peer"
Marten Seemann's avatar
Marten Seemann committed
9
	tpt "github.com/libp2p/go-libp2p-transport"
10
	quic "github.com/lucas-clemente/quic-go"
Marten Seemann's avatar
Marten Seemann committed
11
	ma "github.com/multiformats/go-multiaddr"
12
	manet "github.com/multiformats/go-multiaddr-net"
Marten Seemann's avatar
Marten Seemann committed
13
	"github.com/whyrusleeping/mafmt"
Marten Seemann's avatar
Marten Seemann committed
14
15
)

16
var quicDialAddr = quic.DialAddr
Marten Seemann's avatar
Marten Seemann committed
17

18
19
20
21
// The Transport implements the tpt.Transport interface for QUIC connections.
type transport struct {
	privKey   ic.PrivKey
	localPeer peer.ID
Marten Seemann's avatar
Marten Seemann committed
22
}
Marten Seemann's avatar
Marten Seemann committed
23

24
var _ tpt.Transport = &transport{}
25

26
27
28
29
30
// NewTransport creates a new QUIC transport
func NewTransport(key ic.PrivKey) (tpt.Transport, error) {
	localPeer, err := peer.IDFromPrivateKey(key)
	if err != nil {
		return nil, err
Marten Seemann's avatar
Marten Seemann committed
31
	}
32
33
34
35
	return &transport{
		privKey:   key,
		localPeer: localPeer,
	}, nil
Marten Seemann's avatar
Marten Seemann committed
36
37
}

38
39
40
41
42
// Dial dials a new QUIC connection
func (t *transport) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.ID) (tpt.Conn, error) {
	_, host, err := manet.DialArgs(raddr)
	if err != nil {
		return nil, err
Marten Seemann's avatar
Marten Seemann committed
43
	}
44
	tlsConf, err := GenerateConfig(t.privKey)
Marten Seemann's avatar
Marten Seemann committed
45
46
47
	if err != nil {
		return nil, err
	}
48
49
50
	sess, err := quicDialAddr(host, tlsConf, &quic.Config{Versions: []quic.VersionNumber{101}})
	if err != nil {
		return nil, err
51
	}
52
53
54
	remotePubKey, err := getRemotePubKey(sess)
	if err != nil {
		return nil, err
Marten Seemann's avatar
Marten Seemann committed
55
	}
56
	localMultiaddr, err := quicMultiaddr(sess.LocalAddr())
Marten Seemann's avatar
Marten Seemann committed
57
58
59
	if err != nil {
		return nil, err
	}
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
	if !p.MatchesPublicKey(remotePubKey) {
		err := errors.New("peer IDs don't match")
		sess.Close(err)
		return nil, err
	}
	return &conn{
		privKey:         t.privKey,
		localPeer:       t.localPeer,
		localMultiaddr:  localMultiaddr,
		remotePubKey:    remotePubKey,
		remotePeerID:    p,
		remoteMultiaddr: raddr,
	}, nil
}

// CanDial determines if we can dial to an address
func (t *transport) CanDial(addr ma.Multiaddr) bool {
	return mafmt.QUIC.Matches(addr)
}
Marten Seemann's avatar
Marten Seemann committed
79

80
81
82
// Listen listens for new QUIC connections on the passed multiaddr.
func (t *transport) Listen(addr ma.Multiaddr) (tpt.Listener, error) {
	return newListener(addr, t, t.localPeer, t.privKey)
Marten Seemann's avatar
Marten Seemann committed
83
84
}

85
86
87
// Proxy returns true if this transport proxies.
func (t *transport) Proxy() bool {
	return false
Marten Seemann's avatar
Marten Seemann committed
88
89
}

90
91
92
93
94
95
96
97
// Protocols returns the set of protocols handled by this transport.
func (t *transport) Protocols() []int {
	return []int{ma.P_QUIC}
}

func (t *transport) String() string {
	return "QUIC"
}