diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 363e698c..17faf4be 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -1,4 +1,5 @@ name: Deploy Worker + on: workflow_dispatch: repository_dispatch: @@ -17,27 +18,31 @@ jobs: with: node-version: '21' - - name: Install dependencies - run: npm install - - - name: Install wrangler - run: npm install -g wrangler + - name: Install dependencies and wrangler + run: | + npm install + npm install -g wrangler - - name: Check and Create R2 Bucket + - name: Check and Create KV Namespace env: CLOUDFLARE_API_TOKEN: ${{ secrets.CF_API_TOKEN }} CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }} run: | - BUCKET_NAME="sublink-worker" - if ! wrangler r2 bucket list | grep -q "$BUCKET_NAME"; then - echo "Bucket $BUCKET_NAME does not exist. Creating..." - wrangler r2 bucket create "$BUCKET_NAME" - echo "Bucket $BUCKET_NAME created successfully." - else - echo "Bucket $BUCKET_NAME already exists. Skipping creation." + KV_NAMESPACE="SUBLINK_KV" + KV_ID=$(wrangler kv:namespace list | jq -r '.[] | select(.title == "sublink-worker-'$KV_NAMESPACE'") | .id') + if [ -z "$KV_ID" ]; then + KV_ID=$(wrangler kv:namespace create "$KV_NAMESPACE" | jq -r '.id') fi - + echo "KV_ID=$KV_ID" >> $GITHUB_ENV + + - name: Update wrangler.toml + run: sed -i 's/id = "[^"]*"/id = "'$KV_ID'"/' wrangler.toml + - name: Deploy to Cloudflare Workers uses: cloudflare/wrangler-action@2.0.0 with: - apiToken: ${{ secrets.CF_API_TOKEN }} \ No newline at end of file + apiToken: ${{ secrets.CF_API_TOKEN }} + + - name: Display deployment logs + if: failure() + run: wrangler tail --format pretty \ No newline at end of file diff --git a/README.md b/README.md index 207dd7fb..c79111c6 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Sublink Worker 是一个可部署在 Cloudflare Worker 上轻量级的订阅转 - Sing-Box - Clash - Xray/V2Ray -- 支持短链接生成(基于 R2) +- 支持短链接生成(基于 KV) - 浅色/深色主题切换 - 灵活的 API,支持脚本化操作 - 用户友好的 Web 界面,灵活的自定义规则 @@ -25,7 +25,7 @@ Sublink Worker 是一个可部署在 Cloudflare Worker 上轻量级的订阅转 [![Deploy to Cloudflare Workers](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/7Sageer/sublink-worker) -> 注意:确保你的 Cloudflare 账户已经开通 R2 存储服务 +> 注意:9月2日之后,短连接服务由 R2 迁移到 KV,请确保API令牌有对应权限 ## API 文档 @@ -40,6 +40,10 @@ Sublink Worker 是一个可部署在 Cloudflare Worker 上轻量级的订阅转 ## 最近更新 +### 2024-09-02 + +- 现在使用 KV 存储短链接,不再依赖 R2 + ### 2024-09-01 - 自定义规则现在支持以下规则: diff --git a/src/index.js b/src/index.js index 0424e522..4b11bfa3 100644 --- a/src/index.js +++ b/src/index.js @@ -93,30 +93,28 @@ async function handleRequest(request) { } ); - } if (url.pathname === '/shorten') { + } else if (url.pathname === '/shorten') { const originalUrl = url.searchParams.get('url'); if (!originalUrl) { return new Response('Missing URL parameter', { status: 400 }); } - + const shortCode = GenerateWebPath(); - await R2_BUCKET.put(`urls/${shortCode}`, originalUrl); - + await SUBLINK_KV.put(shortCode, originalUrl); + const shortUrl = `${url.origin}/s/${shortCode}`; return new Response(JSON.stringify({ shortUrl }), { headers: { 'Content-Type': 'application/json' } }); - } - - if (url.pathname.startsWith('/s/')) { + } else if (url.pathname.startsWith('/s/')) { const shortCode = url.pathname.split('/')[2]; - const originalUrl = await R2_BUCKET.get(`urls/${shortCode}`); - + const originalUrl = await SUBLINK_KV.get(shortCode); + if (originalUrl === null) { return new Response('Short URL not found', { status: 404 }); } - - return Response.redirect(await originalUrl.text(), 302); + + return Response.redirect(originalUrl, 302); } else if (url.pathname.startsWith('/xray')) { // Handle Xray config requests const inputString = url.searchParams.get('config'); diff --git a/wrangler.toml b/wrangler.toml index ba891ba4..e0875395 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -4,8 +4,6 @@ main = "src/index.js" compatibility_date = "2024-07-24" compatibility_flags = ["nodejs_compat"] -# Bind an R2 Bucket. Use R2 to store arbitrarily large blobs of data, such as files. -# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#r2-buckets -[[r2_buckets]] -binding = "R2_BUCKET" -bucket_name = "sublink-worker" \ No newline at end of file +kv_namespaces = [ + { binding = "SUBLINK_KV", id = "e55b046290924cb087f168a7085a4aee" } +] \ No newline at end of file