-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproxy.go
73 lines (67 loc) · 1.19 KB
/
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
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
package main
import (
"crypto/tls"
"io"
"net"
)
// forward conn to another conn of a remote address.
func forward(conn net.Conn, remote string) error {
bconn, err := net.Dial("tcp", remote)
if err != nil {
return err
}
defer bconn.Close()
go func() {
defer conn.Close()
io.Copy(conn, bconn)
}()
io.Copy(bconn, conn)
return nil
}
// listenAndServe accepts connections on addr and forwards them to another backend.
func listenAndServe(backend, addr string, conf *tls.Config) error {
ln, err := tls.Listen("tcp", addr, conf)
if err != nil {
return err
}
connCh := make(chan net.Conn, 1)
errCh := make(chan error, 1)
doneCh := make(chan struct{})
defer close(doneCh)
go func() {
defer close(connCh)
for {
conn, err := ln.Accept()
if err != nil {
select {
case errCh <- err:
default:
}
return
}
select {
case connCh <- conn:
case <-doneCh:
return
}
}
}()
for {
select {
case conn, ok := <-connCh:
if !ok {
return io.EOF
}
go func(conn net.Conn) {
if err := forward(conn, backend); err != nil {
select {
case errCh <- err:
default:
}
}
}(conn)
case err := <-errCh:
return err
}
}
}