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.merge-requests/1/merge
parent
577612cee5
commit
935b850bcd
@ -0,0 +1,11 @@ |
|||||||
|
CREATE USER 'satyr'@'localhost' IDENTIFIED BY 'password'; |
||||||
|
CREATE DATABASE satyr_db; |
||||||
|
GRANT ALL PRIVILEGES ON satyr_db.* TO 'satyr'@'localhost'; |
||||||
|
USE satyr_db; |
||||||
|
CREATE TABLE users( |
||||||
|
username VARCHAR(25), |
||||||
|
password_hash BINARY(60), |
||||||
|
stream_key CHAR(20), |
||||||
|
record_flag TINYINT, |
||||||
|
is_mod TINYINT |
||||||
|
); |
@ -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 }; |
@ -1,82 +1,92 @@ |
|||||||
import * as NodeMediaServer from "node-media-server"; |
import * as NodeMediaServer from "node-media-server"; |
||||||
import { mkdir } from "fs"; |
import { mkdir } from "fs"; |
||||||
|
import * as db from "./database"; |
||||||
|
const isLocal = require("check-localhost"); |
||||||
const { exec } = require('child_process'); |
const { exec } = require('child_process'); |
||||||
|
|
||||||
//initialize configs, eventually grab from runtime config file
|
function boot (mediaconfig: any) { |
||||||
function initConfig(): void{ |
const nms = new NodeMediaServer(mediaconfig); |
||||||
; |
|
||||||
} |
|
||||||
|
|
||||||
/*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); |
|
||||||
|
|
||||||
nms.run(); |
nms.run(); |
||||||
|
|
||||||
nms.on('prePublish', (id, StreamPath, args) => { |
nms.on('postPublish', (id, StreamPath, args) => { |
||||||
console.log("[NodeMediaServer] Prepublish Hook for stream id=",id); |
console.log("[NodeMediaServer] Prepublish Hook for stream:",id); |
||||||
let session = nms.getSession(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 app: string = StreamPath.split("/")[1]; |
||||||
let key: string = StreamPath.split("/")[2]; |
let key: string = StreamPath.split("/")[2]; |
||||||
console.log("[NodeMediaServer] Authenticating stream with credentials: ",`app=${app} key=${key}`); |
if (StreamPath.split("/").length > 3){ |
||||||
if (app !== "stream"){ |
console.log("[NodeMediaServer] Malformed URL, closing connection for stream:",id); |
||||||
console.log("[NodeMediaServer] Invalid app name, closing connection."); |
|
||||||
session.reject(); |
session.reject(); |
||||||
return false; |
return false; |
||||||
} |
} |
||||||
console.log("[NodeMediaServer] App name ok."); |
if(app === "live") { |
||||||
//TODO: Hook up to DB and redirect from query
|
isLocal(session.ip).then( (local) => { |
||||||
if (key !== "temp"){ |
if(local) { |
||||||
console.log("[NodeMediaServer] Invalid stream key, closing connection."); |
console.log("[NodeMediaServer] Local publish, stream:",`${id} ok.`); |
||||||
|
} |
||||||
|
else{ |
||||||
|
console.log("[NodeMediaServer] Non-local Publish to /live, rejecting stream:",id); |
||||||
session.reject(); |
session.reject(); |
||||||
return false; |
|
||||||
} |
} |
||||||
console.log("[NodeMediaServer] Stream key ok."); |
|
||||||
session.publishStreamPath = "/live/amy"; |
|
||||||
}); |
}); |
||||||
|
console.log("[NodeMediaServer] Public endpoint, checking record flag."); |
||||||
nms.on('postPublish', (id, StreamPath, args) => { |
db.raw.query('select username,record_flag from users where username=\''+key+'\' and record_flag=true limit 1', (error, results, fields) => { |
||||||
console.log('[NodeMediaServer] Checking record flag for ', `id=${id} StreamPath=${StreamPath}`); |
if (error) {throw error;} |
||||||
//Hook up to postgres DB.
|
if(results[0]){ |
||||||
if(true){ |
console.log('[NodeMediaServer] Initiating recording for stream:',id); |
||||||
console.log('[NodeMediaServer] Initiating recording for ', `id=${id} StreamPath=${StreamPath}`); |
mkdir('./site/live/'+results[0].username, { recursive : true }, (err) => { |
||||||
mkdir('./media'+StreamPath, { recursive : true }, (err) => { |
|
||||||
if (err) throw err; |
if (err) throw err; |
||||||
}); |
}); |
||||||
let subprocess = exec('ffmpeg -i rtmp://127.0.0.1'+StreamPath+' -vcodec copy -acodec copy ./media'+StreamPath+'/$(date +%d%b%Y-%H%M).mp4',{ |
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, |
detached : true, |
||||||
stdio : 'inherit' |
stdio : 'inherit' |
||||||
}); |
}); |
||||||
subprocess.unref(); |
subprocess.unref(); |
||||||
//spawn an ffmpeg process to record the stream, then detach it completely
|
//spawn an ffmpeg process to record the stream, then detach it completely
|
||||||
|
} |
||||||
|
else { |
||||||
|
console.log('[NodeMediaServer] Skipping recording for stream:',id); |
||||||
|
} |
||||||
|
}); |
||||||
return true; |
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 }; |
export { boot }; |
Reference in new issue