The cloudflare-tunnel simplifies creating and managing Cloudflare Tunnels to route traffic from your local machine to a custom domain.
- Automatically authenticate and create Cloudflare Tunnels.
- Route traffic to a specific local port through a custom domain.
- Manage DNS records using Cloudflare API.
- Ensure secure and seamless public access to your local services.
-
Cloudflare Account:
- Sign up if you don't have one.
-
Cloudflare API Token:
- Create an API token with the following permissions:
- Zone:DNS:Edit
- Zone:DNS:Read
- Create an API token with the following permissions:
-
Cloudflare Tunnel Installed:
- Install
cloudflared:
- Install
brew install cloudflare/cloudflare/cloudflaredgit clone <repo-url>
cd cloudflare-tunnelCopy the sample configuration files and update them with your details.
Rename api-keys.sample.json to api-keys.json and update with your Cloudflare API Token and Zone ID:
{
"ApiToken": "<CLOUDFLARE_API_TOKEN>",
"ZoneId": "<ZONE_ID>"
}Stored in credentials.json:
{
"AccountTag": "<ACCOUNT_TAG>",
"TunnelSecret": "<TUNNEL_SECRET>",
"TunnelID": "<TUNNEL_ID>"
}Saved in <TUNNEL_ID>-config.yml:
tunnel: <Tunnel ID>
credentials-file: ./credentials.json
ingress:
- hostname: <Domain>
service: http://localhost:<Port>
- service: http_status:404Ensure you have Go installed. Then, run:
go mod tidyRun the app with the following command:
go run main.go --port=5173 --tunnel=my-tunnel --domain=anik.cc --credentials=./credentials.json --apiKeys=./api-keys.json- --
port: The local port to forward traffic. - --
tunnel: The name of the Cloudflare Tunnel. - --
domain: Your custom domain. - --
credentials: Path to the credentials file. - --
apiKeys: Path to the API keys file.
Sample Command:
go run main.go --port=5173 --tunnel=my-tunnel --domain=anik.cc --credentials=./credentials.json --apiKeys=./api-keys.jsonFile Structure:
.
├── main.go # Application source code
├── api-keys.sample.json # Sample API keys file
├── credentials.sample.json # Sample Tunnel credentials file
├── config.sample.yml # Sample Tunnel configuration file
├── README.md # Project README
- Failed to create DNS record: An A, AAAA, or CNAME record already exists.
- A DNS record already exists for the domain. Ensure the record matches the tunnel.
- Cloudflare authentication failed.
- Ensure cloudflared is installed and the API Token is valid.
- Tunnel credentials not found.
- Verify that the credentials.json file exists and contains valid credentials.
This project is licensed under the MIT License. See LICENSE for details.