-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
54 lines (47 loc) · 955 Bytes
/
utils.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
package main
import (
"io"
"log"
"net"
)
func handleErrors(prompt string, err error) {
if err != nil {
log.Fatalln(prompt, err)
}
}
func proxy(conn chan net.Conn, dstAddresses []string) {
loadBalancerIndex := 0
for {
if loadBalancerIndex >= len(dstAddresses) {
loadBalancerIndex = 0
}
dst := dstAddresses[loadBalancerIndex]
select {
case c := <-conn:
log.Println("Proxying to", dst)
destination, err := net.Dial("tcp", dst)
if err != nil {
loadBalancerIndex += 1
log.Println("Unable to dial host", err)
c.Close()
} else {
defer destination.Close()
go func() {
_, err = io.Copy(c, destination)
if err != nil {
log.Println("Unable to copy client", err)
c.Close()
}
}()
_, err = io.Copy(destination, c)
if err != nil {
log.Println("Unable to copy destination", err)
c.Close()
}
loadBalancerIndex += 1
c.Close()
}
default:
}
}
}