-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
37 lines (33 loc) · 826 Bytes
/
proxy.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
package main
import (
"io"
"go.uber.org/zap"
)
func LogIfErr(err error) {
if err != nil {
zap.L().Error("Error", zap.Error(err))
}
}
// handleProxy is spawned to copy data
func handleProxy(log *zap.Logger, incoming, outgoing io.ReadWriteCloser) {
defer func() { LogIfErr(incoming.Close()) }()
defer func() { LogIfErr(outgoing.Close()) }()
// Forward data between connections
closedSrcDest := make(chan struct{})
closedDestSrc := make(chan struct{})
go pipe(log, incoming, outgoing, closedSrcDest)
go pipe(log, outgoing, incoming, closedDestSrc)
for {
select {
case <-closedSrcDest:
closedSrcDest = nil
case <-closedDestSrc:
closedDestSrc = nil
}
if closedDestSrc == nil && closedSrcDest == nil {
log.Debug("All connections finished")
break
}
}
log.Debug("Proxy session finished")
}