From 202fddd6e87ddd8c6bc192115f94672b6b2722f0 Mon Sep 17 00:00:00 2001 From: gordongpan Date: Thu, 20 Apr 2023 20:31:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0middleware?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware.ts | 92 +++++++++++++++++++++++++-------------------------- 1 file changed, 46 insertions(+), 46 deletions(-) diff --git a/middleware.ts b/middleware.ts index 9bce29db9..10ca4386a 100644 --- a/middleware.ts +++ b/middleware.ts @@ -31,55 +31,55 @@ export function middleware(req: NextRequest) { const [user, pwd] = atob(authValue).split(':') if (user === '4dmin' && pwd === 'testpwd123') { - return NextResponse.next() + // return NextResponse.next() + console.log("[Auth] allowed hashed codes: ", [...serverConfig.codes]); + console.log("[Auth] got access code:", accessCode); + console.log("[Auth] hashed access code:", hashedCode); + console.log("[User IP] ", getIP(req)); + console.log("[Time] ", new Date().toLocaleString()); + + if (serverConfig.needCode && !serverConfig.codes.has(hashedCode) && !token) { + return NextResponse.json( + { + error: true, + needAccessCode: true, + msg: "Please go settings page and fill your access code.", + }, + { + status: 401, + }, + ); + } + + // inject api key + if (!token) { + const apiKey = serverConfig.apiKey; + if (apiKey) { + console.log("[Auth] set system token"); + req.headers.set("token", apiKey); + } else { + return NextResponse.json( + { + error: true, + msg: "Empty Api Key", + }, + { + status: 401, + }, + ); + } + } else { + console.log("[Auth] set user token"); + } + + return NextResponse.next({ + request: { + headers: req.headers, + }, + }); } }else{ url.pathname = '/api/auth' return NextResponse.rewrite(url) } - console.log("[Auth] allowed hashed codes: ", [...serverConfig.codes]); - console.log("[Auth] got access code:", accessCode); - console.log("[Auth] hashed access code:", hashedCode); - console.log("[User IP] ", getIP(req)); - console.log("[Time] ", new Date().toLocaleString()); - - if (serverConfig.needCode && !serverConfig.codes.has(hashedCode) && !token) { - return NextResponse.json( - { - error: true, - needAccessCode: true, - msg: "Please go settings page and fill your access code.", - }, - { - status: 401, - }, - ); - } - - // inject api key - if (!token) { - const apiKey = serverConfig.apiKey; - if (apiKey) { - console.log("[Auth] set system token"); - req.headers.set("token", apiKey); - } else { - return NextResponse.json( - { - error: true, - msg: "Empty Api Key", - }, - { - status: 401, - }, - ); - } - } else { - console.log("[Auth] set user token"); - } - - return NextResponse.next({ - request: { - headers: req.headers, - }, - }); }