-
Notifications
You must be signed in to change notification settings - Fork 11
/
udp.go
245 lines (199 loc) · 5.12 KB
/
udp.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package tuna
import (
"fmt"
"io"
"net"
"strconv"
"sync"
"time"
stream "github.com/nknorg/encrypted-stream"
"github.com/nknorg/tuna/pb"
)
const (
PrefixLen = 4
DefaultUDPBufferSize = 8192
MaxUDPBufferSize = 65527
)
type UDPConn interface {
WriteMsgUDP(b, oob []byte, addr *net.UDPAddr) (n, oobn int, err error)
ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error)
Close() error
LocalAddr() net.Addr
RemoteAddr() net.Addr
SetWriteBuffer(bytes int) error
SetReadBuffer(bytes int) error
}
type EncryptUDPConn struct {
conn UDPConn
encoders sync.Map
decoders sync.Map
lock sync.RWMutex
isClosed bool
readLock sync.Mutex
writeLock sync.Mutex
readBuffer []byte
writeBuffer []byte
}
func NewEncryptUDPConn(conn *net.UDPConn) *EncryptUDPConn {
ec := &EncryptUDPConn{
conn: conn,
readBuffer: make([]byte, MaxUDPBufferSize),
writeBuffer: make([]byte, MaxUDPBufferSize),
}
conn.SetReadBuffer(MaxUDPBufferSize)
conn.SetWriteBuffer(MaxUDPBufferSize)
return ec
}
func (ec *EncryptUDPConn) AddCodec(addr *net.UDPAddr, encryptKey *[32]byte, encryptionAlgo pb.EncryptionAlgo, initiator bool) error {
var cipher stream.Cipher
var err error
switch encryptionAlgo {
case pb.EncryptionAlgo_ENCRYPTION_NONE:
cipher = nil
case pb.EncryptionAlgo_ENCRYPTION_XSALSA20_POLY1305:
cipher = stream.NewXSalsa20Poly1305Cipher(encryptKey)
case pb.EncryptionAlgo_ENCRYPTION_AES_GCM:
cipher, err = stream.NewAESGCMCipher(encryptKey[:])
if err != nil {
return err
}
default:
return fmt.Errorf("unsupported encryption algo %v", encryptionAlgo)
}
encoder, err := stream.NewEncoder(cipher, initiator, false)
if err != nil {
return err
}
decoder, err := stream.NewDecoder(cipher, initiator, false, true)
if err != nil {
return err
}
ec.encoders.Store(addr.String(), encoder)
ec.decoders.Store(addr.String(), decoder)
return nil
}
func (ec *EncryptUDPConn) ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) {
n, addr, _, err = ec.ReadFromUDPEncrypted(b)
return
}
func (ec *EncryptUDPConn) ReadFromUDPEncrypted(b []byte) (n int, addr *net.UDPAddr, encrypted bool, err error) {
if ec == nil {
return 0, nil, false, fmt.Errorf("unconnected udp conn")
}
if ec.IsClosed() {
return 0, nil, false, io.ErrClosedPipe
}
ec.readLock.Lock()
defer ec.readLock.Unlock()
n, addr, err = ec.conn.ReadFromUDP(ec.readBuffer)
if err != nil {
return 0, addr, false, err
}
d, ok := ec.decoders.Load(addr.String())
if !ok {
copy(b, ec.readBuffer[:n])
return n, addr, false, nil
}
decoder := d.(*stream.Decoder)
plain, err := decoder.Decode(b, ec.readBuffer[:n])
if err != nil {
return 0, addr, false, err
}
return len(plain), addr, true, nil
}
func (ec *EncryptUDPConn) WriteMsgUDP(b, oob []byte, addr *net.UDPAddr) (n, oobn int, err error) {
n, oobn, _, err = ec.WriteMsgUDPEncrypted(b, oob, addr)
return
}
func (ec *EncryptUDPConn) WriteMsgUDPEncrypted(b, oob []byte, addr *net.UDPAddr) (n, oobn int, encrypted bool, err error) {
if ec == nil {
return 0, 0, false, fmt.Errorf("unconnected udp conn")
}
if ec.IsClosed() {
return 0, 0, false, io.ErrClosedPipe
}
ec.writeLock.Lock()
defer ec.writeLock.Unlock()
var k string
var ciphertext []byte
encrypted = false
if addr == nil {
k = ec.RemoteUDPAddr().String()
} else {
k = addr.String()
}
e, ok := ec.encoders.Load(k)
if !ok {
ciphertext = b
} else {
encoder := e.(*stream.Encoder)
ciphertext, err = encoder.Encode(ec.writeBuffer, b)
if err != nil {
return 0, 0, false, err
}
encrypted = true
}
n, oobn, err = ec.conn.WriteMsgUDP(ciphertext, oob, addr)
if err != nil {
return 0, 0, false, err
}
if n != len(ciphertext) {
return 0, 0, false, io.ErrShortWrite
}
return len(b), oobn, encrypted, err
}
func (ec *EncryptUDPConn) SetWriteBuffer(size int) error {
return ec.conn.SetWriteBuffer(size)
}
func (ec *EncryptUDPConn) SetReadBuffer(size int) error {
return ec.conn.SetReadBuffer(size)
}
func (ec *EncryptUDPConn) LocalAddr() net.Addr {
return ec.conn.LocalAddr()
}
func (ec *EncryptUDPConn) IsClosed() bool {
ec.lock.RLock()
defer ec.lock.RUnlock()
return ec.isClosed
}
func (ec *EncryptUDPConn) Close() error {
ec.lock.Lock()
defer ec.lock.Unlock()
ec.isClosed = true
return ec.conn.Close()
}
func (ec *EncryptUDPConn) RemoteAddr() net.Addr {
return ec.conn.RemoteAddr()
}
func (ec *EncryptUDPConn) RemoteUDPAddr() *net.UDPAddr {
host, portStr, _ := net.SplitHostPort(ec.RemoteAddr().String())
port, _ := strconv.Atoi(portStr)
return &net.UDPAddr{IP: net.ParseIP(host), Port: port}
}
func (ec *EncryptUDPConn) SetDeadline(t time.Time) error {
ec.lock.RLock()
defer ec.lock.RUnlock()
if ec.isClosed {
return ErrClosed
}
c := ec.conn.(*net.UDPConn)
return c.SetDeadline(t)
}
func (ec *EncryptUDPConn) SetReadDeadline(t time.Time) error {
ec.lock.RLock()
defer ec.lock.RUnlock()
if ec.isClosed {
return ErrClosed
}
c := ec.conn.(*net.UDPConn)
return c.SetReadDeadline(t)
}
func (ec *EncryptUDPConn) SetWriteDeadline(t time.Time) error {
ec.lock.RLock()
defer ec.lock.RUnlock()
if ec.isClosed {
return ErrClosed
}
c := ec.conn.(*net.UDPConn)
return c.SetWriteDeadline(t)
}