localtunnel

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 30, 2017 License: MIT Imports: 12 Imported by: 0

README

localtunnel

Expose yourself behind NAT or Firewall to the World!

Compatibility

Both server and client are compatible with Node.JS version of localtunnel.

Features

  • Show your work to anyone
  • Use the API to test webhooks
  • Test your UI in cloud browsers

How it works

         +--------------------+
         | Localtunnel Server |
         |--------------------|         +----------+
         | Backend | Frontend |<--------+TCP Client|
         +---------+----------+         +----------+
            ^  ^^
            |  ||
            |  ||
     Control|  ||Proxy
  Connection|  ||Connections
            |  ||
         +--+--++-------------+         +----------+
         | Localtunnel Client +-------->|TCP Server|
         +--------------------+         +----------+

In a nutshell, localtunnel consists of two components. A server and a client. The server uses express to listen for incoming requests. These requests can either be from a connecting client that wishes to expose its local port to the public net or a client wishing to connect to an already established service at a subdomain.

When a request comes in from the localtunnel client component, it makes a request to https://thelocaltunnelserver/?new and localtunnel server fires up a new TCP server on a randomly generated port greater than 1023 (non-privileged).

The server then returns this randomly generated port to the localtunnel client and gives the client 5 seconds to connect. If the localtunnel client does not establish a connection to the TCP port within 5 seconds, the server is closed and the localtunnel client will have to reconnect to try again.

If the localtunnel client is able to connect to the localtunnel server’s randomly generated TCP port, by default it opens 10 TCP sockets to the server. These connections are held open, even if no data is being transferred. The localtunnel client then waits for requests to come in over any of these 10 TCP sockets. When a request comes in, it is piped to a TCP client that connects to localhost for the desired service.

In order to expose the localtunnel client’s local service to the web, the localtunnel server waits for requests to come in on the subdomain chosen by the localtunnel client. If it matches the subdomain of a currently connected client, the localtunnel server proxies the request to one (or more) of the 10 TCP sockets being held open by the localtunnel client.

Building project

Get the package.

go get -u github.com/bleenco/localtunnel

cd into directory.

cd $GOPATH/src/github.com/bleenco/localtunnel

Install tools.

make get_tools

Install dependencies.

make install

Build the project.

make build

Docker Image

Change ENV DOMAIN and ENV SECURE variables in Dockerfile to fit your needs, then build Docker image.

make docker_image

Run container from localtunnel image.

docker run -dit --restart always --net host --name localtunnel localtunnel

Usage Example

Start the server hosted on domain example.com.

./lt-server -d example.com

Create the tunnel to localhost:8000.

./lt-client -h http://example.com -p 8000

You should get generated URL, something like http://7e400f6d.example.com.

Open your browser on http://7e400f6d.example.com to check if its working then share the URL with your friends.

Use Locally (development)

For usage on local machine you first need to setup wildcard DNS for localhost development.

Installation guide for MacOS.

brew install dnsmasq

At the bottom of /usr/local/etc/dnsmasq.conf add

address=/localnet/127.0.0.1

Start dnsmasq.

sudo brew services start dnsmasq

For MacOS to resolve requests from *.localnet to localhost we need to add a resolver.

sudo mkdir /etc/resolver
sudo touch /etc/resolver/localnet

Add following line to /etc/resolver/localnet

nameserver 127.0.0.1

Reboot the machine to enable the resolver.

Start the your server using -d localnet flag

go run cmd/server/lt-server.go -d localnet -p 80

Start some web service, for example on port 6510, then connect with the lt-client.

go run cmd/client/lt-client -h http://localhost -p 6510

and you should get something like

your url is: http://33412dea.localnet

Open the URL in the browser and it should work.

Licence

MIT

Documentation

Overview

Exposing a local port:

import "github.com/jweslley/localtunnel"

...

var port := 8000
var tunnel := localtunnel.NewLocalTunnel(port)
var err := tunnel.Open()
if (err != nil) {
	fmt.Printf("your url is: %s\n", tunnel.URL())
}

...

tunnel.Close()

Index

Constants

This section is empty.

Variables

View Source
var DefaultClient = NewClient("https://bleenco.space")

DefaultClient is the default Client and is used by NewLocalTunnel and NewTunnel.

Functions

func SetupServer

func SetupServer(port, serverDomain string, isSecure bool) *http.Server

SetupServer creates main HTTP server

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

A Client is an localtunnel client.

func NewClient

func NewClient(url string) *Client

NewClient returns a client using the given end point.

func (*Client) NewLocalTunnel

func (c *Client) NewLocalTunnel(port int) *Tunnel

NewLocalTunnel create a tunnel for a server in a given port from localhost.

func (*Client) NewTunnel

func (c *Client) NewTunnel(host string, port int) *Tunnel

NewTunnel create a tunnel for a server in a given host and port.

type Proxy

type Proxy struct {
	// contains filtered or unexported fields
}

Proxy holds data of the Proxy TCP Server

func NewProxy

func NewProxy(id string) *Proxy

NewProxy creates new Proxy instance

type Socket

type Socket struct {
	// contains filtered or unexported fields
}

Socket is a TCP tunnel established between the client and server

type Tunnel

type Tunnel struct {
	// contains filtered or unexported fields
}

Tunnel forwards remote requests to another server, typically to a port on localhost.

func NewLocalTunnel

func NewLocalTunnel(port int) *Tunnel

NewLocalTunnel create a tunnel for a server in a given port from localhost using the DefaultClient.

func NewTunnel

func NewTunnel(host string, port int) *Tunnel

NewTunnel create a tunnel for a server in a given host and port using the DefaultClient.

func (*Tunnel) Close

func (t *Tunnel) Close()

Close closes all tunnel's connections.

func (*Tunnel) Closing

func (t *Tunnel) Closing() <-chan struct{}

Closing is a channel which is closed when the tunnel is closed.

func (*Tunnel) LocalHost

func (t *Tunnel) LocalHost() string

func (*Tunnel) LocalPort

func (t *Tunnel) LocalPort() int

func (*Tunnel) MaxConn

func (t *Tunnel) MaxConn() int

MaxConn is the maximum number of connections allowed.

func (*Tunnel) Open

func (t *Tunnel) Open() error

Open setup the tunnel creating connections between the remote and local servers.

func (*Tunnel) OpenAs

func (t *Tunnel) OpenAs(subdomain string) error

OpenAs setup the tunnel creating connections between the remote and local servers with a custom subdomain.

func (*Tunnel) RemoteHost

func (t *Tunnel) RemoteHost() string

func (*Tunnel) RemotePort

func (t *Tunnel) RemotePort() int

func (*Tunnel) Subdomain

func (t *Tunnel) Subdomain() string

func (*Tunnel) URL

func (t *Tunnel) URL() string

URL at which the localtunnel is exposed.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL