mirror of
https://github.com/mollersuite/monofile.git
synced 2024-11-28 16:36:27 -08:00
add run api for administrators
This commit is contained in:
parent
8845e9c775
commit
1703320b84
|
@ -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
|
||||
|
|
68
src/server/routes/api/v1/server/run.ts
Normal file
68
src/server/routes/api/v1/server/run.ts
Normal 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
|
||||
}
|
Loading…
Reference in a new issue