-
Notifications
You must be signed in to change notification settings - Fork 3
/
teleport.go
83 lines (78 loc) · 1.57 KB
/
teleport.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 (
"fmt"
"net"
"os"
"github.com/codegangsta/cli"
"github.com/marcinwyszynski/tcproxy"
"github.com/mgutz/ansi"
)
func handleConn(local net.Conn, remoteAddr string) {
remote, err := net.Dial("tcp", remoteAddr)
if err != nil {
fmt.Fprintf(
os.Stderr,
"Error connecting to remote host %s: %v\n",
remoteAddr,
err,
)
os.Exit(3)
}
tcproxy.TCProxy(remote.(*net.TCPConn), local.(*net.TCPConn))
}
func serve(ctx *cli.Context) {
fmt.Fprintln(
os.Stderr,
"Made with",
ansi.Color("<3", "red"),
"by codequest.com",
)
remoteAddr, localPort := ctx.String("remote"), ctx.Int("port")
fmt.Fprintf(
os.Stderr,
"Creating a tunnel between %q and localhost:%d\n",
remoteAddr,
localPort,
)
listener, err := net.Listen(
"tcp",
fmt.Sprintf("127.0.0.1:%d", localPort),
)
if err != nil {
fmt.Fprintf(os.Stderr, "Error starting local server: %v\n", err)
os.Exit(1)
}
defer listener.Close()
for {
local, err := listener.Accept()
if err != nil {
fmt.Fprintf(
os.Stderr,
"Error accepting incoming connection: %v\n",
err,
)
os.Exit(2)
}
go handleConn(local, remoteAddr)
}
}
func main() {
app := cli.NewApp()
app.Name = "teleport"
app.Usage = "Like ngrok, but reverse - tunnel a remote endpoint to your localhost"
app.Version = "0.0.1alpha"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "remote, r",
Usage: "remote endpoint - eg. 0.tcp.ngrok.io:42644",
Value: "",
},
cli.IntFlag{
Name: "port, p",
Usage: "local port to expose",
Value: 3000,
},
}
app.Action = serve
app.Run(os.Args)
}