format with prettier

This commit is contained in:
Elijah R 2024-06-23 18:45:14 -04:00
parent 5fd343c87b
commit c0e1f9c830
4 changed files with 728 additions and 653 deletions

1
.prettierrc Normal file
View file

@ -0,0 +1 @@
{}

402
client.js
View file

@ -1,259 +1,267 @@
// Small single-file CollabVM client library, in semi-modern Javascript
import * as ws from 'ws';
import * as ws from "ws";
const guacutils = {
parse: (string) => {
let pos = -1;
let sections = [];
parse: (string) => {
let pos = -1;
let sections = [];
for(;;) {
let len=string.indexOf('.', pos + 1);
for (;;) {
let len = string.indexOf(".", pos + 1);
if(len === -1)
break;
if (len === -1) break;
pos=parseInt(string.slice(pos + 1, len)) + len + 1
sections.push(string.slice(len + 1, pos)
.replace(/'/g, "'")
.replace(/"/g, '"')
.replace(///g, '/')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&amp;/g, '&')
);
pos = parseInt(string.slice(pos + 1, len)) + len + 1;
sections.push(
string
.slice(len + 1, pos)
.replace(/&#x27;/g, "'")
.replace(/&quot;/g, '"')
.replace(/&#x2F;/g, "/")
.replace(/&lt;/g, "<")
.replace(/&gt;/g, ">")
.replace(/&amp;/g, "&"),
);
if(string.slice(pos, pos + 1) === ';')
break;
}
return sections;
},
encode: (cypher) =>{
let command = '';
for(var i = 0; i < cypher.length; i++) {
let current = cypher[i];
command += current.length + '.' + current;
command += ( i < cypher.length - 1 ? ',' : ';');
}
return command;
if (string.slice(pos, pos + 1) === ";") break;
}
return sections;
},
encode: (cypher) => {
let command = "";
for (var i = 0; i < cypher.length; i++) {
let current = cypher[i];
command += current.length + "." + current;
command += i < cypher.length - 1 ? "," : ";";
}
return command;
},
};
const ConnectionState = Object.freeze({
CLOSED: 0,
CONNECTING: 1,
CONNECTED: 2
CLOSED: 0,
CONNECTING: 1,
CONNECTED: 2,
});
// System chat messages have a nil username.
function IsSystemChatInstruction(inst) {
return inst[1] == '';
return inst[1] == "";
}
class UserData {
#_name;
#_rank;
#_name;
#_rank;
constructor(name, rank) {
this._name = name;
this._rank = rank;
}
constructor(name, rank) {
this._name = name;
this._rank = rank;
}
GetName() { return this._name; }
GetRank() { return this._rank; }
GetName() {
return this._name;
}
GetRank() {
return this._rank;
}
UpdateRank(new_rank) { this._rank = new_rank; }
UpdateRank(new_rank) {
this._rank = new_rank;
}
}
export default class CollabVMClient {
constructor() {
this._state = ConnectionState.CLOSED;
this._users = [];
}
constructor() {
this._state = ConnectionState.CLOSED;
this._users = [];
}
GetState() {
return this._state;
}
GetState() { return this._state; }
Connect(uri) {
this._ws = new ws.WebSocket(uri, "guacamole", {
origin: "https://computernewb.com",
});
this._ws.onopen = this.OnWebSocketOpen.bind(this);
this._ws.onclose = this.OnWebSocketClose.bind(this);
this._ws.onerror = this.OnWebSocketError.bind(this);
this._ws.onmessage = this.OnWebSocketMessage.bind(this);
this._state = ConnectionState.CONNECTING;
}
Connect(uri) {
this._ws = new ws.WebSocket(uri, 'guacamole', {
origin: "https://computernewb.com"
});
this._ws.onopen = this.OnWebSocketOpen.bind(this);
this._ws.onclose = this.OnWebSocketClose.bind(this);
this._ws.onerror = this.OnWebSocketError.bind(this)
this._ws.onmessage = this.OnWebSocketMessage.bind(this);
this._state = ConnectionState.CONNECTING;
}
OnWebSocketOpen() {
this._state = ConnectionState.CONNECTED;
this.OnOpen();
}
OnWebSocketOpen() {
this._state = ConnectionState.CONNECTED;
this.OnOpen();
}
OnWebSocketClose() {
this._state = ConnectionState.CLOSED;
this.OnClose(arguments);
}
OnWebSocketClose() {
this._state = ConnectionState.CLOSED;
this.OnClose(arguments);
}
OnWebSocketError() {
// fire the close handler
this._state = ConnectionState.CLOSED;
this.OnClose(arguments);
}
OnWebSocketError() {
// fire the close handler
this._state = ConnectionState.CLOSED;
this.OnClose(arguments);
}
Close() {
this._state = ConnectionState.CLOSED;
this._ws.close();
}
Close() {
this._state = ConnectionState.CLOSED;
this._ws.close();
}
OnWebSocketMessage(ev) {
// cvm server should never send binary data
if (typeof ev.data !== "string") return;
OnWebSocketMessage(ev) {
// cvm server should never send binary data
if(typeof(ev.data) !== "string")
return;
let message = guacutils.parse(ev.data);
if (message.length === 0) return;
let message = guacutils.parse(ev.data);
if(message.length === 0)
return;
// Hardcoded, we need to keep this to be alive
if (message[0] === "nop") {
this.SendGuacamoleMessage("nop");
return;
}
// Hardcoded, we need to keep this to be alive
if(message[0] === "nop") {
this.SendGuacamoleMessage("nop");
return;
}
//if(message[0] === "chat") {
//if(message[0] === "chat") {
// console.log(`FUCK (${message.length}) ${message[1]} ${message[2]}`)
//}
// console.log(`FUCK (${message.length}) ${message[1]} ${message[2]}`)
//}
if(message[0] === "chat" && message.length === 3 && !IsSystemChatInstruction(message)) {
if(message[1] != this._username) {
//console.log(`FUCK 2 (${message.length}) ${message[1]} ${message[2]}`)
this.OnChat(message[1], message[2]);
return;
}
}
if (
message[0] === "chat" &&
message.length === 3 &&
!IsSystemChatInstruction(message)
) {
if (message[1] != this._username) {
//console.log(`FUCK 2 (${message.length}) ${message[1]} ${message[2]}`)
this.OnChat(message[1], message[2]);
return;
}
}
if(message[0] === "adduser") {
this.OnAddUser(message.slice(2), parseInt(message[1]));
return;
}
if (message[0] === "adduser") {
this.OnAddUser(message.slice(2), parseInt(message[1]));
return;
}
if(message[0] === "remuser")
this.OnRemUser(message.slice(2), parseInt(message[1]));
if (message[0] === "remuser")
this.OnRemUser(message.slice(2), parseInt(message[1]));
// Handle renames
if(message[0] === "rename") {
if(message.length === 5) {
if(message[1] == '1') {
this._username = message[3];
}
}
}
// Handle renames
if (message[0] === "rename") {
if (message.length === 5) {
if (message[1] == "1") {
this._username = message[3];
}
}
}
this.OnGuacamoleMessage(message);
}
this.OnGuacamoleMessage(message);
}
OnAddUser(users, count) {
//console.log(users);
for(var i = 0; i < count * 2; i += 2) {
let name = users[i];
let rank = users[i + 1];
OnAddUser(users, count) {
//console.log(users);
for (var i = 0; i < count * 2; i += 2) {
let name = users[i];
let rank = users[i + 1];
//console.log(`[${this.GetVM()}] user ${name} rank ${rank}`)
//console.log(`[${this.GetVM()}] user ${name} rank ${rank}`)
let existingUser = this._users.find(elem => elem.GetName() == name);
if(existingUser === undefined) {
//console.log(`[${this.GetVM()}] New user ${name} rank ${rank}`)
this._users.push(new UserData(name, rank));
} else {
// Handle admin/mod rank update
if(existingUser.GetRank() != rank) {
//console.log(`[${this.GetVM()}] updating ${name} to rank ${rank}`)
existingUser.UpdateRank(rank);
}
}
}
this.OnAddUser_Bot(this.GetUserCount());
}
let existingUser = this._users.find((elem) => elem.GetName() == name);
if (existingUser === undefined) {
//console.log(`[${this.GetVM()}] New user ${name} rank ${rank}`)
this._users.push(new UserData(name, rank));
} else {
// Handle admin/mod rank update
if (existingUser.GetRank() != rank) {
//console.log(`[${this.GetVM()}] updating ${name} to rank ${rank}`)
existingUser.UpdateRank(rank);
}
}
}
this.OnAddUser_Bot(this.GetUserCount());
}
OnRemUser(users, count) {
for(var i = 0; i < count; i++) {
let saveUserTemp = this.GetUser(users[i]);
this._users = this._users.filter(user => user.GetName() != users[i]);
}
OnRemUser(users, count) {
for (var i = 0; i < count; i++) {
let saveUserTemp = this.GetUser(users[i]);
this._users = this._users.filter((user) => user.GetName() != users[i]);
}
this.OnRemUser_Bot(this.GetUserCount());
}
this.OnRemUser_Bot(this.GetUserCount());
}
// This subtracts bots, including ourselves
GetUserCount() {
const KnownBots = [
this.GetUsername(),
//"General Darian"
// logger bots
"Specialized Egg",
"Emperor Kevin"
];
// This subtracts bots, including ourselves
GetUserCount() {
const KnownBots = [
this.GetUsername(),
//"General Darian"
// logger bots
"Specialized Egg",
"Emperor Kevin",
];
var len = this._users.length;
var len = this._users.length;
// subtract known bots
for(var i = len - 1; i != 0; --i) {
// ?
if(this._users[i] === undefined)
return;
// subtract known bots
for (var i = len - 1; i != 0; --i) {
// ?
if (this._users[i] === undefined) return;
var name = this._users[i].GetName();
if(KnownBots.find(elem => name == elem) !== undefined) {
//console.log("found blacklisted username", name)
len--;
}
}
var name = this._users[i].GetName();
if (KnownBots.find((elem) => name == elem) !== undefined) {
//console.log("found blacklisted username", name)
len--;
}
}
return len;
}
return len;
}
GetUserCountFull() {
return this._users.length;
}
GetUserCountFull() {
return this._users.length;
}
GetUsers() {
this._users;
}
GetUsers() {
this._users;
}
GetUser(username) {
let existingUser = this._users.find((elem) => elem.GetName() == username);
GetUser(username) {
let existingUser = this._users.find(elem => elem.GetName() == username);
// Apparently this can fail somehow..?
if (existingUser === undefined) return null;
// Apparently this can fail somehow..?
if(existingUser === undefined)
return null;
return existingUser;
}
return existingUser;
}
GetUsername() { return this._username; }
GetUsername() {
return this._username;
}
Rename(name) {
this._username = name;
this.SendGuacamoleMessage("rename", name);
}
Rename(name) {
this._username = name;
this.SendGuacamoleMessage("rename", name);
}
GetVM() { return this._vm; }
ConnectToVM(vm) {
this._vm = vm;
this.SendGuacamoleMessage("connect", vm);
}
GetVM() {
return this._vm;
}
SendGuacamoleMessage() {
if(this._state !== ConnectionState.CONNECTED)
return;
ConnectToVM(vm) {
this._vm = vm;
this.SendGuacamoleMessage("connect", vm);
}
this._ws.send(guacutils.encode(Array.prototype.slice.call(arguments)));
}
SendGuacamoleMessage() {
if (this._state !== ConnectionState.CONNECTED) return;
this._ws.send(guacutils.encode(Array.prototype.slice.call(arguments)));
}
}

969
index.js

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
{
"name": "AnyOSInstallBot",
"name": "anyosinstallbot",
"version": "1.0.0",
"description": "Helper bot to insert media into AnyOS VMs",
"main": "index.js",
@ -8,5 +8,12 @@
"type": "module",
"dependencies": {
"ws": "^8.3.0"
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e",
"scripts": {
"format": "prettier *.js -w"
},
"devDependencies": {
"prettier": "^3.3.2"
}
}