chore: refactor proxy

This commit is contained in:
Ou 2024-10-31 14:21:11 +08:00
parent 743778e757
commit be288acf52
14 changed files with 56 additions and 13 deletions

View File

@ -12,3 +12,4 @@ services:
- G_CLIENT_SECRET=
- JWT_SECRET=
- INIT_TABLE=true
- ENABLE_CACHE=true

View File

@ -1,4 +1,5 @@
G_CLIENT_ID=
G_CLIENT_SECRET=
JWT_SECRET=
INIT_TABLE=true
INIT_TABLE=true
ENABLE_CACHE=true

View File

@ -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 = {

View File

@ -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": "*",
},
})
}
})

View File

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

View File

@ -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
}

View File

@ -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),
},
}
})
})

View File

@ -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),
},
}
})
})

View File

@ -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,
},
},

View File

@ -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}`,
}

17
server/utils/base64.ts Normal file
View File

@ -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")
}

3
server/utils/proxy.ts Normal file
View File

@ -0,0 +1,3 @@
export function proxyPicture(url: string) {
return `/api/proxy/img.png?url=${encodeBase64URL(url)}`
}

View File

@ -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"}
/>
)
}