From 6aec00b3f4ab35a579410f59c367843645bb653e Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 10 Apr 2020 10:05:10 +0200 Subject: [PATCH] Send ICE configuration to server --- config.json | 5 +++- src/controllers/RTCRelayController.ts | 42 +++++++++++++++++++++++---- src/helpers/ConfigHelper.ts | 1 + 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/config.json b/config.json index 076de95..799586b 100644 --- a/config.json +++ b/config.json @@ -14,6 +14,9 @@ "rtc_relay": { "ip": "::ffff:127.0.0.1", - "token": "SecretToken" + "token": "SecretToken", + "iceServers": [ + "stun:stun.l.google.com:19302" + ] } } \ No newline at end of file diff --git a/src/controllers/RTCRelayController.ts b/src/controllers/RTCRelayController.ts index 71d8b44..278b9bc 100644 --- a/src/controllers/RTCRelayController.ts +++ b/src/controllers/RTCRelayController.ts @@ -24,7 +24,7 @@ export class RTCRelayController { public static async OpenWS(req: Request, ws: ws) { // First, check if sockets are enabled - if(!conf().rtc_relay) { + if (!conf().rtc_relay) { ws.send("rtc relay not configured!"); ws.close(); return; @@ -33,21 +33,21 @@ export class RTCRelayController { const cnf = conf().rtc_relay; // Then check remote IP address - if(cnf.ip && cnf.ip != req.ip) { - ws.send("unkown IP address : "+req.ip) + if (cnf.ip && cnf.ip != req.ip) { + ws.send("unkown IP address : " + req.ip) ws.close(); return; } // Finally, check access token - if(!req.query.hasOwnProperty("token") || req.query.token !== cnf.token) { + if (!req.query.hasOwnProperty("token") || req.query.token !== cnf.token) { ws.send("invalid token!"); ws.close(); return; } // Close previous connection - if(this.currWs && this.currWs.readyState == ws.OPEN) { + if (this.currWs && this.currWs.readyState == ws.OPEN) { this.currWs.close(); } @@ -55,6 +55,38 @@ export class RTCRelayController { this.currWs = ws; // Register to events + ws.addEventListener("close", () => this.WSClosed()); + + // Send ice configuration to server + this.SendMessage("config", { + iceServers: cnf.iceServers + }) } + /** + * 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 + } + + /** + * Send a new message to the server + * + * @param title The title of the message to send + * @param content The content of the message + */ + private static async SendMessage(title: string, content: any) { + this.currWs.send(JSON.stringify({ + title: title, + data: content + })); + } } \ No newline at end of file diff --git a/src/helpers/ConfigHelper.ts b/src/helpers/ConfigHelper.ts index 0184523..0a91d51 100644 --- a/src/helpers/ConfigHelper.ts +++ b/src/helpers/ConfigHelper.ts @@ -18,6 +18,7 @@ export interface DatabaseConfiguration { export interface RTCRelayConfiguration { ip ?: string, token: string, + iceServers: string[] } export interface Configuration {