From 050b670f77de91c9ec0b3bad83056c224a748f36 Mon Sep 17 00:00:00 2001 From: Elijah R Date: Sun, 23 Jun 2024 23:08:01 -0400 Subject: [PATCH] add personal USB disks --- src/config.ts | 1 + src/index.ts | 42 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/config.ts b/src/config.ts index c7b2d28..bd73c53 100644 --- a/src/config.ts +++ b/src/config.ts @@ -6,6 +6,7 @@ export default interface Config { id: string; usesIde2: boolean; hasFloppy: boolean; + hasUsb: boolean; }[]; BOT_PREFIX: string; ADMIN_TOKEN: string; diff --git a/src/index.ts b/src/index.ts index f3e4b07..960fd70 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import * as fs from "fs"; import * as path from "path"; +import * as child_process from "child_process"; import CollabVMClient from "./client.js"; import Config from "./config.js"; @@ -9,6 +10,9 @@ const blankflp = Buffer.alloc(1440000); if (!fs.existsSync("media/flp/")) fs.mkdirSync("media/flp/", { recursive: true }); +if (!fs.existsSync("media/img/")) + fs.mkdirSync("media/img/", { recursive: true }); + function Log(...args: string[]) { console.log("[AnyOSBot]", args.join(" ")); } @@ -101,16 +105,18 @@ class HelperBot extends CollabVMClient { private _vmId: string; private _ide2: boolean; private _hasFloppy: boolean; + private _hasUsb: boolean; private GeneralCmdLimit: RateLimit | undefined; private RebootLimit: RateLimit | undefined; - constructor(wsUri: string, vmId: string, ide2: boolean, floppy: boolean) { + constructor(wsUri: string, vmId: string, ide2: boolean, floppy: boolean, usb: boolean) { super(); this._wsUri = wsUri; this._vmId = vmId; this._ide2 = ide2; this._hasFloppy = floppy; + this._hasUsb = usb; } DoConn() { @@ -177,6 +183,7 @@ class HelperBot extends CollabVMClient { } QemuChangeDevice(devname: string, source: string, opts: string) { + console.log(`change ${devname} "${source}" ${opts}`); this.SendMonitorCommand(`change ${devname} "${source}" ${opts}`); } @@ -189,6 +196,10 @@ class HelperBot extends CollabVMClient { if (this._hasFloppy) this.QemuEjectDevice("vm.floppy"); } + QemuRemoveUSB() { + if (this._hasUsb) this.QemuEjectDevice("vm.usbstorage"); + } + QemuChangeCd(source: string, opts: string) { if (this._ide2) this.QemuChangeDevice("ide2-cd0", source, opts); else this.QemuChangeDevice("vm.cd", source, opts); @@ -201,6 +212,10 @@ class HelperBot extends CollabVMClient { if (this._hasFloppy) this.QemuChangeDevice("vm.floppy", source, opts); } + QemuChangeUSB(source: string) { + this.QemuChangeDevice("vm.usbstorage", source, ""); + } + OnChat(username: string, message: string) { if (username == this.GetUsername()) return; @@ -304,6 +319,10 @@ class HelperBot extends CollabVMClient { help: "Insert and/or create your personal floppy disk", usesFloppy: true, }, + { + command: "myusb", + help: "Insert and/or create your personal 100MB USB Drive", + }, { command: "lilyflp [path]", help: "Change Floppy image to Lily IMG/flp image", @@ -314,7 +333,7 @@ class HelperBot extends CollabVMClient { help: "Change CD image to HTTP server ISO file. Whitelisted domains only (see computernewb.com/CHOCOLATEMAN/domains.txt for a list)", }, { - command: "eject [cd/flp]", + command: "eject [cd/flp/usb]", help: "Ejects media from the specified drive.", }, { @@ -514,6 +533,9 @@ class HelperBot extends CollabVMClient { case "flp": this.QemuEjectFloppy(); break; + case "usb": + this.QemuRemoveUSB(); + break; } } break; @@ -543,6 +565,20 @@ class HelperBot extends CollabVMClient { this.Chat("Tried to put media into specified device."); break; } + case "myusb": { + if (!DoLimit(this.GeneralCmdLimit!)) return; + if (!this._hasUsb) { + this.Chat("This VM does not have a USB drive."); + return; + } + let imgpath = path.resolve(`media/img/${username}.qcow2`); + if (!fs.existsSync(imgpath)) { + child_process.execSync(`qemu-img create -f qcow2 "${imgpath}" 100M`); + } + this.QemuChangeUSB(imgpath); + this.Chat("Tried to put media into specified device."); + break; + } default: this.Chat(`Unknown command ${command}. See !help?`); break; @@ -595,5 +631,5 @@ class HelperBot extends CollabVMClient { for (let vm of config.INSTALLBOT_VMS) { // initalize this bot instance - new HelperBot(vm.uri, vm.id, vm.usesIde2, vm.hasFloppy).DoConn(); + new HelperBot(vm.uri, vm.id, vm.usesIde2, vm.hasFloppy, vm.hasUsb).DoConn(); }