-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.go
137 lines (124 loc) · 2.98 KB
/
server.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
package perf
import (
"context"
"crypto/ed25519"
"crypto/rand"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/binary"
"encoding/pem"
"fmt"
"io"
"log"
"math/big"
"net"
"time"
"github.com/quic-go/quic-go"
)
func RunServer(addr string, keyLogFile io.Writer) error {
tlsConf, err := generateSelfSignedTLSConfig()
if err != nil {
log.Fatal(err)
}
tlsConf.NextProtos = []string{ALPN}
tlsConf.KeyLogWriter = keyLogFile
conf := config.Clone()
ln, err := quic.ListenAddr(addr, tlsConf, conf)
if err != nil {
return err
}
log.Println("Listening on", ln.Addr())
defer ln.Close()
for {
conn, err := ln.Accept(context.Background())
if err != nil {
return fmt.Errorf("accept errored: %w", err)
}
go func(conn quic.Connection) {
if err := handleConn(conn); err != nil {
log.Printf("handling conn from %s failed: %s", conn.RemoteAddr(), err)
}
}(conn)
}
}
func handleConn(conn quic.Connection) error {
for {
str, err := conn.AcceptStream(context.Background())
if err != nil {
return err
}
go func(str quic.Stream) {
if err := handleServerStream(str); err != nil {
log.Printf("handling stream from %s failed: %s", conn.RemoteAddr(), err)
}
}(str)
}
}
func handleServerStream(str io.ReadWriteCloser) error {
b := make([]byte, 8)
if _, err := io.ReadFull(str, b); err != nil {
return err
}
amount := binary.BigEndian.Uint64(b)
b = make([]byte, 16*1024)
// receive data until the client sends a FIN
for {
if _, err := str.Read(b); err != nil {
if err == io.EOF {
break
}
return err
}
}
// send as much data as the client requested
for amount > 0 {
if amount < uint64(len(b)) {
b = b[:amount]
}
n, err := str.Write(b)
if err != nil {
return err
}
amount -= uint64(n)
}
return str.Close()
}
func generateSelfSignedTLSConfig() (*tls.Config, error) {
pubKey, privKey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return nil, err
}
notBefore := time.Now()
notAfter := notBefore.Add(365 * 24 * time.Hour)
template := &x509.Certificate{
SerialNumber: big.NewInt(1),
Subject: pkix.Name{
CommonName: "localhost",
},
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
IsCA: false,
IPAddresses: []net.IP{net.ParseIP("127.0.0.1")},
}
derBytes, err := x509.CreateCertificate(rand.Reader, template, template, pubKey, privKey)
if err != nil {
return nil, err
}
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
b, err := x509.MarshalPKCS8PrivateKey(privKey)
if err != nil {
return nil, err
}
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: b})
cert, err := tls.X509KeyPair(certPEM, keyPEM)
if err != nil {
return nil, err
}
return &tls.Config{
Certificates: []tls.Certificate{cert},
}, nil
}