1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-12-27 14:08:53 +00:00

Send ICE configuration to server

This commit is contained in:
Pierre HUBERT 2020-04-10 10:05:10 +02:00
parent e8a0e70ffe
commit 6aec00b3f4
3 changed files with 42 additions and 6 deletions

View File

@ -14,6 +14,9 @@
"rtc_relay": {
"ip": "::ffff:127.0.0.1",
"token": "SecretToken"
"token": "SecretToken",
"iceServers": [
"stun:stun.l.google.com:19302"
]
}
}

View File

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

View File

@ -18,6 +18,7 @@ export interface DatabaseConfiguration {
export interface RTCRelayConfiguration {
ip ?: string,
token: string,
iceServers: string[]
}
export interface Configuration {