-
Notifications
You must be signed in to change notification settings - Fork 3
/
conn.go
47 lines (42 loc) · 832 Bytes
/
conn.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
package tcpproxy
import (
"io"
"log"
"net"
"sync"
"time"
)
type proxyConn struct {
in, out net.Conn
sync.RWMutex
}
func newProxyConn(in net.Conn) *proxyConn {
return &proxyConn{
in: in,
}
}
func (p *proxyConn) copyStream() <-chan error {
errc := make(chan error, 2)
cp := func(dst io.Writer, src io.Reader) {
_, err := io.Copy(dst, src)
errc <- err
}
go cp(p.in, p.out)
go cp(p.out, p.in)
return errc
}
func setKeepAlive(conn net.Conn, keepAlivePeriod time.Duration) {
tconn, ok := conn.(*net.TCPConn)
if !ok {
log.Println("cannot set TCP keepalive: not TCP connection")
return
}
err := tconn.SetKeepAlivePeriod(keepAlivePeriod)
if err != nil {
log.Println("cannot set keepalive period:", err)
}
err = tconn.SetKeepAlive(true)
if err != nil {
log.Println("cannot set keepalive:", err)
}
}