Localtunnel is free and open source client and server which provides http tunneling to expose locally hosted web application (eg. home web server). It does not require you to have public or static IP and can work behind NAT or firewall. If you have ever deployed anything with localtunnel or any other http tunneling service then you would wonder how does it work. In this post we will attempt to demystify internals of localtunnel by understanding architecture.
Client Server Architecture
Localtunnel uses client server architecture where you install localtunnel client on home machine and communicate with localtunnel server to expose your web application. By default localtunnel client uses server hosted by localtunnel team on localtunnel.me
however server code is freely available and if required you can deploy your own server and point it to your domain.
How client register an application
If you have an web application running locally say on port 5000 then you can register application with following command and get subdomain for your application on the server.
lt -p 5000
Related : Localtunnel installation and command line reference.
How Server Keeps Track of an Application
When you register new application using your client it opens TCP connection with Localtunnel server. Server accepts your TCP request and register your application. It makes an entry for your application by assigning it one of the internal server port and provides you subdomain where application would be available over Internet.
10 TCP connections and Tunnel
When you register application you open a single tcp connection. After registering application your local computer then opens 10 different TCP Connections with localtunnel server. These 10 connections remain always active until you abort it from localtunnel client.
If any of the connection gets dropped due to any issue (network issue, server issue) then client again opens new tcp connection to compensate for dropped connection. Bottomline is at any time you would have 10 TCP connections open with localtunnel server. Since these connections are always open we call it TCP tunnel. HTTP runs on the top of TCP hence we also call it HTTP Tunnel.
Catering HTTP Requests
Localtunnel server runs it's own web server (ngnix) which uses internal mapping it has created while registering your application. When request arrives for your subdomain it lands on the web server of localtunnel then localtunnel sends it to your local computer using one of the TCP connection and relay back the response to serve request.
How does it work in Practice
In the next post we will take hands on approach to analyze localtunnel with simple command line utility called tcpdump
.