Tunnel your localhost via cloudflare

/ Work

Have you heard of Ngrok? It's a great tunneling service that allows you to test your localhost by exposing it to the internet. This is particularly useful when you need to test something like webhooks, where a service needs to send data to your server, but you are working on localhost.

There are many tunneling services available, some of which are free and others are not.

One of the challenges with using tunneling services is the need for a custom domain. Many tunneling services do not allow you to set a custom domain, which can be frustrating as it means that every time you restart your server, the tunnel loses the connection and you have to restart the tunnel, which then provides you with a new URL.

Dealing with constantly changing tunnel URLs can be a hassle. Services like Ngrok or Pagekite do provide the ability to set a custom domain in their paid versions.

One service that stands out in the market is Cloudflare Argo Tunnel, which is provided by a popular CDN provider. It's likely that you may already be using this platform in some parts of your infrastructure.

Cloudflare Tunnels is a new tool that can be used instead of well-known tools like Ngrok and localtunnel to make locally hosted websites and apps available on the internet.

One of the key advantages of Cloudflare Tunnels is that it offers free tunnels with permanent domains. This is particularly useful for testing and debugging webhooks with local apps, as it eliminates the need to change the URL every time the server is restarted.

 

Install cloudflared

To get started, start by installing the latest version of the cloudflared package:

wget -q https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb

dpkg -i cloudflared-linux-amd64.deb

Authentication

Next, you will need to authenticate with Cloudflare and select the domain you'd like to utilize for this tunnel running the following command:

cloudflared tunnel login

notion image

Creating a Tunnel

It is now time to create your tunnel using the command:

cloudflared tunnel create name_of_tunnel

The name of your tunnel is entirely up to you, but it cannot be changed later, so make sure to choose a name that is memorable. After creating your tunnel, you will receive a bit of information, including the unique UUID of your tunnel. This UUID will be required for any additional configurations you may want to make to your tunnel. Additionally, you can view all active tunnels by using the command cloudflared tunnel list , which will give you the name and UUID of each active tunnel.

Creating DNS Records

If you're familiar with creating DNS records through the Cloudflare dashboard, this guide will take you through the process of using the command line interface (CLI) for setting up tunnels. Some people, including myself, prefer the CLI for this task as it provides more flexibility and ease of use.

To create a DNS record for your hostname/service listed in the previous step, use the command:

cloudflared tunnel route dns <UUID or NAME of tunnel> <hostname>

For example, if you wanted to create a DNS record for yourdomain.com, you would run:

cloudflared tunnel route dns <UUID or NAME of tunnel> yourdomain.com

It is worth noting that this change will be reflected in the DNS section of the Cloudflare dashboard.

Starting the Tunnel

Now that your configuration is completed, it is time to test out the tunnel using the following command:

cloudflared tunnel run <UUID or NAME of tunnel> --url localhost:3000

By using this method, a secure connection will be established between your local machine and the Cloudflare network, enabling you to test your local development environment on your own custom domain.

Want to check out your local development setup on your own domain? It's easy! Just open your web browser, type in the url https:/{tunnel-name}.yourdomain.com and voila! Your local development environment will be right there in front of you, as if it's live on a server.

Cloudflare Argo Tunnel is a powerful solution that simplifies the process of testing your local development environment on a live domain. With its easy-to-use features, you can quickly set it up and start testing and debugging your code with ease, giving you the confidence that your code will work seamlessly when deployed.

Mahendra Rathod
Developer from 🇮🇳
@maddygoround
© 2024 Mahendra Rathod · Source