Using Cloudflared & Python for remote executing stuff on your closed home network!

As you might be aware Cloudflare recently started offering a tunnel so that you could access your home network from your own domain, this is great news as before we had to open ports or made more complex configurations to make this work.

So one of the first uses that comes to mind is remote executing stuff on your computer when your away! this opens so many possibilities. I created a python program that basically is a webhook server. It keeps listening for requests and once it has those it will open a program in your local machine! How cool that sounds!

Th final program looks like this:

I will go in more detail about how this webhook program works in another post, this post is mostly about how to setup Cloudflared and quicktunnels if you want to skip the creation of a domain.

Let’s get into the details

In order to expose a webhook server so it can be accessed from an external source we need to allow external traffic into our network, this is most of the times very risky so in order to allow traffic and not expose ourselves by opening ports in our router and firewall, we can use a tunneling solution, one that’s easy to set up and free to use is the one that is provided by Cloudflare called Cloudflared.

To make this work the summarized steps are:

  • Get a free domain name *Create a cloudflare account *Add domain name to cloudflare account, point nameservers to cloudflare. *Setup tunnel inside cloudflare account *Setup cloudflared tunnel inside pc.

To install it on other machines only the last two steps are needed.

A guide on how to install CloudFlared can be found in the following links:

https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/tunnel-guide/remote/

https://www.makeuseof.com/use-cloudflare-tunnel-expose-local-servers-internet/

After setting up the tunnel we can just start the webhook server, and every request sent to the domain configured when creating the tunnel in Cloudflared will be received by our webhook server.

Some common webhook parameters to configure in the solution that sends the webhooks are:

API Endpoint URL:

This needs to be the domain you configured when setting up your tunneling solution) (For example: www.mydomain.com/webhook/docx)

Payload Type:

Json

Wrap Request In Array:

No

Authentication:

Basic Authentication (The user and password you configured in the webhook server configuration tab)

Add Headers,Set parameters, etc :

Unchecked

Webhook Server Configuration

In order to use the webhook server we need to define some parameters.

Host: the default ip address where the webhook server will run: 127.0.0.1 is the default as this means “run on the local machine”.

Port: You can choose any port you want that is unused.

Username: the username used for basic authentication when sending webhooks from the outside world.

Password: the password used for basic authentication when sending webhooks from the outside world.

URL_Prefix: if no URL prefix is desired for the webhook receiving url, then just input “

Adding new webhook endpoints: Then in the part below you can associate any webhook endpoint to any file you want in your computer, and the server will open the file whenever a new webhook in this endpoint, windows will use the default program associated with the file extension of this file.

Thne just hit run and you should start receiving webhooks on your machine!

Setting up Cloudflared without a Domain.

To bypass setting up a domain name: (using Quick tunnels) If used for production setting up a domain name is recommended.

https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/do-more-with-tunnels/trycloudflare/

Steps to create a Quick Tunnel.

-Go to https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/installation/

-Download The CloudFlared Tunnel Setup

-Grab the .MSI version x64 from here: https://github.com/cloudflare/cloudflared/releases and install it.

-Open the command line and run the following command specifying the port where the webhook server is running:

cloudflared tunnel --url <http://localhost:7000>

-Copy this address to setup the webhook for example:

-Then webhooks can be received by sending webhooks to this address and adding basic authentication.: