Open In App

HTTP server outside localhost using NGROK

Last Updated : 01 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

An HTTP (Hypertext Transfer Protocol) server is a program that handles incoming HTTP requests from clients and sends responses back to them. HTTP is the protocol used for communication on the World Wide Web, and HTTP servers are essential components of the web infrastructure.

When a user types a URL (Uniform Resource Locator) into their web browser, the browser sends an HTTP request to the server specified in the URL. The HTTP server processes the request and sends a response back to the client, typically in the form of an HTML document or other web content.

Working of HTTP Server

Working of HTTP Server

Python’s http.server

Python comes with a built-in module called http.server that allows you to create a simple HTTP server. This module provides classes for implementing HTTP servers (Web servers) that can be used to serve static files or dynamic content generated by Python scripts.

What is ngrok?

Ngrok is a tool that allows you to expose a local web server to the internet. It creates a secure tunnel from a public endpoint (generated by Ngrok) to a locally running web service, allowing you to access the local server from the internet.

Ngrok works by creating a secure tunnel between your local server and a public endpoint provided by Ngrok. You start by downloading and installing the Ngrok client on your local machine and then running a command that creates a tunnel to your local server’s port.

How Ngrok Works?

Ngrok is able to bypass NAT Mapping and firewall restrictions by creating a long-lived TCP tunnel from a randomly generated subdomain on ngrok.com (e.g. 3gf892ks.ngrok.com) to the local machine.

We can start a TCP connection with just one command of ngrok and it makes us a long tunnel bypassing any firewall/NAT.

Working of Ngrok

Working of Ngrok

Security in Ngrok Service

Ngrok Secure Tunnels allow you to instantly open access to remote systems without touching any of your network settings or opening any ports on your router. This means we get a secure, reliable tunnel for your developer box, IoT device, or just about anything that has access to the internet. When a user hits the public ngrok endpoint, the ngrok edge figures out where to route the request and forwards it over an encrypted connection to the locally running ngrok agent. From there, the local ngrok agent takes care of sending traffic to your upstream service. The communication between the ngrok edge and the agent is secure and encrypted. Traffic from the user to the ngrok edge, and from the ngrok agent to the upstream service rely on the protocol you are using for encryption. For protocols that support end-to-end encryption using TLS, we provide a TLS tunnel option.

Starting the Ngrok service outside LAN

Follow the steps below to start the http server outside the LAN.

Step 1: Download Ngrok by using the link below.

Visit the official download page of the ngrok service and download ngrok for Kali Linux {https://ngrok.com/download}

https://ngrok.com/download

It will start downloading the zip file.

Ngrok downloaded file

Ngrok downloaded file

After that let’s extract the file, after extracting you will see the ngrok file as shown in the below screenshot.

Ngrok Executable File

Ngrok Executable File

Now we have to give an auth key to run the ngrok service, it will be on the downloaded dashboard. Login to the ngrok website or if you are newly create an account.

Ngrok auth token

Ngrok auth token

Copy this command and run it in your terminal with ./ preeceeding it:

Example:

./ngrok config add-authtoken <your authtoken>
Adding auth-token to connect ngrok with the account

Adding auth-token to connect ngrok with the account

Now, let’s check if we have set up the ngrok service in the right way or not,

./ngrok http 8080
Create a http server

Create a http server

Let’s start a TCP connection in your computer at port 8080.

If you succeed you will get this message with a URL of port forwarding.

Successful TCP Connection

Successful TCP Connection

 So, let’s start our http server, for this, we will need python3, so download the Python3 language using the below command:

sudo apt install python3

Step 2: Setting up Ngrok for the http server

Let’s assume there is a file that we have to share with another system that is not in the same network. First of all, we will create the ngrok service for http at port 8080(port 8080 is for the web interface). 

Open up the terminal where ngrok is downloaded and type:

./ngrok http 8080

This command will open up an http port at 8080 in your system allowing all http connections through it. Learn more about http connection here. As soon as you see the session status online, know that a port at 8080 has started an http server outside your network and will allow connection outside your network. The forwarding link provided will be its access key whoever clicks on that link will enter your system through an http port.

Ngrok Tunnel Started

Ngrok Tunnel Started

Step 3: Setting up http.sever with python3

Launch the terminal in the directory we want to share and execute the below command in the terminal.

python3 -m http.server 8080
The server is started in the ngrok tunnel

The server is started in the ngrok tunnel

Now go back to the ngrok terminal and copy the link forwarding.

Copy the link to access the file

Copy the link to access the file

Now you can share the link with other users and they can access the shared folder.

Shared directory

Shared directory 

Here you can see the URL given by ngrok redirects us to the shared folder through the Python http server. It is well-secured and is closed instantly after use without leaving any traces behind.

Alternatives for Ngrok

  1. LocalTunnel: To host your local web applications over the cloud and access the app from a publicly accessible web URL, you can use the free tunneling technology known as Localtunnel. It is considered the best alternative to Ngrok.
  2. Serveo: Another user-friendly option for Ngrok is Serveo. You can build local tunnels using it without having to install any additional third-party software. With Serveo, the SSH server provides a port forwarding feature for the locally hosted application.
  3. Teleconsole: Teleconsole is another well-known alternative to Ngrok. It allows free service to help users share their terminal sessions with others. By generating a unique session id, Teleconsole enables you to share your terminal session over the Internet in contrast to HTTP / HTTPS. It must be used carefully and should only be disclosed to someone you can completely trust because doing so is equivalent to giving them full access to your terminal.

Conclusion

Never leave the Ngrok running continuously because doing so puts your privacy in danger. When the shared process is finished, press Ctrl+C to end the connection with both ngrok and the Python HTTP server.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads