From 8b6e8af43df2370ff6da3ff934d4a527614a5082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=8F=B6?= <1936472877@qq.com> Date: Sun, 1 Dec 2024 22:30:32 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20steam=E9=9D=99=E6=80=81=E5=9B=BE?= =?UTF-8?q?=E8=B5=B0=E9=80=9A=E7=94=A8=E5=8F=8D=E4=BB=A3,=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=E5=8F=8D=E4=BB=A3=E6=95=99=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 33 +++++++++++++++++++-------------- components/Version.js | 2 +- models/utils/index.js | 18 +++++++++++++----- models/utils/request.js | 7 ++++++- 4 files changed, 39 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index d167314..21b86e4 100644 --- a/README.md +++ b/README.md @@ -107,22 +107,27 @@ pnpm install --filter=steam-plugin 1. 需要`cloudflare账号`, 以及在`cf托管的域名` 2. 打开[Workers 和 Pages](https://dash.cloudflare.com/1e36e2833bb5f40af76d604e0894cb93/workers-and-pages), 点击`创建`, 然后点击`创建 Worker` -3. 名字随意, 可参考`api-steam` 然后点击`部署` 再点击`编辑代码` +3. 名字随意, 可参考`steam` 然后点击`部署` 再点击`编辑代码` 4. 复制以下代码到编辑器, `覆盖`原内容, 然后点击`部署`, 出现`版本已保存`即可 - ```js - export default { - async fetch(request) { - const url = new URL(request.url) - url.hostname = "api.steampowered.com" - const newRequest = new Request(url, request) - return await fetch(newRequest) + ```js + export default { + async fetch(request) { + const url = new URL(request.url) + const path = decodeURIComponent(url.pathname.replace('/','')) + if (!path || !path.startsWith('http')) { + return new Response('Ciallo~(∠・ω< )⌒☆'); + } + const target = new URL(path) + url.hostname = path.replace(/https?:\/\//,'') + url.protocol = target.protocol + url.pathname = target.pathname + return await fetch(new Request(url, request)) + } } - } - ``` -5. 依次点击`左上角第3步填写的名字`, `设置`, `域和路由`右边的`添加`, `自定义域`, 然后填入你想设置的二级或多级域名, 比如`api.steam.example.com`, 然后点`添加域` -6. 测试(可选): 浏览器访问`https://api.steam.example.com/ISteamWebAPIUtil/GetServerInfo/v1/`, `api.steam.example.com`替换成第5步设置的域名, 如果能看到`servertime`字段, 说明配置成功 -7. 对你的Bot发送`#steam设置api反代https://api.steam.example.com`, 域名替换成第5步设置的域名 -8. store反代重复步骤2-6,把api换成store即可 + ``` +5. 依次点击`左上角第3步填写的名字`, `设置`, `域和路由`右边的`添加`, `自定义域`, 然后填入你想设置的二级或多级域名, 比如`steam.example.com`, 然后点`添加域` +6. 测试(可选): 浏览器访问`https://steam.example.com/https://api.steampowered.com/ISteamWebAPIUtil/GetServerInfo/v1/`, `steam.example.com`替换成第5步设置的域名, 如果能看到`servertime`字段, 说明配置成功 +7. 对你的Bot发送`#steam设置通用反代https://steam.example.com/{{url}}`, 域名替换成第5步设置的域名 ### 注意事项 diff --git a/components/Version.js b/components/Version.js index b14d131..e638e6e 100644 --- a/components/Version.js +++ b/components/Version.js @@ -23,7 +23,7 @@ const BotName = (() => { if (/^karin/i.test(pluginName)) { return 'Karin' } else if (BotPackage.dependencies.react) { - fs.rmdirSync(pluginPath, { recursive: true }) + fs.rmSync(pluginPath, { recursive: true }) return 'Yunzai-Next' } else if (Array.isArray(global.Bot?.uin)) { return 'Trss-Yunzai' diff --git a/models/utils/index.js b/models/utils/index.js index 8046dce..ef19cd9 100644 --- a/models/utils/index.js +++ b/models/utils/index.js @@ -10,7 +10,7 @@ export { request } const tempDir = join(Version.pluginPath, 'temp') if (fs.existsSync(tempDir)) { - fs.rmdirSync(tempDir, { recursive: true }) + fs.rmSync(tempDir, { recursive: true }) } fs.mkdirSync(tempDir) @@ -220,17 +220,21 @@ export function getStaticUrl (path) { */ export async function getImgUrlBuffer (url, retry = 3) { if (!url) return null + const path = new URL(url) retry = Number(retry) || 3 for (let i = 0; i < retry; i++) { try { - const buffer = await request.get(url, { responseType: 'arraybuffer', baseURL: '' }).then(res => res.data) + const buffer = await request.get(path.pathname, { + responseType: 'arraybuffer', + baseURL: path.origin + }).then(res => res.data) if (Version.BotName === 'Karin') { return `base64://${buffer.toString('base64')}` } else { return buffer } } catch (error) { - logger.error(`获取图片${url}失败: ${error.message}, 第${i + 1}次重试`) + logger.error(`获取图片${url}失败, 第${i + 1}次重试\n`, error.message) } } return null @@ -244,11 +248,15 @@ export async function getImgUrlBuffer (url, retry = 3) { */ export async function saveImg (url, retry = 3) { if (!url) return '' + const path = new URL(url) retry = Number(retry) || 3 for (let i = 0; i < retry; i++) { try { let ext = '' - const buffer = await request.get(url, { responseType: 'arraybuffer', baseURL: '' }).then(res => { + const buffer = await request.get(path.pathname, { + responseType: 'arraybuffer', + baseURL: path.origin + }).then(res => { ext = res.headers['content-type']?.split('/')?.pop() || 'png' return res.data }) @@ -260,7 +268,7 @@ export async function saveImg (url, retry = 3) { }, 1000 * 60 * 10) // 10分钟后删除 return filepath.replace(/\\/g, '/') } catch (error) { - logger.error(`保存图片${url}失败: ${error.message}, 第${i + 1}次重试`) + logger.error(`保存图片${url}失败, 第${i + 1}次重试\n${error.message}`) } } return '' diff --git a/models/utils/request.js b/models/utils/request.js index c9ceb49..b431944 100644 --- a/models/utils/request.js +++ b/models/utils/request.js @@ -20,7 +20,12 @@ export default async function request (url, options = {}) { return url } })() - const baseURL = options.baseURL?.replace(/\/$/, '') ?? steamApi + const baseURL = (() => { + if (options.baseURL && options.baseURL.includes('steam') && Config.steam.commonProxy) { + return Config.steam.commonProxy.replace('{{url}}', options.baseURL) + } + return options.baseURL ?? steamApi + })() return await axios.request({ url, baseURL,