seepur/app/Models/IceServer.js
2020-05-13 22:36:24 -04:00

33 lines
906 B
JavaScript

'use strict'
/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
const Model = use('Model')
const {v4: uuidv4} = require('uuid');
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