-
Notifications
You must be signed in to change notification settings - Fork 19
/
padding.go
195 lines (161 loc) · 4.33 KB
/
padding.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
// Copyright (C) 2020, IrineSistiana
//
// This file is part of simple-tls.
//
// simple-tls is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// simple-tls 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 core
import (
"errors"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net"
"sync"
"time"
)
const (
maxPaddingSize uint16 = 0xffff // 64Kb
paddingIntervalThreshold = time.Millisecond * 10
)
var (
zeros = make([]byte, maxPaddingSize)
)
type frameType uint8
const (
headerNil frameType = 0
headerData frameType = 1
headerPadding frameType = 2
)
type paddingConn struct {
net.Conn
paddingInRead bool
paddingOnWrite bool
rl sync.Mutex
currentFrame frameType
frameLeft uint16
wl *locker
}
func newPaddingConn(c net.Conn, paddingInRead, paddingOnWrite bool) *paddingConn {
return &paddingConn{Conn: c, paddingInRead: paddingInRead, paddingOnWrite: paddingOnWrite, wl: newLocker()}
}
func (c *paddingConn) Read(b []byte) (n int, err error) {
if !c.paddingInRead {
return c.Conn.Read(b)
}
c.rl.Lock()
defer c.rl.Unlock()
read:
if c.currentFrame == headerNil {
t, l, err := c.readHeader() // new frame
if err != nil {
return 0, err
}
c.currentFrame = t
c.frameLeft = l
}
switch c.currentFrame {
case headerData:
if c.frameLeft == 0 {
return 0, io.EOF
}
n1, err := io.LimitReader(c.Conn, int64(c.frameLeft)).Read(b)
c.frameLeft -= uint16(n1)
if c.frameLeft == 0 { // this frame is eof
c.currentFrame = headerNil // reset currentFrame
if err == io.EOF && n1 != 0 { // don't raise this EOF
err = nil
}
}
return n1, err
case headerPadding:
buf := acquireIOBuf()
n1, err := io.CopyBuffer(ioutil.Discard, io.LimitReader(c.Conn, int64(c.frameLeft)), buf)
releaseIOBuf(buf)
c.frameLeft -= uint16(n1)
if c.frameLeft == 0 { // this frame is eof
c.currentFrame = headerNil
if err == io.EOF { // don't raise this EOF
err = nil
}
}
goto read
default:
return 0, fmt.Errorf("unexpect frame type, %d", c.currentFrame)
}
}
var paddingHeaderPool = sync.Pool{New: func() interface{} { return make([]byte, 3) }}
func (c *paddingConn) readHeader() (t frameType, l uint16, err error) {
h := paddingHeaderPool.Get().([]byte)
defer paddingHeaderPool.Put(h)
_, err = io.ReadFull(c.Conn, h)
if err != nil {
return 0, 0, err
}
return frameType(h[0]), uint16(h[1])<<8 | uint16(h[2]), nil
}
func (c *paddingConn) Write(b []byte) (n int, err error) {
if !c.paddingOnWrite {
return c.Conn.Write(b)
}
return c.writeFrame(headerData, b, false)
}
var errPaddingDisabled = errors.New("connection padding opt is disabled")
func (c *paddingConn) writePadding(l uint16) (n int, err error) {
if !c.paddingOnWrite {
return 0, errPaddingDisabled
}
return c.writeFrame(headerPadding, zeros[:l], false)
}
func (c *paddingConn) tryWritePaddingInOtherGoRoutine(l uint16) {
if !c.paddingOnWrite {
return
}
if c.wl.tryLock() == true {
go func() {
c.writeFrame(headerPadding, zeros[:l], true)
c.wl.unlock()
}()
}
}
// wBufPool is a 64Kb buffer pool
var wBufPool = sync.Pool{New: func() interface{} { return make([]byte, 0xffff) }}
func (c *paddingConn) writeFrame(t frameType, b []byte, disableLocker bool) (n int, err error) {
if !disableLocker {
c.wl.lock()
defer c.wl.unlock()
}
// Note:
// We try to align this to tls frame. So, the largest frame here is 0xffff - 3 bytes.
buf := wBufPool.Get().([]byte)
defer wBufPool.Put(buf)
for n < len(b) {
f := copy(buf[3:], b[n:])
buf[0] = byte(t)
buf[1] = byte(f >> 8)
buf[2] = byte(f)
n2, err := c.Conn.Write(buf[:3+f])
if n2 > 3 {
n += n2 - 3 // 3 bytes header
}
if err != nil {
return n, err
}
}
return n, nil
}
// randomPaddingSize returns a random num between 4 ~ 16
func randomPaddingSize() uint16 {
return 4 + uint16(rand.Int31n(12))
}