mirror of
https://github.com/ourongxing/newsnow.git
synced 2025-01-18 18:59:15 +08:00
chore: refactor proxy
This commit is contained in:
parent
743778e757
commit
be288acf52
@ -12,3 +12,4 @@ services:
|
|||||||
- G_CLIENT_SECRET=
|
- G_CLIENT_SECRET=
|
||||||
- JWT_SECRET=
|
- JWT_SECRET=
|
||||||
- INIT_TABLE=true
|
- INIT_TABLE=true
|
||||||
|
- ENABLE_CACHE=true
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
G_CLIENT_ID=
|
G_CLIENT_ID=
|
||||||
G_CLIENT_SECRET=
|
G_CLIENT_SECRET=
|
||||||
JWT_SECRET=
|
JWT_SECRET=
|
||||||
INIT_TABLE=true
|
INIT_TABLE=true
|
||||||
|
ENABLE_CACHE=true
|
@ -31,6 +31,11 @@ if (process.env.VERCEL) {
|
|||||||
nitroOption.preset = "vercel-edge"
|
nitroOption.preset = "vercel-edge"
|
||||||
// You can use other online database, do it yourself. For more info: https://db0.unjs.io/connectors
|
// You can use other online database, do it yourself. For more info: https://db0.unjs.io/connectors
|
||||||
nitroOption.database = undefined
|
nitroOption.database = undefined
|
||||||
|
// nitroOption.vercel = {
|
||||||
|
// config: {
|
||||||
|
// cache: []
|
||||||
|
// },
|
||||||
|
// }
|
||||||
} else if (process.env.CF_PAGES) {
|
} else if (process.env.CF_PAGES) {
|
||||||
nitroOption.preset = "cloudflare-pages"
|
nitroOption.preset = "cloudflare-pages"
|
||||||
nitroOption.database = {
|
nitroOption.database = {
|
||||||
|
14
server/api/proxy/img.png.ts
Normal file
14
server/api/proxy/img.png.ts
Normal 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": "*",
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
@ -49,7 +49,7 @@ export async function getCacheTable() {
|
|||||||
try {
|
try {
|
||||||
// 如果没有数据库,这里不会报错,只会在第一次访问的时候报错
|
// 如果没有数据库,这里不会报错,只会在第一次访问的时候报错
|
||||||
const db = useDatabase()
|
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)
|
const cacheTable = new Cache(db)
|
||||||
if (process.env.INIT_TABLE !== "false") await cacheTable.init()
|
if (process.env.INIT_TABLE !== "false") await cacheTable.init()
|
||||||
return cacheTable
|
return cacheTable
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
// https://github.com/DIYgod/RSSHub/blob/master/lib/routes/coolapk/utils.ts
|
// https://github.com/DIYgod/RSSHub/blob/master/lib/routes/coolapk/utils.ts
|
||||||
import { Buffer } from "node:buffer"
|
|
||||||
|
|
||||||
function getRandomDEVICE_ID() {
|
function getRandomDEVICE_ID() {
|
||||||
const r = [10, 6, 6, 6, 14]
|
const r = [10, 6, 6, 6, 14]
|
||||||
const id = r.map(i => Math.random().toString(36).substring(2, i))
|
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 hex_now = `0x${now.toString(16)}`
|
||||||
const md5_now = await md5(now.toString())
|
const md5_now = await md5(now.toString())
|
||||||
const s = `token://com.coolapk.market/c67ef5943784d09750dcfbb31020f0ab?${md5_now}$${DEVICE_ID}&com.coolapk.market`
|
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
|
const token = md5_s + DEVICE_ID + hex_now
|
||||||
return token
|
return token
|
||||||
}
|
}
|
||||||
|
@ -53,11 +53,9 @@ export default defineSource(async () => {
|
|||||||
id: hotSearchWord,
|
id: hotSearchWord,
|
||||||
title: hotItem.name,
|
title: hotItem.name,
|
||||||
url: `https://www.kuaishou.com/search/video?searchKey=${encodeURIComponent(hotItem.name)}`,
|
url: `https://www.kuaishou.com/search/video?searchKey=${encodeURIComponent(hotItem.name)}`,
|
||||||
extra: hotItem.iconUrl
|
extra: {
|
||||||
? {
|
icon: hotItem.iconUrl && proxyPicture(hotItem.iconUrl),
|
||||||
icon: `/api/proxy?img=${encodeURIComponent(hotItem.iconUrl)}`,
|
},
|
||||||
}
|
|
||||||
: {},
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -6,6 +6,9 @@ interface Res {
|
|||||||
Image: {
|
Image: {
|
||||||
url: string
|
url: string
|
||||||
}
|
}
|
||||||
|
LabelUri?: {
|
||||||
|
url: string
|
||||||
|
}
|
||||||
}[]
|
}[]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -18,6 +21,9 @@ export default defineSource(async () => {
|
|||||||
id: k.ClusterIdStr,
|
id: k.ClusterIdStr,
|
||||||
title: k.Title,
|
title: k.Title,
|
||||||
url: `https://www.toutiao.com/trending/${k.ClusterIdStr}/`,
|
url: `https://www.toutiao.com/trending/${k.ClusterIdStr}/`,
|
||||||
|
extra: {
|
||||||
|
icon: k.LabelUri?.url && proxyPicture(k.LabelUri.url),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -37,7 +37,7 @@ export default defineSource(async () => {
|
|||||||
title: k.word,
|
title: k.word,
|
||||||
extra: {
|
extra: {
|
||||||
icon: k.icon && {
|
icon: k.icon && {
|
||||||
url: `/api/proxy?img=${encodeURIComponent(k.icon)}`,
|
url: proxyPicture(k.icon),
|
||||||
scale: 1.5,
|
scale: 1.5,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -29,7 +29,7 @@ export default defineSource({
|
|||||||
id: k.target.id,
|
id: k.target.id,
|
||||||
title: k.target.title,
|
title: k.target.title,
|
||||||
extra: {
|
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}`,
|
url: `https://www.zhihu.com/question/${k.target.id}`,
|
||||||
}
|
}
|
||||||
|
17
server/utils/base64.ts
Normal file
17
server/utils/base64.ts
Normal 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
3
server/utils/proxy.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export function proxyPicture(url: string) {
|
||||||
|
return `/api/proxy/img.png?url=${encodeBase64URL(url)}`
|
||||||
|
}
|
@ -217,7 +217,7 @@ function ExtraInfo({ item }: { item: NewsItem }) {
|
|||||||
transform: `scale(${scale ?? 1})`,
|
transform: `scale(${scale ?? 1})`,
|
||||||
}}
|
}}
|
||||||
className="h-4 inline mt--1"
|
className="h-4 inline mt--1"
|
||||||
onError={e => e.currentTarget.hidden = true}
|
onError={e => e.currentTarget.style.display = "none"}
|
||||||
/>
|
/>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user