50 lines
1.8 KiB
TypeScript
50 lines
1.8 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { authOptions } from '@/lib/auth'
|
|
import { featureFlags } from '@/lib/config'
|
|
import { canUseIntranetPenetration } from '@/lib/license'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { isCurrentSuperAdmin } from '@/lib/admin-permissions'
|
|
import { proxyIntranetModule } from '@/lib/tunnel/intranet-module'
|
|
|
|
async function ensureTunnelEnabled() {
|
|
if (featureFlags.tunnelEnabled) return true
|
|
const settings = await prisma.systemSettings.findFirst({
|
|
select: { enableTunnelService: true },
|
|
})
|
|
return !!settings?.enableTunnelService
|
|
}
|
|
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
if (!session) return NextResponse.json({ error: '未登录' }, { status: 401 })
|
|
|
|
if ((session.user as any)?.role !== 'ADMIN' || !(await isCurrentSuperAdmin(session))) {
|
|
return NextResponse.json({ error: '仅最高权限账号可操作内网穿透' }, { status: 403 })
|
|
}
|
|
|
|
const enabled = await ensureTunnelEnabled()
|
|
if (!enabled) {
|
|
return NextResponse.json({ error: '内网穿透未启用' }, { status: 501 })
|
|
}
|
|
|
|
if (!(await canUseIntranetPenetration())) {
|
|
return NextResponse.json({ error: '当前许可证不包含内网穿透功能,请联系管理员处理许可证' }, { status: 403 })
|
|
}
|
|
|
|
const moduleResponse = await proxyIntranetModule('start')
|
|
if (moduleResponse) return moduleResponse
|
|
|
|
return NextResponse.json(
|
|
{ error: '内网穿透服务尚未接入真实提供商,不能启动占位隧道' },
|
|
{ status: 501 }
|
|
)
|
|
} catch (e) {
|
|
console.error('Tunnel start error:', e)
|
|
return NextResponse.json({ error: '服务异常' }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
|