2024-11-10 20:42:02 -05:00
|
|
|
async function fetchRoomInfo(r) {
|
|
|
|
|
try {
|
|
|
|
|
// Make request to API to get room info
|
2024-11-10 22:52:04 -05:00
|
|
|
let res = await r.subrequest('/api/room/' + r.variables.room_id, {
|
|
|
|
|
method: 'GET'
|
|
|
|
|
});
|
2024-11-10 20:42:02 -05:00
|
|
|
|
|
|
|
|
if (res.status !== 200) {
|
|
|
|
|
r.error(`Failed to fetch room info: ${res.status}`);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-11-10 22:52:04 -05:00
|
|
|
|
|
|
|
|
let room = JSON.parse(res.responseText);
|
|
|
|
|
r.error(`Debug: Room info: ${JSON.stringify(room)}`); // Debug log
|
|
|
|
|
return room;
|
2024-11-10 20:42:02 -05:00
|
|
|
} catch (error) {
|
|
|
|
|
r.error(`Error fetching room info: ${error}`);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function routeWebSocket(r) {
|
|
|
|
|
try {
|
|
|
|
|
const roomInfo = await fetchRoomInfo(r);
|
|
|
|
|
|
|
|
|
|
if (!roomInfo || !roomInfo.host) {
|
2024-11-10 22:52:04 -05:00
|
|
|
r.error(`Debug: Invalid room info: ${JSON.stringify(roomInfo)}`);
|
2024-11-10 20:42:02 -05:00
|
|
|
r.return(404, 'Room not found or invalid');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-10 22:52:04 -05:00
|
|
|
// Make sure the host includes protocol if not already present
|
|
|
|
|
let proxyUrl = roomInfo.host;
|
|
|
|
|
if (!proxyUrl.startsWith('http://') && !proxyUrl.startsWith('https://')) {
|
|
|
|
|
proxyUrl = 'http://' + proxyUrl;
|
|
|
|
|
}
|
2024-11-10 20:42:02 -05:00
|
|
|
|
2024-11-10 22:52:04 -05:00
|
|
|
r.error(`Debug: Original URL: ${r.uri}`);
|
|
|
|
|
r.error(`Debug: Setting proxy target to: ${proxyUrl}`);
|
|
|
|
|
r.error(`Debug: Headers: ${JSON.stringify(r.headersIn)}`);
|
|
|
|
|
|
|
|
|
|
// Set the proxy target variable
|
|
|
|
|
r.variables.proxy_target = proxyUrl;
|
|
|
|
|
|
|
|
|
|
// Redirect to the websocket proxy
|
|
|
|
|
r.internalRedirect('@websocket_proxy');
|
|
|
|
|
|
2024-11-10 20:42:02 -05:00
|
|
|
} catch (error) {
|
|
|
|
|
r.error(`WebSocket routing error: ${error}`);
|
|
|
|
|
r.return(500, 'Internal routing error');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-10 22:52:04 -05:00
|
|
|
export default { routeWebSocket };
|