@@ -2,11 +2,13 @@ import express from 'express'
2
2
import jwt from 'jsonwebtoken'
3
3
import { ObjectId } from 'mongodb'
4
4
import type { ChatContext , ChatMessage } from './chatgpt'
5
- import { chatConfig , chatReplyProcess , currentModel } from './chatgpt'
5
+ import { chatConfig , chatReplyProcess , currentModel , initApi } from './chatgpt'
6
6
import { 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'
8
9
import { 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'
10
12
import { sendMail } from './utils/mail'
11
13
import { checkUserVerify , getUserVerifyUrl , md5 } from './utils/security'
12
14
@@ -175,14 +177,14 @@ router.post('/chat-process', auth, async (req, res) => {
175
177
176
178
router . post ( '/user-register' , async ( req , res ) => {
177
179
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 ) {
180
182
res . send ( { status : 'Fail' , message : '注册账号功能未启用 | Register account is disabled!' , data : null } )
181
183
return
182
184
}
183
- if ( typeof process . env . REGISTER_MAILS === 'string' && process . env . REGISTER_MAILS . length > 0 ) {
185
+ if ( isNotEmptyString ( config . siteConfig . registerMails ) ) {
184
186
let allowSuffix = false
185
- const emailSuffixs = process . env . REGISTER_MAILS . split ( ',' )
187
+ const emailSuffixs = config . siteConfig . registerMails . split ( ',' )
186
188
for ( let index = 0 ; index < emailSuffixs . length ; index ++ ) {
187
189
const element = emailSuffixs [ index ]
188
190
allowSuffix = username . toLowerCase ( ) . endsWith ( element )
@@ -207,13 +209,19 @@ router.post('/user-register', async (req, res) => {
207
209
res . send ( { status : 'Success' , message : '注册成功 | Register success' , data : null } )
208
210
}
209
211
else {
210
- sendMail ( username , getUserVerifyUrl ( username ) )
212
+ await sendMail ( username , await getUserVerifyUrl ( username ) )
211
213
res . send ( { status : 'Success' , message : '注册成功, 去邮箱中验证吧 | Registration is successful, you need to go to email verification' , data : null } )
212
214
}
213
215
} )
214
216
215
- router . post ( '/config' , async ( req , res ) => {
217
+ router . post ( '/config' , auth , async ( req , res ) => {
216
218
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
+
217
225
const response = await chatConfig ( )
218
226
res . send ( response )
219
227
}
@@ -226,7 +234,7 @@ router.post('/session', async (req, res) => {
226
234
try {
227
235
const AUTH_SECRET_KEY = process . env . AUTH_SECRET_KEY
228
236
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
230
238
res . send ( { status : 'Success' , message : '' , data : { auth : hasAuth , allowRegister, model : currentModel ( ) } } )
231
239
}
232
240
catch ( error ) {
@@ -293,6 +301,77 @@ router.post('/verify', async (req, res) => {
293
301
}
294
302
} )
295
303
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
+
296
375
app . use ( '' , router )
297
376
app . use ( '/api' , router )
298
377
0 commit comments