Skip to content

Commit

Permalink
feat: second path mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jetsung committed Jan 3, 2024
1 parent 4337680 commit c104378
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 33 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/build-deoply.yml
Original file line number Diff line number Diff line change
@@ -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 }}
38 changes: 16 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/<ORG>/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/)
Expand Down Expand Up @@ -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
53 changes: 44 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,49 @@
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 === '/') {
return new Response(
`[CDN] eg: https://${request.headers.get('host')}/example.com/file/image.png`
);
}

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,
Expand Down
5 changes: 3 additions & 2 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
]

0 comments on commit c104378

Please sign in to comment.