Initial commit

This commit is contained in:
monoadmin
2026-04-10 15:36:33 -07:00
commit d6d7338a39
134 changed files with 16232 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
import { db } from '@/lib/db'
import { machines, sessionCodes } from '@/lib/db/schema'
import { eq, and, isNotNull, gt } from 'drizzle-orm'
import { NextRequest, NextResponse } from 'next/server'
export async function POST(request: NextRequest) {
try {
const { accessKey } = await request.json()
if (!accessKey) {
return NextResponse.json({ error: 'Access key required' }, { status: 400 })
}
const result = await db
.update(machines)
.set({ isOnline: true, lastSeen: new Date() })
.where(eq(machines.accessKey, accessKey))
.returning({ id: machines.id })
if (!result[0]) {
return NextResponse.json({ error: 'Invalid access key' }, { status: 401 })
}
const machineId = result[0].id
// Check for a pending connection (code used recently)
const pending = await db
.select()
.from(sessionCodes)
.where(
and(
eq(sessionCodes.machineId, machineId),
eq(sessionCodes.isActive, true),
gt(sessionCodes.expiresAt, new Date()),
isNotNull(sessionCodes.usedAt)
)
)
.orderBy(sessionCodes.usedAt)
.limit(1)
return NextResponse.json({
success: true,
pendingConnection: pending[0]
? { sessionCodeId: pending[0].id, usedBy: pending[0].usedBy }
: null,
})
} catch (error) {
console.error('[Heartbeat] Error:', error)
return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
}
}

View File

@@ -0,0 +1,46 @@
import { db } from '@/lib/db'
import { machines } from '@/lib/db/schema'
import { eq } from 'drizzle-orm'
import { NextRequest, NextResponse } from 'next/server'
export async function POST(request: NextRequest) {
try {
const body = await request.json()
const { accessKey, name, hostname, os, osVersion, agentVersion, ipAddress } = body
if (!accessKey) {
return NextResponse.json({ error: 'Access key required' }, { status: 400 })
}
const result = await db
.select()
.from(machines)
.where(eq(machines.accessKey, accessKey))
.limit(1)
const machine = result[0]
if (!machine) {
return NextResponse.json({ error: 'Invalid access key' }, { status: 401 })
}
await db
.update(machines)
.set({
name: name || machine.name,
hostname: hostname || machine.hostname,
os: os || machine.os,
osVersion: osVersion || machine.osVersion,
agentVersion: agentVersion || machine.agentVersion,
ipAddress: ipAddress || machine.ipAddress,
isOnline: true,
lastSeen: new Date(),
updatedAt: new Date(),
})
.where(eq(machines.id, machine.id))
return NextResponse.json({ success: true, machineId: machine.id })
} catch (error) {
console.error('[Agent Register] Error:', error)
return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
}
}

View File

@@ -0,0 +1,69 @@
import { db } from '@/lib/db'
import { machines, sessionCodes } from '@/lib/db/schema'
import { eq, and } from 'drizzle-orm'
import { NextRequest, NextResponse } from 'next/server'
function generateSessionCode(): string {
const chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789'
let code = ''
for (let i = 0; i < 6; i++) {
code += chars.charAt(Math.floor(Math.random() * chars.length))
}
return code
}
export async function POST(request: NextRequest) {
try {
const { accessKey } = await request.json()
if (!accessKey) {
return NextResponse.json({ error: 'Access key required' }, { status: 400 })
}
const machineResult = await db
.select()
.from(machines)
.where(eq(machines.accessKey, accessKey))
.limit(1)
const machine = machineResult[0]
if (!machine) {
return NextResponse.json({ error: 'Invalid access key' }, { status: 401 })
}
// Generate a unique code
let code = ''
for (let attempts = 0; attempts < 10; attempts++) {
const candidate = generateSessionCode()
const existing = await db
.select({ id: sessionCodes.id })
.from(sessionCodes)
.where(and(eq(sessionCodes.code, candidate), eq(sessionCodes.isActive, true)))
.limit(1)
if (!existing[0]) {
code = candidate
break
}
}
if (!code) {
return NextResponse.json({ error: 'Failed to generate unique code' }, { status: 500 })
}
const expiresAt = new Date(Date.now() + 10 * 60 * 1000)
await db.insert(sessionCodes).values({
code,
machineId: machine.id,
createdBy: machine.userId,
expiresAt,
isActive: true,
})
return NextResponse.json({ success: true, code, expiresAt: expiresAt.toISOString(), expiresIn: 600 })
} catch (error) {
console.error('[Session Code] Error:', error)
return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
}
}