-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.go
47 lines (40 loc) · 966 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
38
39
40
41
42
43
44
45
46
47
package main
import (
"io"
"log"
"net"
"sync"
)
func (instance *Config) handleConnection(clientConn *net.TCPConn) {
defer clientConn.Close()
targetTCPAddr := instance.Target.getTCPAddr()
log.Printf("Serving %s to %s\n", clientConn.RemoteAddr().String(), targetTCPAddr)
targetConn, err := net.DialTCP("tcp", nil, targetTCPAddr)
checkErrFatal(err)
defer targetConn.Close()
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
io.Copy(clientConn, targetConn)
clientConn.CloseWrite()
}()
go func() {
defer wg.Done()
io.Copy(targetConn, clientConn)
clientConn.CloseWrite()
}()
wg.Wait()
}
func (instance *Config) server() {
target := instance.Server.getTCPAddr()
l, err := net.ListenTCP("tcp", target)
log.Println("Listing on", target, "Forwarding connections to", instance.Target.getTCPAddr())
checkErrFatal(err)
defer l.Close()
for {
c, err := l.AcceptTCP()
checkErrFatal(err)
go instance.handleConnection(c)
}
}