- Removing peers module for simplicity and reduction of data complexity

Basically the peer map system that we had before isn't going to work simply because
succesful client connections already have the data we need inside them.
Adding a whole wrapper around this just complicates things in a way that doesn't
really give any benefit.

For now every message is echo'd to all connections but that is easy enough to change
This commit is contained in:
shockrah 2021-04-01 17:21:57 -07:00
parent d8171f8b03
commit 715f334619
4 changed files with 29 additions and 95 deletions

View File

@ -13,6 +13,9 @@ exports.verify = function(token) {
* @retusn false on failure
*/
console.log('given token: ', token)
const vconfig = {
clockTolerance: 3,
}
try {
const decoded = jsonwebtoken.verify(token, USER_HMAC, vconfig);
return 'user'
@ -21,7 +24,6 @@ exports.verify = function(token) {
const decoded = jsonwebtoken.verify(token, SERVER_HMAC, {ignoreNotBefore: true})
return 'server'
} catch (err) {
console.log('failed server check: ', err)
return false
}
}

View File

@ -1,43 +1,44 @@
const ws = require('ws')
const peersapi = require('./peers.js')
const auth = require('./auth.js')
const server = new ws.Server({ port: 5648 })
const peers = new peersapi.PeerMap()
const server = new ws.Server({
port: 5648,
clientTracking: true
})
server.on('connection', function(ws, req) {
server.on('connection', function(socket, req) {
let [jwt, path] = auth.prepare_auth(req)
let conn = auth.verify(jwt)
if(conn == 'server') {
console.log('[WSS] New server connection')
// Peer map initialization
ws.on('open', function() {
peers.add_server(ws)
})
ws.on('close', function() {
console.log('server left')
peers.remove_server(ws)
socket.on('close', function() {
console.log('[WSS] Server connection closed')
})
ws.on('message', function(message) {
peers.notify(message)
socket.on('message', function(message) {
// do some parsing, then emit to everyone
server.clients.forEach(client => {
if (client !== socket && client.readyState === ws.OPEN) {
client.send(message)
}
})
})
} else if(conn == 'user') {
ws.on('open', function() {
console.log('adding with path', req.url)
peers.add_user(ws, req.url)
socket.on('message', function(message) {
// some kind of parsing here
// no actual emitting to all users
server.clients.forEach(client => {
if (client !== socket && client.readyState === ws.OPEN) {
client.send(message)
}
})
})
ws.on('close', function() {
peers.remove_user(ws)
})
console.log('[WSS] New user conncetion')
} else {
console.log('[WSS] No valid auth')
ws.close()
console.log('[WSS] No valid auth', conn)
socket.close()
}
})

View File

@ -4,7 +4,7 @@
"description": "RTC for Freechat",
"main": "main.js",
"scripts": {
"start": "node main.js",
"start": "node main.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {

View File

@ -1,69 +0,0 @@
class Event {
/**
* @param raw {String}
*
*/
constructor(raw) {
this._raw = raw
let obj = JSON.parse(raw)
this.type = obj['type']
if(this.type == 'message') {
this.message = obj['message']
} else if (this.type == 'channel') {
this.channel = obj['channel']
}
}
}
class Peer {
/*
* @param type {String}
* @param channel {String}
*/
constructor(type, channel) {
this.type = type
this.channel = channel
}
}
exports.PeerMap = class {
/*
* @field users Map<Socket, Peer>
* @field server Map<Socket, Peer>
*/
constructor() {
// Map of sockets -> Peer
this.users = {}
this.server = {}
}
/**
* @params message {String|JSON} Used for emitting server messages to user peers
*/
notify(message) {
let e_type = new Event(message)
if(e_type.type) {
for(const conn of Object.keys(this.users)) {
}
} else {
console.log('[WSS] Invalid event type given from server')
}
}
// As the names imply these methods help maintain the peer map
add_server(socket) {
this.server[socket] = new Peer('server', null)
}
remove_server(socket) {
delete this.server[socket]
}
add_user(socket, channel) {
this.users[socket] = new Peer('user', channel)
}
remove_user(socket) {
delete this.user[socket]
}
}