Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
530e1a3d5d
commit
195069450f
480 files changed
+15507
-24650
No files matched your search
@@ -1,12 +1,12 @@
|
||||
import { createHash, timingSafeEqual } from "node:crypto"
|
||||
import { createHash, timingSafeEqual } from 'node:crypto'
|
||||
|
||||
import { eq } from "drizzle-orm"
|
||||
import { Hono, type Context } from "hono"
|
||||
import { z } from "zod"
|
||||
import { eq } from 'drizzle-orm'
|
||||
import { Hono, type Context } from 'hono'
|
||||
import { z } from 'zod'
|
||||
|
||||
import { config } from "../config"
|
||||
import { db, schema } from "../db"
|
||||
import { failure, readJson } from "../http"
|
||||
import { config } from '../config'
|
||||
import { db, schema } from '../db'
|
||||
import { failure, readJson } from '../http'
|
||||
|
||||
const heartbeatSchema = z.object({
|
||||
hostname: z.string().min(1).max(128),
|
||||
@@ -14,7 +14,7 @@ const heartbeatSchema = z.object({
|
||||
cpu_core: z.number().int().positive(),
|
||||
memory: z.number().min(0).max(100),
|
||||
cpu: z.number().min(0).max(100),
|
||||
action: z.literal("heartbeat"),
|
||||
action: z.literal('heartbeat'),
|
||||
service_url: z.string().min(1).max(256),
|
||||
})
|
||||
|
||||
@@ -22,22 +22,20 @@ export const judgeServerRoutes = new Hono()
|
||||
|
||||
function tokenMatches(value: string | undefined) {
|
||||
if (!value) return false
|
||||
const expected = createHash("sha256").update(config.judgeServerToken).digest("hex")
|
||||
const expected = createHash('sha256').update(config.judgeServerToken).digest('hex')
|
||||
const actualBuffer = Buffer.from(value)
|
||||
const expectedBuffer = Buffer.from(expected)
|
||||
return (
|
||||
actualBuffer.length === expectedBuffer.length && timingSafeEqual(actualBuffer, expectedBuffer)
|
||||
)
|
||||
return actualBuffer.length === expectedBuffer.length && timingSafeEqual(actualBuffer, expectedBuffer)
|
||||
}
|
||||
|
||||
async function heartbeat(c: Context) {
|
||||
if (!tokenMatches(c.req.header("X-Judge-Server-Token"))) {
|
||||
return failure(c, 403, "invalid-judge-token", "Invalid token")
|
||||
if (!tokenMatches(c.req.header('X-Judge-Server-Token'))) {
|
||||
return failure(c, 403, 'invalid-judge-token', 'Invalid token')
|
||||
}
|
||||
|
||||
const parsed = await readJson(c, heartbeatSchema)
|
||||
if (!parsed.success) {
|
||||
return failure(c, 400, "invalid-heartbeat", "Invalid heartbeat payload")
|
||||
return failure(c, 400, 'invalid-heartbeat', 'Invalid heartbeat payload')
|
||||
}
|
||||
|
||||
const now = new Date().toISOString()
|
||||
@@ -48,7 +46,7 @@ async function heartbeat(c: Context) {
|
||||
.limit(1)
|
||||
|
||||
const common = {
|
||||
ip: c.req.header("x-forwarded-for")?.split(",")[0]?.trim() || c.req.header("x-real-ip") || null,
|
||||
ip: c.req.header('x-forwarded-for')?.split(',')[0]?.trim() || c.req.header('x-real-ip') || null,
|
||||
judgerVersion: parsed.data.judger_version,
|
||||
cpuCore: parsed.data.cpu_core,
|
||||
memoryUsage: parsed.data.memory,
|
||||
@@ -73,5 +71,5 @@ async function heartbeat(c: Context) {
|
||||
return c.json({ error: null, data: null })
|
||||
}
|
||||
|
||||
judgeServerRoutes.post("/judge-server/heartbeat", heartbeat)
|
||||
judgeServerRoutes.post("/judge-server/heartbeat/", heartbeat)
|
||||
judgeServerRoutes.post('/judge-server/heartbeat', heartbeat)
|
||||
judgeServerRoutes.post('/judge-server/heartbeat/', heartbeat)
|
||||
Reference in new issue
Block a user