feat(utilities): changed extractPeerId to validate project_key length
This commit is contained in:
parent
108452d05c
commit
207638fca2
1 changed files with 14 additions and 9 deletions
|
|
@ -1,11 +1,16 @@
|
|||
var express = require('express');
|
||||
var peerRouter = express.Router();
|
||||
|
||||
|
||||
let PROJECT_KEY_LENGTH = parseInt(process.env.PROJECT_KEY_LENGTH) || 20;
|
||||
let debug = process.env.debug === "1" || false;
|
||||
const extractPeerId = (peerId) => {
|
||||
let splited = peerId.split("-");
|
||||
if (splited.length !== 2) {
|
||||
console.error(`cannot split peerId: ${peerId}`);
|
||||
debug && console.error(`cannot split peerId: ${peerId}`);
|
||||
return {};
|
||||
}
|
||||
if (PROJECT_KEY_LENGTH > 0 && splited[0].length !== PROJECT_KEY_LENGTH) {
|
||||
debug && console.error(`wrong project key length for peerId: ${peerId}`);
|
||||
return {};
|
||||
}
|
||||
return {projectKey: splited[0], sessionId: splited[1]};
|
||||
|
|
@ -13,30 +18,30 @@ const extractPeerId = (peerId) => {
|
|||
const connectedPeers = {};
|
||||
|
||||
const peerConnection = (client) => {
|
||||
console.log(`initiating ${client.id}`);
|
||||
debug && console.log(`initiating ${client.id}`);
|
||||
const {projectKey, sessionId} = extractPeerId(client.id);
|
||||
if (projectKey === undefined || sessionId === undefined) {
|
||||
return;
|
||||
}
|
||||
connectedPeers[projectKey] = connectedPeers[projectKey] || [];
|
||||
if (connectedPeers[projectKey].indexOf(sessionId) === -1) {
|
||||
console.log(`new connexion ${client.id}`);
|
||||
debug && console.log(`new connexion ${client.id}`);
|
||||
connectedPeers[projectKey].push(sessionId);
|
||||
} else {
|
||||
console.log(`reconnecting peer ${client.id}`);
|
||||
debug && console.log(`reconnecting peer ${client.id}`);
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
const peerDisconnect = (client) => {
|
||||
console.log(`disconnect ${client.id}`);
|
||||
debug && console.log(`disconnect ${client.id}`);
|
||||
const {projectKey, sessionId} = extractPeerId(client.id);
|
||||
if (projectKey === undefined || sessionId === undefined) {
|
||||
return;
|
||||
}
|
||||
const i = (connectedPeers[projectKey] || []).indexOf(sessionId);
|
||||
if (i === -1) {
|
||||
console.log(`session not found ${client.id}`);
|
||||
debug && console.log(`session not found ${client.id}`);
|
||||
} else {
|
||||
connectedPeers[projectKey].splice(i, 1);
|
||||
}
|
||||
|
|
@ -49,13 +54,13 @@ const peerError = (error) => {
|
|||
|
||||
|
||||
peerRouter.get(`/${process.env.S3_KEY}/peers`, function (req, res) {
|
||||
console.log("looking for all available sessions");
|
||||
debug && console.log("looking for all available sessions");
|
||||
res.statusCode = 200;
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.end(JSON.stringify({"data": connectedPeers}));
|
||||
});
|
||||
peerRouter.get(`/${process.env.S3_KEY}/peers/:projectKey`, function (req, res) {
|
||||
console.log(`looking for available sessions for ${req.params.projectKey}`);
|
||||
debug && console.log(`looking for available sessions for ${req.params.projectKey}`);
|
||||
res.statusCode = 200;
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.end(JSON.stringify({"data": connectedPeers[req.params.projectKey] || []}));
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue