How to use Cloudflare Tunnels with Salad

How To Setup A Cloudflare tunnel And Expose Your Local Service Or Application.

What is Cloudflare?
It is a cloud hosting platform or service that helps provide network and DDoS (Distributed Denial of Service) mitigation for any content to be deployed on it.

What is a tunnel?
A tunnel is a secure connection between your internet and your local host.
Example : https://localhost:5000

In this section, We will go through how to set up your Cloudflare tunnels for:

  • Windows
  • MacOS
  • Linux

You can read this documentation and set up your first tunnel here using:

  • Remotely on the Zero Trust Dashboard ( For beginners that have little knowledge of the command line)
  • Using your command prompt (CLI Tool)

To create a Cloudflare Tunnel, you will need to use the Cloudflare Tunnel command-line interface (CLI). You can install the Cloudflare Tunnel CLI by following the instructions at the following link: https://developers.cloudflare.com/tunnel/quickstart/install/.

Once you have installed the Cloudflare Tunnel CLI, you can create a Tunnel by running the following command :

Cloudflare tunnel create

Setting up an Express server with Cloudflare Tunnel

This guide will walk you through the process of setting up an Express server that listens on port 5000, and using Cloudflare Tunnel to establish a connection to the server.

Prerequisites

  • Node.js and npm installed on your machine
  • A Cloudflare account with the cloudflared binary installed
  • A Cloudflare API token

Installing the required dependencies

First, we need to install the express and child_process modules. These can be installed by running the following command in your terminal:

npm install express child_process

Setting up the Express server

Next, we will set up an instance of an Express app and have it listen on port 5000.

Add the following code to your script:

const express = require('express');
const app = express();

// This creates an instance of an Express app and sets it to listen on port 5000.
app.listen(5000, () => {
  console.log(`Server is running on port 5000`);
});

Establishing the Cloudflare Tunnel

To establish a Cloudflare Tunnel, we will use the cloudflared command, which will authenticate with the Cloudflare API using a token. Add the following code to your script, replacing cftoken ` with your actual Cloudflare API token:

//Imports the 'spawn' method from the 'child_process' module and the 'express' module.
const { spawn } = require("child_process");

const cloudflared = spawn("cloudflared", [
  "tunnel",
  "--no-autoupdate",
  "run",
  "--token",
  cftoken,
]);

Handling the close and exit events

It is a good idea to handle the close and exit events of the cloudflared process, so that you can be notified when the process closes or exits. You can do this by adding the following code to your script:

cloudflared.on("close", (code) => {
  console.log(`cloudflared closed with code ${code}`);
});

//Defines a route for the app that sends a response of 'Hello World' to the user.
cloudflared.on("exit", (code, signal) => {
  console.log(`cloudflared exited with code ${code} & signal ${signal}`);
});

Defining a route for the Express app

Finally, we will define a route for the Express app that sends a response of "Hello World" to the user when the root path ('/') is requested. Add the following code to your script:

//This shows the response that will sent to the user
app.get("/", (req, res) => {
  res.send("Hello World");
});

That's it! Your Express server is now set up with Cloudflare Tunnel. When you run your script, you should see the message "Server is running on port 5000" in the console, indicating that the server has started. You can test the route by visiting http://localhost:5000/ in your web browser.

Below is the complete code you can copy to your Express app (Hello world) application file named app.js :

Note: The line 10 let cftoken = process.env.CFT_TOKEN assigns a string value to the variable cftoken generated to be the cloudflared token.

This string is a JSON Web Token (JWT), which is used to authenticate API requests. This token is being used to authenticate with the cloudflared command as seen below :

require('dotenv').config()
const {spawn}=require ("child_process")
const express = require('express');
const app = express();

// This creates an instance of an Express app and sets it to listen on port 5000.
app.listen(5000, () => {
    console.log(`Server is running on port 5000`)
})
let cftoken = process.env.CFT_TOKEN

const cloudflared = spawn("cloudflared", [
    "tunnel",
    "--no-autoupdate",
    "run",
    "--token",
cftoken,
])

cloudflared.on("close", (code)=> {
    console.log(`cloudflared closed with code ${code}`)
})
//Defines a route for the app that sends a response of 'Hello World' to the user.
cloudflared.on("exit", (code,signal) => {
    console.log(`cloudflared exited with code ${code} & signal ${signal}`)
})

//This shows the response that will sent to the user
app.get("/", (req,res) => {
    res.send('Hello World')
})