All Articles

šŸŽšŸ” Launch your Mac from a browser with Cloudflare

I donā€™t think I have taken my personal MacBook Air outside of my home in over two years. My iPhone handles most of what I need and I do real work on my work laptop. However, I do sometimes find a reason to need to grab my personal MacBook. Maybe I left a file locally on it that I need, Iā€™m using an iPad and I need the desktop version of a site, or I just want to write a longer response to an iMessage thread and prefer a keyboard.

Last summer, I wrote about running your own browser-based VS Code. I love the idea of moving more use cases into a browser. Any device with a modern can become part of your workflow with just a couple clicks. In todayā€™s post, Iā€™m going to go beyond just a single use case or application - instead, Iā€™m going to walk through moving my entire Mac into a browser.


šŸŽÆ I have a few goals for this project:

  • Be able to reach my personal Mac remotely from any device, in any location, with just a browser.
  • Restrict access to that Mac to just me.
  • Do not punch holes in any firewalls or expose any ports or IPs to the Internet.

šŸ—ŗļø This walkthrough covers how to:

  • Build a Zero Trust access control rule that integrates with identity providers to secure access to my Mac.
  • Deploy Cloudflare Tunnel to create an outbound-only connection to Cloudflareā€™s network to make my Mac available remotely without a private network.
  • Enable the VNC server on my Mac.
  • Connect from a browser on a different device.

ā²ļøTime to complete: ~45 minutes


šŸ‘” I work there. I work at Cloudflare. Several of my posts on this blog that discuss Cloudflare focus on building things with Cloudflare Workers. Iā€™m a Workers customer and pay my invoice to use it. However, this tutorial uses products where Iā€™m on the team (Access and Tunnel).

Build a Zero Trust rule

I want to be the only person who can reach this device. To do that, Iā€™m going to start by building a rule in Cloudflare Access, the access control layer of Cloudflareā€™s Zero Trust portfolio.

Cloudflare Access follows a Zero Trust architecture: by default, Access assumes every connection or request is untrusted and forces the user (or service) to authenticate. This gives me the security posture I need to build a simple rule to only allow myself to reach this device.

For obscurity purposes, Iā€™m building all of these rules and configuring the steps in this tutorial with a different hostname from what Iā€™ll use long-term.

To begin, Iā€™m going to navigate to the Cloudflare for Teams dashboard. Iā€™ll choose Access from the sidebar and Iā€™ll click Add an application.

Add Rule

Iā€™ll give the application a name and input the URL where the application will be available. Weā€™ll create the DNS record for that URL later in this tutorial when we connect my Mac with Cloudflare Tunnel

Add App

Next, Iā€™m going to create a rule that will only allow me to reach this destination. The action here is just Allow. Thanks to the Zero Trust model in Cloudflare Access, everything not allowed will be denied by default so this is the only policy I need.

Allow Rule

Iā€™ll scroll down to the bottom of the page to define who is allowed by defining an Include rule. in this case, itā€™s a single email address. Iā€™ll come back and add a Require rule to force logins to use a hardkey as the multifactor method.

Include Rule

Alright, now the fun part. Iā€™m first going to configure a custom logo for this application. Iā€™ve found an Apple logo at a public URL that Iā€™ll paste into the field here.

Custom Logo

Next, I want to control the identity providers this application can use. As you can see, Iā€™ve integrated several, but I will limit it to just a few options.

IdP Control

Finally, Iā€™m going to set this to be a VNC application on the last page of this configuration flow. Choosing VNC will tell Cloudflare to render this non-HTTP use case in the browser.

VNC Config

Once saved, the application (and the custom logo) will appear in my Application list and I can begin connecting my Mac.

App List

Configure Mac VNC

macOS devices can create VNC servers using its default ā€œScreen Sharingā€ feature. To begin, Iā€™ll navigate to my system settings and choose Sharing. Once there, I need to toggle ā€œScreen Sharingā€ to enabled.

Screen Sharing

Next, Iā€™ll click ā€œComputer Settingsā€¦ā€ where I will add a password to allow viewers to control the screen.

Add Password

Youā€™ll probably notice that this requires a URL available on my local network. My local network in this case is just my home. Connecting still requires my device password, so itā€™s not terribly different from someone already in my home walking over to my device and trying to type in my password. That said, itā€™s something to keep in mind and a topic Iā€™m going to address in a follow-up blog on home network security.

Deploy Cloudflare Tunnel

I mentioned earlier that I want the convenience of being able to reach this from the public Internet, but I do not want to expose any ports or IPs to the public Internet. Cloudflare Tunnel solves that for me.

Cloudflare Tunnel relies on a lightweight connector, cloudflared, to create an outbound-only connection from my infrastructure (in this case my Mac) to Cloudflareā€™s edge. I can run a single instance of cloudflared to support multiple applications and hostnames, but in this example Iā€™m just running it for this particular workflow.

Iā€™m running cloudflared on my Mac as a manual process. In the ā€œWhatā€™s Next?ā€ section below, Iā€™ll add instructions on running it as a service and my plans to put it on a Raspberry Pi in front of the Mac.

First, Iā€™ll download cloudflared on to the Mac that I want to reach. Iā€™ll use the package manager brew but you can also build cloudflared yourself or download the latest release using its open-source repository.

brew install cloudflare/cloudflare/cloudflared

Next, I need to authenticate this instance of cloudflared into my Cloudflare account. This step allows the connector to create connections and DNS records in my account.

cloudflared tunnel login

Tunnel Login

Iā€™ll be prompted to sign in to my Cloudflare account in my browser and Cloudflare will return a certificate to my device.

Auth into Cloudflare

I can now create the Tunnel in my acccount with the following command.

cloduflared tunnel create macbook

Once created, Cloudflare will generate a UUID and credentials file for the Tunnel. To run this Tunnel, at any time, all I need is that credentials file. I can delete the original authentication certificate after creating the DNS records if I want to do so.

Create Tunnel

Next, I want to configure cloudflared to send traffic destined for the hostname I protected earlier to the VNC port on my target Mac.

cd and vim

The configuration file below takes the UUID of the Tunnel I created earlier, the credentials file for that Tunnel, and defines ingress for that Tunnel.

tunnel: 5493469b-01fe-8b3f-0003abb31654
credentials-file: /Users/samuelrhea/.cloudflared/5493469b-01fe-8b3f-0003abb31654.json

ingress:
  - hostname: mac-browser.widgetcorp.tech
    service: tcp://localhost:5900
  - service: http_status:404

Once I save that configuration file, I need to tie the hostname I want to use to the Tunnel I just created. I can do that without leaving the command line. The command below will create a DNS record in my Cloudflare account and associate it with this Tunnel.

cloudflared tunnel route dns macbook mac-browser.widgetcorp.tech

Route DNS

And with that, I can go ahead and run the Tunnel and it will connect to two nearby data centers in Cloudflareā€™s network.

cloudflared tunnel run macbook

Run Tunnel

Connect from a browser

I can now test this out. Iā€™ll go to a different machine and visit the URL in my browser.

When I do, Iā€™m prompted to authenticate with one of the three identity providers I allowed earlier.

Login

Alternatively, I can go to my App Launcher where I can launch this application and any other Iā€™m able to reach.

App Launch

Either way, when I connect to the application Iā€™ll be prompted to input the VNC password I created previously.

VNC Password

And, once Iā€™ve input my password, I can remotely access my Mac in Chrome on a different device.

Post Login

Whatā€™s next?

The steps described in this tutorial require that cloudflared is running (in addition to the Mac itself). I can go go ahead and use launchtl on macOS to run cloudflared like a service, but what I really want to do is instead put cloudflared on a Raspberry Pi in my home to serve this use case and others. Iā€™ve promised that tutorial in past posts and Iā€™ll get to it.

A couple of notes on set up and usage, though:

  • The Cloudflare Tunnel configuration work is still more burdensome than I want it to be. Weā€™re working on that. Are there specific pain points that frustrate you as well? Tell us here.
  • Iā€™m not planning on using this as a daily driver. First, any use case on my Mac that can run in a browser as a dedicated web-based application is probably going to be a better experience in that specific web app. Second, we need to keep making this faster. This set up is catch all, for now.

Published Aug 29, 2021

Austinite in Lisbon. VP of Product at Cloudflare.