Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] added cloudflare static tunnels support #282

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

waruhachi
Copy link
Contributor

This pertains to #276

Copy link

vercel bot commented Sep 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
robo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 16, 2024 8:14pm

Copy link
Member

@Pkmmte Pkmmte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good! Just have a few questions on the execution of things.

Only required changes are using the PORT environment variable when applicable and running the new code through Prettier for format consistency.

"ingress": [
{
"hostname": `robo.${process.env.CLOUDFLARE_DOMAIN}`,
"service": "http://localhost:3000"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Local service port should be process.env.PORT by default if that exists.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Port will default to 3000 if none is set in the .env file

cloudflareLogger.debug(`Updated .env file with ${key}=${value}`)
}

function installService(token: string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want to install cloudflared as a service on the user machine even though startCloudflared already runs the tunnel associated with that token?

export function startCloudflared(url: string) {
const tunnelId = process.env.CLOUDFLARE_TUNNEL_ID;
const tunnelToken = process.env.CLOUDFLARE_TUNNEL_TOKEN;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we reduce the number of items requested from the user by requesting the tunnel token from Cloudflare ourselves?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently only the Account ID, API Key, and the domain are requested from the user. The tunnel ID and token are stored in the .env file after the initial tunnel creation for later use.

const tunnelToken = process.env.CLOUDFLARE_TUNNEL_TOKEN;
const tunnelDomain = process.env.CLOUDFLARE_DOMAIN;

const commandArgs = (tunnelId && tunnelToken && tunnelDomain) ? ['tunnel', '--no-autoupdate', 'run', '--token', tunnelToken, '--url', url, tunnelId] : ['tunnel', '--url', url, '--no-autoupdate'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ternary operation is a bit tough to read quickly. Mind cleaning it by appending the token conditionally instead? The code overall would be longer but more legible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored for easier readability


try {
if (process.env.CLOUDFLARE_TUNNEL_ID) {
const roboTunnel = await cloudflareRequest(`/accounts/${process.env.CLOUDFLARE_ACCOUNT_ID}/cfd_tunnel/${process.env.CLOUDFLARE_TUNNEL_ID}/token`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do Cloudflare's tunnel tokens expire at some point? If so, do we have a way to auto refresh them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was unable to find any documentation that would hint that they expire or any endpoints that let us refresh them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants