-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
main.go
161 lines (145 loc) · 3.78 KB
/
main.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
151
152
153
154
155
156
157
158
159
160
161
package main
import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
_ "embed"
"encoding/binary"
"fmt"
"io"
"log"
"math/big"
"net/http"
"strings"
"time"
"github.com/quic-go/quic-go/http3"
"github.com/quic-go/webtransport-go"
)
//go:embed index.html
var indexHTML string
var data []byte
func init() {
data = make([]byte, 1<<20)
rand.Read(data)
}
func main() {
tlsConf, err := getTLSConf(time.Now(), time.Now().Add(10*24*time.Hour))
if err != nil {
log.Fatal(err)
}
hash := sha256.Sum256(tlsConf.Certificates[0].Leaf.Raw)
go runHTTPServer(hash)
wmux := http.NewServeMux()
s := webtransport.Server{
H3: http3.Server{
TLSConfig: tlsConf,
Addr: "localhost:12345",
Handler: wmux,
},
CheckOrigin: func(r *http.Request) bool { return true },
}
defer s.Close()
wmux.HandleFunc("/unidirectional", func(w http.ResponseWriter, r *http.Request) {
conn, err := s.Upgrade(w, r)
if err != nil {
log.Printf("upgrading failed: %s", err)
w.WriteHeader(500)
return
}
runUnidirectionalTest(conn)
})
if err := s.ListenAndServe(); err != nil {
log.Fatal(err)
}
}
func runHTTPServer(certHash [32]byte) {
mux := http.NewServeMux()
mux.HandleFunc("/webtransport", func(w http.ResponseWriter, _ *http.Request) {
fmt.Println("handler hit")
content := strings.ReplaceAll(indexHTML, "%%CERTHASH%%", formatByteSlice(certHash[:]))
content = strings.ReplaceAll(content, "%%DATA%%", formatByteSlice(data))
content = strings.ReplaceAll(content, "%%TEST%%", "unidirectional")
w.Write([]byte(content))
})
http.ListenAndServe("localhost:8080", mux)
}
func runUnidirectionalTest(sess *webtransport.Session) {
for i := 0; i < 5; i++ {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
str, err := sess.AcceptUniStream(ctx)
if err != nil {
log.Fatalf("failed to accept unidirectional stream: %v", err)
}
rvcd, err := io.ReadAll(str)
if err != nil {
log.Fatalf("failed to read all data: %v", err)
}
if !bytes.Equal(rvcd, data) {
log.Fatal("data doesn't match")
}
}
select {
case <-sess.Context().Done():
fmt.Println("done")
case <-time.After(5 * time.Second):
log.Fatal("timed out waiting for the session to be closed")
}
}
func getTLSConf(start, end time.Time) (*tls.Config, error) {
cert, priv, err := generateCert(start, end)
if err != nil {
return nil, err
}
return &tls.Config{
Certificates: []tls.Certificate{{
Certificate: [][]byte{cert.Raw},
PrivateKey: priv,
Leaf: cert,
}},
}, nil
}
func generateCert(start, end time.Time) (*x509.Certificate, *ecdsa.PrivateKey, error) {
b := make([]byte, 8)
if _, err := rand.Read(b); err != nil {
return nil, nil, err
}
serial := int64(binary.BigEndian.Uint64(b))
if serial < 0 {
serial = -serial
}
certTempl := &x509.Certificate{
SerialNumber: big.NewInt(serial),
Subject: pkix.Name{},
NotBefore: start,
NotAfter: end,
IsCA: true,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
BasicConstraintsValid: true,
}
caPrivateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return nil, nil, err
}
caBytes, err := x509.CreateCertificate(rand.Reader, certTempl, certTempl, &caPrivateKey.PublicKey, caPrivateKey)
if err != nil {
return nil, nil, err
}
ca, err := x509.ParseCertificate(caBytes)
if err != nil {
return nil, nil, err
}
return ca, caPrivateKey, nil
}
func formatByteSlice(b []byte) string {
s := strings.ReplaceAll(fmt.Sprintf("%#v", b[:]), "[]byte{", "[")
s = strings.ReplaceAll(s, "}", "]")
return s
}