@@ -2,11 +2,13 @@ import express from 'express'
22import jwt from 'jsonwebtoken'
33import { ObjectId } from 'mongodb'
44import type { ChatContext , ChatMessage } from './chatgpt'
5- import { chatConfig , chatReplyProcess , currentModel } from './chatgpt'
5+ import { chatConfig , chatReplyProcess , currentModel , initApi } from './chatgpt'
66import { auth } from './middleware/auth'
7- import type { ChatOptions , UserInfo } from './storage/model'
7+ import { clearConfigCache , getCacheConfig , getOriginConfig } from './storage/config'
8+ import type { ChatOptions , Config , MailConfig , SiteConfig , UserInfo } from './storage/model'
89import { Status } from './storage/model'
9- import { clearChat , createChatRoom , createUser , deleteChat , deleteChatRoom , existsChatRoom , getChat , getChatRooms , getChats , getUser , getUserById , insertChat , renameChatRoom , updateChat , updateUserInfo , verifyUser } from './storage/mongo'
10+ import { clearChat , createChatRoom , createUser , deleteChat , deleteChatRoom , existsChatRoom , getChat , getChatRooms , getChats , getUser , getUserById , insertChat , renameChatRoom , updateChat , updateConfig , updateUserInfo , verifyUser } from './storage/mongo'
11+ import { isNotEmptyString } from './utils/is'
1012import { sendMail } from './utils/mail'
1113import { checkUserVerify , getUserVerifyUrl , md5 } from './utils/security'
1214
@@ -175,14 +177,14 @@ router.post('/chat-process', auth, async (req, res) => {
175177
176178router . post ( '/user-register' , async ( req , res ) => {
177179 const { username, password } = req . body as { username : string ; password : string }
178-
179- if ( process . env . REGISTER_ENABLED !== 'true' ) {
180+ const config = await getCacheConfig ( )
181+ if ( config . siteConfig . registerEnabled ) {
180182 res . send ( { status : 'Fail' , message : '注册账号功能未启用 | Register account is disabled!' , data : null } )
181183 return
182184 }
183- if ( typeof process . env . REGISTER_MAILS === 'string' && process . env . REGISTER_MAILS . length > 0 ) {
185+ if ( isNotEmptyString ( config . siteConfig . registerMails ) ) {
184186 let allowSuffix = false
185- const emailSuffixs = process . env . REGISTER_MAILS . split ( ',' )
187+ const emailSuffixs = config . siteConfig . registerMails . split ( ',' )
186188 for ( let index = 0 ; index < emailSuffixs . length ; index ++ ) {
187189 const element = emailSuffixs [ index ]
188190 allowSuffix = username . toLowerCase ( ) . endsWith ( element )
@@ -207,13 +209,19 @@ router.post('/user-register', async (req, res) => {
207209 res . send ( { status : 'Success' , message : '注册成功 | Register success' , data : null } )
208210 }
209211 else {
210- sendMail ( username , getUserVerifyUrl ( username ) )
212+ await sendMail ( username , await getUserVerifyUrl ( username ) )
211213 res . send ( { status : 'Success' , message : '注册成功, 去邮箱中验证吧 | Registration is successful, you need to go to email verification' , data : null } )
212214 }
213215} )
214216
215- router . post ( '/config' , async ( req , res ) => {
217+ router . post ( '/config' , auth , async ( req , res ) => {
216218 try {
219+ const userId = new ObjectId ( req . headers . userId . toString ( ) )
220+
221+ const user = await getUserById ( userId )
222+ if ( user == null || user . status !== Status . Normal || user . email . toLowerCase ( ) !== process . env . ROOT_USER )
223+ throw new Error ( '无权限 | No permission.' )
224+
217225 const response = await chatConfig ( )
218226 res . send ( response )
219227 }
@@ -226,7 +234,7 @@ router.post('/session', async (req, res) => {
226234 try {
227235 const AUTH_SECRET_KEY = process . env . AUTH_SECRET_KEY
228236 const hasAuth = typeof AUTH_SECRET_KEY === 'string' && AUTH_SECRET_KEY . length > 0
229- const allowRegister = process . env . REGISTER_ENABLED === 'true'
237+ const allowRegister = ( await getCacheConfig ( ) ) . siteConfig . registerEnabled
230238 res . send ( { status : 'Success' , message : '' , data : { auth : hasAuth , allowRegister, model : currentModel ( ) } } )
231239 }
232240 catch ( error ) {
@@ -293,6 +301,77 @@ router.post('/verify', async (req, res) => {
293301 }
294302} )
295303
304+ router . post ( '/setting-base' , auth , async ( req , res ) => {
305+ try {
306+ const { apiKey, apiModel, apiBaseUrl, accessToken, timeoutMs, socksProxy, httpsProxy } = req . body as Config
307+ const userId = new ObjectId ( req . headers . userId . toString ( ) )
308+
309+ if ( apiKey == null && accessToken == null )
310+ throw new Error ( 'Missing OPENAI_API_KEY or OPENAI_ACCESS_TOKEN environment variable.' )
311+
312+ const user = await getUserById ( userId )
313+ if ( user == null || user . status !== Status . Normal || user . email . toLowerCase ( ) !== process . env . ROOT_USER )
314+ throw new Error ( '无权限 | No permission.' )
315+
316+ const thisConfig = await getOriginConfig ( )
317+ thisConfig . apiKey = apiKey
318+ thisConfig . apiModel = apiModel
319+ thisConfig . apiBaseUrl = apiBaseUrl
320+ thisConfig . accessToken = accessToken
321+ thisConfig . timeoutMs = timeoutMs
322+ thisConfig . socksProxy = socksProxy
323+ thisConfig . httpsProxy = httpsProxy
324+ await updateConfig ( thisConfig )
325+ clearConfigCache ( )
326+ initApi ( )
327+ const response = await chatConfig ( )
328+ res . send ( { status : 'Success' , message : '操作成功 | Successfully' , data : response . data } )
329+ }
330+ catch ( error ) {
331+ res . send ( { status : 'Fail' , message : error . message , data : null } )
332+ }
333+ } )
334+
335+ router . post ( '/setting-site' , auth , async ( req , res ) => {
336+ try {
337+ const config = req . body as SiteConfig
338+ const userId = new ObjectId ( req . headers . userId . toString ( ) )
339+
340+ const user = await getUserById ( userId )
341+ if ( user == null || user . status !== Status . Normal || user . email . toLowerCase ( ) !== process . env . ROOT_USER )
342+ throw new Error ( '无权限 | No permission.' )
343+
344+ const thisConfig = await getOriginConfig ( )
345+ thisConfig . siteConfig = config
346+ const result = await updateConfig ( thisConfig )
347+ clearConfigCache ( )
348+ res . send ( { status : 'Success' , message : '操作成功 | Successfully' , data : result . siteConfig } )
349+ }
350+ catch ( error ) {
351+ res . send ( { status : 'Fail' , message : error . message , data : null } )
352+ }
353+ } )
354+
355+ router . post ( '/setting-mail' , auth , async ( req , res ) => {
356+ try {
357+ const config = req . body as MailConfig
358+ const userId = new ObjectId ( req . headers . userId . toString ( ) )
359+
360+ const user = await getUserById ( userId )
361+ if ( user == null || user . status !== Status . Normal || user . email . toLowerCase ( ) !== process . env . ROOT_USER )
362+ throw new Error ( '无权限 | No permission.' )
363+
364+ const thisConfig = await getOriginConfig ( )
365+ thisConfig . mailConfig = config
366+ const result = await updateConfig ( thisConfig )
367+ clearConfigCache ( )
368+ res . send ( { status : 'Success' , message : '操作成功 | Successfully' , data : result . mailConfig } )
369+ }
370+ catch ( error ) {
371+ res . send ( { status : 'Fail' , message : error . message , data : null } )
372+ }
373+ } )
374+
296375app . use ( '' , router )
297376app . use ( '/api' , router )
298377
0 commit comments