|
1 | 1 | import asyncio
|
2 | 2 | import json
|
3 | 3 | import simpleobsws
|
4 |
| -import liteconfig |
5 | 4 | import aiohttp
|
6 | 5 | from aiohttp import web
|
| 6 | +from configparser import ConfigParser |
7 | 7 |
|
8 |
| -cfg = liteconfig.Config("config.ini") |
| 8 | +config = ConfigParser() |
| 9 | +config.read('config.ini') |
| 10 | +httpAddress = config.get('http', 'bind_to_address') |
| 11 | +httpPort = config.getint('http', 'bind_to_port') |
| 12 | +httpAuthKey = config.get('http', 'authentication_key') |
| 13 | +if httpAuthKey == '': |
| 14 | + print('Starting HTTP server without authentication.') |
| 15 | + httpAuthKey = None |
| 16 | +else: |
| 17 | + print('Starting HTTP server with AuthKey set to "{}"'.format(httpAuthKey)) |
| 18 | +wsAddress = config.get('obsws', 'ws_address') |
| 19 | +wsPort = config.getint('obsws', 'ws_port') |
| 20 | +wsPassword = config.get('obsws', 'ws_password') |
9 | 21 | loop = asyncio.get_event_loop()
|
10 |
| -ws = simpleobsws.obsws(host=cfg.obsws.ws_address, port=cfg.obsws.ws_port, password=cfg.obsws.ws_password, loop=loop) |
| 22 | +ws = simpleobsws.obsws(host=wsAddress, port=wsPort, password=wsPassword, loop=loop) |
| 23 | + |
| 24 | +def statusmessage(message): |
| 25 | + print(str(message) + '... ', end='', flush=True) |
11 | 26 |
|
12 | 27 | async def handle_emit_request(request):
|
13 | 28 | """Handler function for all emit-based HTTP requests. Assumes that you know what you are doing because it will never return an error."""
|
14 |
| - requesttype = request.match_info['type'] |
15 |
| - requestdata = await request.json() |
16 |
| - await ws.emit(requesttype, requestdata) |
17 |
| - return web.json_response({'status':'ok'}) |
| 29 | + if ('AuthKey' not in request.headers) and httpAuthKey != None: |
| 30 | + return web.json_response({'status':'error', 'error':'AuthKey header is required.'}) |
| 31 | + if httpAuthKey == None or (request.headers['AuthKey'] == httpAuthKey): |
| 32 | + requesttype = request.match_info['type'] |
| 33 | + try: |
| 34 | + requestdata = await request.json() |
| 35 | + except json.decoder.JSONDecodeError: |
| 36 | + requestdata = None |
| 37 | + await ws.emit(requesttype, requestdata) |
| 38 | + return web.json_response({'status':'ok'}) |
| 39 | + else: |
| 40 | + return web.json_response({'status':'error', 'error':'Bad AuthKey'}) |
18 | 41 |
|
19 | 42 | async def handle_call_request(request):
|
20 | 43 | """Handler function for all call-based HTTP requests."""
|
21 |
| - pass |
| 44 | + if ('AuthKey' not in request.headers) and httpAuthKey != None: |
| 45 | + return web.json_response({'status':'error', 'error':'AuthKey header is required.'}) |
| 46 | + if httpAuthKey == None or (request.headers['AuthKey'] == httpAuthKey): |
| 47 | + requesttype = request.match_info['type'] |
| 48 | + try: |
| 49 | + requestdata = await request.json() |
| 50 | + except json.decoder.JSONDecodeError: |
| 51 | + if (await request.text()) == '': |
| 52 | + requestdata = None |
| 53 | + try: |
| 54 | + responsedata = await ws.call(requesttype, requestdata) |
| 55 | + except simpleobsws.MessageTimeout: |
| 56 | + responsedata = {'status':'error', 'error':'The obs-websocket request timed out.'} |
| 57 | + return web.json_response(responsedata) |
| 58 | + else: |
| 59 | + return web.json_response({'status':'error', 'error':'Bad AuthKey'}) |
22 | 60 |
|
23 | 61 | app = web.Application()
|
24 |
| -app.add_routes([web.post('/emit/{type}, handle_emit_request), web.post('/call/{type}, handle_call_request)]) |
| 62 | +app.add_routes([web.post('/emit/{type}', handle_emit_request), web.post('/call/{type}', handle_call_request)]) |
| 63 | +statusmessage('Connecting to obs-websocket') |
25 | 64 | loop.run_until_complete(ws.connect())
|
| 65 | +print('[Connected.]') |
26 | 66 | try:
|
27 |
| - web.run_app(app) |
| 67 | + web.run_app(app, host=httpAddress, port=httpPort) |
28 | 68 | except KeyboardInterrupt:
|
29 | 69 | print('Shutting down...')
|
0 commit comments