Skip to content

Commit

Permalink
perf: steam静态图走通用反代,更新反代教程
Browse files Browse the repository at this point in the history
  • Loading branch information
XasYer committed Dec 1, 2024
1 parent 6c48870 commit 8b6e8af
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 21 deletions.
33 changes: 19 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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步设置的域名

### 注意事项

Expand Down
2 changes: 1 addition & 1 deletion components/Version.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
18 changes: 13 additions & 5 deletions models/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand All @@ -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
})
Expand All @@ -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 ''
Expand Down
7 changes: 6 additions & 1 deletion models/utils/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8b6e8af

Please sign in to comment.