You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

105 lines
2.8 KiB

import logging
import os
import signal
import sys
import yaml
from flask import Flask, request, make_response
from systemd.journal import JournalHandler
app = Flask(__name__)
config = []
def reload_config(signum, frame):
log.info('SIGHUP received. Reloading config!')
load_config()
def load_config():
global config
try:
with open(os.getcwd() + '/config.yml', 'r') as f:
config = yaml.load(f, Loader=yaml.BaseLoader)
# Flip keys and rewrite
new_keys = {}
for key in config['keys']:
new_keys[config['keys'][key]] = key
config['keys'] = new_keys
except IOError:
log.error('Config file not found! Exiting.')
sys.exit()
@app.route('/')
def root():
return 'RTMP Auth Server'
@app.route('/on_publish', methods=['POST'])
def on_publish():
global config
resp = make_response()
resp.autocorrect_location_header = False
http_code = 200
stream_key = request.form['name']
foreign_address = request.form['addr']
stream_endpoint = request.form['app']
log.info(foreign_address + " connected with key \'" + stream_key + "\' on endpoint \'" + stream_endpoint + "\'")
if stream_key in config['keys'].keys():
rewrite = config['keys'][stream_key]
if rewrite == '':
log.info("Authentication successful.")
else:
http_code = 302
resp.headers['Location'] = rewrite
log.info("Authentication successful. Rewriting to \'" + rewrite + "\'")
else:
http_code = 400
log.warning("Access denied. Unknown stream key \'" + stream_key + "\'")
return resp, http_code
@app.route('/on_publish_done', methods=['POST'])
def on_publish_done():
stream_key = request.form['name']
stream_endpoint = request.form['app']
log.info("Stream with key \'" + stream_key + "\' on endpoint \'" + stream_endpoint + "\' has been closed.")
return '', 200
@app.route('/on_play', methods=['POST'])
def on_play():
global config
resp = make_response()
http_code = 200
rewritten_stream_key = request.form['name']
foreign_address = request.form['addr']
if rewritten_stream_key in config['keys'].values():
log.info(foreign_address + " started watching \'" + rewritten_stream_key + "\'.")
else:
http_code = 400
log.warning("Unknown stream key \'" + rewritten_stream_key + "\'")
return resp, http_code
if __name__ == '__main__':
signal.signal(signal.SIGHUP, reload_config)
log = logging.getLogger('rtmp-auth-server')
log.addHandler(JournalHandler())
log.setLevel(logging.INFO)
load_config()
log.info('Starting server')
log.info('My PID is ' + str(os.getpid()))
app.run(config['server']['hostname'], config['server']['port'])