From e8a0e70ffe0b01d35fef584b1d7c3bc7cab66c2d Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Fri, 10 Apr 2020 09:46:44 +0200 Subject: [PATCH] Accept connection from RTC relay --- config.json | 2 +- src/controllers/RTCRelayController.ts | 60 +++++++++++++++++++++++++++ src/controllers/Routes.ts | 6 +++ 3 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/controllers/RTCRelayController.ts diff --git a/config.json b/config.json index b6a4144..076de95 100644 --- a/config.json +++ b/config.json @@ -13,7 +13,7 @@ "force_clients_https": null, "rtc_relay": { - "ip": "127.0.0.1", + "ip": "::ffff:127.0.0.1", "token": "SecretToken" } } \ No newline at end of file diff --git a/src/controllers/RTCRelayController.ts b/src/controllers/RTCRelayController.ts new file mode 100644 index 0000000..71d8b44 --- /dev/null +++ b/src/controllers/RTCRelayController.ts @@ -0,0 +1,60 @@ +import * as ws from 'ws'; +import { Request } from 'express'; +import { conf } from '../helpers/ConfigHelper'; + +/** + * RTC WebSocket relay controller + * + * @author Pierre HUBERT + */ + +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 + if(!conf().rtc_relay) { + ws.send("rtc relay not configured!"); + ws.close(); + return; + } + + const cnf = conf().rtc_relay; + + // Then check remote IP address + 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) { + ws.send("invalid token!"); + ws.close(); + return; + } + + // Close previous connection + if(this.currWs && this.currWs.readyState == ws.OPEN) { + this.currWs.close(); + } + + console.info("Connected to RTC relay."); + this.currWs = ws; + + // Register to events + } + +} \ No newline at end of file diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 374486a..21876e5 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -19,6 +19,7 @@ import { SettingsController } from "./SettingsController"; import { Request } from "express"; import * as ws from 'ws'; import { UserWebSocketController } from "./UserWebSocketController"; +import { RTCRelayController } from "./RTCRelayController"; /** * Controllers routes @@ -49,6 +50,11 @@ export const Routes : Route[] = [ {type: RouteType.WS, path: "/ws", cb: () => {throw Error()}, wsCallback: (r, w) => UserWebSocketController.UserWS(r, w) }, + + // RTC relay control websocket + {type: RouteType.WS, path: "/rtc_proxy/ws", cb: () => {throw Error()}, wsCallback: (r, w) => RTCRelayController.OpenWS(r, w)}, + + // Welcome controller {type: RouteType.GET, path: "/", cb: WelcomeController.HomeMessage, needLogin: false},