How localtunnel works under the hood?
Asked Answered
E

1

5

I've been using localtunnel for quite a while and now I wonder how it works under the hood. Here is one article I read which explains the same.

Here are few terminologies which I don't understand from the above article:

the localtunnel server fires up a new TCP server on a randomly generated port greater than 1023

How is it possible to fire another server within a server?

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.

What is the purpose of opening TCP sockets with the server when client can connect to the server by hitting some specific URL whenever required?

I've also tried to read the code from it's Github repository, but It's too complicated to understand the basic concept, as I'm beginner to Nodejs and it's frameworks.

Any basic explanation would help!

Empson answered 6/11, 2018 at 10:33 Comment(2)
are you looking for an alternative answer or a better clarification? @EmpsonHenhouse
I'm looking for easy to understand clarification. I read your answer, but as I'm beginner in networking, didn't understand it clearly.Empson
H
8

Here is a diagram of all he services involved in localtunnel, grouped by host.

       localhost:             [localtunnel client]  --- [HTTP client] --- [your server]
                               |                |  
                               |                | 
localtunnel host:      [express server] --- [TCP server]
                                                    |
                                                    |
   internet user:                                 [app]

Basically when you type lt --port 8000 in your console, it launches the localtunnel client. This connects to the express server located in the cloud. This express server gives you back the address where your app should connect. Then it launches the tcp server that listens for connections from your app. The tcp server also opens 10 sockets with the localtunnel client, for sending data. When a connection comes from the app, the tcp server sends the data to the localtunnel client on one of the 10 sockets. The data is then piped to a local http client that issues the request to your server.

To answer your questions:

How is it possible to fire another server within a server?

A node.js server can launch other processes by using child_process.

What is the purpose of opening TCP sockets with the server when client can connect to the server by hitting some specific URL whenever required?

Here you are confusing the localtunnel client with the internet client (the app in my diagram). The TCP server needs to hold 2 connections: one with the outside world (the app), and one with the localtunnel client. The 10 sockets referred in the article are the connection to the localtunnel client, and are used for sending data.

Henhouse answered 6/11, 2018 at 22:3 Comment(1)
I don't understand this: The data is then piped to a local http client that issues the request to your server. How exactly is this possible?Empson

© 2022 - 2024 — McMap. All rights reserved.