1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 21:39:22 +00:00
comunicapiv2/src/controllers/RTCRelayController.ts

113 lines
2.3 KiB
TypeScript
Raw Normal View History

2020-04-10 07:46:44 +00:00
import * as ws from 'ws';
import { Request } from 'express';
import { conf } from '../helpers/ConfigHelper';
2020-04-10 14:19:46 +00:00
import { EventsHelper } from '../helpers/EventsHelper';
2020-04-10 07:46:44 +00:00
/**
* RTC WebSocket relay controller
*
* @author Pierre HUBERT
*/
2020-04-10 08:23:32 +00:00
export interface RTCSocketMessage {
title: string,
2020-04-11 06:35:46 +00:00
callHash ?: string,
2020-04-10 08:23:32 +00:00
peerId ?: string,
data: any
}
2020-04-10 07:46:44 +00:00
export class RTCRelayController {
/**
* Current WebSocket connection
*/
private static currWs: ws;
/**
* Open new websocket
*
* @param req Request
* @param ws Websocket
*/
public static async OpenWS(req: Request, ws: ws) {
// First, check if sockets are enabled
2020-04-10 08:05:10 +00:00
if (!conf().rtc_relay) {
2020-04-10 07:46:44 +00:00
ws.send("rtc relay not configured!");
ws.close();
return;
}
const cnf = conf().rtc_relay;
// Then check remote IP address
2020-04-10 08:05:10 +00:00
if (cnf.ip && cnf.ip != req.ip) {
ws.send("unkown IP address : " + req.ip)
2020-04-10 07:46:44 +00:00
ws.close();
return;
}
// Finally, check access token
2020-04-10 08:05:10 +00:00
if (!req.query.hasOwnProperty("token") || req.query.token !== cnf.token) {
2020-04-10 07:46:44 +00:00
ws.send("invalid token!");
ws.close();
return;
}
// Close previous connection
2020-04-10 08:05:10 +00:00
if (this.currWs && this.currWs.readyState == ws.OPEN) {
2020-04-10 07:46:44 +00:00
this.currWs.close();
}
console.info("Connected to RTC relay.");
this.currWs = ws;
// Register to events
2020-04-10 08:05:10 +00:00
ws.addEventListener("close", () => this.WSClosed());
// Send ice configuration to server
2020-04-10 08:23:32 +00:00
this.SendMessage({
title: "config",
data: {
iceServers: cnf.iceServers
},
2020-04-10 08:05:10 +00:00
})
}
/**
* Check out wheter a relay is currently connected to the API
*/
public static get IsConnected() : boolean {
return this.currWs && this.currWs.readyState == ws.OPEN;
}
2020-04-10 08:05:10 +00:00
/**
* Method called when a websocket connection is closed
*/
private static async WSClosed() {
// Check if this is caused to a new connection
if (this.currWs.readyState != ws.OPEN)
this.currWs = null;
console.info("Closed a connection to RTC relay");
2020-04-10 14:19:46 +00:00
// Propagate information
await EventsHelper.Emit("rtc_relay_ws_closed", {});
2020-04-10 07:46:44 +00:00
}
2020-04-10 08:05:10 +00:00
/**
* Send a new message to the server
*
* @param title The title of the message to send
* @param content The content of the message
*/
2020-04-10 08:23:32 +00:00
private static async SendMessage(msg: RTCSocketMessage) {
2020-04-10 08:05:10 +00:00
this.currWs.send(JSON.stringify({
2020-04-10 08:23:32 +00:00
title: msg.title,
2020-04-11 06:35:46 +00:00
callHash: msg.callHash,
2020-04-10 08:23:32 +00:00
peerId: msg.peerId,
data: msg.data
2020-04-10 08:05:10 +00:00
}));
}
2020-04-10 07:46:44 +00:00
}