-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
quic.go
173 lines (159 loc) · 3.81 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) 2016-present Cloud <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 3 of the GNU General Public
// License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package brook
import (
"context"
"crypto/tls"
"net"
"time"
"github.com/quic-go/quic-go"
)
func QUICDialUDP(src, dst, addr string, tc *tls.Config, idleTime int) (net.Conn, error) {
var rc *net.UDPConn
var err error
if src == "" || dst == "" {
rc, err = ListenUDP("udp", nil)
}
if src != "" && dst != "" {
rc, err = NATListenUDP("udp", src, dst)
}
if err != nil {
return nil, err
}
raddr, err := Resolve("udp", addr)
if err != nil {
rc.Close()
return nil, err
}
rc1, err := quic.Dial(context.Background(), rc, raddr, tc, &quic.Config{MaxIdleTimeout: time.Duration(idleTime) * time.Second, EnableDatagrams: true})
if err != nil {
rc.Close()
return nil, err
}
return &QUICConn{
UDPConn: rc,
Conn: rc1,
LAddr: rc1.LocalAddr(),
RAddr: rc1.RemoteAddr(),
}, nil
}
func QUICDialTCP(src, dst, addr string, tc *tls.Config, idleTime int) (net.Conn, error) {
var rc *net.UDPConn
var err error
if src == "" || dst == "" {
rc, err = ListenUDP("udp", nil)
}
if src != "" && dst != "" {
rc, err = NATListenUDP("udp", src, dst)
}
if err != nil {
return nil, err
}
raddr, err := Resolve("udp", addr)
if err != nil {
rc.Close()
return nil, err
}
rc1, err := quic.Dial(context.Background(), rc, raddr, tc, &quic.Config{MaxIdleTimeout: time.Duration(idleTime) * time.Second})
if err != nil {
rc.Close()
return nil, err
}
s, err := rc1.OpenStreamSync(context.Background())
if err != nil {
rc1.CloseWithError(0, err.Error())
rc.Close()
return nil, err
}
return &QUICConn{
UDPConn: rc,
Conn: rc1,
Stream: s,
LAddr: &net.TCPAddr{
IP: rc1.LocalAddr().(*net.UDPAddr).IP,
Port: rc1.LocalAddr().(*net.UDPAddr).Port,
Zone: rc1.LocalAddr().(*net.UDPAddr).Zone,
},
RAddr: &net.TCPAddr{
IP: rc1.RemoteAddr().(*net.UDPAddr).IP,
Port: rc1.RemoteAddr().(*net.UDPAddr).Port,
Zone: rc1.RemoteAddr().(*net.UDPAddr).Zone,
},
}, nil
}
type QUICConn struct {
UDPConn *net.UDPConn
Conn quic.Connection
Stream quic.Stream
LAddr net.Addr
RAddr net.Addr
}
func (c *QUICConn) Read(b []byte) (int, error) {
if c.Stream != nil {
return c.Stream.Read(b)
}
b1, err := c.Conn.ReceiveDatagram(context.Background())
if err != nil {
return 0, err
}
i := copy(b, b1)
return i, nil
}
func (c *QUICConn) Write(b []byte) (int, error) {
if c.Stream != nil {
return c.Stream.Write(b)
}
if err := c.Conn.SendDatagram(b); err != nil {
return 0, err
}
return len(b), nil
}
func (c *QUICConn) Close() error {
if c.Stream != nil {
c.Stream.CancelRead(0)
c.Stream.Close()
}
if c.Conn != nil {
c.Conn.CloseWithError(0, "close")
}
if c.UDPConn != nil {
c.UDPConn.Close()
}
return nil
}
func (c *QUICConn) LocalAddr() net.Addr {
return c.LAddr
}
func (c *QUICConn) RemoteAddr() net.Addr {
return c.RAddr
}
func (c *QUICConn) SetDeadline(t time.Time) error {
if c.Stream != nil {
return c.Stream.SetDeadline(t)
}
// prefer quic.Config.MaxIdleTimeout
return nil
}
func (c *QUICConn) SetReadDeadline(t time.Time) error {
if c.Stream != nil {
return c.Stream.SetReadDeadline(t)
}
return nil
}
func (c *QUICConn) SetWriteDeadline(t time.Time) error {
if c.Stream != nil {
return c.Stream.SetWriteDeadline(t)
}
return nil
}