├── .gitignore ├── LICENSE ├── README.md └── teleport.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 code quest 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # teleport - like ngrok but reverse 2 | 3 | [ngrok](https://ngrok.io) is awesome. For those not in the know, ngrok addresses a use case where you want to expose a local server behind a NAT or firewall to the internet. Say you want to show your work to a colleague not on the local network - just run `ngrok http 3000` (assuming you're running your local server on port 3000) and you're given an HTTP address on the Wild Wild Web that you can share with your colleagues. 4 | 5 | Except things sometimes won't work this way - sometimes things in your project only really work when run on localhost. This is the problem [teleport](https://github.com/codequest-eu/teleport) wants to address. It creates a TCP tunnel to a remote endpoint and passes connections from a selected port on the localhost (3000 by default) to the remote endpoint. This is a perfect companion for ngrok. 6 | 7 | _Developer A_ wants to show her work: 8 | ```bash 9 | $ ngrok tcp 3000 # gets an address like 0.tcp.ngrok.io:42644 10 | ``` 11 | 12 | _Developer B_ wants to look at _A's_ work from the cosy confines of his localhost: 13 | ```bash 14 | $ teleport -r 0.tcp.ngrok.io:42644 15 | ``` 16 | 17 | Now for as long as this is running _Developer B_ can access localhost:3000 locally and connect via a magic gateway to _Developer A's_ server. Note that teleport works on TCP level so you will need to expose a TCP port with ngrok. 18 | 19 | Of course nothing requires you to use teleport _only_ with ngrok - teleport will work standalone just fine so you can connect to a server running on your VPS as long as you can access it from your local machine. 20 | 21 | # Installing 22 | If you have Go installed on your system you can use Go's package manager to install the newest version binary by running `go get -u github.com/codequest-eu/teleport`. Otherwise there are pre-built binaries for the plaftorm of your choice available [here](https://gobuilder.me/github.com/codequest-eu/teleport). 23 | -------------------------------------------------------------------------------- /teleport.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net" 6 | "os" 7 | 8 | "github.com/codegangsta/cli" 9 | "github.com/marcinwyszynski/tcproxy" 10 | "github.com/mgutz/ansi" 11 | ) 12 | 13 | func handleConn(local net.Conn, remoteAddr string) { 14 | remote, err := net.Dial("tcp", remoteAddr) 15 | if err != nil { 16 | fmt.Fprintf( 17 | os.Stderr, 18 | "Error connecting to remote host %s: %v\n", 19 | remoteAddr, 20 | err, 21 | ) 22 | os.Exit(3) 23 | } 24 | tcproxy.TCProxy(remote.(*net.TCPConn), local.(*net.TCPConn)) 25 | } 26 | 27 | func serve(ctx *cli.Context) { 28 | fmt.Fprintln( 29 | os.Stderr, 30 | "Made with", 31 | ansi.Color("<3", "red"), 32 | "by codequest.com", 33 | ) 34 | remoteAddr, localPort := ctx.String("remote"), ctx.Int("port") 35 | fmt.Fprintf( 36 | os.Stderr, 37 | "Creating a tunnel between %q and localhost:%d\n", 38 | remoteAddr, 39 | localPort, 40 | ) 41 | listener, err := net.Listen( 42 | "tcp", 43 | fmt.Sprintf("127.0.0.1:%d", localPort), 44 | ) 45 | if err != nil { 46 | fmt.Fprintf(os.Stderr, "Error starting local server: %v\n", err) 47 | os.Exit(1) 48 | } 49 | defer listener.Close() 50 | for { 51 | local, err := listener.Accept() 52 | if err != nil { 53 | fmt.Fprintf( 54 | os.Stderr, 55 | "Error accepting incoming connection: %v\n", 56 | err, 57 | ) 58 | os.Exit(2) 59 | } 60 | go handleConn(local, remoteAddr) 61 | } 62 | } 63 | 64 | func main() { 65 | app := cli.NewApp() 66 | app.Name = "teleport" 67 | app.Usage = "Like ngrok, but reverse - tunnel a remote endpoint to your localhost" 68 | app.Version = "0.0.1alpha" 69 | app.Flags = []cli.Flag{ 70 | cli.StringFlag{ 71 | Name: "remote, r", 72 | Usage: "remote endpoint - eg. 0.tcp.ngrok.io:42644", 73 | Value: "", 74 | }, 75 | cli.IntFlag{ 76 | Name: "port, p", 77 | Usage: "local port to expose", 78 | Value: 3000, 79 | }, 80 | } 81 | app.Action = serve 82 | app.Run(os.Args) 83 | } 84 | --------------------------------------------------------------------------------