Cloudflared
Simplifying Secure and Fast Access to Your Applications
Cloudflared is an indispensable tool for developers who need to securely and efficiently expose local applications. In this article, we’ll delve into what cloudflared is, why it was created, how to use it across different operating systems, and, most importantly, explore real-world examples of how it can facilitate daily development tasks.
What Is Cloudflared and Why Was It Created?
Cloudflared is a client for Cloudflare Tunnel, a service that establishes secure tunnels between a local application and Cloudflare’s global network. It allows you to expose an application running on your local machine to the internet without configuring routers, dynamic DNS, or opening firewall ports.
The motivation behind cloudflared arose from the need to simplify the process of making local services accessible externally. In an environment where security is a constant concern and networks are increasingly complex, having a tool that streamlines this process without compromising security is crucial.
Advantages of Using Cloudflared
Using cloudflared offers several advantages. It enables the creation of encrypted tunnels that protect data traffic between your machine and Cloudflare. The configuration is straightforward and doesn’t require complex network changes or firewall adjustments. Additionally, it simplifies remote access to applications in development, facilitating collaboration and communication with clients or team members. It works on Windows, Linux, and macOS, making it a versatile tool for different development environments.
How to Install Cloudflared
Windows
To install cloudflared on Windows, visit the official Cloudflare website and download the installer for Windows. After downloading, run the installer and follow the on-screen instructions. To verify a successful installation, open the Command Prompt and type:
cloudflared --version
Linux
On Linux, you need to add Cloudflare’s repository and install the package. Use the following commands:
sudo curl -L https://pkg.cloudflare.com/cloudflare-warp-ascii.gpg | sudo apt-key add -
sudo echo 'deb http://pkg.cloudflare.com/ buster main' | sudo tee /etc/apt/sources.list.d/cloudflare-main.list
sudo apt update
sudo apt install cloudflared
macOS
To install on macOS, use Homebrew with the command:
brew install cloudflared
Verify the installation with:
cloudflared --version
How to Use Cloudflared
Creating a Simple Tunnel
If you have an application running locally on port 8080 and want to expose it, execute:
cloudflared tunnel --url http://localhost:8080
This command will generate a public URL that redirects to your local application, allowing others to access it.
Setting Up a Named Tunnel
For persistent tunnels with specific configurations, start by logging into Cloudflare:
cloudflared tunnel login
Next, create a named tunnel:
cloudflared tunnel create my-tunnel
Edit the generated config.yml
configuration file to define your application's specifications. For example:
tunnel: my-tunnel
credentials-file: /home/user/.cloudflared/my-tunnel.json
ingress:
- hostname: app.mydomain.com
service: http://localhost:8080
- service: http_status:404
Start the tunnel with:
cloudflared tunnel run my-tunnel
Real-World Use Cases
Remote Collaborative Development
Suppose you’re developing a web application and need another developer to test a specific feature on your local machine. With cloudflared, you can temporarily expose your application so they can access it from anywhere. Simply start the tunnel and share the generated URL.
For example, by executing:
cloudflared tunnel --url http://localhost:3000
A public URL will be created. The remote developer can access this URL and interact with the application as if it were running on a public server.
Testing Webhooks
Services like Stripe, PayPal, or GitHub use webhooks to send notifications to your applications. Testing webhooks locally can be challenging because the external service needs to reach your development environment. With cloudflared, you can expose your local endpoint to receive these notifications.
Execute:
cloudflared tunnel --url http://localhost:5000
Configure the external service to send webhooks to the provided URL, allowing you to test the integration in real time.
Presentations and Demos for Clients
If you need to showcase a new feature to a client who isn’t physically present, cloudflared simplifies the process. By exposing your local application, the client can access it directly through their browser without the need for deployment or additional configurations.
Start the tunnel:
cloudflared tunnel --url http://localhost:8080
Share the URL with the client so they can navigate the application and provide immediate feedback.
Remote Access to Internal Tools
Distributed teams may need access to internal tools like dashboards or management systems that aren’t publicly available. With cloudflared, you can make these tools accessible securely without fully exposing them on the internet.
For example, to expose an administrative dashboard running on port 8000:
cloudflared tunnel --url http://localhost:8000
Combine this with Cloudflare’s authentication features to restrict access to authorized users only.
Overcoming Network Restrictions
In environments where internet access is restricted by firewalls or security policies, cloudflared can be used to establish secure outbound connections. If you need to access external resources that are blocked, creating a tunnel might be the solution.
For example, to forward requests to a specific service:
cloudflared access ssh --hostname remote.server.com
Load Balancing and High Availability
In critical applications requiring high availability, cloudflared can be configured to balance the load between multiple service instances. By setting up multiple tunnels pointing to different servers, Cloudflare can manage the traffic efficiently.
Configure config.yml
to include multiple services:
tunnel: my-tunnel
credentials-file: /home/user/.cloudflared/my-tunnel.json
ingress:
- hostname: app.mydomain.com
service: http://localhost:8080
- hostname: app.mydomain.com
service: http://localhost:8081
- service: http_status:404
Start the tunnel, and Cloudflare will distribute the traffic among the defined instances.
Implementing Testing Environments
For teams working with agile methodologies that require quick testing environments, cloudflared allows the creation of temporary environments accessible by the QA team or stakeholders. This accelerates the validation and feedback process.
For instance, after finalizing a feature, the developer can expose the testing environment:
cloudflared tunnel --url http://localhost:8080
The QA team accesses the provided URL and begins testing immediately.
Disadvantages and Considerations
Despite its advantages, it’s important to be aware of some limitations. Cloudflared relies on Cloudflare’s infrastructure, meaning that issues in Cloudflare’s network can affect access to your application. For applications with high traffic volume or requiring minimal latency, this solution might not be the most suitable.
Additionally, even with the security offered by encrypted tunnels, it’s essential to properly manage access permissions. Exposing a local application to the internet always involves risks, so use authentication mechanisms and restrict access when necessary.
Conclusion
Cloudflared is a powerful tool that simplifies the process of securely exposing local applications. It enables developers to collaborate more efficiently, test complex integrations, and present solutions to clients without additional complications. By exploring the various use cases presented, it’s clear that cloudflared can be a significant ally in daily development tasks.