seepur/app/Models/IceServer.js
2020-04-12 10:25:42 -04:00

33 lines
903 B
JavaScript

'use strict'
/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
const Model = use('Model')
const uuidv4 = require('uuid').v4;
const crypto = require('crypto')
class IceServer extends Model {
toJSON() {
const json = {};
if (this.type === 'STUN') {
json.urls = `stun:${this.url}:${this.port}`;
} else {
json.urls = '';
json.username = `${Math.ceil(Date.now() / 1000)}:${uuidv4()}`
json.credential = this.getCredentials(json.username);
if (this.protocol === 'UDP') {
json.urls = `turn:${this.url}:${this.port}?transport=udp`;
} else if (this.protocol === 'TCP') {
json.urls = `turn:${this.url}:${this.port}?transport=tcp`;
}
}
return json;
}
getCredentials(username) {
return crypto.createHmac('sha1', this.secret)
.update(username)
.digest('base64');
}
}
module.exports = IceServer