HTTP server outside localhost using NGROK
Last Updated :
01 Sep, 2023
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 ServerPython'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 NgrokSecurity 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 fileAfter that let's extract the file, after extracting you will see the ngrok file as shown in the below screenshot.
Ngrok Executable FileNow 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 tokenCopy 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 accountNow, let's check if we have set up the ngrok service in the right way or not,
./ngrok http 8080
Create a http serverLet'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 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 StartedStep 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 tunnelNow go back to the ngrok terminal and copy the link forwarding.
Copy the link to access the fileNow you can share the link with other users and they can access the shared folder.
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
- 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.
- 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.
- 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.
Similar Reads
Calling Web Service Using Curl With Telnet Connection
Prerequisite: Telnet, Curl Terminal Network as the name suggests is an application layer protocol used to connect with hosts (remote machines) over a TCP/IP network. It is based on a client-server architecture where the telnet client makes a connection with the telnet server and once the connection
3 min read
How To Install Ngrok & Setup the Auth-Token on Linux?
Ngrok is a tool that allows you to create a secure tunnel from a public endpoint to a locally running web service on your machine. In simpler terms, ngrok lets you expose a web server running on your local machine to the internet. So others can access what you are running locally. For example, if yo
4 min read
Understanding HTTP using Browsers
For understanding what is HTTP and how does it work we need to first understand how the Web Server works and requests go to the server and how the server responds to that request after that we will be able to understand what is HTTP. HTTP : HTTP is a client-server protocol that means when a request
4 min read
Hosting a Web Application on Microsoft Azure in IAAS
Azure provides a lot of services to host a web application whether it be static or dynamic in a number of ways. In this article, we will host a simple static web application taking Infrastructure as a Service on Microsoft Azure, by using a free and open-source cross-platform web server software name
8 min read
Difference Between SSH Local and Remote Port Forwarding
SSH stands for "Secure Shell" or "Secure Socket Shell". It is a cryptographic network protocol that allows two computers to communicate and share the data over on insecure network such as the Internet. SSH protocols protect the network from various attacks. Local Port ForwardingLocal Port Forwarding
6 min read
What is Local Host?
When you call an IP address on your computer, you try to contact another computer on the internet, but when you call the IP address 127.0.0.1, you are communicating with the local host. Localhost is always your computer. Your computer is talking to itself when you call the local host. Your computer
8 min read
Port Forwarding on Router and Why do we need It
Router port forwarding is used to allow the remote computers to connect to a specific service or computer on a Local Area Network. Now the question is why do we need it. And we will discuss all of these things one by one. Use Cases : Suppose you are at the office and want to connect to your computer
4 min read
SSH Port Forwarding
SSH Port forwarding is a method used for securing TCP/IP connections. The TCP/IP packets can be tunneled through a SSH link making the data obscure thus protecting the link from attacks. SSH Port forwarding can be also seen as a form of Virtual Private Network(VPN). Types of Port Forwarding: There a
2 min read
What is a Loopback Address?
In the area of networking and computer systems, understanding the concept of a loopback address is necessary. Often known as the "localhost" or "loopback interface," this interaction has a significant impact on how well computers operate. We go into the fundamentals of loopback addresses, their func
7 min read
Cross Origin Resource Sharing (CORS)
CORS (Cross-Origin Resource Sharing) is a mechanism by which data or any other resource of a site could be shared intentionally to a third party website when there is a need. Generally, access to resources that are residing in a third party site is restricted by the browser clients for security purp
3 min read