auth: Make more resilant to backend failures

This commit is contained in:
modeco80
2024-05-26 16:33:35 -04:00
parent e184bfb085
commit 173ee8149f
2 changed files with 41 additions and 30 deletions

View File

@@ -25,12 +25,14 @@ export default class AuthManager {
}) })
}); });
// Make sure the fetch returned okay
if(!response.ok)
throw new Error(`Failed to query quth server: ${response.statusText}`)
let json = (await response.json()) as JoinResponse; let json = (await response.json()) as JoinResponse;
if (!json.success) { if (!json.success)
this.logger.Error(`Failed to query auth server: ${json.error}`); throw new Error(json.error);
process.exit(1);
}
return json; return json;
} }

View File

@@ -294,32 +294,38 @@ export default class WSServer {
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; return;
} }
var res = await this.auth!.Authenticate(msgArr[1], client); try {
if (res.clientSuccess) { let res = await this.auth!.Authenticate(msgArr[1], client);
this.logger.Info(`${client.IP.address} logged in as ${res.username}`); if (res.clientSuccess) {
client.sendMsg(guacutils.encode('login', '1')); this.logger.Info(`${client.IP.address} logged in as ${res.username}`);
var old = this.clients.find((c) => c.username === res.username); client.sendMsg(guacutils.encode('login', '1'));
if (old) { let old = this.clients.find((c) => c.username === res.username);
// kick() doesnt wait until the user is actually removed from the list and itd be anal to make it do that if (old) {
// so we call connectionClosed manually here. When it gets called on kick(), it will return because the user isn't in the list // kick() doesnt wait until the user is actually removed from the list and itd be anal to make it do that
this.connectionClosed(old); // so we call connectionClosed manually here. When it gets called on kick(), it will return because the user isn't in the list
await old.kick(); this.connectionClosed(old);
} await old.kick();
// Set username }
this.renameUser(client, res.username); // Set username
// Set rank this.renameUser(client, res.username);
client.rank = res.rank; // Set rank
if (client.rank === Rank.Admin) { client.rank = res.rank;
client.sendMsg(guacutils.encode('admin', '0', '1')); if (client.rank === Rank.Admin) {
} else if (client.rank === Rank.Moderator) { client.sendMsg(guacutils.encode('admin', '0', '1'));
client.sendMsg(guacutils.encode('admin', '0', '3', this.ModPerms.toString())); } else if (client.rank === Rank.Moderator) {
} 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()))); }
} else { this.clients.forEach((c) => c.sendMsg(guacutils.encode('adduser', '1', client.username!, client.rank.toString())));
client.sendMsg(guacutils.encode('login', '0', res.error!)); } else {
if (res.error === 'You are banned') { client.sendMsg(guacutils.encode('login', '0', res.error!));
client.kick(); if (res.error === 'You are banned') {
client.kick();
}
} }
} catch(err) {
this.logger.Error(`Error authenticating client ${client.IP.address}: ${(err as Error).message}`);
// for now?
client.sendMsg(guacutils.encode('login', '0', 'There was an internal error while authenticating. Please let a staff member know as soon as possible'))
} }
break; break;
case 'list': case 'list':
@@ -915,7 +921,10 @@ export default class WSServer {
async getThumbnail(): Promise<string> { async getThumbnail(): Promise<string> {
let display = this.VM.GetDisplay(); let display = this.VM.GetDisplay();
if (!display.Connected()) throw new Error('VM display is not connected');
// oh well
if (!display.Connected())
return "";
let buf = await JPEGEncoder.EncodeThumbnail(display.Buffer(), display.Size()); let buf = await JPEGEncoder.EncodeThumbnail(display.Buffer(), display.Size());
return buf.toString('base64'); return buf.toString('base64');