Created
April 18, 2023 17:27
-
-
Save msp1974/6c75b59de08aceb6bf448cb5ddd64499 to your computer and use it in GitHub Desktop.
A Visonic Alarm Rest Viewer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import logging | |
import requests | |
import ssl | |
from flask import Flask, request | |
from urllib.parse import urlparse | |
app = Flask(__name__) | |
_session = requests.session() | |
_timeout = 5 | |
_LOGGER = logging.getLogger(__name__) | |
#logging.basicConfig(encoding='utf-8', level=logging.INFO) #Uncoment to see all messages | |
logging.basicConfig(filename='visonic.log', encoding='utf-8', level=logging.DEBUG) | |
PRINT_FORWARDED_REQUEST = False # Set to true to see forwarded request | |
host = "https://visonic.tycomonitor.com" # Set to your montitoring service | |
def forward_request(): | |
headers = {} | |
url = str(host + request.path) | |
args = str(urlparse(request.url).query) | |
# Add args to url | |
if args: | |
url = url + '?' + args | |
if request.is_json: | |
data = json.dumps(request.get_json(), separators=(',', ':')) | |
else: | |
data = request.get_data() | |
remove_headers = ['Host'] | |
for header in request.headers: | |
if header not in remove_headers: | |
headers.update({header[0]: header[1]}) | |
# Add host | |
headers['Host'] = str(urlparse(url).hostname) | |
if PRINT_FORWARDED_REQUEST: | |
print_forwarded_request("FORWARDED REQUEST", request.method, url, headers, args, data) | |
# Perform the request and raise an exception | |
# if the response is not OK (HTML 200) | |
try: | |
if request.method == 'GET': | |
response = _session.get(url, headers=headers, timeout=_timeout) | |
elif request.method == 'POST': | |
response = _session.post(url, headers=headers, data=data, timeout=_timeout) | |
response.raise_for_status() | |
except ( | |
requests.exceptions.ConnectTimeout, | |
requests.exceptions.HTTPError) as ex: | |
_LOGGER.error(f"Error is {ex}") | |
return {} | |
# Check HTTP response code | |
if response.status_code == requests.codes.ok: | |
print_response(response.content.decode('utf-8')) | |
return json.loads(response.content.decode('utf-8')) | |
else: | |
return {} | |
def print_forwarded_request(heading, method, url, headers, args, data): | |
print("===================================================================") | |
print(f"======================= {heading} =========================") | |
print("===================================================================") | |
print(f"{method} {url}") | |
print() | |
print('== ARGS ==') | |
print(args) | |
print() | |
print('== HEADER ==') | |
print(headers) | |
#for header in headers: | |
# print(f"{header}") | |
print() | |
if request.is_json: | |
print('== DATA (JSON) ==') | |
print(str(data)) | |
else: | |
print('== DATA (RAW) ==') | |
print(str(data)) | |
print() | |
def print_request(): | |
print("==========================================================") | |
print(f"======================= REQUEST =========================") | |
print("==========================================================") | |
print(f"{request.method} {request.url}") | |
print() | |
print('== ARGS ==') | |
print(request.args) | |
print() | |
print('== HEADER ==') | |
for header in request.headers: | |
print(f"{header[0]}: {header[1]}") | |
print() | |
if request.is_json: | |
print('== DATA (JSON) ==') | |
print(str(request.get_json())) | |
else: | |
print('== DATA (RAW) ==') | |
print(str(request.get_data())) | |
print() | |
def print_response(data): | |
print('== RESPONSE ==') | |
print(data) | |
print() | |
@app.get('/rest_api/<path:text>') | |
@app.post('/rest_api/<path:text>') | |
def all(text): | |
print_request() | |
return forward_request() | |
if __name__ == '__main__': | |
# Setup Flask | |
context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) | |
context.load_cert_chain("fullchain.pem","privkey.pem") | |
app.run(host='0.0.0.0', port=8443, ssl_context=context) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment