|
|
|
|
@@ -28,8 +28,13 @@ const __dirname = import.meta.dirname;
|
|
|
|
|
const kCVMTSAssetsRoot = path.resolve(__dirname, '../../assets');
|
|
|
|
|
|
|
|
|
|
type ChatHistory = {
|
|
|
|
|
user: string,
|
|
|
|
|
msg: string
|
|
|
|
|
user: string;
|
|
|
|
|
msg: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type VoteTally = {
|
|
|
|
|
yes: number;
|
|
|
|
|
no: number;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// A good balance. TODO: Configurable?
|
|
|
|
|
@@ -41,7 +46,7 @@ function GetRawSharpOptions(size: Size): sharp.CreateRaw {
|
|
|
|
|
width: size.width,
|
|
|
|
|
height: size.height,
|
|
|
|
|
channels: 4
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Thread pool for doing JPEG encoding for rects.
|
|
|
|
|
@@ -63,9 +68,7 @@ async function EncodeJpeg(canvas: Buffer, displaySize: Size, rect: Rect): Promis
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// TODO: There's probably (definitely) a better way to fix this
|
|
|
|
|
if(res == undefined)
|
|
|
|
|
return Buffer.from([]);
|
|
|
|
|
|
|
|
|
|
if (res == undefined) return Buffer.from([]);
|
|
|
|
|
|
|
|
|
|
// have to manually turn it back into a buffer because
|
|
|
|
|
// Piscina for some reason turns it into a Uint8Array
|
|
|
|
|
@@ -80,7 +83,7 @@ export default class WSServer {
|
|
|
|
|
|
|
|
|
|
private clients: User[];
|
|
|
|
|
|
|
|
|
|
private ChatHistory : CircularBuffer<ChatHistory>
|
|
|
|
|
private ChatHistory: CircularBuffer<ChatHistory>;
|
|
|
|
|
|
|
|
|
|
private TurnQueue: Queue<User>;
|
|
|
|
|
|
|
|
|
|
@@ -123,7 +126,7 @@ export default class WSServer {
|
|
|
|
|
// Authentication manager
|
|
|
|
|
private auth: AuthManager | null;
|
|
|
|
|
|
|
|
|
|
private logger = new Logger("CVMTS.Server");
|
|
|
|
|
private logger = new Logger('CVMTS.Server');
|
|
|
|
|
|
|
|
|
|
constructor(config: IConfig, vm: QemuVM, auth: AuthManager | null) {
|
|
|
|
|
this.Config = config;
|
|
|
|
|
@@ -136,8 +139,8 @@ export default class WSServer {
|
|
|
|
|
this.voteCooldown = 0;
|
|
|
|
|
this.turnsAllowed = true;
|
|
|
|
|
this.screenHidden = false;
|
|
|
|
|
this.screenHiddenImg = readFileSync(path.join(kCVMTSAssetsRoot, "screenhidden.jpeg")).toString("base64");
|
|
|
|
|
this.screenHiddenThumb = readFileSync(path.join(kCVMTSAssetsRoot, "screenhiddenthumb.jpeg")).toString("base64");
|
|
|
|
|
this.screenHiddenImg = readFileSync(path.join(kCVMTSAssetsRoot, 'screenhidden.jpeg')).toString('base64');
|
|
|
|
|
this.screenHiddenThumb = readFileSync(path.join(kCVMTSAssetsRoot, 'screenhiddenthumb.jpeg')).toString('base64');
|
|
|
|
|
|
|
|
|
|
this.indefiniteTurn = null;
|
|
|
|
|
this.ModPerms = Utilities.MakeModPerms(this.Config.collabvm.moderatorPermissions);
|
|
|
|
|
@@ -146,7 +149,7 @@ export default class WSServer {
|
|
|
|
|
this.httpServer.on('upgrade', (req: http.IncomingMessage, socket: internal.Duplex, head: Buffer) => this.httpOnUpgrade(req, socket, head));
|
|
|
|
|
this.httpServer.on('request', (req, res) => {
|
|
|
|
|
res.writeHead(426);
|
|
|
|
|
res.write("This server only accepts WebSocket connections.");
|
|
|
|
|
res.write('This server only accepts WebSocket connections.');
|
|
|
|
|
res.end();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@@ -172,11 +175,11 @@ export default class WSServer {
|
|
|
|
|
|
|
|
|
|
private httpOnUpgrade(req: http.IncomingMessage, socket: internal.Duplex, head: Buffer) {
|
|
|
|
|
var killConnection = () => {
|
|
|
|
|
socket.write("HTTP/1.1 400 Bad Request\n\n400 Bad Request");
|
|
|
|
|
socket.write('HTTP/1.1 400 Bad Request\n\n400 Bad Request');
|
|
|
|
|
socket.destroy();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (req.headers['sec-websocket-protocol'] !== "guacamole") {
|
|
|
|
|
if (req.headers['sec-websocket-protocol'] !== 'guacamole') {
|
|
|
|
|
killConnection();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
@@ -200,7 +203,7 @@ export default class WSServer {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// detect fake origin headers
|
|
|
|
|
if (_uri.pathname !== "/" || _uri.search !== "") {
|
|
|
|
|
if (_uri.pathname !== '/' || _uri.search !== '') {
|
|
|
|
|
killConnection();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
@@ -220,13 +223,13 @@ export default class WSServer {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// Make sure x-forwarded-for is set
|
|
|
|
|
if (req.headers["x-forwarded-for"] === undefined) {
|
|
|
|
|
if (req.headers['x-forwarded-for'] === undefined) {
|
|
|
|
|
killConnection();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
// Get the first IP from the X-Forwarded-For variable
|
|
|
|
|
ip = req.headers["x-forwarded-for"]?.toString().replace(/\ /g, "").split(",")[0];
|
|
|
|
|
ip = req.headers['x-forwarded-for']?.toString().replace(/\ /g, '').split(',')[0];
|
|
|
|
|
} catch {
|
|
|
|
|
// If we can't get the IP, kill the connection
|
|
|
|
|
killConnection();
|
|
|
|
|
@@ -248,10 +251,10 @@ export default class WSServer {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the amount of active connections coming from the requesting IP.
|
|
|
|
|
let connections = this.clients.filter(client => client.IP.address == ip);
|
|
|
|
|
let connections = this.clients.filter((client) => client.IP.address == ip);
|
|
|
|
|
// If it exceeds the limit set in the config, reject the connection with a 429.
|
|
|
|
|
if (connections.length + 1 > this.Config.http.maxConnections) {
|
|
|
|
|
socket.write("HTTP/1.1 429 Too Many Requests\n\n429 Too Many Requests");
|
|
|
|
|
socket.write('HTTP/1.1 429 Too Many Requests\n\n429 Too Many Requests');
|
|
|
|
|
socket.destroy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -283,19 +286,18 @@ export default class WSServer {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
this.onMessage(user, buf.toString());
|
|
|
|
|
} catch {
|
|
|
|
|
}
|
|
|
|
|
} catch {}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (this.Config.auth.enabled) {
|
|
|
|
|
user.sendMsg(guacutils.encode("auth", this.Config.auth.apiEndpoint));
|
|
|
|
|
user.sendMsg(guacutils.encode('auth', this.Config.auth.apiEndpoint));
|
|
|
|
|
}
|
|
|
|
|
user.sendMsg(this.getAdduserMsg());
|
|
|
|
|
this.logger.Info(`Connect from ${user.IP.address}`);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private connectionClosed(user: User) {
|
|
|
|
|
let clientIndex = this.clients.indexOf(user)
|
|
|
|
|
let clientIndex = this.clients.indexOf(user);
|
|
|
|
|
if (clientIndex === -1) return;
|
|
|
|
|
|
|
|
|
|
if (user.IP.vote != null) {
|
|
|
|
|
@@ -310,33 +312,32 @@ export default class WSServer {
|
|
|
|
|
|
|
|
|
|
this.clients.splice(clientIndex, 1);
|
|
|
|
|
|
|
|
|
|
this.logger.Info(`Disconnect From ${user.IP.address}${user.username ? ` with username ${user.username}` : ""}`);
|
|
|
|
|
this.logger.Info(`Disconnect From ${user.IP.address}${user.username ? ` with username ${user.username}` : ''}`);
|
|
|
|
|
if (!user.username) return;
|
|
|
|
|
if (this.TurnQueue.toArray().indexOf(user) !== -1) {
|
|
|
|
|
var hadturn = (this.TurnQueue.peek() === user);
|
|
|
|
|
this.TurnQueue = Queue.from(this.TurnQueue.toArray().filter(u => u !== user));
|
|
|
|
|
var hadturn = this.TurnQueue.peek() === user;
|
|
|
|
|
this.TurnQueue = Queue.from(this.TurnQueue.toArray().filter((u) => u !== user));
|
|
|
|
|
if (hadturn) this.nextTurn();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.clients.forEach((c) => c.sendMsg(guacutils.encode("remuser", "1", user.username!)));
|
|
|
|
|
this.clients.forEach((c) => c.sendMsg(guacutils.encode('remuser', '1', user.username!)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private async onMessage(client: User, message: string) {
|
|
|
|
|
var msgArr = guacutils.decode(message);
|
|
|
|
|
if (msgArr.length < 1) return;
|
|
|
|
|
switch (msgArr[0]) {
|
|
|
|
|
case "login":
|
|
|
|
|
case 'login':
|
|
|
|
|
if (msgArr.length !== 2 || !this.Config.auth.enabled) return;
|
|
|
|
|
if (!client.connectedToNode) {
|
|
|
|
|
client.sendMsg(guacutils.encode("login", "0", "You must connect to the VM before logging in."));
|
|
|
|
|
client.sendMsg(guacutils.encode('login', '0', 'You must connect to the VM before logging in.'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var res = await this.auth!.Authenticate(msgArr[1], client);
|
|
|
|
|
if (res.clientSuccess) {
|
|
|
|
|
this.logger.Info(`${client.IP.address} logged in as ${res.username}`);
|
|
|
|
|
client.sendMsg(guacutils.encode("login", "1"));
|
|
|
|
|
var old = this.clients.find(c=>c.username === res.username);
|
|
|
|
|
client.sendMsg(guacutils.encode('login', '1'));
|
|
|
|
|
var old = this.clients.find((c) => c.username === res.username);
|
|
|
|
|
if (old) {
|
|
|
|
|
// kick() doesnt wait until the user is actually removed from the list and itd be anal to make it do that
|
|
|
|
|
// so we call connectionClosed manually here. When it gets called on kick(), it will return because the user isn't in the list
|
|
|
|
|
@@ -348,99 +349,99 @@ export default class WSServer {
|
|
|
|
|
// Set rank
|
|
|
|
|
client.rank = res.rank;
|
|
|
|
|
if (client.rank === Rank.Admin) {
|
|
|
|
|
client.sendMsg(guacutils.encode("admin", "0", "1"));
|
|
|
|
|
client.sendMsg(guacutils.encode('admin', '0', '1'));
|
|
|
|
|
} else if (client.rank === Rank.Moderator) {
|
|
|
|
|
client.sendMsg(guacutils.encode("admin", "0", "3", this.ModPerms.toString()));
|
|
|
|
|
client.sendMsg(guacutils.encode('admin', '0', '3', this.ModPerms.toString()));
|
|
|
|
|
}
|
|
|
|
|
this.clients.forEach((c) => c.sendMsg(guacutils.encode("adduser", "1", client.username!, client.rank.toString())));
|
|
|
|
|
this.clients.forEach((c) => c.sendMsg(guacutils.encode('adduser', '1', client.username!, client.rank.toString())));
|
|
|
|
|
} else {
|
|
|
|
|
client.sendMsg(guacutils.encode("login", "0", res.error!));
|
|
|
|
|
if (res.error === "You are banned") {
|
|
|
|
|
client.sendMsg(guacutils.encode('login', '0', res.error!));
|
|
|
|
|
if (res.error === 'You are banned') {
|
|
|
|
|
client.kick();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "list":
|
|
|
|
|
client.sendMsg(guacutils.encode("list", this.Config.collabvm.node, this.Config.collabvm.displayname, this.screenHidden ? this.screenHiddenThumb : await this.getThumbnail()));
|
|
|
|
|
case 'list':
|
|
|
|
|
client.sendMsg(guacutils.encode('list', this.Config.collabvm.node, this.Config.collabvm.displayname, this.screenHidden ? this.screenHiddenThumb : await this.getThumbnail()));
|
|
|
|
|
break;
|
|
|
|
|
case "connect":
|
|
|
|
|
case 'connect':
|
|
|
|
|
if (!client.username || msgArr.length !== 2 || msgArr[1] !== this.Config.collabvm.node) {
|
|
|
|
|
client.sendMsg(guacutils.encode("connect", "0"));
|
|
|
|
|
client.sendMsg(guacutils.encode('connect', '0'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
client.connectedToNode = true;
|
|
|
|
|
client.sendMsg(guacutils.encode("connect", "1", "1", this.Config.vm.snapshots ? "1" : "0", "0"));
|
|
|
|
|
client.sendMsg(guacutils.encode('connect', '1', '1', this.Config.vm.snapshots ? '1' : '0', '0'));
|
|
|
|
|
if (this.ChatHistory.size !== 0) client.sendMsg(this.getChatHistoryMsg());
|
|
|
|
|
if (this.Config.collabvm.motd) client.sendMsg(guacutils.encode("chat", "", this.Config.collabvm.motd));
|
|
|
|
|
if (this.Config.collabvm.motd) client.sendMsg(guacutils.encode('chat', '', this.Config.collabvm.motd));
|
|
|
|
|
if (this.screenHidden) {
|
|
|
|
|
client.sendMsg(guacutils.encode("size", "0", "1024", "768"));
|
|
|
|
|
client.sendMsg(guacutils.encode("png", "0", "0", "0", "0", this.screenHiddenImg));
|
|
|
|
|
client.sendMsg(guacutils.encode('size', '0', '1024', '768'));
|
|
|
|
|
client.sendMsg(guacutils.encode('png', '0', '0', '0', '0', this.screenHiddenImg));
|
|
|
|
|
} else {
|
|
|
|
|
await this.SendFullScreenWithSize(client);
|
|
|
|
|
}
|
|
|
|
|
client.sendMsg(guacutils.encode("sync", Date.now().toString()));
|
|
|
|
|
client.sendMsg(guacutils.encode('sync', Date.now().toString()));
|
|
|
|
|
if (this.voteInProgress) this.sendVoteUpdate(client);
|
|
|
|
|
this.sendTurnUpdate(client);
|
|
|
|
|
break;
|
|
|
|
|
case "view":
|
|
|
|
|
case 'view':
|
|
|
|
|
if (client.connectedToNode) return;
|
|
|
|
|
if (client.username || msgArr.length !== 3 || msgArr[1] !== this.Config.collabvm.node) {
|
|
|
|
|
// The use of connect here is intentional.
|
|
|
|
|
client.sendMsg(guacutils.encode("connect", "0"));
|
|
|
|
|
client.sendMsg(guacutils.encode('connect', '0'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (msgArr[2]) {
|
|
|
|
|
case "0":
|
|
|
|
|
case '0':
|
|
|
|
|
client.viewMode = 0;
|
|
|
|
|
break;
|
|
|
|
|
case "1":
|
|
|
|
|
case '1':
|
|
|
|
|
client.viewMode = 1;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
client.sendMsg(guacutils.encode("connect", "0"));
|
|
|
|
|
client.sendMsg(guacutils.encode('connect', '0'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client.sendMsg(guacutils.encode("connect", "1", "1", this.Config.vm.snapshots ? "1" : "0", "0"));
|
|
|
|
|
client.sendMsg(guacutils.encode('connect', '1', '1', this.Config.vm.snapshots ? '1' : '0', '0'));
|
|
|
|
|
if (this.ChatHistory.size !== 0) client.sendMsg(this.getChatHistoryMsg());
|
|
|
|
|
if (this.Config.collabvm.motd) client.sendMsg(guacutils.encode("chat", "", this.Config.collabvm.motd));
|
|
|
|
|
if (this.Config.collabvm.motd) client.sendMsg(guacutils.encode('chat', '', this.Config.collabvm.motd));
|
|
|
|
|
|
|
|
|
|
if (client.viewMode == 1) {
|
|
|
|
|
if (this.screenHidden) {
|
|
|
|
|
client.sendMsg(guacutils.encode("size", "0", "1024", "768"));
|
|
|
|
|
client.sendMsg(guacutils.encode("png", "0", "0", "0", "0", this.screenHiddenImg));
|
|
|
|
|
client.sendMsg(guacutils.encode('size', '0', '1024', '768'));
|
|
|
|
|
client.sendMsg(guacutils.encode('png', '0', '0', '0', '0', this.screenHiddenImg));
|
|
|
|
|
} else {
|
|
|
|
|
await this.SendFullScreenWithSize(client);
|
|
|
|
|
}
|
|
|
|
|
client.sendMsg(guacutils.encode("sync", Date.now().toString()));
|
|
|
|
|
client.sendMsg(guacutils.encode('sync', Date.now().toString()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.voteInProgress) this.sendVoteUpdate(client);
|
|
|
|
|
this.sendTurnUpdate(client);
|
|
|
|
|
break;
|
|
|
|
|
case "rename":
|
|
|
|
|
case 'rename':
|
|
|
|
|
if (!client.RenameRateLimit.request()) return;
|
|
|
|
|
if (client.connectedToNode && client.IP.muted) return;
|
|
|
|
|
if (this.Config.auth.enabled && client.rank !== Rank.Unregistered) {
|
|
|
|
|
client.sendMsg(guacutils.encode("chat", "", "Go to your account settings to change your username."));
|
|
|
|
|
client.sendMsg(guacutils.encode('chat', '', 'Go to your account settings to change your username.'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (this.Config.auth.enabled && msgArr[1] !== undefined) {
|
|
|
|
|
// Don't send system message to a user without a username since it was likely an automated attempt by the webapp
|
|
|
|
|
if (client.username) client.sendMsg(guacutils.encode("chat", "", "You need to log in to do that."));
|
|
|
|
|
if (client.username) client.sendMsg(guacutils.encode('chat', '', 'You need to log in to do that.'));
|
|
|
|
|
if (client.rank !== Rank.Unregistered) return;
|
|
|
|
|
this.renameUser(client, undefined);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.renameUser(client, msgArr[1]);
|
|
|
|
|
break;
|
|
|
|
|
case "chat":
|
|
|
|
|
case 'chat':
|
|
|
|
|
if (!client.username) return;
|
|
|
|
|
if (client.IP.muted) return;
|
|
|
|
|
if (msgArr.length !== 2) return;
|
|
|
|
|
if (this.Config.auth.enabled && client.rank === Rank.Unregistered && !this.Config.auth.guestPermissions.chat) {
|
|
|
|
|
client.sendMsg(guacutils.encode("chat", "", "You need to login to do that."));
|
|
|
|
|
client.sendMsg(guacutils.encode('chat', '', 'You need to login to do that.'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var msg = Utilities.HTMLSanitize(msgArr[1]);
|
|
|
|
|
@@ -448,14 +449,14 @@ export default class WSServer {
|
|
|
|
|
if (msg.length > this.Config.collabvm.maxChatLength) msg = msg.substring(0, this.Config.collabvm.maxChatLength);
|
|
|
|
|
if (msg.trim().length < 1) return;
|
|
|
|
|
|
|
|
|
|
this.clients.forEach(c => c.sendMsg(guacutils.encode("chat", client.username!, msg)));
|
|
|
|
|
this.clients.forEach((c) => c.sendMsg(guacutils.encode('chat', client.username!, msg)));
|
|
|
|
|
this.ChatHistory.push({ user: client.username, msg: msg });
|
|
|
|
|
client.onMsgSent();
|
|
|
|
|
break;
|
|
|
|
|
case "turn":
|
|
|
|
|
case 'turn':
|
|
|
|
|
if ((!this.turnsAllowed || this.Config.collabvm.turnwhitelist) && client.rank !== Rank.Admin && client.rank !== Rank.Moderator && client.rank !== Rank.Turn) return;
|
|
|
|
|
if (this.Config.auth.enabled && client.rank === Rank.Unregistered && !this.Config.auth.guestPermissions.turn) {
|
|
|
|
|
client.sendMsg(guacutils.encode("chat", "", "You need to login to do that."));
|
|
|
|
|
client.sendMsg(guacutils.encode('chat', '', 'You need to login to do that.'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!client.TurnRateLimit.request()) return;
|
|
|
|
|
@@ -463,14 +464,15 @@ export default class WSServer {
|
|
|
|
|
if (msgArr.length > 2) return;
|
|
|
|
|
var takingTurn: boolean;
|
|
|
|
|
if (msgArr.length === 1) takingTurn = true;
|
|
|
|
|
else switch (msgArr[1]) {
|
|
|
|
|
case "0":
|
|
|
|
|
else
|
|
|
|
|
switch (msgArr[1]) {
|
|
|
|
|
case '0':
|
|
|
|
|
if (this.indefiniteTurn === client) {
|
|
|
|
|
this.indefiniteTurn = null;
|
|
|
|
|
}
|
|
|
|
|
takingTurn = false;
|
|
|
|
|
break;
|
|
|
|
|
case "1":
|
|
|
|
|
case '1':
|
|
|
|
|
takingTurn = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
@@ -486,20 +488,20 @@ export default class WSServer {
|
|
|
|
|
if (client.IP.muted) return;
|
|
|
|
|
if (this.Config.collabvm.turnlimit.enabled) {
|
|
|
|
|
// Get the amount of users in the turn queue with the same IP as the user requesting a turn.
|
|
|
|
|
let turns = currentQueue.filter(user => user.IP.address == client.IP.address);
|
|
|
|
|
let turns = currentQueue.filter((user) => user.IP.address == client.IP.address);
|
|
|
|
|
// If it exceeds the limit set in the config, ignore the turn request.
|
|
|
|
|
if (turns.length + 1 > this.Config.collabvm.turnlimit.maximum) return;
|
|
|
|
|
}
|
|
|
|
|
this.TurnQueue.enqueue(client);
|
|
|
|
|
if (this.TurnQueue.size === 1) this.nextTurn();
|
|
|
|
|
} else {
|
|
|
|
|
var hadturn = (this.TurnQueue.peek() === client);
|
|
|
|
|
this.TurnQueue = Queue.from(this.TurnQueue.toArray().filter(u => u !== client));
|
|
|
|
|
var hadturn = this.TurnQueue.peek() === client;
|
|
|
|
|
this.TurnQueue = Queue.from(this.TurnQueue.toArray().filter((u) => u !== client));
|
|
|
|
|
if (hadturn) this.nextTurn();
|
|
|
|
|
}
|
|
|
|
|
this.sendTurnUpdate();
|
|
|
|
|
break;
|
|
|
|
|
case "mouse":
|
|
|
|
|
case 'mouse':
|
|
|
|
|
if (this.TurnQueue.peek() !== client && client.rank !== Rank.Admin) return;
|
|
|
|
|
var x = parseInt(msgArr[1]);
|
|
|
|
|
var y = parseInt(msgArr[2]);
|
|
|
|
|
@@ -507,79 +509,76 @@ export default class WSServer {
|
|
|
|
|
if (x === undefined || y === undefined || mask === undefined) return;
|
|
|
|
|
this.VM.GetDisplay()!.MouseEvent(x, y, mask);
|
|
|
|
|
break;
|
|
|
|
|
case "key":
|
|
|
|
|
case 'key':
|
|
|
|
|
if (this.TurnQueue.peek() !== client && client.rank !== Rank.Admin) return;
|
|
|
|
|
var keysym = parseInt(msgArr[1]);
|
|
|
|
|
var down = parseInt(msgArr[2]);
|
|
|
|
|
if (keysym === undefined || (down !== 0 && down !== 1)) return;
|
|
|
|
|
this.VM.GetDisplay()!.KeyboardEvent(keysym, down === 1 ? true : false);
|
|
|
|
|
break;
|
|
|
|
|
case "vote":
|
|
|
|
|
case 'vote':
|
|
|
|
|
if (!this.Config.vm.snapshots) return;
|
|
|
|
|
if ((!this.turnsAllowed || this.Config.collabvm.turnwhitelist) && client.rank !== Rank.Admin && client.rank !== Rank.Moderator && client.rank !== Rank.Turn) return;
|
|
|
|
|
if (!client.connectedToNode) return;
|
|
|
|
|
if (msgArr.length !== 2) return;
|
|
|
|
|
if (!client.VoteRateLimit.request()) return;
|
|
|
|
|
switch (msgArr[1]) {
|
|
|
|
|
case "1":
|
|
|
|
|
case '1':
|
|
|
|
|
if (!this.voteInProgress) {
|
|
|
|
|
if (this.voteCooldown !== 0) {
|
|
|
|
|
client.sendMsg(guacutils.encode("vote", "3", this.voteCooldown.toString()));
|
|
|
|
|
client.sendMsg(guacutils.encode('vote', '3', this.voteCooldown.toString()));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.startVote();
|
|
|
|
|
this.clients.forEach(c => c.sendMsg(guacutils.encode("chat", "", `${client.username} has started a vote to reset the VM.`)));
|
|
|
|
|
}
|
|
|
|
|
else if (client.IP.vote !== true)
|
|
|
|
|
this.clients.forEach(c => c.sendMsg(guacutils.encode("chat", "", `${client.username} has voted yes.`)));
|
|
|
|
|
this.clients.forEach((c) => c.sendMsg(guacutils.encode('chat', '', `${client.username} has started a vote to reset the VM.`)));
|
|
|
|
|
} else if (client.IP.vote !== true) this.clients.forEach((c) => c.sendMsg(guacutils.encode('chat', '', `${client.username} has voted yes.`)));
|
|
|
|
|
client.IP.vote = true;
|
|
|
|
|
break;
|
|
|
|
|
case "0":
|
|
|
|
|
case '0':
|
|
|
|
|
if (!this.voteInProgress) return;
|
|
|
|
|
if (client.IP.vote !== false)
|
|
|
|
|
this.clients.forEach(c => c.sendMsg(guacutils.encode("chat", "", `${client.username} has voted no.`)));
|
|
|
|
|
if (client.IP.vote !== false) this.clients.forEach((c) => c.sendMsg(guacutils.encode('chat', '', `${client.username} has voted no.`)));
|
|
|
|
|
client.IP.vote = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
this.sendVoteUpdate();
|
|
|
|
|
break;
|
|
|
|
|
case "admin":
|
|
|
|
|
case 'admin':
|
|
|
|
|
if (msgArr.length < 2) return;
|
|
|
|
|
switch (msgArr[1]) {
|
|
|
|
|
case "2":
|
|
|
|
|
case '2':
|
|
|
|
|
// Login
|
|
|
|
|
if (this.Config.auth.enabled) {
|
|
|
|
|
client.sendMsg(guacutils.encode("chat", "", "This server does not support staff passwords. Please log in to become staff."));
|
|
|
|
|
client.sendMsg(guacutils.encode('chat', '', 'This server does not support staff passwords. Please log in to become staff.'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!client.LoginRateLimit.request() || !client.username) return;
|
|
|
|
|
if (msgArr.length !== 3) return;
|
|
|
|
|
var sha256 = createHash("sha256");
|
|
|
|
|
var sha256 = createHash('sha256');
|
|
|
|
|
sha256.update(msgArr[2]);
|
|
|
|
|
var pwdHash = sha256.digest('hex');
|
|
|
|
|
sha256.destroy();
|
|
|
|
|
if (pwdHash === this.Config.collabvm.adminpass) {
|
|
|
|
|
client.rank = Rank.Admin;
|
|
|
|
|
client.sendMsg(guacutils.encode("admin", "0", "1"));
|
|
|
|
|
client.sendMsg(guacutils.encode('admin', '0', '1'));
|
|
|
|
|
} else if (this.Config.collabvm.moderatorEnabled && pwdHash === this.Config.collabvm.modpass) {
|
|
|
|
|
client.rank = Rank.Moderator;
|
|
|
|
|
client.sendMsg(guacutils.encode("admin", "0", "3", this.ModPerms.toString()));
|
|
|
|
|
client.sendMsg(guacutils.encode('admin', '0', '3', this.ModPerms.toString()));
|
|
|
|
|
} else if (this.Config.collabvm.turnwhitelist && pwdHash === this.Config.collabvm.turnpass) {
|
|
|
|
|
client.rank = Rank.Turn;
|
|
|
|
|
client.sendMsg(guacutils.encode("chat", "", "You may now take turns."));
|
|
|
|
|
client.sendMsg(guacutils.encode('chat', '', 'You may now take turns.'));
|
|
|
|
|
} else {
|
|
|
|
|
client.sendMsg(guacutils.encode("admin", "0", "0"));
|
|
|
|
|
client.sendMsg(guacutils.encode('admin', '0', '0'));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (this.screenHidden) {
|
|
|
|
|
await this.SendFullScreenWithSize(client);
|
|
|
|
|
|
|
|
|
|
client.sendMsg(guacutils.encode("sync", Date.now().toString()));
|
|
|
|
|
client.sendMsg(guacutils.encode('sync', Date.now().toString()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.clients.forEach((c) => c.sendMsg(guacutils.encode("adduser", "1", client.username!, client.rank.toString())));
|
|
|
|
|
this.clients.forEach((c) => c.sendMsg(guacutils.encode('adduser', '1', client.username!, client.rank.toString())));
|
|
|
|
|
break;
|
|
|
|
|
case "5":
|
|
|
|
|
case '5':
|
|
|
|
|
// QEMU Monitor
|
|
|
|
|
if (client.rank !== Rank.Admin) return;
|
|
|
|
|
/* Surely there could be rudimentary processing to convert some qemu monitor syntax to [XYZ hypervisor] if possible
|
|
|
|
|
@@ -590,51 +589,51 @@ export default class WSServer {
|
|
|
|
|
*/
|
|
|
|
|
if (msgArr.length !== 4 || msgArr[2] !== this.Config.collabvm.node) return;
|
|
|
|
|
var output = await this.VM.MonitorCommand(msgArr[3]);
|
|
|
|
|
client.sendMsg(guacutils.encode("admin", "2", String(output)));
|
|
|
|
|
client.sendMsg(guacutils.encode('admin', '2', String(output)));
|
|
|
|
|
break;
|
|
|
|
|
case "8":
|
|
|
|
|
case '8':
|
|
|
|
|
// Restore
|
|
|
|
|
if (client.rank !== Rank.Admin && (client.rank !== Rank.Moderator || !this.Config.collabvm.moderatorPermissions.restore)) return;
|
|
|
|
|
this.VM.Reset();
|
|
|
|
|
break;
|
|
|
|
|
case "10":
|
|
|
|
|
case '10':
|
|
|
|
|
// Reboot
|
|
|
|
|
if (client.rank !== Rank.Admin && (client.rank !== Rank.Moderator || !this.Config.collabvm.moderatorPermissions.reboot)) return;
|
|
|
|
|
if (msgArr.length !== 3 || msgArr[2] !== this.Config.collabvm.node) return;
|
|
|
|
|
this.VM.MonitorCommand("system_reset");
|
|
|
|
|
this.VM.MonitorCommand('system_reset');
|
|
|
|
|
break;
|
|
|
|
|
case "12":
|
|
|
|
|
case '12':
|
|
|
|
|
// Ban
|
|
|
|
|
if (client.rank !== Rank.Admin && (client.rank !== Rank.Moderator || !this.Config.collabvm.moderatorPermissions.ban)) return;
|
|
|
|
|
var user = this.clients.find(c => c.username === msgArr[2]);
|
|
|
|
|
var user = this.clients.find((c) => c.username === msgArr[2]);
|
|
|
|
|
if (!user) return;
|
|
|
|
|
user.ban();
|
|
|
|
|
case "13":
|
|
|
|
|
case '13':
|
|
|
|
|
// Force Vote
|
|
|
|
|
if (msgArr.length !== 3) return;
|
|
|
|
|
if (client.rank !== Rank.Admin && (client.rank !== Rank.Moderator || !this.Config.collabvm.moderatorPermissions.forcevote)) return;
|
|
|
|
|
if (!this.voteInProgress) return;
|
|
|
|
|
switch (msgArr[2]) {
|
|
|
|
|
case "1":
|
|
|
|
|
case '1':
|
|
|
|
|
this.endVote(true);
|
|
|
|
|
break;
|
|
|
|
|
case "0":
|
|
|
|
|
case '0':
|
|
|
|
|
this.endVote(false);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "14":
|
|
|
|
|
case '14':
|
|
|
|
|
// Mute
|
|
|
|
|
if (client.rank !== Rank.Admin && (client.rank !== Rank.Moderator || !this.Config.collabvm.moderatorPermissions.mute)) return;
|
|
|
|
|
if (msgArr.length !== 4) return;
|
|
|
|
|
var user = this.clients.find(c => c.username === msgArr[2]);
|
|
|
|
|
var user = this.clients.find((c) => c.username === msgArr[2]);
|
|
|
|
|
if (!user) return;
|
|
|
|
|
var permamute;
|
|
|
|
|
switch (msgArr[3]) {
|
|
|
|
|
case "0":
|
|
|
|
|
case '0':
|
|
|
|
|
permamute = false;
|
|
|
|
|
break;
|
|
|
|
|
case "1":
|
|
|
|
|
case '1':
|
|
|
|
|
permamute = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
@@ -642,105 +641,105 @@ export default class WSServer {
|
|
|
|
|
}
|
|
|
|
|
user.mute(permamute);
|
|
|
|
|
break;
|
|
|
|
|
case "15":
|
|
|
|
|
case '15':
|
|
|
|
|
// Kick
|
|
|
|
|
if (client.rank !== Rank.Admin && (client.rank !== Rank.Moderator || !this.Config.collabvm.moderatorPermissions.kick)) return;
|
|
|
|
|
var user = this.clients.find(c => c.username === msgArr[2]);
|
|
|
|
|
var user = this.clients.find((c) => c.username === msgArr[2]);
|
|
|
|
|
if (!user) return;
|
|
|
|
|
user.kick();
|
|
|
|
|
break;
|
|
|
|
|
case "16":
|
|
|
|
|
case '16':
|
|
|
|
|
// End turn
|
|
|
|
|
if (client.rank !== Rank.Admin && (client.rank !== Rank.Moderator || !this.Config.collabvm.moderatorPermissions.bypassturn)) return;
|
|
|
|
|
if (msgArr.length !== 3) return;
|
|
|
|
|
var user = this.clients.find(c => c.username === msgArr[2]);
|
|
|
|
|
var user = this.clients.find((c) => c.username === msgArr[2]);
|
|
|
|
|
if (!user) return;
|
|
|
|
|
this.endTurn(user);
|
|
|
|
|
break;
|
|
|
|
|
case "17":
|
|
|
|
|
case '17':
|
|
|
|
|
// Clear turn queue
|
|
|
|
|
if (client.rank !== Rank.Admin && (client.rank !== Rank.Moderator || !this.Config.collabvm.moderatorPermissions.bypassturn)) return;
|
|
|
|
|
if (msgArr.length !== 3 || msgArr[2] !== this.Config.collabvm.node) return;
|
|
|
|
|
this.clearTurns();
|
|
|
|
|
break;
|
|
|
|
|
case "18":
|
|
|
|
|
case '18':
|
|
|
|
|
// Rename user
|
|
|
|
|
if (client.rank !== Rank.Admin && (client.rank !== Rank.Moderator || !this.Config.collabvm.moderatorPermissions.rename)) return;
|
|
|
|
|
if (this.Config.auth.enabled) {
|
|
|
|
|
client.sendMsg(guacutils.encode("chat", "", "Cannot rename users on a server that uses authentication."));
|
|
|
|
|
client.sendMsg(guacutils.encode('chat', '', 'Cannot rename users on a server that uses authentication.'));
|
|
|
|
|
}
|
|
|
|
|
if (msgArr.length !== 4) return;
|
|
|
|
|
var user = this.clients.find(c => c.username === msgArr[2]);
|
|
|
|
|
var user = this.clients.find((c) => c.username === msgArr[2]);
|
|
|
|
|
if (!user) return;
|
|
|
|
|
this.renameUser(user, msgArr[3]);
|
|
|
|
|
break;
|
|
|
|
|
case "19":
|
|
|
|
|
case '19':
|
|
|
|
|
// Get IP
|
|
|
|
|
if (client.rank !== Rank.Admin && (client.rank !== Rank.Moderator || !this.Config.collabvm.moderatorPermissions.grabip)) return;
|
|
|
|
|
if (msgArr.length !== 3) return;
|
|
|
|
|
var user = this.clients.find(c => c.username === msgArr[2]);
|
|
|
|
|
var user = this.clients.find((c) => c.username === msgArr[2]);
|
|
|
|
|
if (!user) return;
|
|
|
|
|
client.sendMsg(guacutils.encode("admin", "19", msgArr[2], user.IP.address));
|
|
|
|
|
client.sendMsg(guacutils.encode('admin', '19', msgArr[2], user.IP.address));
|
|
|
|
|
break;
|
|
|
|
|
case "20":
|
|
|
|
|
case '20':
|
|
|
|
|
// Steal turn
|
|
|
|
|
if (client.rank !== Rank.Admin && (client.rank !== Rank.Moderator || !this.Config.collabvm.moderatorPermissions.bypassturn)) return;
|
|
|
|
|
this.bypassTurn(client);
|
|
|
|
|
break;
|
|
|
|
|
case "21":
|
|
|
|
|
case '21':
|
|
|
|
|
// XSS
|
|
|
|
|
if (client.rank !== Rank.Admin && (client.rank !== Rank.Moderator || !this.Config.collabvm.moderatorPermissions.xss)) return;
|
|
|
|
|
if (msgArr.length !== 3) return;
|
|
|
|
|
switch (client.rank) {
|
|
|
|
|
case Rank.Admin:
|
|
|
|
|
|
|
|
|
|
this.clients.forEach(c => c.sendMsg(guacutils.encode("chat", client.username!, msgArr[2])));
|
|
|
|
|
this.clients.forEach((c) => c.sendMsg(guacutils.encode('chat', client.username!, msgArr[2])));
|
|
|
|
|
|
|
|
|
|
this.ChatHistory.push({ user: client.username!, msg: msgArr[2] });
|
|
|
|
|
break;
|
|
|
|
|
case Rank.Moderator:
|
|
|
|
|
this.clients.filter((c) => c.rank !== Rank.Admin).forEach((c) => c.sendMsg(guacutils.encode('chat', client.username!, msgArr[2])));
|
|
|
|
|
|
|
|
|
|
this.clients.filter(c => c.rank !== Rank.Admin).forEach(c => c.sendMsg(guacutils.encode("chat", client.username!, msgArr[2])));
|
|
|
|
|
|
|
|
|
|
this.clients.filter(c => c.rank === Rank.Admin).forEach(c => c.sendMsg(guacutils.encode("chat", client.username!, Utilities.HTMLSanitize(msgArr[2]))));
|
|
|
|
|
this.clients.filter((c) => c.rank === Rank.Admin).forEach((c) => c.sendMsg(guacutils.encode('chat', client.username!, Utilities.HTMLSanitize(msgArr[2]))));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "22":
|
|
|
|
|
case '22':
|
|
|
|
|
// Toggle turns
|
|
|
|
|
if (client.rank !== Rank.Admin) return;
|
|
|
|
|
if (msgArr.length !== 3) return;
|
|
|
|
|
switch (msgArr[2]) {
|
|
|
|
|
case "0":
|
|
|
|
|
case '0':
|
|
|
|
|
this.clearTurns();
|
|
|
|
|
this.turnsAllowed = false;
|
|
|
|
|
break;
|
|
|
|
|
case "1":
|
|
|
|
|
case '1':
|
|
|
|
|
this.turnsAllowed = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "23":
|
|
|
|
|
case '23':
|
|
|
|
|
// Indefinite turn
|
|
|
|
|
if (client.rank !== Rank.Admin) return;
|
|
|
|
|
this.indefiniteTurn = client;
|
|
|
|
|
this.TurnQueue = Queue.from([client, ...this.TurnQueue.toArray().filter(c=>c!==client)]);
|
|
|
|
|
this.TurnQueue = Queue.from([client, ...this.TurnQueue.toArray().filter((c) => c !== client)]);
|
|
|
|
|
this.sendTurnUpdate();
|
|
|
|
|
break;
|
|
|
|
|
case "24":
|
|
|
|
|
case '24':
|
|
|
|
|
// Hide screen
|
|
|
|
|
if (client.rank !== Rank.Admin) return;
|
|
|
|
|
if (msgArr.length !== 3) return;
|
|
|
|
|
switch (msgArr[2]) {
|
|
|
|
|
case "0":
|
|
|
|
|
case '0':
|
|
|
|
|
this.screenHidden = true;
|
|
|
|
|
this.clients.filter(c => c.rank == Rank.Unregistered).forEach(client => {
|
|
|
|
|
client.sendMsg(guacutils.encode("size", "0", "1024", "768"));
|
|
|
|
|
client.sendMsg(guacutils.encode("png", "0", "0", "0", "0", this.screenHiddenImg));
|
|
|
|
|
client.sendMsg(guacutils.encode("sync", Date.now().toString()));
|
|
|
|
|
this.clients
|
|
|
|
|
.filter((c) => c.rank == Rank.Unregistered)
|
|
|
|
|
.forEach((client) => {
|
|
|
|
|
client.sendMsg(guacutils.encode('size', '0', '1024', '768'));
|
|
|
|
|
client.sendMsg(guacutils.encode('png', '0', '0', '0', '0', this.screenHiddenImg));
|
|
|
|
|
client.sendMsg(guacutils.encode('sync', Date.now().toString()));
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
case "1":
|
|
|
|
|
case '1':
|
|
|
|
|
this.screenHidden = false;
|
|
|
|
|
let displaySize = this.VM.GetDisplay().Size();
|
|
|
|
|
|
|
|
|
|
@@ -751,31 +750,27 @@ export default class WSServer {
|
|
|
|
|
height: displaySize.height
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.clients.forEach(async client => {
|
|
|
|
|
client.sendMsg(guacutils.encode("size", "0", displaySize.width.toString(), displaySize.height.toString()));
|
|
|
|
|
client.sendMsg(guacutils.encode("png", "0", "0", "0", "0", encoded));
|
|
|
|
|
client.sendMsg(guacutils.encode("sync", Date.now().toString()));
|
|
|
|
|
this.clients.forEach(async (client) => {
|
|
|
|
|
client.sendMsg(guacutils.encode('size', '0', displaySize.width.toString(), displaySize.height.toString()));
|
|
|
|
|
client.sendMsg(guacutils.encode('png', '0', '0', '0', '0', encoded));
|
|
|
|
|
client.sendMsg(guacutils.encode('sync', Date.now().toString()));
|
|
|
|
|
});
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case "25":
|
|
|
|
|
if (client.rank !== Rank.Admin || msgArr.length !== 3)
|
|
|
|
|
return;
|
|
|
|
|
this.clients.forEach(c => c.sendMsg(guacutils.encode("chat", "", msgArr[2])));
|
|
|
|
|
case '25':
|
|
|
|
|
if (client.rank !== Rank.Admin || msgArr.length !== 3) return;
|
|
|
|
|
this.clients.forEach((c) => c.sendMsg(guacutils.encode('chat', '', msgArr[2])));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getUsernameList(): string[] {
|
|
|
|
|
var arr: string[] = [];
|
|
|
|
|
|
|
|
|
|
this.clients.filter(c => c.username).forEach((c) => arr.push(c.username!));
|
|
|
|
|
this.clients.filter((c) => c.username).forEach((c) => arr.push(c.username!));
|
|
|
|
|
return arr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -784,63 +779,58 @@ export default class WSServer {
|
|
|
|
|
var hadName: boolean = client.username ? true : false;
|
|
|
|
|
var oldname: any;
|
|
|
|
|
if (hadName) oldname = client.username;
|
|
|
|
|
var status = "0";
|
|
|
|
|
var status = '0';
|
|
|
|
|
if (!newName) {
|
|
|
|
|
client.assignGuestName(this.getUsernameList());
|
|
|
|
|
} else {
|
|
|
|
|
newName = newName.trim();
|
|
|
|
|
if (hadName && newName === oldname) {
|
|
|
|
|
|
|
|
|
|
client.sendMsg(guacutils.encode("rename", "0", "0", client.username!, client.rank.toString()));
|
|
|
|
|
client.sendMsg(guacutils.encode('rename', '0', '0', client.username!, client.rank.toString()));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (this.getUsernameList().indexOf(newName) !== -1) {
|
|
|
|
|
client.assignGuestName(this.getUsernameList());
|
|
|
|
|
if (client.connectedToNode) {
|
|
|
|
|
status = "1";
|
|
|
|
|
status = '1';
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
if (!/^[a-zA-Z0-9\ \-\_\.]+$/.test(newName) || newName.length > 20 || newName.length < 3) {
|
|
|
|
|
} else if (!/^[a-zA-Z0-9\ \-\_\.]+$/.test(newName) || newName.length > 20 || newName.length < 3) {
|
|
|
|
|
client.assignGuestName(this.getUsernameList());
|
|
|
|
|
status = "2";
|
|
|
|
|
} else
|
|
|
|
|
if (this.Config.collabvm.usernameblacklist.indexOf(newName) !== -1) {
|
|
|
|
|
status = '2';
|
|
|
|
|
} else if (this.Config.collabvm.usernameblacklist.indexOf(newName) !== -1) {
|
|
|
|
|
client.assignGuestName(this.getUsernameList());
|
|
|
|
|
status = "3";
|
|
|
|
|
status = '3';
|
|
|
|
|
} else client.username = newName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client.sendMsg(guacutils.encode("rename", "0", status, client.username!, client.rank.toString()));
|
|
|
|
|
client.sendMsg(guacutils.encode('rename', '0', status, client.username!, client.rank.toString()));
|
|
|
|
|
if (hadName) {
|
|
|
|
|
this.logger.Info(`Rename ${client.IP.address} from ${oldname} to ${client.username}`);
|
|
|
|
|
this.clients.forEach((c) =>
|
|
|
|
|
|
|
|
|
|
c.sendMsg(guacutils.encode("rename", "1", oldname, client.username!, client.rank.toString())));
|
|
|
|
|
this.clients.forEach((c) => c.sendMsg(guacutils.encode('rename', '1', oldname, client.username!, client.rank.toString())));
|
|
|
|
|
} else {
|
|
|
|
|
this.logger.Info(`Rename ${client.IP.address} to ${client.username}`);
|
|
|
|
|
this.clients.forEach((c) =>
|
|
|
|
|
|
|
|
|
|
c.sendMsg(guacutils.encode("adduser", "1", client.username!, client.rank.toString())));
|
|
|
|
|
this.clients.forEach((c) => c.sendMsg(guacutils.encode('adduser', '1', client.username!, client.rank.toString())));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getAdduserMsg(): string {
|
|
|
|
|
var arr : string[] = ["adduser", this.clients.filter(c=>c.username).length.toString()];
|
|
|
|
|
var arr: string[] = ['adduser', this.clients.filter((c) => c.username).length.toString()];
|
|
|
|
|
|
|
|
|
|
this.clients.filter(c=>c.username).forEach((c) => arr.push(c.username!, c.rank.toString()));
|
|
|
|
|
this.clients.filter((c) => c.username).forEach((c) => arr.push(c.username!, c.rank.toString()));
|
|
|
|
|
return guacutils.encode(...arr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getChatHistoryMsg(): string {
|
|
|
|
|
var arr : string[] = ["chat"];
|
|
|
|
|
this.ChatHistory.forEach(c => arr.push(c.user, c.msg));
|
|
|
|
|
var arr: string[] = ['chat'];
|
|
|
|
|
this.ChatHistory.forEach((c) => arr.push(c.user, c.msg));
|
|
|
|
|
return guacutils.encode(...arr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private sendTurnUpdate(client?: User) {
|
|
|
|
|
var turnQueueArr = this.TurnQueue.toArray();
|
|
|
|
|
var turntime;
|
|
|
|
|
if (this.indefiniteTurn === null) turntime = (this.TurnTime * 1000);
|
|
|
|
|
if (this.indefiniteTurn === null) turntime = this.TurnTime * 1000;
|
|
|
|
|
else turntime = 9999999999;
|
|
|
|
|
var arr = ["turn", turntime.toString(), this.TurnQueue.size.toString()];
|
|
|
|
|
var arr = ['turn', turntime.toString(), this.TurnQueue.size.toString()];
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
this.TurnQueue.forEach((c) => arr.push(c.username));
|
|
|
|
|
var currentTurningUser = this.TurnQueue.peek();
|
|
|
|
|
@@ -848,18 +838,19 @@ export default class WSServer {
|
|
|
|
|
client.sendMsg(guacutils.encode(...arr));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.clients.filter(c => (c !== currentTurningUser && c.connectedToNode)).forEach((c) => {
|
|
|
|
|
this.clients
|
|
|
|
|
.filter((c) => c !== currentTurningUser && c.connectedToNode)
|
|
|
|
|
.forEach((c) => {
|
|
|
|
|
if (turnQueueArr.indexOf(c) !== -1) {
|
|
|
|
|
var time;
|
|
|
|
|
if (this.indefiniteTurn === null) time = ((this.TurnTime * 1000) + ((turnQueueArr.indexOf(c) - 1) * this.Config.collabvm.turnTime * 1000));
|
|
|
|
|
if (this.indefiniteTurn === null) time = this.TurnTime * 1000 + (turnQueueArr.indexOf(c) - 1) * this.Config.collabvm.turnTime * 1000;
|
|
|
|
|
else time = 9999999999;
|
|
|
|
|
c.sendMsg(guacutils.encode(...arr, time.toString()));
|
|
|
|
|
} else {
|
|
|
|
|
c.sendMsg(guacutils.encode(...arr));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (currentTurningUser)
|
|
|
|
|
currentTurningUser.sendMsg(guacutils.encode(...arr));
|
|
|
|
|
if (currentTurningUser) currentTurningUser.sendMsg(guacutils.encode(...arr));
|
|
|
|
|
}
|
|
|
|
|
private nextTurn() {
|
|
|
|
|
clearInterval(this.TurnInterval);
|
|
|
|
|
@@ -878,14 +869,14 @@ export default class WSServer {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bypassTurn(client: User) {
|
|
|
|
|
var a = this.TurnQueue.toArray().filter(c => c !== client);
|
|
|
|
|
var a = this.TurnQueue.toArray().filter((c) => c !== client);
|
|
|
|
|
this.TurnQueue = Queue.from([client, ...a]);
|
|
|
|
|
this.nextTurn();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
endTurn(client: User) {
|
|
|
|
|
var hasTurn = (this.TurnQueue.peek() === client);
|
|
|
|
|
this.TurnQueue = Queue.from(this.TurnQueue.toArray().filter(c => c !== client));
|
|
|
|
|
var hasTurn = this.TurnQueue.peek() === client;
|
|
|
|
|
this.TurnQueue = Queue.from(this.TurnQueue.toArray().filter((c) => c !== client));
|
|
|
|
|
if (hasTurn) this.nextTurn();
|
|
|
|
|
else this.sendTurnUpdate();
|
|
|
|
|
}
|
|
|
|
|
@@ -902,17 +893,21 @@ export default class WSServer {
|
|
|
|
|
private async OnDisplayRectangle(rect: Rect) {
|
|
|
|
|
let encodedb64 = await this.MakeRectData(rect);
|
|
|
|
|
|
|
|
|
|
this.clients.filter(c => c.connectedToNode || c.viewMode == 1).forEach(c => {
|
|
|
|
|
this.clients
|
|
|
|
|
.filter((c) => c.connectedToNode || c.viewMode == 1)
|
|
|
|
|
.forEach((c) => {
|
|
|
|
|
if (this.screenHidden && c.rank == Rank.Unregistered) return;
|
|
|
|
|
c.sendMsg(guacutils.encode("png", "0", "0", rect.x.toString(), rect.y.toString(), encodedb64));
|
|
|
|
|
c.sendMsg(guacutils.encode("sync", Date.now().toString()));
|
|
|
|
|
c.sendMsg(guacutils.encode('png', '0', '0', rect.x.toString(), rect.y.toString(), encodedb64));
|
|
|
|
|
c.sendMsg(guacutils.encode('sync', Date.now().toString()));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private OnDisplayResized(size: Size) {
|
|
|
|
|
this.clients.filter(c => c.connectedToNode || c.viewMode == 1).forEach(c => {
|
|
|
|
|
this.clients
|
|
|
|
|
.filter((c) => c.connectedToNode || c.viewMode == 1)
|
|
|
|
|
.forEach((c) => {
|
|
|
|
|
if (this.screenHidden && c.rank == Rank.Unregistered) return;
|
|
|
|
|
c.sendMsg(guacutils.encode("size", "0", size.width.toString(), size.height.toString()))
|
|
|
|
|
c.sendMsg(guacutils.encode('size', '0', size.width.toString(), size.height.toString()));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -927,8 +922,8 @@ export default class WSServer {
|
|
|
|
|
height: displaySize.height
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
client.sendMsg(guacutils.encode("size", "0", displaySize.width.toString(), displaySize.height.toString()));
|
|
|
|
|
client.sendMsg(guacutils.encode("png", "0", "0", "0", "0", encoded));
|
|
|
|
|
client.sendMsg(guacutils.encode('size', '0', displaySize.width.toString(), displaySize.height.toString()));
|
|
|
|
|
client.sendMsg(guacutils.encode('png', '0', '0', '0', '0', encoded));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async MakeRectData(rect: Rect) {
|
|
|
|
|
@@ -943,8 +938,7 @@ export default class WSServer {
|
|
|
|
|
getThumbnail(): Promise<string> {
|
|
|
|
|
return new Promise(async (res, rej) => {
|
|
|
|
|
let display = this.VM.GetDisplay();
|
|
|
|
|
if(display == null)
|
|
|
|
|
return;
|
|
|
|
|
if (display == null) return;
|
|
|
|
|
|
|
|
|
|
// TODO: pass custom options to Sharp.resize() probably
|
|
|
|
|
let out = await sharp(display.Buffer(), { raw: GetRawSharpOptions(display.Size()) })
|
|
|
|
|
@@ -959,7 +953,7 @@ export default class WSServer {
|
|
|
|
|
startVote() {
|
|
|
|
|
if (this.voteInProgress) return;
|
|
|
|
|
this.voteInProgress = true;
|
|
|
|
|
this.clients.forEach(c => c.sendMsg(guacutils.encode("vote", "0")));
|
|
|
|
|
this.clients.forEach((c) => c.sendMsg(guacutils.encode('vote', '0')));
|
|
|
|
|
this.voteTime = this.Config.collabvm.voteTime;
|
|
|
|
|
this.voteInterval = setInterval(() => {
|
|
|
|
|
this.voteTime--;
|
|
|
|
|
@@ -974,37 +968,34 @@ export default class WSServer {
|
|
|
|
|
this.voteInProgress = false;
|
|
|
|
|
clearInterval(this.voteInterval);
|
|
|
|
|
var count = this.getVoteCounts();
|
|
|
|
|
this.clients.forEach((c) => c.sendMsg(guacutils.encode("vote", "2")));
|
|
|
|
|
this.clients.forEach((c) => c.sendMsg(guacutils.encode('vote', '2')));
|
|
|
|
|
if (result === true || (result === undefined && count.yes >= count.no)) {
|
|
|
|
|
this.clients.forEach(c => c.sendMsg(guacutils.encode("chat", "", "The vote to reset the VM has won.")));
|
|
|
|
|
this.clients.forEach((c) => c.sendMsg(guacutils.encode('chat', '', 'The vote to reset the VM has won.')));
|
|
|
|
|
this.VM.Reset();
|
|
|
|
|
} else {
|
|
|
|
|
this.clients.forEach(c => c.sendMsg(guacutils.encode("chat", "", "The vote to reset the VM has lost.")));
|
|
|
|
|
this.clients.forEach((c) => c.sendMsg(guacutils.encode('chat', '', 'The vote to reset the VM has lost.')));
|
|
|
|
|
}
|
|
|
|
|
this.clients.forEach(c => {
|
|
|
|
|
this.clients.forEach((c) => {
|
|
|
|
|
c.IP.vote = null;
|
|
|
|
|
});
|
|
|
|
|
this.voteCooldown = this.Config.collabvm.voteCooldown;
|
|
|
|
|
this.voteCooldownInterval = setInterval(() => {
|
|
|
|
|
this.voteCooldown--;
|
|
|
|
|
if (this.voteCooldown < 1)
|
|
|
|
|
clearInterval(this.voteCooldownInterval);
|
|
|
|
|
if (this.voteCooldown < 1) clearInterval(this.voteCooldownInterval);
|
|
|
|
|
}, 1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendVoteUpdate(client?: User) {
|
|
|
|
|
if (!this.voteInProgress) return;
|
|
|
|
|
var count = this.getVoteCounts();
|
|
|
|
|
var msg = guacutils.encode("vote", "1", (this.voteTime * 1000).toString(), count.yes.toString(), count.no.toString());
|
|
|
|
|
if (client)
|
|
|
|
|
client.sendMsg(msg);
|
|
|
|
|
else
|
|
|
|
|
this.clients.forEach((c) => c.sendMsg(msg));
|
|
|
|
|
var msg = guacutils.encode('vote', '1', (this.voteTime * 1000).toString(), count.yes.toString(), count.no.toString());
|
|
|
|
|
if (client) client.sendMsg(msg);
|
|
|
|
|
else this.clients.forEach((c) => c.sendMsg(msg));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getVoteCounts() : {yes:number,no:number} {
|
|
|
|
|
var yes = 0;
|
|
|
|
|
var no = 0;
|
|
|
|
|
getVoteCounts(): VoteTally {
|
|
|
|
|
let yes = 0;
|
|
|
|
|
let no = 0;
|
|
|
|
|
IPDataManager.ForEachIPData((c) => {
|
|
|
|
|
if (c.vote === true) yes++;
|
|
|
|
|
if (c.vote === false) no++;
|
|
|
|
|
|