Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Merge origin/main into the installer branch

milind-sonic156a6c87d02
diff --cc connector/src/main.ts
index be0f88da1c,f913c5d945..64f61e1bad
--- a/connector/src/main.ts
+++ b/connector/src/main.ts
@@@ -3,7 -4,8 +4,8 @@@ import { existsSync, readFileSync, stat
import { join } from "node:path";
import { Agent } from "./agent.ts";
+ import { AuditLog, verifyAudit } from "./audit.ts";
-import { SocketDocker, type DockerInfo } from "./docker.ts";
+import { SocketDocker, dockerApiProblem, type DockerInfo } from "./docker.ts";
import { adminOrigin, enroll } from "./enroll.ts";
import { readHealth } from "./home-health.ts";
import { Home } from "./home.ts";
@@@ -93,15 -98,16 +98,21 @@@ async function run(): Promise<void>
const docker = new SocketDocker(PATHS.docker);
const instance = process.env.MAUSBOT_INSTANCE || "unknown";
log(`MausBot connector ${CONNECTOR_VERSION} starting`);
+ const audit = auditLog();
+ const note = (event: Parameters<AuditLog["append"]>[0]) => {
+ try { audit.append(event); } catch (error) { log(`could not write the audit log ${audit.file}: ${errorText(error)}`); }
+ };
+ note({ event: "connector", what: "started", version: CONNECTOR_VERSION, image: identity().image });
// A connector removed with SIGKILL never said it went offline.
state.saveStatus({ connected: false });
+ for (let problem = await dockerApiProblem(docker, PATHS.docker); problem; problem = await dockerApiProblem(docker, PATHS.docker)) {
+ log(problem);
+ state.saveStatus({ lastError: problem });
+ await sleep(10 * 60_000);
+ }
let record: AgentRecord | null = state.agent();
if (!record) log("waiting for this server to be connected: run the install command with the code MausBot shows");
+ const enrolledNow = !record;
while (!record) {
await sleep(2_000);
record = state.agent();
@@@ -221,12 -229,38 +244,40 @@@ async function probeCommand(): Promise<
let box: BoxProbeResult | null = null;
try { box = image ? await probeBox(docker, image, seccomp()) : null; } catch (error) { box = { ok: false, fix: errorText(error) }; }
const checks = hostChecks(await docker.info(), reader, { clockSkewS: null, box });
- for (const check of checks) console.log(check.ok && !check.warning ? `ok ${check.id}` : `${check.ok ? "warn" : "fail"} ${check.id} ${check.fix ?? ""}`);
- if (checks.some((check) => !check.ok)) process.exitCode = 1;
+ const ok = checks.every((check) => check.ok);
+ if (process.argv.includes("--json")) console.log(JSON.stringify({ ok, checks }));
+ else for (const check of checks) console.log(check.ok && !check.warning ? `ok ${check.id}` : `${check.ok ? "warn" : "fail"} ${check.id} ${check.fix ?? ""}`);
+ if (!ok) process.exitCode = 1;
}
+ /** `job <type> [args as JSON]`: one job, run by the running connector; prints its reply, exits 1 when it failed. */
+ async function jobCommand(): Promise<void> {
+ const [type, args] = process.argv.slice(3);
+ if (!type) throw new Error("usage: mausbot-connector job <type> [args as JSON], e.g. job update '{\"image\":\"ghcr.io/…@sha256:…\"}'");
+ let parsed: unknown = {};
+ if (args) {
+ try { parsed = JSON.parse(args); } catch { throw new Error("the job's arguments are not JSON"); }
+ }
+ const reply = await sendLocalJob(PATHS.jobs, type, parsed);
+ console.log(JSON.stringify(reply));
+ if (reply.ok !== true) process.exitCode = 1;
+ }
+
+ /** `audit [--tail N]` prints the newest lines; `audit --verify` checks every line's link to the one before it. */
+ function auditCommand(): void {
+ const audit = auditLog();
+ const args = process.argv.slice(3);
+ if (args.includes("--verify")) {
+ const verdict = verifyAudit(existsSync(audit.file) ? readFileSync(audit.file, "utf8") : "");
+ console.log(verdict.ok ? `${audit.file}: ${verdict.lines} lines, each linked to the one before it` : `${audit.file}: line ${verdict.line}: ${verdict.why}`);
+ if (!verdict.ok) process.exitCode = 1;
+ return;
+ }
+ const at = args.indexOf("--tail");
+ const count = at >= 0 ? Number(args[at + 1]) : 50;
+ for (const line of audit.tail(Number.isInteger(count) && count > 0 ? count : 50)) console.log(line);
+ }
+
const commands: Record<string, () => Promise<void> | void> = {
run,
enroll: enrollCommand,
@@@ -237,7 -273,7 +290,7 @@@
const command = commands[process.argv[2] ?? "run"];
if (!command) {
- console.error("usage: mausbot-connector run | enroll | status | probe | version");
- console.error("usage: mausbot-connector run | enroll | status | probe | job <type> [args] | audit [--tail N | --verify] | version");
++ console.error("usage: mausbot-connector run | enroll | status | probe [--json] | job <type> [args] | audit [--tail N | --verify] | version");
process.exitCode = 2;
} else {
Promise.resolve().then(command).catch((error) => {