-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathquic_test.go
150 lines (124 loc) · 3.38 KB
/
quic_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package pfilter
import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"math/big"
"net"
"testing"
"time"
"github.com/pkg/errors"
"github.com/quic-go/quic-go"
)
const quicSize = 5 << 20
func BenchmarkQuic(b *testing.B) {
server, err := net.ListenPacket("udp", "127.0.0.1:0")
if err != nil {
b.Fatal(err)
}
defer server.Close()
writer, reader := wrapQuic(server)
benchmark(b, writer, reader, quicSize)
}
func BenchmarkQuicPfilter(b *testing.B) {
server, err := net.ListenPacket("udp", "127.0.0.1:0")
if err != nil {
b.Fatal(err)
}
defer server.Close()
pfilter := NewPacketFilter(server)
pfilterServer := pfilter.NewConn(10, nil)
pfilter.Start()
writer, reader := wrapQuic(pfilterServer)
benchmark(b, writer, reader, quicSize)
}
func wrapQuic(server net.PacketConn) (quic.Stream, quic.Stream) {
tlsCfg := &tls.Config{
// TLS 1.3 is the minimum we accept
MinVersion: tls.VersionTLS13,
}
cert, err := cert()
if err != nil {
panic(err)
}
tlsCfg.Certificates = []tls.Certificate{cert}
tlsCfg.NextProtos = []string{"bench"}
tlsCfg.ClientAuth = tls.RequestClientCert
tlsCfg.SessionTicketsDisabled = true
tlsCfg.InsecureSkipVerify = true
qcfg := &quic.Config{
ConnectionIDLength: 4,
KeepAlivePeriod: 15 * time.Second,
}
l, err := quic.Listen(server, tlsCfg, qcfg)
cses, err := quic.DialAddr(l.Addr().String(), tlsCfg, qcfg)
if err != nil {
panic(err)
}
cstrm, err := cses.OpenStream()
if err != nil {
panic(err)
}
sses, err := l.Accept(context.TODO())
if err != nil {
panic(err)
}
data := []byte("hello")
if _, err := cstrm.Write(data); err != nil {
panic(err)
}
sstrm, err := sses.AcceptStream(context.TODO())
if err != nil {
panic(err)
}
if _, err := sstrm.Read(data); err != nil {
panic(err)
}
return cstrm, sstrm
}
func cert() (tls.Certificate, error) {
priv, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
if err != nil {
return tls.Certificate{}, errors.Wrap(err, "generate key")
}
template := x509.Certificate{
SerialNumber: new(big.Int).SetUint64(1),
Subject: pkix.Name{
CommonName: "test",
Organization: []string{"test"},
OrganizationalUnit: []string{"Automatically Generated"},
},
DNSNames: []string{"test"},
NotBefore: time.Now().Truncate(time.Hour),
NotAfter: time.Now().Add(time.Hour),
SignatureAlgorithm: x509.ECDSAWithSHA256,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth},
BasicConstraintsValid: true,
}
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, priv.Public(), priv)
if err != nil {
return tls.Certificate{}, errors.Wrap(err, "create cert")
}
var certOut bytes.Buffer
err = pem.Encode(&certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
if err != nil {
return tls.Certificate{}, errors.Wrap(err, "save cert")
}
var keyOut bytes.Buffer
b, err := x509.MarshalECPrivateKey(priv)
if err != nil {
return tls.Certificate{}, errors.Wrap(err, "marshal key")
}
err = pem.Encode(&keyOut, &pem.Block{Type: "EC PRIVATE KEY", Bytes: b})
if err != nil {
return tls.Certificate{}, errors.Wrap(err, "save key")
}
return tls.X509KeyPair(certOut.Bytes(), keyOut.Bytes())
}