-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
83 lines (66 loc) · 1.98 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
package main
import (
"io"
"log"
"net"
"os"
"time"
"github.com/integrii/flaggy"
)
// Listener is the listen address for incoming connections
var Listener string
// Destination is the target that connections will be forwarded to
var Destination string
func init() {
Listener = os.Getenv("LISTENER")
Destination = os.Getenv("DESTINATION")
flaggy.String(&Listener, "f", "from", "The address and port that wormhole should listen on. Connections enter here.")
flaggy.String(&Destination, "t", "to", "Specifies the address and port that wormhole should redirect TCP connections to. Connections exit here.")
flaggy.Parse()
}
func main() {
if len(Listener) == 0 {
Listener="127.0.0.1:7777"
}
if len(Destination) == 0 {
log.Fatalln("You must specify a destination with --to. Example: worhole --from 127.0.0.1:7777 --to 127.0.0.1:77")
}
log.Println("Starting up. Forwarding connections from", Listener, "to", Destination)
// Listen on the specified TCP port on all interfaces.
l, err := net.Listen("tcp", Listener)
if err != nil {
log.Fatal(err)
}
defer l.Close()
for {
// Wait for a connection.
c, err := l.Accept()
if err != nil {
log.Println("Error establshing incoming connection:", err)
continue
}
log.Println("Client connected from", c.RemoteAddr())
// handle the connection in a goroutine
go wormhole(c)
}
}
// wormhole opens a wormhole from the client connection
// to user the specified destination
func wormhole(c net.Conn) {
defer c.Close()
log.Println("Opening wormhole from", c.RemoteAddr())
start := time.Now()
// connect to the destination tcp port
destConn, err := net.Dial("tcp", Destination)
if err != nil {
log.Println("Error connecting to destination port:", err)
return
}
defer destConn.Close()
log.Println("Wormhole open from", c.RemoteAddr())
go func() { io.Copy(c, destConn) }()
io.Copy(destConn, c)
end := time.Now()
duration := end.Sub(start)
log.Println("Closing wormhole from", c.RemoteAddr(), "after", duration)
}