forked from mkevac/goduplicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
376 lines (315 loc) · 8.18 KB
/
main.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package main
import (
"flag"
"fmt"
"io"
"log"
"net"
"os"
"strings"
"sync/atomic"
"time"
"golang.org/x/sys/unix"
)
const (
defaultBufferSize = 1024
SPLICE_F_MOVE = 1
SPLICE_F_NONBLOCK = 2
SPLICE_F_MORE = 4
SPLICE_F_GIFT = 8
MaxUint = ^uint(0)
MaxInt = int(MaxUint >> 1)
)
type mirror struct {
addr string
conn net.Conn
closed uint32
}
func readAndDiscard(m mirror, closeCh chan error) {
for {
var b [defaultBufferSize]byte
_, err := m.conn.Read(b[:])
if err != nil {
m.conn.Close()
atomic.StoreUint32(&m.closed, 1)
select {
case closeCh <- err:
default:
}
return
}
}
}
func forward(from net.Conn, to net.Conn, closeCh chan error) {
for {
var b [defaultBufferSize]byte
n, err := from.Read(b[:])
if err != nil {
if err != io.EOF {
closeCh <- fmt.Errorf("from.Read() failed: %w", err)
} else {
closeCh <- nil
}
return
}
_, err = to.Write(b[:n])
if err != nil {
closeCh <- fmt.Errorf("to.Write() failed: %w", err)
return
}
}
}
func forwardZeroCopy(from net.Conn, to net.Conn, closeCh chan error) {
var (
p [2]int
nullPtr *int64
)
if err := unix.Pipe(p[:]); err != nil {
closeCh <- fmt.Errorf("pipe() error: %w", err)
return
}
fromFile, err := from.(*net.TCPConn).File()
if err != nil {
closeCh <- fmt.Errorf("error while creating File() from incoming connection: %w", err)
return
}
toFile, err := to.(*net.TCPConn).File()
if err != nil {
closeCh <- fmt.Errorf("error while creating File() from outgoing connection: %w", err)
return
}
for {
_, err = unix.Splice(int(fromFile.Fd()), nullPtr, p[1], nullPtr, MaxInt, SPLICE_F_MOVE)
if err != nil {
closeCh <- fmt.Errorf("error while splicing from conn to pipe: %w", err)
return
}
_, err = unix.Splice(p[0], nullPtr, int(toFile.Fd()), nullPtr, MaxInt, SPLICE_F_MOVE)
if err != nil {
closeCh <- fmt.Errorf("error while splicing from pipe to conn: %w", err)
return
}
}
}
func forwardAndZeroCopy(from net.Conn, to net.Conn, mirrors []mirror, closeCh, errorCh chan error) {
type mirrorInt struct {
mirror
mirrorFile *os.File
mirrorPipe [2]int
}
var (
p [2]int
nullPtr *int64
mirrorsInt []mirrorInt
)
if err := unix.Pipe(p[:]); err != nil {
errorCh <- fmt.Errorf("pipe() error: %w", err)
return
}
fromFile, err := from.(*net.TCPConn).File()
if err != nil {
errorCh <- fmt.Errorf("error while creating File() from incoming connection: %w", err)
return
}
toFile, err := to.(*net.TCPConn).File()
if err != nil {
errorCh <- fmt.Errorf("error while creating File() from outgoing connection: %w", err)
return
}
for _, m := range mirrors {
mFile, err := m.conn.(*net.TCPConn).File()
if err != nil {
errorCh <- fmt.Errorf("error while creating File() from mirror connection: %w", err)
}
var mPipe [2]int
if err := unix.Pipe(mPipe[:]); err != nil {
errorCh <- fmt.Errorf("pipe() error: %w", err)
return
}
mirrorsInt = append(mirrorsInt, mirrorInt{
mirror: m,
mirrorPipe: mPipe,
mirrorFile: mFile,
})
}
for _, m := range mirrorsInt {
go func(m mirrorInt) { // splice data from pipe to conn
for {
_, err = unix.Splice(m.mirrorPipe[0], nullPtr, int(m.mirrorFile.Fd()), nullPtr, MaxInt, SPLICE_F_MOVE)
if err != nil {
select {
case errorCh <- fmt.Errorf("error while splicing from pipe to conn: %w", err):
default:
}
return
}
}
}(m)
}
for {
_, err = unix.Splice(int(fromFile.Fd()), nullPtr, p[1], nullPtr, MaxInt, SPLICE_F_MOVE)
if err != nil {
closeCh <- fmt.Errorf("error while splicing from conn to pipe: %w", err)
return
}
nteed := int64(MaxInt)
for _, m := range mirrorsInt {
if closed := atomic.LoadUint32(&m.closed); closed == 1 {
continue
}
nteed, err = unix.Tee(p[0], m.mirrorPipe[1], MaxInt, SPLICE_F_MOVE)
if err != nil {
m.conn.Close()
atomic.StoreUint32(&m.closed, 1)
select {
case errorCh <- fmt.Errorf("error while tee(): %w", err):
default:
}
}
}
_, err = unix.Splice(p[0], nullPtr, int(toFile.Fd()), nullPtr, int(nteed), SPLICE_F_MOVE)
if err != nil {
closeCh <- fmt.Errorf("error while splice(): %w", err)
return
}
}
}
var writeTimeout time.Duration
func forwardAndCopy(from net.Conn, to net.Conn, mirrors []mirror, closeCh, errorCh chan error) {
for {
var b [defaultBufferSize]byte
n, err := from.Read(b[:])
if err != nil {
if err != io.EOF {
closeCh <- fmt.Errorf("from.Read() failed: %w", err)
} else {
closeCh <- nil
}
return
}
_, err = to.Write(b[:n])
if err != nil {
closeCh <- fmt.Errorf("to.Write() failed: %w", err)
return
}
for i := 0; i < len(mirrors); i++ {
if closed := atomic.LoadUint32(&mirrors[i].closed); closed == 1 {
continue
}
mirrors[i].conn.SetWriteDeadline(time.Now().Add(writeTimeout))
_, err = mirrors[i].conn.Write(b[:n])
if err != nil {
mirrors[i].conn.Close()
atomic.StoreUint32(&mirrors[i].closed, 1)
select {
case errorCh <- err:
default:
}
}
}
}
}
func connect(origin net.Conn, forwarder net.Conn, mirrors []mirror, useZeroCopy bool, closeCh chan error, errorCh chan error) {
for i := 0; i < len(mirrors); i++ {
go readAndDiscard(mirrors[i], closeCh)
}
if useZeroCopy {
go forwardZeroCopy(forwarder, origin, closeCh)
go forwardAndZeroCopy(origin, forwarder, mirrors, closeCh, errorCh)
} else {
go forward(forwarder, origin, closeCh)
go forwardAndCopy(origin, forwarder, mirrors, closeCh, errorCh)
}
}
type mirrorList []string
func (l *mirrorList) String() string {
return fmt.Sprint(*l)
}
func (l *mirrorList) Set(value string) error {
for _, m := range strings.Split(value, ",") {
*l = append(*l, m)
}
return nil
}
func main() {
var (
connectTimeout time.Duration
delay time.Duration
listenAddress string
forwardAddress string
mirrorAddresses mirrorList
useZeroCopy bool
)
flag.BoolVar(&useZeroCopy, "z", false, "use zero copy")
flag.StringVar(&listenAddress, "l", "", "listen address (e.g. 'localhost:8080')")
flag.StringVar(&forwardAddress, "f", "", "forward to address (e.g. 'localhost:8081')")
flag.Var(&mirrorAddresses, "m", "comma separated list of mirror addresses (e.g. 'localhost:8082,localhost:8083')")
flag.DurationVar(&connectTimeout, "t", 500*time.Millisecond, "mirror connect timeout")
flag.DurationVar(&delay, "d", 20*time.Second, "delay connecting to mirror after unsuccessful attempt")
flag.DurationVar(&writeTimeout, "wt", 20*time.Millisecond, "mirror write timeout")
flag.Parse()
if listenAddress == "" || forwardAddress == "" {
flag.Usage()
return
}
l, err := net.Listen("tcp", listenAddress)
if err != nil {
log.Fatalf("error while listening: %s", err)
}
var connNo uint64
for {
c, err := l.Accept()
if err != nil {
log.Printf("Error while accepting: %s", err)
continue
}
log.Printf("accepted connection %d (%s <-> %s)", connNo, c.RemoteAddr(), c.LocalAddr())
go func(connClient net.Conn) {
defer connClient.Close()
connForwardee, err := net.Dial("tcp", forwardAddress)
if err != nil {
log.Printf("error while connecting to forwarder (%s), will close client connection", err)
return
}
defer connForwardee.Close()
var mirrors []mirror
for _, addr := range mirrorAddresses {
connMirror, err := net.DialTimeout("tcp", addr, connectTimeout)
if err != nil {
log.Printf("error while connecting to mirror %s (%s), will continue", addr, err)
continue
}
mirrors = append(mirrors, mirror{
addr: addr,
conn: connMirror,
closed: 0,
})
}
defer func() {
for i, m := range mirrors {
if closed := atomic.LoadUint32(&mirrors[i].closed); closed == 1 {
continue
}
m.conn.Close()
}
}()
closeCh := make(chan error, 1024)
errorCh := make(chan error, 1024)
connect(connClient, connForwardee, mirrors, useZeroCopy, closeCh, errorCh)
for {
select {
case err := <-errorCh:
if err != nil {
log.Printf("got error (%s), will continue", err)
}
case err := <-closeCh:
if err != nil {
log.Printf("got error (%s), will close client connection", err)
}
return
}
}
}(c)
connNo += 1
}
}