add run api for administrators

This commit is contained in:
May 2024-06-17 13:12:27 -07:00
parent 8845e9c775
commit 1703320b84
2 changed files with 73 additions and 1 deletions

View file

@ -1,4 +1,4 @@
import { APIDefinition } from "../../api.js";
import type { APIDefinition } from "../../api.js";
export default {
"name": "v1",
@ -28,6 +28,10 @@ export default {
{
"file": "file/individual",
"to": "/file"
},
{
"file": "/server/run",
"to": "/server/run"
}
]
} satisfies APIDefinition

View file

@ -0,0 +1,68 @@
import { Hono } from "hono"
import * as Accounts from "../../../../lib/accounts.js"
import * as auth from "../../../../lib/auth.js"
import { HttpBindings } from "@hono/node-server"
import config, { ClientConfiguration } from "../../../../lib/config.js"
import type Files from "../../../../lib/files.js"
import { getAccount, requiresAccount, requiresAdmin } from "../../../../lib/middleware.js"
import { Writable } from "node:stream"
const router = new Hono<{
Variables: {
account: Accounts.Account
},
Bindings: HttpBindings
}>()
router.use(getAccount, requiresAccount, requiresAdmin)
class Collect extends Writable {
collected: {t: number, packet: Buffer}[] = []
_write(data: Buffer, _: string, cb: () => void) {
this.collected.push({t: Date.now(), packet: data})
cb();
}
}
class VirtualConsole extends console.Console {
readonly stdout: Collect
readonly stderr: Collect
constructor() {
const stdout = new Collect(), stderr = new Collect()
super(stdout, stderr)
this.stdout = stdout, this.stderr = stderr
}
}
export default function(files: Files) {
router.post("/", async (ctx) => {
let vconsole = new VirtualConsole()
let evaluated
try {
let fn = new Function(
"accounts",
"auth",
"files",
"console",
await ctx.req.text()
)
evaluated = await fn(Accounts, auth, files, vconsole)
} catch (err) {
vconsole.error(err)
}
return ctx.json({
stdout: vconsole.stdout.collected,
stderr: vconsole.stderr.collected,
evaluated
})
})
return router
}