debug: delay websocket close and detail auth error
This commit is contained in:
@@ -30,10 +30,12 @@ export function setupWebSocket(server, hub, limiter) {
|
|||||||
|
|
||||||
// Hand over control to 'ws' library to complete the upgrade protocol handshake.
|
// Hand over control to 'ws' library to complete the upgrade protocol handshake.
|
||||||
wss.handleUpgrade(request, socket, head, (wsConn) => {
|
wss.handleUpgrade(request, socket, head, (wsConn) => {
|
||||||
// Set remote address on socket mock for later client IP queries.
|
if (wsConn._socket) {
|
||||||
// This ensures we can resolve client IP even after connection is upgraded.
|
Object.defineProperty(wsConn._socket, 'remoteAddress', {
|
||||||
wsConn._socket = wsConn._socket || {};
|
value: ip,
|
||||||
wsConn._socket.remoteAddress = ip;
|
configurable: true
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Instantiate a new Client wrapper.
|
// Instantiate a new Client wrapper.
|
||||||
// Event listeners are automatically attached in the Client constructor.
|
// Event listeners are automatically attached in the Client constructor.
|
||||||
|
|||||||
@@ -213,14 +213,14 @@ export class Hub {
|
|||||||
if (!client.userID) {
|
if (!client.userID) {
|
||||||
if (base.type !== protocol.TypeAuthenticate) {
|
if (base.type !== protocol.TypeAuthenticate) {
|
||||||
client.send(protocol.newError(protocol.ErrTokenInvalid, 'Authentication required'));
|
client.send(protocol.newError(protocol.ErrTokenInvalid, 'Authentication required'));
|
||||||
client.close();
|
setTimeout(() => client.close(), 200);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { session_id, user_id } = base;
|
const { session_id, user_id } = base;
|
||||||
if (!session_id || !user_id) {
|
if (!session_id || !user_id) {
|
||||||
client.send(protocol.newError(protocol.ErrTokenInvalid, 'Missing session_id or user_id'));
|
client.send(protocol.newError(protocol.ErrTokenInvalid, 'Missing session_id or user_id'));
|
||||||
client.close();
|
setTimeout(() => client.close(), 200);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -228,13 +228,13 @@ export class Hub {
|
|||||||
const sess = this.store.getSession(session_id);
|
const sess = this.store.getSession(session_id);
|
||||||
if (!sess) {
|
if (!sess) {
|
||||||
client.send(protocol.newError(protocol.ErrSessionNotFound, 'No active session found'));
|
client.send(protocol.newError(protocol.ErrSessionNotFound, 'No active session found'));
|
||||||
client.close();
|
setTimeout(() => client.close(), 200);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sess.status === 'ended') {
|
if (sess.status === 'ended') {
|
||||||
client.send(protocol.newError(protocol.ErrSessionNotFound, 'Session already ended'));
|
client.send(protocol.newError(protocol.ErrSessionNotFound, 'Session already ended'));
|
||||||
client.close();
|
setTimeout(() => client.close(), 200);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -244,19 +244,19 @@ export class Hub {
|
|||||||
role = 'driver';
|
role = 'driver';
|
||||||
if (sess.driverConn) {
|
if (sess.driverConn) {
|
||||||
client.send(protocol.newError(protocol.ErrSessionExists, 'Driver already connected'));
|
client.send(protocol.newError(protocol.ErrSessionExists, 'Driver already connected'));
|
||||||
client.close();
|
setTimeout(() => client.close(), 200);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else if (user_id === sess.passengerID) {
|
} else if (user_id === sess.passengerID) {
|
||||||
role = 'passenger';
|
role = 'passenger';
|
||||||
if (sess.passengerConn) {
|
if (sess.passengerConn) {
|
||||||
client.send(protocol.newError(protocol.ErrSessionExists, 'Passenger already connected'));
|
client.send(protocol.newError(protocol.ErrSessionExists, 'Passenger already connected'));
|
||||||
client.close();
|
setTimeout(() => client.close(), 200);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
client.send(protocol.newError(protocol.ErrUnauthorizedUser, 'User not authorized for this session'));
|
client.send(protocol.newError(protocol.ErrUnauthorizedUser, `User not authorized for this session. Got: ${user_id}, expected driver: ${sess.driverID} or passenger: ${sess.passengerID}`));
|
||||||
client.close();
|
setTimeout(() => client.close(), 200);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user