From be288acf5295e3243307f4971f1f69729c75b4c2 Mon Sep 17 00:00:00 2001 From: Ou Date: Thu, 31 Oct 2024 14:21:11 +0800 Subject: [PATCH] chore: refactor proxy --- docker-compose.yml | 1 + example.env.server | 3 ++- nitro.config.ts | 5 +++++ server/api/proxy/img.png.ts | 14 ++++++++++++++ server/api/{proxy.ts => proxy/index.ts} | 0 server/database/cache.ts | 2 +- server/sources/coolapk/utils.ts | 4 +--- server/sources/kuaishou.ts | 8 +++----- server/sources/toutiao.ts | 6 ++++++ server/sources/weibo.ts | 2 +- server/sources/zhihu.ts | 2 +- server/utils/base64.ts | 17 +++++++++++++++++ server/utils/proxy.ts | 3 +++ src/components/column/card.tsx | 2 +- 14 files changed, 56 insertions(+), 13 deletions(-) create mode 100644 server/api/proxy/img.png.ts rename server/api/{proxy.ts => proxy/index.ts} (100%) create mode 100644 server/utils/base64.ts create mode 100644 server/utils/proxy.ts diff --git a/docker-compose.yml b/docker-compose.yml index 1c1b2eb..a08c765 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,3 +12,4 @@ services: - G_CLIENT_SECRET= - JWT_SECRET= - INIT_TABLE=true + - ENABLE_CACHE=true diff --git a/example.env.server b/example.env.server index 2a3de8a..8c920c5 100644 --- a/example.env.server +++ b/example.env.server @@ -1,4 +1,5 @@ G_CLIENT_ID= G_CLIENT_SECRET= JWT_SECRET= -INIT_TABLE=true \ No newline at end of file +INIT_TABLE=true +ENABLE_CACHE=true \ No newline at end of file diff --git a/nitro.config.ts b/nitro.config.ts index 6281dfb..6d89170 100644 --- a/nitro.config.ts +++ b/nitro.config.ts @@ -31,6 +31,11 @@ if (process.env.VERCEL) { nitroOption.preset = "vercel-edge" // You can use other online database, do it yourself. For more info: https://db0.unjs.io/connectors nitroOption.database = undefined + // nitroOption.vercel = { + // config: { + // cache: [] + // }, + // } } else if (process.env.CF_PAGES) { nitroOption.preset = "cloudflare-pages" nitroOption.database = { diff --git a/server/api/proxy/img.png.ts b/server/api/proxy/img.png.ts new file mode 100644 index 0000000..9541b3a --- /dev/null +++ b/server/api/proxy/img.png.ts @@ -0,0 +1,14 @@ +export default defineEventHandler(async (event) => { + const img = getQuery(event).url + if (img) { + const url = decodeBase64URL(img as string) + return sendProxy(event, url, { + headers: { + "Access-Control-Allow-Origin": "*", + "Access-Control-Allow-Credentials": "*", + "Access-Control-Allow-Methods": "GET, HEAD, POST, PUT, OPTIONS", + "Access-Control-Allow-Headers": "*", + }, + }) + } +}) diff --git a/server/api/proxy.ts b/server/api/proxy/index.ts similarity index 100% rename from server/api/proxy.ts rename to server/api/proxy/index.ts diff --git a/server/database/cache.ts b/server/database/cache.ts index 0fc524a..36377e7 100644 --- a/server/database/cache.ts +++ b/server/database/cache.ts @@ -49,7 +49,7 @@ export async function getCacheTable() { try { // 如果没有数据库,这里不会报错,只会在第一次访问的时候报错 const db = useDatabase() - if (process.env.NODE_ENV && process.env.NODE_ENV !== "production") return + if (process.env.ENABLE_CACHE === "false") return const cacheTable = new Cache(db) if (process.env.INIT_TABLE !== "false") await cacheTable.init() return cacheTable diff --git a/server/sources/coolapk/utils.ts b/server/sources/coolapk/utils.ts index c315dc5..e93ade8 100644 --- a/server/sources/coolapk/utils.ts +++ b/server/sources/coolapk/utils.ts @@ -1,6 +1,4 @@ // https://github.com/DIYgod/RSSHub/blob/master/lib/routes/coolapk/utils.ts -import { Buffer } from "node:buffer" - function getRandomDEVICE_ID() { const r = [10, 6, 6, 6, 14] const id = r.map(i => Math.random().toString(36).substring(2, i)) @@ -13,7 +11,7 @@ async function get_app_token() { const hex_now = `0x${now.toString(16)}` const md5_now = await md5(now.toString()) const s = `token://com.coolapk.market/c67ef5943784d09750dcfbb31020f0ab?${md5_now}$${DEVICE_ID}&com.coolapk.market` - const md5_s = await md5(Buffer.from(s).toString("base64")) + const md5_s = await md5(encodeBase64(s)) const token = md5_s + DEVICE_ID + hex_now return token } diff --git a/server/sources/kuaishou.ts b/server/sources/kuaishou.ts index b371a08..d4bf006 100644 --- a/server/sources/kuaishou.ts +++ b/server/sources/kuaishou.ts @@ -53,11 +53,9 @@ export default defineSource(async () => { id: hotSearchWord, title: hotItem.name, url: `https://www.kuaishou.com/search/video?searchKey=${encodeURIComponent(hotItem.name)}`, - extra: hotItem.iconUrl - ? { - icon: `/api/proxy?img=${encodeURIComponent(hotItem.iconUrl)}`, - } - : {}, + extra: { + icon: hotItem.iconUrl && proxyPicture(hotItem.iconUrl), + }, } }) }) diff --git a/server/sources/toutiao.ts b/server/sources/toutiao.ts index 93260a9..0a21780 100644 --- a/server/sources/toutiao.ts +++ b/server/sources/toutiao.ts @@ -6,6 +6,9 @@ interface Res { Image: { url: string } + LabelUri?: { + url: string + } }[] } @@ -18,6 +21,9 @@ export default defineSource(async () => { id: k.ClusterIdStr, title: k.Title, url: `https://www.toutiao.com/trending/${k.ClusterIdStr}/`, + extra: { + icon: k.LabelUri?.url && proxyPicture(k.LabelUri.url), + }, } }) }) diff --git a/server/sources/weibo.ts b/server/sources/weibo.ts index 4c20558..0181784 100644 --- a/server/sources/weibo.ts +++ b/server/sources/weibo.ts @@ -37,7 +37,7 @@ export default defineSource(async () => { title: k.word, extra: { icon: k.icon && { - url: `/api/proxy?img=${encodeURIComponent(k.icon)}`, + url: proxyPicture(k.icon), scale: 1.5, }, }, diff --git a/server/sources/zhihu.ts b/server/sources/zhihu.ts index cb9054c..4488750 100644 --- a/server/sources/zhihu.ts +++ b/server/sources/zhihu.ts @@ -29,7 +29,7 @@ export default defineSource({ id: k.target.id, title: k.target.title, extra: { - icon: k.card_label?.night_icon && `/api/proxy?img=${encodeURIComponent(k.card_label?.night_icon)}`, + icon: k.card_label?.night_icon && proxyPicture(k.card_label.night_icon), }, url: `https://www.zhihu.com/question/${k.target.id}`, } diff --git a/server/utils/base64.ts b/server/utils/base64.ts new file mode 100644 index 0000000..2938f15 --- /dev/null +++ b/server/utils/base64.ts @@ -0,0 +1,17 @@ +import { Buffer } from "node:buffer" + +export function decodeBase64URL(str: string) { + return new TextDecoder().decode(Buffer.from(decodeURIComponent(str), "base64")) +} + +export function encodeBase64URL(str: string) { + return encodeURIComponent(Buffer.from(str).toString("base64")) +} + +export function decodeBase64(str: string) { + return new TextDecoder().decode(Buffer.from(str, "base64")) +} + +export function encodeBase64(str: string) { + return Buffer.from(str).toString("base64") +} diff --git a/server/utils/proxy.ts b/server/utils/proxy.ts new file mode 100644 index 0000000..94c63cf --- /dev/null +++ b/server/utils/proxy.ts @@ -0,0 +1,3 @@ +export function proxyPicture(url: string) { + return `/api/proxy/img.png?url=${encodeBase64URL(url)}` +} diff --git a/src/components/column/card.tsx b/src/components/column/card.tsx index 804411b..fde1279 100644 --- a/src/components/column/card.tsx +++ b/src/components/column/card.tsx @@ -217,7 +217,7 @@ function ExtraInfo({ item }: { item: NewsItem }) { transform: `scale(${scale ?? 1})`, }} className="h-4 inline mt--1" - onError={e => e.currentTarget.hidden = true} + onError={e => e.currentTarget.style.display = "none"} /> ) }