feat: 新增ws接口,适配工具 (#482)

* 修复后台API反代地址未能正确显示的问题

* 更新渲染页面配置

* 添加个人聊天模式配置

* 将用户数据获取改到common中

* 修复错误的渲染页面参数

* 修复bug

* 添加Live2D

* 修复渲染页面错误

* 修复渲染传入值

* 更新渲染

* 修复图表渲染bug

* 调整live2d模型大小

* 修复live2d无法关闭问题

* 修复错误的传值

* 修复ai命名

* 更新渲染

* 添加用户独立设定

* 更新渲染配置适配个人设置

* 修复合并导致的渲染文件异常删除

* 修复用户数据缺失问题

* 修复旧版本数据缺失问题

* 修复bing参数不存在问题,兼容miao的截图

* 修复受限token重试时不被排除的问题

* 修复个人模式下结束对话的模式错误

* 更新渲染页面,将预览版转为正式版

* 修复传统渲染无法调用截图功能的问题

* 文字模式也进行一次缓存

* 更新README

* Update README.md

* 更新渲染

* 更新渲染页面

* 添加版本信息

* 遗漏参数

* 丢失引用

* 补充路由

* 添加云转码功能

* 判断node-silk是否正常合成

* 云转码提示

* 修复图片渲染出错

* 云转码支持发送Buffer

* 添加云转码模式支持

* 更新描述

* 更新后台渲染页面

* 更新配置

* 更新渲染页面

* 添加云渲染

* 修复错误的接口调用

* 修复遗漏的数据转换

* 修复获取的图片数据异常问题

* 更新后台配置

* 更新渲染页面

* 修复云渲染访问地址错误

* 更新渲染页面

* 修复遗漏的模型文件

* 修复live2d问题

* 更新live2d以及相关配置

* 修复遗漏的数据参数

* 修复新live2d情况下云渲染错误的问题

* 适配云渲染1.1.2等待参数

* 添加云服务api检查

* 更新渲染页面

* 添加live2d加载检测

* 修复错误的属性判断

* 添加云渲染DPR

* 更新sydney支持内容生成

* 修改文件模式语音转码接收模式

* 添加云转码时recordUrl检查

* 更新后台配置项

* 修复错误的文本描述

* 更新后台页面

* 添加全局对话模式设置,更新后台面板

* 添加第三方渲染服务适配

* 修复第三方服务器live2d加载导致的渲染失败问题

* 修复后台地址无法实时保存的问题

* 添加live2d模型透明度设置

* 合并更新

* 更新渲染页面

* 更新渲染页面

* 使dpr对本地渲染也生效

* 更新渲染页面

* 添加网页截图功能

* 添加后台配置项

* 添加配置导出和导入功能

* 运行通过参数传递用户token

* 登录时将token作为参数返回

* 修复错误

* 添加密码修改和用户删除接口

* 修正密码比对

* 修复user错误

* 优化数据保存时的返回值

* 添加系统额外服务检查api

* 添加AccessToken配置

* 修复错误的导入提示

* 添加ws连接

* 添加ws用户信息获取

* 修复错误的循环

* 修正ws参数

* 添加群消息获取权限

* 添加用户多端登录支持

* 修复错误的路径引用

* 修复错误的路径引用

* 修复错误

* 修复页面数据获取失败问题

---------

Co-authored-by: ikechan8370 <geyinchibuaa@gmail.com>
This commit is contained in:
HalcyonAlcedo 2023-06-25 00:58:41 +08:00 committed by GitHub
parent fbe8953667
commit a3fab7316f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 352 additions and 175 deletions

127
server/modules/user.js Normal file
View file

@ -0,0 +1,127 @@
import { UserInfo, AddUser } from './user_data.js'
import { randomString, getUserData } from '../../utils/common.js'
import fs from 'fs'
async function User(fastify, options) {
// 登录
fastify.post('/login', async (request, reply) => {
const body = request.body || {}
if (body.qq && body.passwd) {
const token = randomString(32)
if (body.qq == Bot.uin && await redis.get('CHATGPT:ADMIN_PASSWD') == body.passwd) {
AddUser({ user: body.qq, token: token, autho: 'admin' })
reply.setCookie('token', token, { path: '/' })
reply.send({ login: true, autho: 'admin', token: token })
} else {
const user = await getUserData(body.qq)
if (user.passwd != '' && user.passwd === body.passwd) {
AddUser({ user: body.qq, token: token, autho: 'user' })
reply.setCookie('token', token, { path: '/' })
reply.send({ login: true, autho: 'user', token: token })
} else {
reply.send({ login: false, err: `用户名密码错误,如果忘记密码请私聊机器人输入 ${body.qq == Bot.uin ? '#修改管理密码' : '#修改用户密码'} 进行修改` })
}
}
} else {
reply.send({ login: false, err: '未输入用户名或密码' })
}
return reply
})
// 检查用户是否存在
fastify.post('/verify', async (request, reply) => {
const token = request.cookies.token || request.body?.token || 'unknown'
const user = UserInfo(token)
if (!user || token === 'unknown') {
reply.send({
verify: false,
})
return
}
reply.send({
verify: true,
user: user.user,
autho: user.autho
})
return reply
})
// 获取用户数据
fastify.post('/userData', async (request, reply) => {
const token = request.cookies.token || request.body?.token || 'unknown'
let user = UserInfo(token)
if (!user) user = { user: '' }
const userData = await getUserData(user.user)
reply.send({
chat: userData.chat || [],
mode: userData.mode || '',
cast: userData.cast || {
api: '', //API设定
bing: '', //必应设定
bing_resource: '', //必应扩展资料
slack: '', //Slack设定
}
})
return reply
})
// 删除用户
fastify.post('/deleteUser', async (request, reply) => {
const token = request.cookies.token || request.body?.token || 'unknown'
const user = UserInfo(token)
if (!user || user === 'unknown') {
reply.send({ state: false, error: '无效token' })
return
}
const filepath = `resources/ChatGPTCache/user/${user.user}.json`
fs.unlinkSync(filepath)
reply.send({ state: true })
return reply
})
// 修改密码
fastify.post('/changePassword', async (request, reply) => {
const token = request.cookies.token || request.body?.token || 'unknown'
const user = UserInfo(token)
if (!user || user === 'unknown') {
reply.send({ state: false, error: '无效的用户信息' })
return
}
const userData = await getUserData(user.user)
const body = request.body || {}
if (!body.newPasswd) {
reply.send({ state: false, error: '无效参数' })
return
}
if (body.passwd && body.passwd != userData.passwd) {
reply.send({ state: false, error: '原始密码错误' })
return
}
if (user.autho === 'admin') {
await redis.set('CHATGPT:ADMIN_PASSWD', body.newPasswd)
} else if (user.autho === 'user') {
const dir = 'resources/ChatGPTCache/user'
const filename = `${user.user}.json`
const filepath = path.join(dir, filename)
fs.mkdirSync(dir, { recursive: true })
if (fs.existsSync(filepath)) {
fs.readFile(filepath, 'utf8', (err, data) => {
if (err) {
console.error(err)
return
}
const config = JSON.parse(data)
config.passwd = body.newPasswd
fs.writeFile(filepath, JSON.stringify(config), 'utf8', (err) => {
if (err) {
console.error(err)
}
})
})
} else {
reply.send({ state: false, error: '错误的用户数据' })
return
}
}
reply.send({ state: true })
return reply
})
}
export default User

View file

@ -0,0 +1,41 @@
let users = {
user: []
}
export const UserData = new Proxy(users, {
set(target, property, value) {
target[property] = value
return true
}
})
// 获取用户信息
export function UserInfo(token) {
const userData = users.user.find(user => user.token.includes(token))
if (userData) {
return {
user: userData.user,
autho: userData.autho,
label: userData.label
}
} else {
return undefined
}
}
// 获取用户数据
export function GetUser(user) {
return users.user.find(user => user === user)
}
// 添加用户token
export function AddUser(data) {
const userIndex = users.user.findIndex(user => user === data.user)
if (userIndex >= 0) {
users.user[userIndex].token.push(data.token)
} else {
users.user.push({
user: data.user,
autho: data.autho,
token: [data.token],
label: data.label || '',
tiem: new Date()
})
}
}

View file

@ -0,0 +1,58 @@
import { UserInfo } from './user_data.js'
import fs from 'fs'
async function routes(fastify, options) {
fastify.get('/page/*', async (request, reply) => {
const stream = fs.createReadStream('plugins/chatgpt-plugin/server/static/index.html')
reply.type('text/html').send(stream)
return reply
})
fastify.get('/help/*', async (request, reply) => {
const stream = fs.createReadStream('plugins/chatgpt-plugin/server/static/index.html')
reply.type('text/html').send(stream)
})
fastify.get('/version', async (request, reply) => {
const stream = fs.createReadStream('plugins/chatgpt-plugin/server/static/index.html')
reply.type('text/html').send(stream)
return reply
})
fastify.get('/auth/*', async (request, reply) => {
const stream = fs.createReadStream('plugins/chatgpt-plugin/server/static/index.html')
reply.type('text/html').send(stream)
})
fastify.get('/admin*', async (request, reply) => {
const token = request.cookies.token || request.body?.token || 'unknown'
const user = UserInfo(token)
if (!user) {
reply.redirect(301, '/auth/login')
}
const stream = fs.createReadStream('plugins/chatgpt-plugin/server/static/index.html')
reply.type('text/html').send(stream)
return reply
})
fastify.get('/admin/dashboard', async (request, reply) => {
const token = request.cookies.token || request.body?.token || 'unknown'
const user = UserInfo(token)
if (!user) {
reply.redirect(301, '/auth/login')
}
if (user.autho === 'admin') {
reply.redirect(301, '/admin/settings')
}
const stream = fs.createReadStream('plugins/chatgpt-plugin/server/static/index.html')
reply.type('text/html').send(stream)
return reply
})
fastify.get('/admin/settings', async (request, reply) => {
const token = request.cookies.token || request.body?.token || 'unknown'
const user = UserInfo(token)
if (!user || user.autho != 'admin') {
reply.redirect(301, '/admin/')
}
const stream = fs.createReadStream('plugins/chatgpt-plugin/server/static/index.html')
reply.type('text/html').send(stream)
return reply
})
}
export default routes