alesan99 / chicken_society

"Become a chicken not a nugget". A full online multiplayer game.
https://alesan99.github.io/chicken_society/
3 stars 0 forks source link

Website: Redirect to Github Pages when chickensociety.net is down. #45

Open alesan99 opened 1 year ago

alesan99 commented 4 months ago

ChatGPT has some suggestions:

Setting up a Cloudflare Argo Tunnel with a fallback mechanism that redirects to a GitHub page in case of an error involves a few steps. Here's a detailed guide to achieve this:

Prerequisites

  1. Cloudflare Account: Ensure you have a Cloudflare account.
  2. Cloudflare Domain: Your domain should be managed by Cloudflare.
  3. Cloudflare Tunnel: Set up Cloudflare Tunnel (previously Argo Tunnel).
  4. GitHub Page: A GitHub Pages site to redirect to in case of an error.

Steps to Set Up Cloudflare Argo Tunnel with Fallback

1. Install Cloudflare Tunnel

First, you need to install the Cloudflare Tunnel (Cloudflared) on your server.

For Linux:

curl -L https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb -o cloudflared.deb
sudo dpkg -i cloudflared.deb

For other operating systems, refer to the Cloudflare Tunnel documentation.

2. Authenticate Cloudflare Tunnel

Authenticate Cloudflare Tunnel with your Cloudflare account:

cloudflared tunnel login

This will open a browser window for you to log in and associate the Tunnel with your Cloudflare account.

3. Create and Configure the Tunnel

Create a new Tunnel:

cloudflared tunnel create my-tunnel

This command creates a Tunnel named my-tunnel. It will provide you with a UUID for the tunnel.

Next, configure the Tunnel to specify the origin server:

cloudflared tunnel route dns my-tunnel example.com

4. Set Up a Fallback Page

To handle fallback when the Tunnel is down, we need to configure the Cloudflare Workers script. This script will check the availability of the Tunnel and redirect to the GitHub Page if there is an issue.

First, create a Cloudflare Worker:

  1. Go to the Cloudflare dashboard.
  2. Navigate to Workers > Create a Worker.
  3. Use the following script for the Worker:
addEventListener('fetch', event => {
    event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
    // The URL of your Cloudflare Tunnel
    const tunnelUrl = 'https://tunnel.example.com'

    try {
        const response = await fetch(tunnelUrl)
        // If the Tunnel responds successfully, return the response
        if (response.ok) {
            return response
        }
        // If the Tunnel responds with an error, redirect to GitHub Pages
        else {
            return Response.redirect('https://username.github.io/', 302)
        }
    } catch (error) {
        // If there is a network error, redirect to GitHub Pages
        return Response.redirect('https://username.github.io/', 302)
    }
}

Replace https://tunnel.example.com with your actual Tunnel URL and https://username.github.io/ with your GitHub Pages URL.

5. Deploy the Worker

Deploy the Worker script and associate it with your domain:

  1. Go to Workers > Manage Workers.
  2. Select the Worker you created.
  3. Click on Routes and add a route such as *example.com/* to associate the Worker with your domain.

6. Test the Setup

  1. Ensure your Tunnel is running:
    cloudflared tunnel run my-tunnel
  2. Visit your domain (https://example.com) to ensure it serves content from your origin server.
  3. Stop the Tunnel to simulate an error:
    cloudflared tunnel stop my-tunnel
  4. Visit your domain again to confirm it redirects to your GitHub Pages site.

Conclusion

With these steps, you set up a Cloudflare Argo Tunnel with a fallback mechanism using Cloudflare Workers. This ensures your users are redirected to a specified GitHub Pages site if the Tunnel experiences an error.