commit ceaeb1632d123303a819a2c299b5e029126ae5f4 Author: Erisa A Date: Tue Oct 12 18:44:43 2021 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3a3840e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +dist/* +worker/* +node_modules/* \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..fa9ca27 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2021 Erisa A. (erisa.uk) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..b1b15b7 --- /dev/null +++ b/README.md @@ -0,0 +1,44 @@ +# Domain Redirecting with Cloudflare Workers + +Easily redirect one entire domain to another with a serverless Cloudflare Worker. + +All paths and other data are kept intact. The redirect served is `302 Found` (temporary redirect) to prevent cache headache if you ever change your mind. + +The aim is to replace lots of Page Rules like this: +![](https://cdn.erisa.moe/firefox_ILlPFFe3Hk.png) + +And instead use a simple map in `domainMaps.json`: +```json +{ + "erisa.moe": "erisa.uk", + "erisa.wales": "erisa.uk", + "erisa.cymru": "erisa.uk", + "link.erisa.moe": "erisa.link", + "cloud.erisa.moe": "erisa.cloud" +} +``` + +**Note**: Cloudflare Workers is a billed product with a free tier available. Page Rules are preferred if cost savings are your priority. This Worker is used to keep domain mapping centralised and to free up Page Rules for other purposes. **This is not always what you want.** + +Code is provided for reference and curiosity purposes, project is only intended to be used personally by the author. +But if you like it, you can use it! + +## Usage + +Required: +- Cloudflare DNS +- Wrangler (`npm i -g wrangler` or `cargo install wrangler`) + +Deploy: +1. Clone the repository +2. Edit `domainMaps.json` with source/target pairs of domains to redirect. +3. `wrangler publish` + +After deploying the worker, ensure that the source domain has a valid DNS record (If not, create an `AAAA` pointing to `100::`) and then head to the Workers tab. Configure `example.domain.com/*` to use your worker: +![](https://cdn.erisa.moe/firefox_lMarhsl7p0.png) + +Domains that are mapped to the Worker but not listed in `domainMaps.json` will return a 404 error: +![](https://cdn.erisa.moe/firefox_jRnHX7iexY.png) + + +That's all! It's quite simple. \ No newline at end of file diff --git a/domainMaps.json b/domainMaps.json new file mode 100644 index 0000000..a7bdacc --- /dev/null +++ b/domainMaps.json @@ -0,0 +1,8 @@ +{ + "source.domain": "target.domain", + "erisa.moe": "erisa.uk", + "erisa.wales": "erisa.uk", + "erisa.cymru": "erisa.uk", + "link.erisa.moe": "erisa.link", + "cloud.erisa.moe": "erisa.cloud" +} diff --git a/index.js b/index.js new file mode 100644 index 0000000..73778f7 --- /dev/null +++ b/index.js @@ -0,0 +1,20 @@ +addEventListener("fetch", event => { + event.respondWith(handleRequest(event.request)) +}) + +async function handleRequest(request) { + const url = new URL(request.url); + const map = require("./domainMaps.json"); + + if (url.hostname in map){ + url.hostname = map[url.hostname]; + return new Response(null, { status: 302, headers: { Location: url.href } }) + } else { + return new Response("404, this domain is not configured", { + status: 404, + headers: { + "content-type": "text/plain" + } + }); + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..d43e2f4 --- /dev/null +++ b/package.json @@ -0,0 +1,12 @@ +{ + "name": "domain-redirect", + "version": "1.0.0", + "description": "Easily redirect one entire domain to another with a serverless Cloudflare Worker.", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": ["cloudflare-worker"], + "author": "Erisa A ", + "license": "MIT" +} diff --git a/wrangler.toml b/wrangler.toml new file mode 100644 index 0000000..41055ee --- /dev/null +++ b/wrangler.toml @@ -0,0 +1,5 @@ +name = "domain-redirect" +type = "webpack" +compatibility_flags = [] +workers_dev = true +compatibility_date = "2021-10-12"