-
Notifications
You must be signed in to change notification settings - Fork 10
/
relay.go
209 lines (184 loc) · 5.01 KB
/
relay.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
package easyss
import (
"errors"
"io"
"net"
"syscall"
"time"
"github.com/nange/easyss/v2/cipherstream"
"github.com/nange/easyss/v2/log"
"github.com/nange/easyss/v2/util/bytespool"
"github.com/nange/easyss/v2/util/netpipe"
)
// RelayBufferSize set to MaxCipherRelaySize
const RelayBufferSize = cipherstream.MaxCipherRelaySize
const RelayBufferSizeString = "24kb"
type closeWriter interface {
CloseWrite() error
}
// relay copies between cipher stream and plaintext stream.
// return the number of bytes copies
// from plaintext stream to cipher stream, from cipher stream to plaintext stream, and needClose on server conn
func relay(cipher, plainTxt net.Conn, timeout time.Duration, tryReuse bool) (int64, int64, error) {
ch1 := make(chan res, 1)
ch2 := make(chan res, 1)
go copyCipherToPlainTxt(plainTxt, cipher, timeout, tryReuse, ch2)
go copyPlainTxtToCipher(cipher, plainTxt, timeout, tryReuse, ch1)
var res1, res2 res
var n1, n2 int64
var err error
for i := 0; i < 2; i++ {
select {
case res1 = <-ch1:
n1 = res1.N
err = errors.Join(err, res1.err)
case res2 = <-ch2:
n2 = res2.N
err = errors.Join(err, res2.err)
}
}
if res1.TryReuse && res2.TryReuse {
reuse := tryReuseFn(cipher, timeout)
if reuse != nil {
if cs, ok := cipher.(*cipherstream.CipherStream); ok {
cs.MarkConnUnusable()
}
log.Warn("[REPAY] underlying proxy connection is unhealthy, need close it", "reuse", reuse)
} else {
log.Debug("[REPAY] underlying proxy connection is healthy, so reuse it")
}
} else {
if cs, ok := cipher.(*cipherstream.CipherStream); ok {
cs.MarkConnUnusable()
}
if tryReuse {
log.Warn("[REPAY] underlying proxy connection is unhealthy, need close it", "err", err)
}
}
return n1, n2, err
}
type res struct {
N int64
err error
TryReuse bool
}
func copyCipherToPlainTxt(plainTxt, cipher net.Conn, timeout time.Duration, tryReuse bool, ch chan res) {
var err error
n, er := io.Copy(plainTxt, cipher)
if ce := CloseWrite(plainTxt); ce != nil {
err = errors.Join(err, ce)
log.Warn("[REPAY] close write for plaintxt stream", "err", ce)
}
if se := plainTxt.SetReadDeadline(time.Now().Add(3 * timeout)); se != nil {
err = errors.Join(err, se)
}
if er != nil && !errors.Is(er, cipherstream.ErrFINRSTStream) {
log.Debug("[REPAY] copy from cipher to plaintxt", "err", err)
if tryReuse {
if er = readAllIgnore(cipher, timeout); er != nil && !errors.Is(er, cipherstream.ErrFINRSTStream) {
if !errors.Is(er, cipherstream.ErrTimeout) {
err = errors.Join(err, er)
}
tryReuse = false
}
}
}
ch <- res{N: n, err: err, TryReuse: tryReuse}
}
func copyPlainTxtToCipher(cipher, plainTxt net.Conn, timeout time.Duration, tryReuse bool, ch chan res) {
var err error
n, er := io.Copy(cipher, plainTxt)
if er != nil {
log.Debug("[REPAY] copy from plaintxt to cipher", "err", err)
}
if er := CloseWrite(cipher); er != nil {
tryReuse = false
err = errors.Join(err, er)
if !errors.Is(er, netpipe.ErrPipeClosed) {
log.Warn("[REPAY] close write for cipher stream", "err", err)
}
}
if er := cipher.SetReadDeadline(time.Now().Add(3 * timeout)); er != nil {
err = errors.Join(err, er)
}
ch <- res{N: n, err: err, TryReuse: tryReuse}
}
func tryReuseFn(cipher net.Conn, timeout time.Duration) error {
if err := cipher.SetReadDeadline(time.Now().Add(timeout)); err != nil {
return err
}
if err := WriteACKToCipher(cipher); err != nil {
return err
}
if err := ReadACKFromCipher(cipher); err != nil {
return err
}
if err := cipher.SetReadDeadline(time.Time{}); err != nil {
return err
}
return nil
}
func CloseWrite(conn net.Conn) error {
var err error
if cw, ok := conn.(closeWriter); ok {
if err = cw.CloseWrite(); err != nil && ErrorCanIgnore(err) {
return nil
}
}
return err
}
func ErrorCanIgnore(err error) bool {
var ne net.Error
if errors.As(err, &ne) && ne.Timeout() {
return true /* ignore I/O timeout */
}
if errors.Is(err, syscall.EPIPE) {
return true /* ignore broken pipe */
}
if errors.Is(err, syscall.ECONNRESET) {
return true /* ignore connection reset by peer */
}
if errors.Is(err, syscall.ENOTCONN) {
return true /* ignore transport endpoint is not connected */
}
if errors.Is(err, syscall.ESHUTDOWN) {
return true /* ignore transport endpoint shutdown */
}
return false
}
func readAllIgnore(conn net.Conn, timeout time.Duration) error {
err := conn.SetReadDeadline(time.Now().Add(timeout))
if err != nil {
return err
}
buf := bytespool.Get(RelayBufferSize)
defer bytespool.MustPut(buf)
for {
_, err = conn.Read(buf)
if err != nil {
break
}
}
return err
}
func WriteACKToCipher(conn net.Conn) error {
if csConn, ok := conn.(*cipherstream.CipherStream); ok {
return csConn.WriteRST(cipherstream.FlagACK)
}
return nil
}
func ReadACKFromCipher(conn net.Conn) error {
buf := bytespool.Get(RelayBufferSize)
defer bytespool.MustPut(buf)
var err error
for {
_, err = conn.Read(buf)
if err != nil {
break
}
}
if errors.Is(err, cipherstream.ErrACKRSTStream) {
return nil
}
return err
}