2020-04-10 07:46:44 +00:00
|
|
|
import * as ws from 'ws';
|
|
|
|
import { Request } from 'express';
|
|
|
|
import { conf } from '../helpers/ConfigHelper';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RTC WebSocket relay controller
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
2020-04-10 08:23:32 +00:00
|
|
|
export interface RTCSocketMessage {
|
|
|
|
title: string,
|
|
|
|
callId ?: string,
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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");
|
|
|
|
|
|
|
|
// TODO : do cleanup
|
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,
|
|
|
|
callId: msg.callId,
|
|
|
|
peerId: msg.peerId,
|
|
|
|
data: msg.data
|
2020-04-10 08:05:10 +00:00
|
|
|
}));
|
|
|
|
}
|
2020-04-10 07:46:44 +00:00
|
|
|
}
|