mirror of
https://github.com/ChenQihan666/Lolia-Nodes-Status-Pages.git
synced 2026-08-14 07:52:34 +08:00
Initial commit
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
import type { NextRequest } from 'next/server'
|
||||
import { workerConfig } from './uptime.config'
|
||||
|
||||
export async function middleware(request: NextRequest) {
|
||||
const passwordProtection = workerConfig.passwordProtection
|
||||
if (passwordProtection) {
|
||||
const authHeader = request.headers.get('Authorization')
|
||||
let authenticated = false
|
||||
const expected = 'Basic ' + btoa(passwordProtection)
|
||||
|
||||
if (authHeader && authHeader.length === expected.length) {
|
||||
// a simple timing-safe compare
|
||||
authenticated = true
|
||||
for (let i = 0; i < authHeader.length; i++) {
|
||||
if (authHeader[i] !== expected[i]) authenticated = false
|
||||
}
|
||||
}
|
||||
|
||||
if (!authenticated) {
|
||||
return NextResponse.json(
|
||||
{ code: 401, message: 'Not authenticated' },
|
||||
{ status: 401, headers: { 'WWW-Authenticate': 'Basic' } }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user