Skip to content

Commit

Permalink
rebasing to flask, adding basic terminal functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
thelamer committed Jul 16, 2019
1 parent ceb78ac commit 3545c6b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 37 deletions.
18 changes: 12 additions & 6 deletions public/ffmpeg-web.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,19 @@ function saveall(){
//// Terminal Page rendering ////
function renderterminal(){
pagepurge();
$('#pagecontent').append('<center><div class="spinner-border" style="width: 6rem; height: 6rem;"></div></center>');
socket.emit('getterminal');
socket.emit('giveterm');
$('#pagecontent').append('<div id="terminal-container"</div>');
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('<center>Raw bash terminal here for users to test commands</center>');
});

///////////////////////// test function client side //////////////////////////////////////
socket.on('testout', function(out){
Expand Down
3 changes: 1 addition & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
aiohttp
python-socketio
flask
pyyaml
91 changes: 62 additions & 29 deletions server.py
Original file line number Diff line number Diff line change
@@ -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)



Expand All @@ -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)
Expand All @@ -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')

0 comments on commit 3545c6b

Please sign in to comment.