fix: use 401 when disconnecting for invalid api key

This commit is contained in:
Alexis Tyler
2021-06-01 17:46:30 +09:30
parent ad750f5753
commit ee21f0b2ec
3 changed files with 17 additions and 5 deletions

View File

@@ -56,7 +56,7 @@ export const myservers = () => {
mothership.close(true, true);
// Disconnect from relay
await sockets.get('relay')?.disconnect();
await sockets.get('relay')?.disconnect(4401);
}
// If we have a my_servers key reconnect to mothership

View File

@@ -103,11 +103,11 @@ am(async () => {
// Disconnect relay
apiManagerLogger.debug('Disconnecting relay');
await sockets.get('relay')?.disconnect();
await sockets.get('relay')?.disconnect(4401);
// Disconnect internal graphql
apiManagerLogger.debug('Disconnecting internalGraphql');
await sockets.get('internalGraphql')?.disconnect();
await sockets.get('internalGraphql')?.disconnect(4401);
} catch (error: unknown) {
apiManagerLogger.error('Failed updating sockets on "expire" event with error %s.', error);
}

View File

@@ -142,10 +142,22 @@ export class CustomSocket {
}
}
public async disconnect() {
public async disconnect(code?: number, message?: string) {
const lock = await this.getLock();
try {
if (this.connection && (this.connection.readyState !== this.connection.CLOSED)) {
// Don't try and disconnect if there's no connection
if (!this.connection) {
return;
}
// If there's a custom code pass it to the close method
if (code) {
this.connection.close(code, message);
return;
}
// Fallback to a "ok" disconnect
if (this.connection.readyState !== this.connection.CLOSED) {
// 4200 === ok
this.connection.close(4200, JSON.stringify({
message: 'OK'