mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2025-02-16 22:22:39 +00:00
60 lines
1.1 KiB
TypeScript
60 lines
1.1 KiB
TypeScript
|
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
|
||
|
}
|
||
|
|
||
|
}
|