-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (121 loc) · 3.02 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
package main
import (
"fmt"
"io"
"log"
"net"
"os"
"os/signal"
"strconv"
"sync"
"syscall"
)
type TunnelServer struct {
localPort int
publicPort int
connections sync.WaitGroup
stopChan chan struct{}
}
func NewTunnelServer(localPort int) *TunnelServer {
return &TunnelServer{
localPort: localPort,
stopChan: make(chan struct{}),
}
}
func (ts *TunnelServer) Start() error {
// Getting your local machine's IP address
addrs, err := net.InterfaceAddrs()
if err != nil {
return fmt.Errorf("failed to get network interfaces: %v", err)
}
var hostIP string
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
hostIP = ipnet.IP.String()
break
}
}
}
//This is like setting up a public phone line on a random available extension
publicListener, err := net.Listen("tcp", ":0")
if err != nil {
return fmt.Errorf("failed to create public listener: %v", err)
}
_, portStr, err := net.SplitHostPort(publicListener.Addr().String())
if err != nil {
return fmt.Errorf("failed to parse listener address: %v", err)
}
ts.publicPort, _ = strconv.Atoi(portStr)
log.Printf(" Tunnel created successfully!")
log.Printf(" Local Server: localhost:%d", ts.localPort)
log.Printf(" Public Endpoint: %s:%d", hostIP, ts.publicPort)
go ts.acceptConnections(publicListener)
return nil
}
func (ts *TunnelServer) acceptConnections(publicListener net.Listener) {
for {
select {
case <-ts.stopChan:
publicListener.Close()
return
default:
// Accept incoming public connections
publicConn, err := publicListener.Accept()
if err != nil {
log.Printf("Error accepting connection: %v", err)
continue
}
ts.connections.Add(1)
go ts.handleConnection(publicConn)
}
}
}
func (ts *TunnelServer) handleConnection(publicConn net.Conn) {
defer ts.connections.Done()
defer publicConn.Close()
// Establish connection to local server
localConn, err := net.Dial("tcp", fmt.Sprintf("localhost:%d", ts.localPort))
if err != nil {
log.Printf("Failed to connect to local server: %v", err)
return
}
defer localConn.Close()
// Bidirectional data transfer
errChan := make(chan error, 2)
go func() {
_, err := io.Copy(localConn, publicConn)
errChan <- err
}()
go func() {
_, err := io.Copy(publicConn, localConn)
errChan <- err
}()
// Wait for either copy operation to complete or fail
<-errChan
}
func (ts *TunnelServer) Stop() {
close(ts.stopChan)
ts.connections.Wait()
log.Println("🛑 Tunnel stopped")
}
func main() {
// Get local port from command line or use default
localPort := 3000
if len(os.Args) > 1 {
port, err := strconv.Atoi(os.Args[1])
if err == nil {
localPort = port
}
}
// Create and start tunnel
tunnel := NewTunnelServer(localPort)
if err := tunnel.Start(); err != nil {
log.Fatalf("Failed to start tunnel: %v", err)
}
// Keep the program running until interrupted
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
<-sigChan
tunnel.Stop()
}