-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
158 lines (132 loc) · 3.8 KB
/
main.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
/// Write by ChatGPT
import (
"fmt"
"io"
"net"
"os"
"strings"
"sync"
)
var (
shutdownChan chan struct{}
wg sync.WaitGroup
)
func handleTCPConnection(localConn net.Conn, remoteConn net.Conn) {
defer wg.Done()
wg.Add(1)
// Use a wait group to keep track of the copy operations
copyWg := sync.WaitGroup{}
copyWg.Add(2)
// Copy local-to-remote
go func() {
_, err := io.Copy(remoteConn, localConn)
if err != nil {
fmt.Println("Error copying data to remote TCP:", err)
}
copyWg.Done()
}()
// Copy remote-to-local
go func() {
_, err := io.Copy(localConn, remoteConn)
if err != nil {
fmt.Println("Error copying data to local TCP:", err)
}
copyWg.Done()
}()
// Wait for both copy operations to complete before closing the connections
copyWg.Wait()
// Close the connections after both copy operations are done
localConn.Close()
remoteConn.Close()
wg.Done()
}
func handleUDPConnection(udpConn *net.UDPConn, remoteAddr *net.UDPAddr) {
defer wg.Done()
buf := make([]byte, 1024)
for {
n, _, err := udpConn.ReadFromUDP(buf)
if err != nil {
// Check if it's a graceful shutdown
select {
case <-shutdownChan:
fmt.Println("Graceful shutdown completed.")
return
default:
fmt.Println("Error reading UDP packet:", err)
continue
}
}
_, err = udpConn.WriteToUDP(buf[:n], remoteAddr)
if err != nil {
fmt.Println("Error writing to remote UDP:", err)
}
}
}
func main() {
mappings := os.Getenv("PORT_MAPPINGS") // Set this environment variable to port mappings (e.g., "8080:example.com:6060,9090:example.com:7070")
if mappings == "" {
fmt.Println("Missing environment variable PORT_MAPPINGS. Please set it to a comma-separated list of localPort:remoteAddress:remotePort mappings.")
return
}
mappingPairs := strings.Split(mappings, ",")
for _, mapping := range mappingPairs {
parts := strings.Split(mapping, ":")
if len(parts) != 3 {
fmt.Println("Invalid mapping format:", mapping)
continue
}
localPort := parts[0]
remoteAddress := parts[1]
remotePort := parts[2]
localTCPAddr := "0.0.0.0:" + localPort
remoteTCPAddr := remoteAddress + ":" + remotePort
// Create TCP listener
tcpListener, err := net.Listen("tcp", localTCPAddr)
if err != nil {
fmt.Println("Error listening on TCP address:", localTCPAddr, err)
continue
}
// Resolve remote TCP address
remoteTCPConn, err := net.Dial("tcp", remoteTCPAddr)
if err != nil {
fmt.Println("Error connecting to remote TCP address:", remoteTCPAddr, err)
tcpListener.Close()
continue
}
fmt.Printf("Port forwarding started on %s to remote %s\n", localTCPAddr, remoteTCPAddr)
wg.Add(1)
go func(localTCPAddr string, remoteTCPAddr string, tcpListener net.Listener, remoteTCPConn net.Conn) {
for {
// Handle TCP connections
tcpConn, err := tcpListener.Accept()
if err != nil {
// Check if it's a graceful shutdown
select {
case <-shutdownChan:
fmt.Println("Graceful shutdown completed for address:", localTCPAddr)
tcpListener.Close()
remoteTCPConn.Close()
wg.Done()
return
default:
fmt.Println("Error accepting TCP connection for address:", localTCPAddr, err)
continue
}
}
remoteTCPConn, err := net.Dial("tcp", remoteTCPAddr)
if err != nil {
fmt.Println("Error connecting to remote TCP address:", remoteTCPAddr, err)
tcpConn.Close()
continue
}
wg.Add(1)
go handleTCPConnection(tcpConn, remoteTCPConn)
}
}(localTCPAddr, remoteTCPAddr, tcpListener, remoteTCPConn)
}
// Wait for incoming signals (graceful shutdown)
<-shutdownChan
fmt.Println("\nGraceful shutdown initiated...")
wg.Wait()
}