1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 05:19:22 +00:00

Accept connection from RTC relay

This commit is contained in:
Pierre HUBERT 2020-04-10 09:46:44 +02:00
parent 98a43a4fd8
commit e8a0e70ffe
3 changed files with 67 additions and 1 deletions

View File

@ -13,7 +13,7 @@
"force_clients_https": null, "force_clients_https": null,
"rtc_relay": { "rtc_relay": {
"ip": "127.0.0.1", "ip": "::ffff:127.0.0.1",
"token": "SecretToken" "token": "SecretToken"
} }
} }

View File

@ -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
}
}

View File

@ -19,6 +19,7 @@ import { SettingsController } from "./SettingsController";
import { Request } from "express"; import { Request } from "express";
import * as ws from 'ws'; import * as ws from 'ws';
import { UserWebSocketController } from "./UserWebSocketController"; import { UserWebSocketController } from "./UserWebSocketController";
import { RTCRelayController } from "./RTCRelayController";
/** /**
* Controllers routes * 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) }, {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 // Welcome controller
{type: RouteType.GET, path: "/", cb: WelcomeController.HomeMessage, needLogin: false}, {type: RouteType.GET, path: "/", cb: WelcomeController.HomeMessage, needLogin: false},