dialer.go 1.01 KB
Newer Older
Marten Seemann's avatar
Marten Seemann committed
1
2
3
4
package libp2pquic

import (
	"context"
Marten Seemann's avatar
Marten Seemann committed
5
	"crypto/tls"
Marten Seemann's avatar
Marten Seemann committed
6
7

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

Marten Seemann's avatar
Marten Seemann committed
14
15
16
17
type dialer struct {
	transport tpt.Transport
}

18
19
var _ tpt.Dialer = &dialer{}

Marten Seemann's avatar
Marten Seemann committed
20
21
22
23
24
func newDialer(transport tpt.Transport) (*dialer, error) {
	return &dialer{
		transport: transport,
	}, nil
}
Marten Seemann's avatar
Marten Seemann committed
25
26

func (d *dialer) Dial(raddr ma.Multiaddr) (tpt.Conn, error) {
Marten Seemann's avatar
Marten Seemann committed
27
28
29
30
31
32
	// TODO: check that raddr is a QUIC address
	_, host, err := manet.DialArgs(raddr)
	if err != nil {
		return nil, err
	}

Marten Seemann's avatar
Marten Seemann committed
33
	qsess, err := quic.DialAddr(host, &tls.Config{InsecureSkipVerify: true}, nil)
Marten Seemann's avatar
Marten Seemann committed
34
35
36
37
	if err != nil {
		return nil, err
	}

38
	return newQuicConn(qsess, d.transport)
Marten Seemann's avatar
Marten Seemann committed
39
40
41
}

func (d *dialer) DialContext(ctx context.Context, raddr ma.Multiaddr) (tpt.Conn, error) {
42
	// TODO: implement the ctx
Marten Seemann's avatar
Marten Seemann committed
43
	return d.Dial(raddr)
Marten Seemann's avatar
Marten Seemann committed
44
45
}

Marten Seemann's avatar
Marten Seemann committed
46
47
func (d *dialer) Matches(a ma.Multiaddr) bool {
	return mafmt.QUIC.Matches(a)
Marten Seemann's avatar
Marten Seemann committed
48
}