From 285d2396c83ccc32fe7984ca44caf7663dbd59ca Mon Sep 17 00:00:00 2001 From: Jetsung Chan Date: Thu, 4 Jan 2024 02:28:47 +0800 Subject: [PATCH] feat: subdirectory support --- .github/workflows/build-deoply.yml | 16 ++++++++++ README.md | 38 ++++++++++-------------- src/index.ts | 47 ++++++++++++++++++++++++------ wrangler.toml | 5 ++-- 4 files changed, 73 insertions(+), 33 deletions(-) create mode 100644 .github/workflows/build-deoply.yml diff --git a/.github/workflows/build-deoply.yml b/.github/workflows/build-deoply.yml new file mode 100644 index 0000000..44f310f --- /dev/null +++ b/.github/workflows/build-deoply.yml @@ -0,0 +1,16 @@ +name: Deploy to Cloudflare + +on: + push: + branches: ['main', 'test'] + +jobs: + deploy: + runs-on: ubuntu-latest + name: Deploy + steps: + - uses: actions/checkout@v4 + - name: Deploy + uses: cloudflare/wrangler-action@v3 + with: + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} diff --git a/README.md b/README.md index 2f8bed3..c4e881e 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,20 @@ 基于 CloudFlare Workers 的网站加速服务 -## 仓库镜像 +## 部署教程 -- https://git.jetsung.com/servless/worker-cdn -- https://framagit.org/servless/worker-cdn -- https://github.com/servless/worker-cdn +### 通过 GitHub Actions 发布至 CloudFlare -## 部署教程 +1. 从 CloudFlare 获取 `CLOUDFLARE_API_TOKEN` 值,并设置到项目。 + + > `https://github.com//worker-cdn/settings/secrets/actions` + +2. **可选**)设置`别名`。创建 `KV`、,并绑定到此 Workers 服务。 + - 2.1a 手动后台绑定,(`Settings` -> `Variables` -> `KV Namespace Bindings` -> `Add binding` -> `Variable name (datastore)`, `选择创建的 KV`) + - 2.1b 通过命令行创建:`wrangler kv:namespace create datastore` +3. `KV` 设置 `别名值`,Key 为别名(单词),Value(目标域名,含 `http(s)://`)。 + +### 本地部署到 CloudFlare 1. 注册 [CloudFlare 账号](https://www.cloudflare.com/),并且设置 **Workers** 域名 (比如:`xxx.workers.dev`) 2. 安装 [Wrangler 命令行工具](https://developers.cloudflare.com/workers/wrangler/)。 @@ -60,21 +67,8 @@ 在 Cloudflare Workers 的管理界面中,点击 `Triggers` 选项卡,然后点击 `Custom Domians` 中的 `Add Custom Domain` 按钮以绑定域名。 -## Template: worker-typescript - -- https://github.com/cloudflare/workers-sdk/tree/main/templates - -```bash -# full repository clone -$ git clone --depth 1 https://github.com/cloudflare/workers-sdk - -# copy the "worker-typescript" example to "my-project" directory -$ cp -rf workers-sdk/templates/worker-typescript my-project - -# setup & begin development -$ cd my-project && npm install && npm run dev -``` +## 仓库镜像 -```bash -HTTP_PROXY=http://localhost:20171 wrangler publish -``` +- https://git.jetsung.com/servless/worker-cdn +- https://framagit.org/servless/worker-cdn +- https://github.com/servless/worker-cdn diff --git a/src/index.ts b/src/index.ts index b2f0311..37d8d0f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,14 +1,43 @@ -export interface Env { - WEB_URL: string; -} - export default { - async fetch(request: Request, env: Env) { - const API_URL = `${env.WEB_URL}`; - + async fetch(request: Request, env: any) { const url = new URL(request.url); - url.host = API_URL.replace(/^https?:\/\//, ''); - const modifiedRequest = new Request(url.toString(), { + + if (url.pathname === '/favicon.ico') { + return new Response(null, { status: 204 }); + } + + const parts = url.pathname.split(/\//); + const domain = parts[1]; + + const isDomainName = (str: string) => { + const domainRegex = /^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; + return domainRegex.test(str); + }; + + let reqURL = await env.datastore.get(domain); + if (!reqURL && isDomainName(domain)) { + reqURL = 'https://' + domain; + } + + let finalURL = ''; + if (reqURL) { + const queryURL = parts.slice(2).join('/'); + finalURL = `${reqURL}/${queryURL}${url.search}`; + url.host = reqURL.replace(/^https?:\/\//, ''); + } else { + const refURL = request.headers.get('referer'); + if (refURL) { + const refParts = refURL.split(/\//); + finalURL = request.url.replace(refParts[2], refParts[3] + '/'); + url.host = refParts[2]; + } + } + + if (!finalURL) { + return new Response(null, { status: 404 }); + } + + const modifiedRequest = new Request(finalURL, { headers: request.headers, method: request.method, body: request.body, diff --git a/wrangler.toml b/wrangler.toml index 034d829..fbfc1ce 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -2,5 +2,6 @@ name = "cdn" # todo main = "./src/index.ts" compatibility_date = "2023-03-25" -[vars] -WEB_URL="" +kv_namespaces = [ + { binding = "datastore", id = "8b580bbb57c84b549e54eecda8d5dccc" } +]