From 31426a0c4168a549a6191f59e4fcd2cd1dcfcad2 Mon Sep 17 00:00:00 2001
From: knotteye <knotteye@airmail.cc>
Date: Sat, 30 Nov 2019 14:15:31 -0600
Subject: [PATCH] Fix a bug where we treated local clients the same as foreign
 ones

---
 src/server.ts | 59 +++++++++++----------------------------------------
 1 file changed, 12 insertions(+), 47 deletions(-)

diff --git a/src/server.ts b/src/server.ts
index d011e00..a52d807 100644
--- a/src/server.ts
+++ b/src/server.ts
@@ -23,40 +23,6 @@ function init (mediaconfig: any, satyrconfig: any) {
 			session.reject();
 			return false;
 		}
-		/*if(app === satyrconfig.publicEndpoint) {
-			if(session.isLocal) {
-				//only allow publish to public endpoint from localhost
-				console.log("[NodeMediaServer] Local publish, stream:",`${id} ok.`);
-			}
-			else{
-				console.log("[NodeMediaServer] Non-local Publish to public endpoint, rejecting stream:",id);
-				session.reject();
-				return false;
-			}
-			console.log("[NodeMediaServer] Public endpoint, checking record flag.");
-			//set live flag
-			db.query('update user_meta set live=true where username=\''+key+'\' limit 1');
-			//if this stream is from the public endpoint, check if we should be recording
-			return db.query('select username,record_flag from users where username=\''+key+'\' limit 1').then((results) => {
-				if(results[0].record_flag && satyrconfig.record){
-					console.log('[NodeMediaServer] Initiating recording for stream:',id);
-					mkdir(satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username, { recursive : true }, (err) => {
-						if (err) throw err;
-						execFile(satyrconfig.ffmpeg, ['-loglevel', 'fatal', '-i', 'rtmp://127.0.0.1:'+mediaconfig.rtmp.port+'/'+satyrconfig.publicEndpoint+'/'+results[0].username, '-vcodec', 'copy', '-acodec', 'copy', satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username+'/'+strf('%d%b%Y-%H%M')+'.mp4'], {
-							detached : true,
-							stdio : 'inherit',
-							maxBuffer: Infinity
-						}).unref();
-						//spawn an ffmpeg process to record the stream, then detach it completely
-						//ffmpeg can then (probably) finalize the recording if satyr crashes mid-stream
-					});
-				}
-				else {
-					console.log('[NodeMediaServer] Skipping recording for stream:',id);
-				}
-				return true;
-			});
-		}*/
 		if(app !== satyrconfig.privateEndpoint){
 			//app isn't at public endpoint if we've reached this point
 			console.log("[NodeMediaServer] Wrong endpoint, rejecting stream:",id);
@@ -72,16 +38,14 @@ function init (mediaconfig: any, satyrconfig: any) {
 				//execFile(satyrconfig.ffmpeg, ['-loglevel', 'fatal', '-analyzeduration', '0', '-i', 'rtmp://127.0.0.1:'+mediaconfig.rtmp.port+'/'+satyrconfig.privateEndpoint+'/'+key, '-vcodec', 'copy', '-acodec', 'copy', '-crf', '18', '-f', 'flv', 'rtmp://127.0.0.1:'+mediaconfig.rtmp.port+'/'+satyrconfig.publicEndpoint+'/'+results[0].username], {maxBuffer: Infinity});
 				//push to mpd after making sure directory exists
 				keystore[results[0].username] = key;
-				mkdir(satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username, { recursive : true }, async (err) => {
-					if (err) throw err;
-					while(true){
-						if(session.audioCodec !== 0 && session.videoCodec !== 0){
-							execFile(satyrconfig.ffmpeg, ['-loglevel', 'fatal', '-y', '-i', 'rtmp://127.0.0.1:'+mediaconfig.rtmp.port+'/'+satyrconfig.privateEndpoint+'/'+key, '-map', '0:2', '-map', '0:2', '-map', '0:2', '-map', '0:1', '-c:a', 'copy', '-c:v:0', 'copy', '-c:v:1', 'libx264', '-c:v:2', 'libx264', '-crf:1', '33', '-crf:2', '40', '-b:v:1', '3000K', '-b:v:2', '1500K', '-remove_at_exit', '1', '-seg_duration', '1', '-window_size', '30', '-f', 'dash', satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username+'/index.mpd'], {maxBuffer: Infinity});
-							break;
-						}
-						await sleep(300);
+				mkdir(satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username, { recursive : true }, ()=>{;});
+				while(true){
+					if(session.audioCodec !== 0 && session.videoCodec !== 0){
+						execFile(satyrconfig.ffmpeg, ['-loglevel', 'fatal', '-y', '-i', 'rtmp://127.0.0.1:'+mediaconfig.rtmp.port+'/'+satyrconfig.privateEndpoint+'/'+key, '-map', '0:2', '-map', '0:2', '-map', '0:2', '-map', '0:1', '-c:a', 'copy', '-c:v:0', 'copy', '-c:v:1', 'libx264', '-c:v:2', 'libx264', '-crf:1', '33', '-crf:2', '40', '-b:v:1', '3000K', '-b:v:2', '1500K', '-remove_at_exit', '1', '-seg_duration', '1', '-window_size', '30', '-f', 'dash', satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username+'/index.mpd'], {maxBuffer: Infinity});
+						break;
 					}
-				});
+					await sleep(300);
+				}
 				if(results[0].record_flag && satyrconfig.record){
 					console.log('[NodeMediaServer] Initiating recording for stream:',id);
 					mkdir(satyrconfig.directory+'/'+satyrconfig.publicEndpoint+'/'+results[0].username, { recursive : true }, (err) => {
@@ -128,20 +92,21 @@ function init (mediaconfig: any, satyrconfig: any) {
 			session.reject();
 			return false;
 		}
-		//disallow playing from the private endpoint for anyone except localhost
-		//(this will be the ffmpeg instance redirecting the stream)
-		if(app === satyrconfig.privateEndpoint && !session.isLocal) {
+		//localhost can play from whatever endpoint
+		//other clients must use private endpoint
+		if(app !== satyrconfig.publicEndpoint && !session.isLocal) {
 			console.log("[NodeMediaServer] Non-local Play from private endpoint, rejecting client:",id);
 			session.reject();
 			return false;
 		}
+		//rewrite playpath to private endpoint serverside
+		//(hopefully)
 		if(app === satyrconfig.publicEndpoint) {
 			if(keystore[key]){
 				session.playStreamPath = '/'+satyrconfig.privateEndpoint+'/'+keystore[key];
 				return true;
 			}
 		}
-		session.reject();
 	});
 }
 export { init };
\ No newline at end of file