Network Tunnel

Shivam Rajput
5 min readMay 19, 2023

Hi, I am Shivam Rajput. Here you will learn how you can access your application from anywhere in the world. Suppose you want to run your application and you don’t want to buy any cloud service or hosting service. Basically you want to save money just removing the cloud server.
If you are removing your cloud provider then what is the alternative? The alternative is you can use your computer/laptop/raspberrypi as a cloud provider. So here you will deploy your application on your local machine and access it by your domain.

Prerequisite

You need an application ready, I will not tell you how to create an application. If you don’t have any server, you can create a dummy server and also create a docker image. Because here I will guide you by using docker image. If you don’t have docker images that is also fine.

If you are thinking of deploying your real application on your machine for that you need an active internet connection 24X7 and power for your machine 24X7.

Deploy your application

First thing you need to deploy your application on your local machine. You can also use the nginx and you can use the rewrite rules. By using the nginx you needn’t to export multiple ports publicly. By nginx you can export only one port publicly.

Before doing anything new let’s understand what you are going to do. You are going to use the network tunnel to make your application publicly available. Now the question is: What is a network tunnel?

What is a network tunnel?

Network tunnels provide a direct connection between a remote server and the local network, and the transmission of data is undetectable by the public network.

The most common use cases for network tunneling are virtual private networks (VPNs), which set up efficient and secure connections between networks, providing a way to overcome limitations when using unsupported network protocols, and allowing users to bypass firewalls.

Network Tunnel

How do network tunnels work?

In order to move requests through the tunnel from the remote client to the local network, requests need to be encapsulated. Network tunnels work using encapsulated packets of private network data and protocol information within the public network transmission. Those packets are then de-capsulated and decrypted at the final destination.

Packets usually consist of two parts: the header, which indicates the packet’s destination and the protocol it uses, and the payload, which includes the packet’s contents. An encapsulated packet is a packet wrapped inside another packet as the payload.

Need a Tunnel?

The main benefit of using network tunnels is that it simplifies communication by avoiding configuration steps. Tunnels allow you to connect two elements within a complex network, avoiding firewalls, and other configuration steps. For example, in order to communicate between your local machine and a server within your company network without a tunnel, you need to create a connection that will go through your internet connection, then through a series of servers until it reaches the final destination. Using a tunnel, you directly connect point A (your machine) to point B (the local network), bypassing all intermediate steps.

Cloudflare

You can use the free service name as cloudflare. Cloudflare is a global network designed to make everything you connect to the Internet secure, private, fast, and reliable. Create an account and set up your domain here. You need to update your nameservers as well. These are the basic setup that you can do yourself.

Cloudflare Tunnel

Cloudflare Tunnel provides you with a secure way to connect your resources to Cloudflare without a publicly routable IP address. With Tunnel, you do not send traffic to an external IP — instead, a lightweight daemon in your infrastructure (cloudflared) creates outbound-only connections to Cloudflare’s global network. Cloudflare Tunnel can connect HTTP web servers, SSH servers, remote desktops, and other protocols safely to Cloudflare. This way, your origins can serve traffic through Cloudflare without being vulnerable to attacks that bypass Cloudflare.

​​How it works

Cloudflared establishes outbound connections (tunnels) between your resources and Cloudflare’s global network. Tunnels are persistent objects that route traffic to DNS records. Within the same tunnel, you can run as many cloudflared processes (connectors) as needed. These processes will establish connections to Cloudflare and send traffic to the nearest Cloudflare data center.

How an HTTP request reaches a resource connected with Cloudflare Tunnel

How to setup cloudflared tunnel

First you need to login into your cloudflare account. Go to the dashboard, click on Zero Trust on the left side menu bar.

Click on Access, then click on Tunnels.

Now you are ready to create a tunnel. Click on Create a tunnel. Give a good name to your tunnel. Click on save. Your tunnel is ready. You need to connect your computer to this tunnel. For that you need to install and run a connector on your machine. You can use it by CLI or docker. But you are going to set up your application by using docker, then connect the tunnel by using docker.

Click on Docker. Copy the command and run it in your machine. It will establish a connection on your machine to the tunnel.

Now you can set up your public hostname so that you can access your application by your host name. Click on the Public Hostname tab. Click on Add a public hostname.
If you want to use subdomain then you can use subdomain, select domain. Path is optional in the public hostname section.

In the service section select Type as Http, Enter the IP Address of your local machine where you are running your application with a port like 192.168.1.100:3000. It shouldn’t be localhost. Then click on Save hostname.

You can test your application by hitting your domain in the browser.

Congratulations you have successfully deployed your application in your local machine.

--

--