From 372d1c4c5dabbaf4d18b9b09d9b65fd43a9d2801 Mon Sep 17 00:00:00 2001 From: Alexandre Iooss Date: Mon, 14 Sep 2020 11:41:20 +0200 Subject: [PATCH] Flask app --- .gitignore | 15 ++++++++++++ MANIFEST.in | 2 ++ ghostream/__init__.py | 46 +++++++++++++++++++++++++++++++++++ ghostream/default_settings.py | 9 +++++++ ghostream/static/style.css | 17 +++++++++++++ ghostream/templates/base.html | 13 ++++++++++ setup.py | 12 +++++++++ 7 files changed, 114 insertions(+) create mode 100644 .gitignore create mode 100644 MANIFEST.in create mode 100644 ghostream/__init__.py create mode 100644 ghostream/default_settings.py create mode 100644 ghostream/static/style.css create mode 100644 ghostream/templates/base.html create mode 100644 setup.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba9aac8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +# Byte-compiled / optimized / DLL files +dist +build +__pycache__ +*.py[cod] +*$py.class +*.swp +*.egg-info +_build +.tox +.coverage +coverage + +# Virtualenv +venv diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..be4de83 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1,2 @@ +recursive-include ghostream/templates * +recursive-include ghostream/static * diff --git a/ghostream/__init__.py b/ghostream/__init__.py new file mode 100644 index 0000000..c24f4c3 --- /dev/null +++ b/ghostream/__init__.py @@ -0,0 +1,46 @@ +from flask import Flask, request, redirect, render_template +import ldap + +app = Flask(__name__) +app.logger.setLevel(20) # log info +app.config.from_object('ghostream.default_settings') +app.config.from_envvar('GHOSTREAM_SETTINGS') + +@app.route('/') +def index(): + """Welcome page""" + return render_template('index.html') + +@app.route('/') +def viewer(path): + """Show stream that match this path""" + return render_template('viewer.html', path=path) + +@app.route('/auth', methods=['POST']) +def auth(): + """Authentication on stream start""" + # Limit to NGINX auth + if request.remote_addr != '127.0.0.1': + return "Permission denied", 403 + + name = request.form.get('name') + password = request.form.get('pass') + + # Stream need a name and password + if name is None or password is None: + # When login success, the RTMP is redirected to remove the "?pass=xxx" + # so just ignore login here, and NGINX will still allow streaming. + return "Malformed request", 400 + + bind_dn = f"cn={name},{app.config.LDAP_USER_DN}" + try: + # Try to bind LDAP as the user + connect = ldap.initialize(app.config.LDAP_URI) + connect.bind_s(bind_dn, password) + connect.unbind_s() + app.logger.info("%s logged in successfully", name) + # Remove "?pass=xxx" from RTMP URL + return redirect(f"rtmp://127.0.0.1:1925/app/{name}", code=302) + except: + app.logger.warning("%s failed to log in", name) + return 'Incorrect credentials', 401 diff --git a/ghostream/default_settings.py b/ghostream/default_settings.py new file mode 100644 index 0000000..ec7d854 --- /dev/null +++ b/ghostream/default_settings.py @@ -0,0 +1,9 @@ +# Default configuration + +# LDAP +LDAP_URI = "ldap://127.0.0.1:389" +LDAP_USER_DN = "cn=users,dc=example,dc=com" + +# Web page +SITE_NAME = "Ghostream" +FAVICON = "/favicon.ico" diff --git a/ghostream/static/style.css b/ghostream/static/style.css new file mode 100644 index 0000000..de33c57 --- /dev/null +++ b/ghostream/static/style.css @@ -0,0 +1,17 @@ +body, html { + height: 100%; +} + +body { + background-color: #3b4b5b; + user-select: none; +} + +.text-select { + user-select: text; +} + +.my-row { + display: flex; + height: 100%; +} diff --git a/ghostream/templates/base.html b/ghostream/templates/base.html new file mode 100644 index 0000000..49a7f70 --- /dev/null +++ b/ghostream/templates/base.html @@ -0,0 +1,13 @@ + + + + + {% block title %}{{ config.SITE_NAME }}{% endblock %} + + + + + + {% block content %}{% endblock %} + + diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..1377937 --- /dev/null +++ b/setup.py @@ -0,0 +1,12 @@ +from setuptools import setup, find_packages + +setup( + name='ghostream', + version='1.0', + packages=find_packages(), + include_package_data=True, + install_requires=[ + 'flask>=1.1.1', + 'python-ldap>=3.2.0', + ], +)