-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquic.go
149 lines (139 loc) · 3.16 KB
/
quic.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
package tunnel
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"io"
"math/big"
"time"
"github.com/quic-go/quic-go"
)
type QuicWrapper struct {
tr *quic.Transport
tunnel *Tunnel
ctx context.Context
}
func upgrade(tunnel *Tunnel) *QuicWrapper {
log.Debugf("upgrade quic\n")
tr := quic.Transport{
Conn: tunnel.conn,
}
return &QuicWrapper{
tr: &tr,
tunnel: tunnel,
ctx: tunnel.ctx,
}
}
func (q *QuicWrapper) listen() {
tr := q.tr
tlsCfg, err := generateTLSConfig()
if err != nil {
log.Debugf("generate tls config error: %v\n", err)
return
}
listener, err := tr.Listen(tlsCfg, nil)
if err != nil {
log.Debugf("listen error: %v\n", err)
return
}
defer listener.Close()
ctx := q.ctx
for {
log.Debugf("quic server accept\n")
session, err := listener.Accept(ctx)
if err != nil {
log.Debugf("listener error: %v", err)
break
}
go serverSessionHandler(ctx, session)
}
}
func (q *QuicWrapper) dial() {
tlsConf := &tls.Config{
InsecureSkipVerify: true,
NextProtos: []string{"tunnel"},
}
tr := quic.Transport{
Conn: q.tunnel.conn,
}
ctx := q.ctx
connection, err := tr.Dial(ctx, &q.tunnel.remoteAddr, tlsConf, nil)
if err != nil {
log.Debugf("dial error: %v\n", err)
return
}
stream, err := connection.OpenStreamSync(ctx)
if err != nil {
log.Debugf("open stream error: %v\n", err)
return
}
tick := time.NewTicker(time.Second)
for range tick.C {
_, err = stream.Write([]byte("foobar"))
if err != nil {
log.Debugf("write error: %s\n", err.Error())
break
}
log.Debugln("client write")
}
log.Debugln("client exit")
}
func serverSessionHandler(ctx context.Context, session quic.Connection) {
log.Debugln("handling session...")
defer func() {
if err := session.CloseWithError(0, "Close"); err != nil {
log.Debugf("session Close error: %v\n", err)
}
}()
for {
stream, err := session.AcceptStream(ctx)
if err != nil {
log.Debugf("session error: %v\n", err)
break
}
go serverStreamHandler(stream)
}
}
func serverStreamHandler(stream io.ReadWriteCloser) {
log.Debugf("handling stream...")
defer stream.Close()
bytes := make([]byte, 1024)
OUTER:
for {
n, err := stream.Read(bytes)
if err != nil {
log.Debugf("read error: %s\n", err.Error())
break OUTER
}
log.Debugf("server read: %s\n", string(bytes[:n]))
_, err = stream.Write(bytes[:n])
if err != nil {
log.Debugf("write error: %s\n", err.Error())
break OUTER
}
}
}
func generateTLSConfig() (*tls.Config, error) {
key, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil {
return nil, err
}
template := x509.Certificate{SerialNumber: big.NewInt(1)}
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &key.PublicKey, key)
if err != nil {
return nil, err
}
keyPEM := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(key)})
certPEM := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: certDER})
tlsCert, err := tls.X509KeyPair(certPEM, keyPEM)
if err != nil {
return nil, err
}
return &tls.Config{
Certificates: []tls.Certificate{tlsCert},
NextProtos: []string{"tunnel"},
}, nil
}