seepur/app/Models/IceServer.js

34 lines
906 B
JavaScript
Raw Normal View History

2020-04-12 14:25:42 +00:00
'use strict'
/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
const Model = use('Model')
2020-05-14 02:36:24 +00:00
const {v4: uuidv4} = require('uuid');
2020-04-12 14:25:42 +00:00
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