Hooked up server.ts to database, it now validates, redirects, and records based on database queries.

Added setup script for database.
Added database.ts to create and manage a pool of connections. Possibly abstracting query logic in the future.
Updated controller to instantiate database.ts.
This commit is contained in:
knotteye
2019-09-22 16:33:18 -05:00
parent 577612cee5
commit 935b850bcd
6 changed files with 171 additions and 87 deletions

View File

@ -1,31 +1,42 @@
import * as mediaserver from "./server";
import * as ircd from "./ircd";
import * as db from "./database";
const mediaconfig: any = {
rtmp: {
port: 1935,
chunk_size: 60000,
gop_cache: true,
ping: 30,
ping_timeout: 60
},
http: {
port:8000,
allow_origin: '*',
mediaroot: './site'
},
trans: {
ffmpeg: '/usr/bin/ffmpeg',
tasks: [
{
app: 'live',
hls: 'true',
hlsFlags: '[hls_time=2:hls_list_size=3:hls_flags=delete_segments]'
}
]
}
};
const dbconfig: any = {
connectionLimit: 50,
host : 'localhost',
user : 'satyr',
password : 'password',
database : 'satyr_db'
};
function boot(): void{
const mediaconfig: any = {
rtmp: {
port: 1935,
chunk_size: 60000,
gop_cache: true,
ping: 30,
ping_timeout: 60
},
http: {
port:8000,
allow_origin: '*',
mediaroot: './site'
},
trans: {
ffmpeg: '/usr/bin/ffmpeg',
tasks: [
{
app: 'live',
hls: 'true',
hlsFlags: '[hls_time=2:hls_list_size=3:hls_flags=delete_segments]'
}
]
}
};
db.run(dbconfig);
mediaserver.boot(mediaconfig);
ircd.boot();
}

24
src/database.ts Normal file
View File

@ -0,0 +1,24 @@
import * as mysql from "mysql";
import * as bcrypt from "bcrypt";
var raw: any;
function run (dbconfig: any){
raw = mysql.createPool(dbconfig);
}
function streamKeyAuth(key: string){
;
}
async function validatePassword(username: string, password: string){
raw.connect();
return raw.query('select password_hash from users where username=\''+username+'\' limit 1', (error, results, fields) => {
if (error) { throw error; }
return bcrypt.compare(password, results[0].password_hash, (err, result) =>{
if (err) { throw err; }
return result;
});
})
}
export { streamKeyAuth, validatePassword, raw, run };

View File

@ -1,82 +1,92 @@
import * as NodeMediaServer from "node-media-server";
import { mkdir } from "fs";
import * as db from "./database";
const isLocal = require("check-localhost");
const { exec } = require('child_process');
//initialize configs, eventually grab from runtime config file
function initConfig(): void{
;
}
/*function streamAuth(path: string){
if (path.split("/").length > 3){
console.log("[NodeMediaServer] Malformed URL, closing connection.");
return false;
}
let app: string = path.split("/")[1];
let key: string = path.split("/")[2];
console.log("[NodeMediaServer] Authenticating stream with credentials: ",`app=${app} key=${key}`);
if (app !== "stream"){
console.log("[NodeMediaServer] Invalid app name, closing connection.");
return false;
}
console.log("[NodeMediaServer] App name ok.");
if (key !== "temp"){
console.log("[NodeMediaServer] Invalid stream key, closing connection.");
return false;
}
console.log("[NodeMediaServer] Stream key ok.");
return true;
}*/
function boot (config: any){
const nms = new NodeMediaServer(config);
function boot (mediaconfig: any) {
const nms = new NodeMediaServer(mediaconfig);
nms.run();
nms.on('prePublish', (id, StreamPath, args) => {
console.log("[NodeMediaServer] Prepublish Hook for stream id=",id);
nms.on('postPublish', (id, StreamPath, args) => {
console.log("[NodeMediaServer] Prepublish Hook for stream:",id);
let session = nms.getSession(id);
if (StreamPath.split("/").length > 3){
console.log("[NodeMediaServer] Malformed URL, closing connection.");
session.reject();
return false;
}
let app: string = StreamPath.split("/")[1];
let key: string = StreamPath.split("/")[2];
console.log("[NodeMediaServer] Authenticating stream with credentials: ",`app=${app} key=${key}`);
if (app !== "stream"){
console.log("[NodeMediaServer] Invalid app name, closing connection.");
if (StreamPath.split("/").length > 3){
console.log("[NodeMediaServer] Malformed URL, closing connection for stream:",id);
session.reject();
return false;
}
console.log("[NodeMediaServer] App name ok.");
//TODO: Hook up to DB and redirect from query
if (key !== "temp"){
console.log("[NodeMediaServer] Invalid stream key, closing connection.");
session.reject();
return false;
}
console.log("[NodeMediaServer] Stream key ok.");
session.publishStreamPath = "/live/amy";
});
nms.on('postPublish', (id, StreamPath, args) => {
console.log('[NodeMediaServer] Checking record flag for ', `id=${id} StreamPath=${StreamPath}`);
//Hook up to postgres DB.
if(true){
console.log('[NodeMediaServer] Initiating recording for ', `id=${id} StreamPath=${StreamPath}`);
mkdir('./media'+StreamPath, { recursive : true }, (err) => {
if (err) throw err;
if(app === "live") {
isLocal(session.ip).then( (local) => {
if(local) {
console.log("[NodeMediaServer] Local publish, stream:",`${id} ok.`);
}
else{
console.log("[NodeMediaServer] Non-local Publish to /live, rejecting stream:",id);
session.reject();
}
});
let subprocess = exec('ffmpeg -i rtmp://127.0.0.1'+StreamPath+' -vcodec copy -acodec copy ./media'+StreamPath+'/$(date +%d%b%Y-%H%M).mp4',{
detached : true,
stdio : 'inherit'
console.log("[NodeMediaServer] Public endpoint, checking record flag.");
db.raw.query('select username,record_flag from users where username=\''+key+'\' and record_flag=true limit 1', (error, results, fields) => {
if (error) {throw error;}
if(results[0]){
console.log('[NodeMediaServer] Initiating recording for stream:',id);
mkdir('./site/live/'+results[0].username, { recursive : true }, (err) => {
if (err) throw err;
});
let subprocess = exec('ffmpeg -i rtmp://127.0.0.1/live/'+results[0].username+' -vcodec copy -acodec copy ./site/live/'+results[0].username+'/$(date +%d%b%Y-%H%M).mp4',{
detached : true,
stdio : 'inherit'
});
subprocess.unref();
//spawn an ffmpeg process to record the stream, then detach it completely
}
else {
console.log('[NodeMediaServer] Skipping recording for stream:',id);
}
});
subprocess.unref();
//spawn an ffmpeg process to record the stream, then detach it completely
return true;
}
console.log('[NodeMediaServer] Skipping recording for ', `id=${id} StreamPath=${StreamPath}`);
if(app !== "stream"){
//app isn't 'live' if we've reached this point
console.log("[NodeMediaServer] Wrong endpoint, rejecting stream:",id);
session.reject();
return false;
}
db.raw.query('select username from users where stream_key=\''+key+'\' limit 1', (error, results, fields) => {
if (error) {throw error;}
if(results[0]){
exec('ffmpeg -analyzeduration 0 -i rtmp://localhost/stream/'+key+' -vcodec copy -acodec copy -crf 18 -f flv rtmp://localhost:1935/live/'+results[0].username);
console.log('[NodeMediaServer] Stream key okay for stream:',id);
}
else{
console.log('[NodeMediaServer] Invalid stream key for stream:',id);
session.reject();
}
});
});
nms.on('prePlay', (id, StreamPath, args) => {
let session = nms.getSession(id);
let app: string = StreamPath.split("/")[1];
let key: string = StreamPath.split("/")[2];
if (StreamPath.split("/").length > 3){
console.log("[NodeMediaServer] Malformed URL, closing connection for stream:",id);
session.reject();
return false;
}
if(app === "stream") {
isLocal(session.ip).then( (local) => {
if(local) {
console.log("[NodeMediaServer] Local play, client:",`${id} ok.`);
}
else{
console.log("[NodeMediaServer] Non-local Play from /stream, rejecting client:",id);
session.reject();
}
});
}
});
}
export { boot };