diff --git a/public/ffmpeg-web.js b/public/ffmpeg-web.js index de1aaf4..d479780 100644 --- a/public/ffmpeg-web.js +++ b/public/ffmpeg-web.js @@ -148,13 +148,19 @@ function saveall(){ //// Terminal Page rendering //// function renderterminal(){ pagepurge(); - $('#pagecontent').append('
'); - socket.emit('getterminal'); + socket.emit('giveterm'); + $('#pagecontent').append('
'); + var terminalContainer = document.getElementById('terminal-container'); + var term = new Terminal({cursorBlink: true}); + term.open(terminalContainer); + term.setOption('theme', { foreground: '#00ff00' }); + socket.on('sendterm', function(data){ + term.write(data); + }); + term.on('data', function (data) { + socket.emit('termdata', data); + }); } -socket.on('sendterminal', function(data){ - pagepurge(); - $('#pagecontent').append('
Raw bash terminal here for users to test commands
'); -}); ///////////////////////// test function client side ////////////////////////////////////// socket.on('testout', function(out){ diff --git a/requirements.txt b/requirements.txt index c5d76eb..b61446c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,2 @@ -aiohttp -python-socketio +flask pyyaml \ No newline at end of file diff --git a/server.py b/server.py index dcc14c8..a2667a4 100644 --- a/server.py +++ b/server.py @@ -1,15 +1,21 @@ -from aiohttp import web -import socketio -import yaml -import os +from flask import Flask, send_from_directory, request +from flask_socketio import SocketIO import glob +import os +import pty import re +import select +import subprocess import time +import yaml +# Error logging only +import logging +log = logging.getLogger('werkzeug') +log.setLevel(logging.ERROR) # Websocket server -sio = socketio.AsyncServer(async_mode='aiohttp') -app = web.Application() -sio.attach(app) +app = Flask(__name__,static_folder="public") +sio = SocketIO(app) @@ -30,53 +36,52 @@ def build_list(extension): return all_files # Background job thread loop for file processing -async def processor(): +def processor(): while True: files = build_list('.mkv') - await sio.emit('testoutt', files) - await sio.sleep(5) + sio.emit('testout', files) + time.sleep(5) sio.start_background_task(processor) ################################ # Web Server # ################################ -# Default returns for static files and index root -async def index(request): - with open('./public/index.html') as f: - return web.Response(text=f.read(), content_type='text/html') -app.router.add_get('/', index) -app.router.add_static('/public/', path=str('./public/')) +# Default index root +static_file_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'public') +@app.route("/") +def index(): + return send_from_directory(static_file_dir, 'index.html') # Send the current config to the user to render @sio.on('getconfig') -async def config(sid): +def config(): with open("./config.yml", 'r') as stream: try: config = yaml.safe_load(stream) - await sio.emit('sendconfig', config, room=sid) + sio.emit('sendconfig', config, room=request.sid) except yaml.YAMLError as e: print(e) # Send the current command examples from github to the user to render @sio.on('getcommands') -async def commands(sid): +def commands(): with open("./commands.yml", 'r') as stream: try: commands = yaml.safe_load(stream) - await sio.emit('sendcommands', commands, room=sid) + sio.emit('sendcommands', commands, room=request.sid) except yaml.YAMLError as e: print(e) # Main page for rendering processing history and current @sio.on('getmain') -async def commands(sid): +def commands(): data = 'test' - await sio.emit('sendmain', data, room=sid) + sio.emit('sendmain', data, room=request.sid) # Save user set config @sio.on('saveconfig') -async def commands(sid, data): +def commands(data): with open("/config/config.yml", 'w') as configfile: try: yaml.dump(data, configfile) @@ -88,16 +93,44 @@ async def commands(sid, data): # User Terminal # ################################ -# Terminal page rendering -@sio.on('getterminal') -async def commands(sid): - data = 'test' - await sio.emit('sendterminal', data, room=sid) +# Globals +app.config['term'] = None +app.config['bash'] = None +# Send terminal data from forked process +def send_term(): + while True: + # Sane delay on data sends + sio.sleep(0.01) + if app.config['term']: + timeout_sec = 0 + (data_ready, _, _) = select.select([app.config['term']], [], [], timeout_sec) + if data_ready: + output = os.read(app.config['term'], 1024 * 20).decode() + sio.emit('sendterm', output) + +# Write user input to terminal +@sio.on('termdata') +def termdata(data): + if app.config['term']: + os.write(app.config['term'], data.encode()) + +# The user requested a terminal +@sio.on('giveterm') +def giveterm(): + if app.config['bash']: + return + (bash, term) = pty.fork() + if bash == 0: + subprocess.run('/bin/bash') + else: + app.config['term'] = term + app.config['bash'] = bash + sio.start_background_task(target=send_term) ################################ # App Run # ################################ if __name__ == '__main__': - web.run_app(app, port=8787) + sio.run(app, port=8787, host='0.0.0.0') \ No newline at end of file