forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.go
92 lines (81 loc) · 1.58 KB
/
io.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
package kcp
import (
"crypto/cipher"
"crypto/rand"
"io"
"v2ray.com/core/transport/internet"
)
type PacketReader interface {
Read([]byte) []Segment
}
type PacketWriter interface {
Overhead() int
io.Writer
}
type KCPPacketReader struct {
Security cipher.AEAD
Header internet.PacketHeader
}
func (r *KCPPacketReader) Read(b []byte) []Segment {
if r.Header != nil {
b = b[r.Header.Size():]
}
if r.Security != nil {
nonceSize := r.Security.NonceSize()
out, err := r.Security.Open(b[nonceSize:nonceSize], b[:nonceSize], b[nonceSize:], nil)
if err != nil {
return nil
}
b = out
}
var result []Segment
for len(b) > 0 {
seg, x := ReadSegment(b)
if seg == nil {
break
}
result = append(result, seg)
b = x
}
return result
}
type KCPPacketWriter struct {
Header internet.PacketHeader
Security cipher.AEAD
Writer io.Writer
buffer [32 * 1024]byte
}
func (w *KCPPacketWriter) Overhead() int {
overhead := 0
if w.Header != nil {
overhead += w.Header.Size()
}
if w.Security != nil {
overhead += w.Security.Overhead()
}
return overhead
}
func (w *KCPPacketWriter) Write(b []byte) (int, error) {
x := w.buffer[:]
size := 0
if w.Header != nil {
nBytes, _ := w.Header.Write(x)
size += nBytes
x = x[nBytes:]
}
if w.Security != nil {
nonceSize := w.Security.NonceSize()
var nonce []byte
if nonceSize > 0 {
nonce = x[:nonceSize]
rand.Read(nonce)
x = x[nonceSize:]
}
x = w.Security.Seal(x[:0], nonce, b, nil)
size += nonceSize + len(x)
} else {
size += copy(x, b)
}
_, err := w.Writer.Write(w.buffer[:size])
return len(b), err
}