后台适配Trss

This commit is contained in:
zyc404 2023-10-08 18:16:50 +08:00
parent 8aec804179
commit 880edace8a
5 changed files with 31 additions and 15 deletions

View file

@ -122,6 +122,12 @@
"label": "额外工具url",
"placeholder": "测试期间提供一个公益接口,一段时间后撤掉",
"data": "extraUrl"
},
{
"type": "text",
"label": "Trss主账号",
"placeholder": "用于Trss配置脚本时使用的主账号",
"data": "trssBotUin"
}
]
},

View file

@ -12,7 +12,7 @@ import websocketclient from 'ws'
import { Config } from '../utils/config.js'
import { UserInfo, GetUser, AddUser } from './modules/user_data.js'
import { getPublicIP, getUserData, getMasterQQ, randomString } from '../utils/common.js'
import { getPublicIP, getUserData, getMasterQQ, randomString, getUin } from '../utils/common.js'
import webRoute from './modules/web_route.js'
import webUser from './modules/user.js'
@ -107,7 +107,7 @@ async function mediaLink() {
ws.on('open', () => {
ws.send(JSON.stringify({
command: 'register',
region: Bot.uin,
region: getUin(),
type: 'server',
}))
})
@ -128,30 +128,30 @@ async function mediaLink() {
const user = UserInfo(data.token)
if (user) {
ws.login = true
ws.send(JSON.stringify({ command: data.command, state: true, region: Bot.uin, type: 'server' }))
ws.send(JSON.stringify({ command: data.command, state: true, region: getUin(), type: 'server' }))
} else {
ws.send(JSON.stringify({ command: data.command, state: false, error: '权限验证失败', region: Bot.uin, type: 'server' }))
ws.send(JSON.stringify({ command: data.command, state: false, error: '权限验证失败', region: getUin(), type: 'server' }))
}
}
break
case 'post_login':
if (data.qq && data.passwd) {
const token = randomString(32)
if (data.qq == Bot.uin && await redis.get('CHATGPT:ADMIN_PASSWD') == data.passwd) {
if (data.qq == getUin() && await redis.get('CHATGPT:ADMIN_PASSWD') == data.passwd) {
AddUser({ user: data.qq, token: token, autho: 'admin' })
ws.send(JSON.stringify({ command: data.command, state: true, autho: 'admin', token: token, region: Bot.uin, type: 'server' }))
ws.send(JSON.stringify({ command: data.command, state: true, autho: 'admin', token: token, region: getUin(), type: 'server' }))
} else {
const user = await getUserData(data.qq)
if (user.passwd != '' && user.passwd === data.passwd) {
AddUser({ user: data.qq, token: token, autho: 'user' })
ws.send(JSON.stringify({ command: data.command, state: true, autho: 'user', token: token, region: Bot.uin, type: 'server' }))
ws.send(JSON.stringify({ command: data.command, state: true, autho: 'user', token: token, region: getUin(), type: 'server' }))
} else {
ws.send(JSON.stringify({ command: data.command, state: false, error: `用户名密码错误,如果忘记密码请私聊机器人输入 ${data.qq == Bot.uin ? '#修改管理密码' : '#修改用户密码'} 进行修改`, region: Bot.uin, type: 'server' }))
ws.send(JSON.stringify({ command: data.command, state: false, error: `用户名密码错误,如果忘记密码请私聊机器人输入 ${data.qq == getUin() ? '#修改管理密码' : '#修改用户密码'} 进行修改`, region: getUin(), type: 'server' }))
}
}
} else {
ws.send(JSON.stringify({ command: data.command, state: false, error: '未输入用户名或密码', region: Bot.uin, type: 'server' }))
ws.send(JSON.stringify({ command: data.command, state: false, error: '未输入用户名或密码', region: getUin(), type: 'server' }))
}
break
case 'post_command':
@ -163,7 +163,7 @@ async function mediaLink() {
const response = await fetch(`http://localhost:${Config.serverPort || 3321}${data.postPath}`, fetchOptions)
if (response.ok) {
const json = await response.json()
ws.send(JSON.stringify({ command: data.command, state: true, region: Bot.uin, type: 'server', path: data.postPath, data: json }))
ws.send(JSON.stringify({ command: data.command, state: true, region: getUin(), type: 'server', path: data.postPath, data: json }))
}
break
}

View file

@ -1,5 +1,5 @@
import { UserInfo, AddUser } from './user_data.js'
import { randomString, getUserData, getMasterQQ } from '../../utils/common.js'
import { randomString, getUserData, getMasterQQ, getUin } from '../../utils/common.js'
import fs from 'fs'
async function User(fastify, options) {
@ -8,7 +8,7 @@ async function User(fastify, options) {
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) {
if (body.qq == getUin() && 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 })
@ -19,16 +19,16 @@ async function User(fastify, options) {
reply.setCookie('token', token, { path: '/' })
reply.send({ login: true, autho: 'user', token: token })
} else {
reply.send({ login: false, err: `用户名密码错误,如果忘记密码请私聊机器人输入 ${body.qq == Bot.uin ? '#修改管理密码' : '#修改用户密码'} 进行修改` })
reply.send({ login: false, err: `用户名密码错误,如果忘记密码请私聊机器人输入 ${body.qq == getUin() ? '#修改管理密码' : '#修改用户密码'} 进行修改` })
}
}
} else if (body.otp) {
const token = randomString(32)
const opt = await redis.get(`CHATGPT:SERVER_QUICK`)
if (opt && body.otp == opt) {
AddUser({ user: Bot.uin, token: token, autho: 'admin' })
AddUser({ user: getUin(), token: token, autho: 'admin' })
reply.setCookie('token', token, { path: '/' })
reply.send({ login: true, autho: 'admin', token: token, user: Bot.uin })
reply.send({ login: true, autho: 'admin', token: token, user: getUin() })
} else {
reply.send({ login: false, err: `快捷登录代码错误,请检查后重试` })
}

View file

@ -821,6 +821,14 @@ export function getMaxModelTokens (model = 'gpt-3.5-turbo') {
}
}
export function getUin (e) {
if (e?.bot?.uin) return e.bot.uin
if (Array.isArray(Bot.uin)) {
if (Config.trssBotUin && Bot.uin.indexOf(Config.trssBotUin) > -1) return Config.trssBotUin
else return Bot.uin[0]
} else return Bot.uin
}
/**
* 生成当前语音模式下可发送的音频信息
* @param e - 上下文对象

View file

@ -151,6 +151,8 @@ const defaultConfig = {
claudeAITimeout: 120,
claudeAIJA3: '772,4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,27-5-65281-13-35-0-51-18-16-43-10-45-11-17513-23,29-23-24,0',
claudeAIUA: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
// trss配置
trssBotUin: '',
version: 'v2.7.5'
}
const _path = process.cwd()