Skip to content

Instantly share code, notes, and snippets.

@minghao912
Created December 22, 2020 20:05
Show Gist options
  • Save minghao912/f44da8926183c8943ce99c7d4e763085 to your computer and use it in GitHub Desktop.
Save minghao912/f44da8926183c8943ce99c7d4e763085 to your computer and use it in GitHub Desktop.
Cloudflare Worker to route URLs to different ports based on their protocol
addEventListener('fetch', event => {
const request = event.request;
let url = new URL(request.url);
console.log(request.url);
console.log(url);
if (url.protocol == "http:")
url.host = 'dragonfruit.tk:8080';
else if (url.protocol == "https:")
url.host = 'dragonfruit.tk:8443';
else
url.host = 'dragonfruit.tk:8080';
console.log(url);
event.respondWith(fetch(url, request));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment