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 |
||||||
|
); |
@ -1,31 +1,42 @@ |
|||||||
import * as mediaserver from "./server"; |
import * as mediaserver from "./server"; |
||||||
import * as ircd from "./ircd"; |
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{ |
function boot(): void{ |
||||||
const mediaconfig: any = { |
db.run(dbconfig); |
||||||
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]' |
|
||||||
} |
|
||||||
] |
|
||||||
} |
|
||||||
}; |
|
||||||
mediaserver.boot(mediaconfig); |
mediaserver.boot(mediaconfig); |
||||||
ircd.boot(); |
ircd.boot(); |
||||||
} |
} |
||||||
|
@ -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); |
||||||
|
let app: string = StreamPath.split("/")[1]; |
||||||
|
let key: string = StreamPath.split("/")[2]; |
||||||
if (StreamPath.split("/").length > 3){ |
if (StreamPath.split("/").length > 3){ |
||||||
console.log("[NodeMediaServer] Malformed URL, closing connection."); |
console.log("[NodeMediaServer] Malformed URL, closing connection for stream:",id); |
||||||
session.reject(); |
session.reject(); |
||||||
return false; |
return false; |
||||||
} |
} |
||||||
let app: string = StreamPath.split("/")[1]; |
if(app === "live") { |
||||||
let key: string = StreamPath.split("/")[2]; |
isLocal(session.ip).then( (local) => { |
||||||
console.log("[NodeMediaServer] Authenticating stream with credentials: ",`app=${app} key=${key}`); |
if(local) { |
||||||
if (app !== "stream"){ |
console.log("[NodeMediaServer] Local publish, stream:",`${id} ok.`); |
||||||
console.log("[NodeMediaServer] Invalid app name, closing connection."); |
} |
||||||
|
else{ |
||||||
|
console.log("[NodeMediaServer] Non-local Publish to /live, rejecting stream:",id); |
||||||
|
session.reject(); |
||||||
|
} |
||||||
|
}); |
||||||
|
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); |
||||||
|
} |
||||||
|
}); |
||||||
|
return true; |
||||||
|
} |
||||||
|
if(app !== "stream"){ |
||||||
|
//app isn't 'live' if we've reached this point
|
||||||
|
console.log("[NodeMediaServer] Wrong endpoint, rejecting stream:",id); |
||||||
session.reject(); |
session.reject(); |
||||||
return false; |
return false; |
||||||
} |
} |
||||||
console.log("[NodeMediaServer] App name ok."); |
db.raw.query('select username from users where stream_key=\''+key+'\' limit 1', (error, results, fields) => { |
||||||
//TODO: Hook up to DB and redirect from query
|
if (error) {throw error;} |
||||||
if (key !== "temp"){ |
if(results[0]){ |
||||||
console.log("[NodeMediaServer] Invalid stream key, closing connection."); |
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(); |
session.reject(); |
||||||
return false; |
return false; |
||||||
} |
} |
||||||
console.log("[NodeMediaServer] Stream key ok."); |
if(app === "stream") { |
||||||
session.publishStreamPath = "/live/amy"; |
isLocal(session.ip).then( (local) => { |
||||||
}); |
if(local) { |
||||||
|
console.log("[NodeMediaServer] Local play, client:",`${id} ok.`); |
||||||
nms.on('postPublish', (id, StreamPath, args) => { |
} |
||||||
console.log('[NodeMediaServer] Checking record flag for ', `id=${id} StreamPath=${StreamPath}`); |
else{ |
||||||
//Hook up to postgres DB.
|
console.log("[NodeMediaServer] Non-local Play from /stream, rejecting client:",id); |
||||||
if(true){ |
session.reject(); |
||||||
console.log('[NodeMediaServer] Initiating recording for ', `id=${id} StreamPath=${StreamPath}`); |
} |
||||||
mkdir('./media'+StreamPath, { recursive : true }, (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',{ |
|
||||||
detached : true, |
|
||||||
stdio : 'inherit' |
|
||||||
}); |
|
||||||
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}`); |
|
||||||
}); |
}); |
||||||
} |
} |
||||||
export { boot }; |
export { boot }; |
Reference in new issue