forked from gaetanlhf/portfwd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp.go
45 lines (35 loc) · 716 Bytes
/
tcp.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
package main
import (
"io"
"log"
"net"
"github.com/libp2p/go-reuseport"
)
func tcpForward(protocol string, from string, to string) {
listener, err := reuseport.Listen(protocol, to)
if err != nil {
log.Printf("The connection failed: %v", err)
}
for {
conn, err := listener.Accept()
if err != nil {
log.Printf("The connection was not accepted: %v", err)
}
client, err := net.Dial(protocol, from)
if err != nil {
log.Printf("The connection failed: %v", err)
defer conn.Close()
continue
}
go func() {
defer client.Close()
defer conn.Close()
io.Copy(client, conn)
}()
go func() {
defer client.Close()
defer conn.Close()
io.Copy(conn, client)
}()
}
}