Skip to main content
  1. Note/

Cloudflare Tunnel

·3 mins· ·
Blog En
Liu Zhe You
Author
Liu Zhe You
Skilled in full-stack development and DevOps, currently focusing on Backend.
Table of Contents

Introduction
#

Currently, I am a teaching assistant for Programming 1.
The IPs of the computers in the lab are internal network IPs.
The Online Judge VM for the course only allows school IPs (140.116.xxx.xxx) to connect,
and does not allow internal network IPs.

Originally, I used ngrok for NAT,
but the free version of ngrok only allows 40 users per tunnel.
With over 240 students, it is impractical to set up 6 tunnels and distribute them to the students.

This is very cumbersome…
Additionally, every time ngrok restarts, it changes the domain.
This requires students to reset their connection domains each time.

Alternative to ngrok
#

Cloudflare Tunnel

The free plan of Cloudflare Tunnel includes:

  • DNS setup
  • No connection limitations (the name server must be on Cloudflare)
  • Other features similar to ngrok

Cloudflare Tunnel Documentation
#

https://developers.cloudflare.com/pages/how-to/preview-with-cloudflare-tunnel/

Installation
#

You can find the appropriate version for your OS at Cloudflare Tunnel Downloads.

For Mac OS:

brew install cloudflare/cloudflare/cloudflared

For Linux:

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

Normal Usage
#

For ngrok:

ngrok http 8888

For Cloudflare Tunnel:

cloudflared tunnel --url http://localhost:8888

start tunnel

This generates a random URL to connect to localhost:8888.
If you want to set your own domain, you need to configure the cloudflared config file.

Advanced Usage
#

Before setting the domain, ensure that the domain’s Nameserver is set to Cloudflare.

After setting it up, you can connect to the server’s localhost:<port> through your DNS.

Next, use the command line to set up the cloudflared config file.

  • Authentication
    cloudflared tunnel login
    
  • Create a Tunnel
    cloudflared create <tunnel_name>
    
    cloudflared tunnel route dns <tunnel_name> <domain_name>
    
    This will generate a <tunnel_id>.json file in ~/.cloudflared/.

You can see the <tunnel_id> under Access -> Tunnel in the Cloudflare dashboard.

access tunnel

In DNS -> Records, you will see the added <domain_name>.
The content part will be <tunnel_id>.cfargotunnel.com.

dns records

Configuration
#

Cloudflare Tunnel: Setup Configuration File

Next, add a config.yaml file in ~/.cloudflared/.

tunnel: <tunnel-id>
credentials-file: /path/to/<tunnel-id>.json # The previously generated JSON file in ~/.cloudflared/
ingress:
  - hostname: <domain-name>
    service: http://localhost:<port1>
  - service: http_status:404

When setting up config.yaml, you can configure multiple ingress entries.
This allows different ports to correspond to different domains.

For example:

  • localhost:8888 corresponds to service1.domain.com
  • localhost:9999 corresponds to service2.domain.com

Start Tunnel
#

To start the tunnel:

cloudflared tunnel run <tunnel_name>

You can use tmux to keep the tunnel running in the background.
Refer to tmux Common Commands!

Using tmux:

tmux new -s cloudflare-tunnel

Then run cloudflared tunnel run <tunnel_name> inside tmux.
Press ctrl + b, then d to detach from tmux.

To reattach to tmux, use tmux attach -t cloudflare-tunnel.

Command List
#

  • cloudflared tunnel login
  • cloudflared create <tunnel_name>
  • cloudflared tunnel route dns <tunnel_name> <domain_name>

    This generates a <tunnel_id>.json file in ~/.cloudflared/ and adds config.yaml here.
    You can see the <tunnel_id> under Access -> Tunnel in the Cloudflare dashboard.

  • cloudflared tunnel run <tunnel_name>

Reference
#

Related

Tmux Cheat Sheet
·3 mins
Blog En
Common tmux commands Cheat Sheet
FastAPI: Mock S3 with Moto
·3 mins
Blog En AWS Backend Testing FastAPI
FastAPI Testing: Mock AWS S3 Boto3 With Moto
k8s: Extract Configmap or Secret to Env File
·2 mins
Blog En Devops Kubernetes
Kubernetes Cheat Sheet: Extract ConfigMap or Secret to .env file
NCKU CSIE Freshman First Semester
·5 mins
Blog En
What did I do in the first semester of freshman year?
Other Test Article
New-Article En
Test Article
New-Article Backend En