定时任务增加鉴权

This commit is contained in:
mtvpls
2026-01-01 22:38:42 +08:00
parent 97d748fa79
commit 70cb6f6589
4 changed files with 17 additions and 4 deletions

View File

@@ -11,8 +11,20 @@ import { SearchResult } from '@/lib/types';
export const runtime = 'nodejs';
export async function GET(request: NextRequest) {
export async function GET(
request: NextRequest,
{ params }: { params: { password: string } }
) {
console.log(request.url);
const cronPassword = process.env.CRON_PASSWORD || 'mtvpls';
if (params.password !== cronPassword) {
return NextResponse.json(
{ success: false, message: 'Unauthorized' },
{ status: 401 }
);
}
try {
console.log('Cron job triggered:', new Date().toISOString());

View File

@@ -133,6 +133,6 @@ function shouldSkipAuth(pathname: string): boolean {
// 配置middleware匹配规则
export const config = {
matcher: [
'/((?!_next/static|_next/image|favicon.ico|login|register|oidc-register|warning|api/login|api/register|api/logout|api/auth/oidc|api/cron|api/server-config|api/proxy-m3u8|api/cms-proxy|api/tvbox/subscribe|api/theme/css|api/openlist/cms-proxy|api/openlist/play).*)',
'/((?!_next/static|_next/image|favicon.ico|login|register|oidc-register|warning|api/login|api/register|api/logout|api/auth/oidc|api/cron/|api/server-config|api/proxy-m3u8|api/cms-proxy|api/tvbox/subscribe|api/theme/css|api/openlist/cms-proxy|api/openlist/play).*)',
],
};

View File

@@ -58,8 +58,9 @@ const intervalId = setInterval(() => {
// 执行 cron 任务的函数
function executeCronJob() {
const cronPassword = process.env.CRON_PASSWORD || 'mtvpls';
const cronUrl = `http://${process.env.HOSTNAME || 'localhost'}:${process.env.PORT || 3000
}/api/cron`;
}/api/cron/${cronPassword}`;
console.log(`Executing cron job: ${cronUrl}`);

View File

@@ -12,7 +12,7 @@
],
"crons": [
{
"path": "/api/cron",
"path": "/api/cron/mtvpls",
"schedule": "0 1 * * *"
}
]