Brief analysis of localtunnel.me principle flow

Source: Internet
Author: User
Tags subdomain subdomain name

The company's recent project requires a backend server that exposes the intranet Web server to the Internet. So, on the Internet to search the open source projects everywhere. Find two good word-of-mouth projects (not verified, conform bar): Localtunnel.me and Ngrok. The former is developed by the JavaScript language, which is developed by the go language. Just writing a front end, learning JavaScript (full stack engineer!!) ), just take a look at Git clone. In this article, only the principles and procedures are explained, not code analysis. I always thought good code was a good document, and although it was just a prototype language of JavaScript, it didn't see the quality of the code, but Localtunnel's comments and the code itself were easy to understand.

When I analyze Localtunnel.me's code, it is the server side and the client to see the comparison. This makes it easier to understand how the server and the client interact.

Generally speaking, the principle of localtunnel.me is very simple, first of all, a server can be accessed in the public network for data transfer, localtunnel.me server-side program is deployed on this top. The server program listens to the client's TCP requests while listening to the browser's HTTP requests. The server forwards the HTTP request to the corresponding client. The client program also forwards the HTTP message to the local corresponding Web server port. In this way, the public network mapping of intranet Web server is realized.

We mainly look at the localtunnel.me workflow:

1. Listen to port 80, and of course it can be another port, waiting for HTTP requests from the client or browser.

2. When an HTTP request is received, the server will determine whether the domain name currently requested is the primary or a subdomain. There are two processing processes, the primary domain provides a new channel creation function, and the subdomain is used to forward the HTTP request

3. If the requested URL is a primary domain, and the URL is a request similar to this: Http://www.exampleserver.com/?new or http://www.exampleserver.com/xxxxxxxxxx. The service-side program creates a TCP server and returns the relevant information to the corresponding client. This information includes the new TCP port number that the server listens to, and the full subdomain name.
Here to illustrate, the domain name www.exampleserver.com must be a top-level domain (of course, it can not, just do two times development).
New indicates that a random subdomain is generated, or the subdomain name is xxxxxxxx.exampleserver.com.

4. After the client receives the information returned by the server, it creates a TCP connection with the server, hostname is the domain name of the server, or Ip,port is the port in the information returned by the server just now. In this way, a long connection is established between the client and the server. When the TCP link between the client and the server is established successfully, a TCP link to the local Web server is established immediately. Note that it is a TCP link, not an HTTP link.

5. When a browser on the earth accesses http://xxxxxxxx.exampleserver.com through a subdomain, the server-side program will find the client socket connection xxxxxxxx the subdomain name in a large table and forward the HTTP request to the past. Localtunnel.me uses the bouncy library to implement the forwarding function.


OK, here, most of the server-side work is done, and now HTTP requests to the client.

6. After the client receives the data for the HTTP request, it establishes a pipe between the remote server and the local Web server. However, before first forwarding, the Host field of the HTTP header needs to be modified to be the local server address



Brief analysis of localtunnel.me principle flow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.