mirror of
https://github.com/ikechan8370/chatgpt-plugin.git
synced 2025-12-18 06:17:06 +00:00
添加锅巴代理接口
This commit is contained in:
parent
f5a31f7601
commit
fd735c1daf
3 changed files with 64 additions and 36 deletions
|
|
@ -16,6 +16,7 @@ import { getPublicIP, getUserData, getMasterQQ, randomString, getUin } from '../
|
||||||
import webRoute from './modules/web_route.js'
|
import webRoute from './modules/web_route.js'
|
||||||
import webUser from './modules/user.js'
|
import webUser from './modules/user.js'
|
||||||
import webPrompt from './modules/prompts.js'
|
import webPrompt from './modules/prompts.js'
|
||||||
|
import Guoba from './modules/guoba.js'
|
||||||
import SettingView from './modules/setting_view.js'
|
import SettingView from './modules/setting_view.js'
|
||||||
|
|
||||||
const __dirname = path.resolve()
|
const __dirname = path.resolve()
|
||||||
|
|
@ -23,22 +24,6 @@ const server = fastify({
|
||||||
logger: Config.debug
|
logger: Config.debug
|
||||||
})
|
})
|
||||||
|
|
||||||
async function getLoad() {
|
|
||||||
// 获取当前操作系统平台
|
|
||||||
const platform = os.platform()
|
|
||||||
// 判断平台是Linux还是Windows
|
|
||||||
if (platform === 'linux') {
|
|
||||||
// 如果是Linux,使用os.loadavg()方法获取负载平均值
|
|
||||||
const loadAvg = os.loadavg()
|
|
||||||
return loadAvg[0] * 100
|
|
||||||
} else if (platform === 'win32') {
|
|
||||||
// 如果是Windows不获取性能
|
|
||||||
return 0
|
|
||||||
} else {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function setUserData(qq, data) {
|
async function setUserData(qq, data) {
|
||||||
const dir = 'resources/ChatGPTCache/user'
|
const dir = 'resources/ChatGPTCache/user'
|
||||||
const filename = `${qq}.json`
|
const filename = `${qq}.json`
|
||||||
|
|
@ -64,6 +49,7 @@ await server.register(webRoute)
|
||||||
await server.register(webUser)
|
await server.register(webUser)
|
||||||
await server.register(SettingView)
|
await server.register(SettingView)
|
||||||
await server.register(webPrompt)
|
await server.register(webPrompt)
|
||||||
|
await server.register(Guoba)
|
||||||
|
|
||||||
// 无法访问端口的情况下创建与media的通讯
|
// 无法访问端口的情况下创建与media的通讯
|
||||||
async function mediaLink() {
|
async function mediaLink() {
|
||||||
|
|
|
||||||
62
server/modules/guoba.js
Normal file
62
server/modules/guoba.js
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
import { UserInfo } from './user_data.js'
|
||||||
|
|
||||||
|
async function Guoba(fastify, options) {
|
||||||
|
// 获取锅巴登陆链接
|
||||||
|
fastify.post('/guobaLogin', async (request, reply) => {
|
||||||
|
const token = request.cookies.token || request.body?.token || 'unknown'
|
||||||
|
let user = UserInfo(token)
|
||||||
|
if (user && user.autho == 'admin') {
|
||||||
|
try {
|
||||||
|
let { LoginService } = await import('../../../Guoba-Plugin/server/service/both/LoginService.js')
|
||||||
|
const guobaLoginService = new LoginService()
|
||||||
|
const guobaAPI = await guobaLoginService.setQuickLogin(user.user)
|
||||||
|
reply.send({ guoba: guobaAPI })
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.error(err)
|
||||||
|
reply.send({ state: false, error: err })
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
reply.send({ state: false, error: '用户权限不足' })
|
||||||
|
}
|
||||||
|
return reply
|
||||||
|
})
|
||||||
|
// 代理锅巴接口
|
||||||
|
fastify.post('/guobaApi', async (request, reply) => {
|
||||||
|
const body = request.body || {}
|
||||||
|
const token = request.cookies.token || request.body?.token || 'unknown'
|
||||||
|
let user = UserInfo(token)
|
||||||
|
if (user && user.autho == 'admin' && body.guobaToken) {
|
||||||
|
try {
|
||||||
|
let { LoginService } = await import('../../../Guoba-Plugin/server/service/both/LoginService.js')
|
||||||
|
const guobaLoginService = new LoginService()
|
||||||
|
const { custom, local, remote } = await guobaLoginService.setQuickLogin(user.user)
|
||||||
|
if (local.length > 0) {
|
||||||
|
const guobaOptions = {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Guoba-Access-Token': body.guobaToken
|
||||||
|
},
|
||||||
|
body: body.data
|
||||||
|
}
|
||||||
|
const response = await fetch(`${local[0]}/${body.path}`, guobaOptions)
|
||||||
|
if (response.ok) {
|
||||||
|
const json = await response.json()
|
||||||
|
reply.send(json)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
reply.send({ state: false, error: '锅巴接口异常' })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
console.error(err)
|
||||||
|
reply.send({ state: false, error: err })
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
reply.send({ state: false, error: '用户权限不足' })
|
||||||
|
}
|
||||||
|
return reply
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Guoba
|
||||||
|
|
@ -88,26 +88,6 @@ async function User(fastify, options) {
|
||||||
})
|
})
|
||||||
return reply
|
return reply
|
||||||
})
|
})
|
||||||
// 获取锅巴登陆链接
|
|
||||||
fastify.post('/guobaLogin', async (request, reply) => {
|
|
||||||
const token = request.cookies.token || request.body?.token || 'unknown'
|
|
||||||
let user = UserInfo(token)
|
|
||||||
if (user && user.autho == 'admin') {
|
|
||||||
try {
|
|
||||||
let { LoginService } = await import('../../../Guoba-Plugin/server/service/both/LoginService.js')
|
|
||||||
const guobaLoginService = new LoginService()
|
|
||||||
const guobaAPI = await guobaLoginService.setQuickLogin(user.user)
|
|
||||||
reply.send({ guoba: guobaAPI })
|
|
||||||
}
|
|
||||||
catch (err) {
|
|
||||||
console.error(err)
|
|
||||||
reply.send({ state: false, error: err })
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
reply.send({ state: false, error: '用户权限不足' })
|
|
||||||
}
|
|
||||||
return reply
|
|
||||||
})
|
|
||||||
// 获取用户数据
|
// 获取用户数据
|
||||||
fastify.post('/userData', async (request, reply) => {
|
fastify.post('/userData', async (request, reply) => {
|
||||||
const token = request.cookies.token || request.body?.token || 'unknown'
|
const token = request.cookies.token || request.body?.token || 'unknown'
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue