Initial commit
This commit is contained in:
commit
ceaeb1632d
7 changed files with 99 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
dist/*
|
||||
worker/*
|
||||
node_modules/*
|
||||
7
LICENSE
Normal file
7
LICENSE
Normal file
|
|
@ -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.
|
||||
44
README.md
Normal file
44
README.md
Normal file
|
|
@ -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:
|
||||

|
||||
|
||||
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:
|
||||

|
||||
|
||||
Domains that are mapped to the Worker but not listed in `domainMaps.json` will return a 404 error:
|
||||

|
||||
|
||||
|
||||
That's all! It's quite simple.
|
||||
8
domainMaps.json
Normal file
8
domainMaps.json
Normal file
|
|
@ -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"
|
||||
}
|
||||
20
index.js
Normal file
20
index.js
Normal file
|
|
@ -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"
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
12
package.json
Normal file
12
package.json
Normal file
|
|
@ -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 <erisa@erisa.uk>",
|
||||
"license": "MIT"
|
||||
}
|
||||
5
wrangler.toml
Normal file
5
wrangler.toml
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
name = "domain-redirect"
|
||||
type = "webpack"
|
||||
compatibility_flags = []
|
||||
workers_dev = true
|
||||
compatibility_date = "2021-10-12"
|
||||
Loading…
Add table
Reference in a new issue