-
Notifications
You must be signed in to change notification settings - Fork 19
/
conn_tunnel.go
157 lines (129 loc) · 3.34 KB
/
conn_tunnel.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
// 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 (
"io"
"net"
"sync"
"time"
)
var (
ioCopyBuffPool = &sync.Pool{New: func() interface{} {
return make([]byte, 16*1024)
}}
)
func acquireIOBuf() []byte {
return ioCopyBuffPool.Get().([]byte)
}
func releaseIOBuf(b []byte) {
ioCopyBuffPool.Put(b)
}
type tunnelContext struct {
sync.Mutex
cancelOnce sync.Once
done bool
err error
}
func (fe *tunnelContext) reportAndCancel(err error) {
fe.Lock()
defer fe.Unlock()
fe.cancelOnce.Do(func() {
fe.done = true
fe.err = err
})
}
func (fe *tunnelContext) getErr() error {
fe.Lock()
defer fe.Unlock()
return fe.err
}
func (fe *tunnelContext) isDone() bool {
fe.Lock()
defer fe.Unlock()
return fe.done
}
func (fe *tunnelContext) setDeadline(c net.Conn, t time.Time) error {
fe.Lock()
defer fe.Unlock()
if fe.done {
return nil
}
return c.SetDeadline(t)
}
// openTunnel opens a tunnel between a and b.
func openTunnel(a, b net.Conn, timeout time.Duration) error {
tc := tunnelContext{}
go openOneWayTunnel(a, b, timeout, &tc)
openOneWayTunnel(b, a, timeout, &tc)
return tc.getErr()
}
// don not use this func, use openTunnel instead
func openOneWayTunnel(dst, src net.Conn, timeout time.Duration, tc *tunnelContext) {
buf := acquireIOBuf()
_, err := copyBuffer(dst, src, buf, timeout, tc)
// a nil err might be an io.EOF err, which is suppressed by copyBuffer.
// report a nil err means one conn was closed by peer.
tc.reportAndCancel(err)
// let another goroutine break from copy loop
// tc is canceled, no race.
src.SetDeadline(time.Now())
dst.SetDeadline(time.Now())
releaseIOBuf(buf)
}
func copyBuffer(dst net.Conn, src net.Conn, buf []byte, timeout time.Duration, tc *tunnelContext) (written int64, err error) {
if len(buf) <= 0 {
panic("buf size <= 0")
}
var previousRead time.Time
for {
tc.setDeadline(src, time.Now().Add(timeout))
nr, er := src.Read(buf)
if er == nil { // only pad when src is healthy(no err)
if ps, ok := src.(*paddingConn); ok && ps.paddingOnWrite {
// don not pad when read is continuous
if time.Since(previousRead) > paddingIntervalThreshold {
tc.setDeadline(ps, time.Now().Add(timeout))
ps.tryWritePaddingInOtherGoRoutine(randomPaddingSize())
}
}
}
// update previousRead
previousRead = time.Now()
if nr > 0 {
tc.setDeadline(dst, time.Now().Add(timeout))
nw, ew := dst.Write(buf[0:nr])
if nw > 0 {
written += int64(nw)
}
if ew != nil {
err = ew
break
}
if nr != nw {
err = io.ErrShortWrite
break
}
}
if er != nil {
if er != io.EOF {
err = er
}
break
}
}
return written, err
}