diff --git a/app.py b/app.py index be5a428..0c5b1a3 100644 --- a/app.py +++ b/app.py @@ -1,9 +1,8 @@ -from flask import Flask, request +from flask import Flask, request, jsonify from flask_httpauth import HTTPBasicAuth from ipaddress import ip_address import logging import re -import json from pfctl import pfctl_table_op, pfctl_cfg_read, pfctl_cfg_write @@ -49,10 +48,10 @@ def flush(name): res = pfctl_table_op('f2b-jail/{remote_user}', table='f2b-{name}', operation='flush') - return json.dumps({'anchor': f'f2b-jail/{remote_user}', - 'table': f'f2b-{name}', - 'operation': 'flush', - 'result': res}) + return jsonify({'anchor': f'f2b-jail/{remote_user}', + 'table': f'f2b-{name}', + 'operation': 'flush', + 'result': res}) @app.route("/register", methods=['PUT', 'DELETE']) @@ -87,12 +86,12 @@ def register(): table=f'f2b-{name}', operation='kill') logging.info(f'pfctl -a f2b-jail/{remote_user} -f-') - return json.dumps({'remote_user': remote_user, 'data': data}) - return json.dumps({'anchor': f'f2b-jail/{remote_user}', - 'table': f'f2b-{name}', - 'action': 'start' if request.method == 'PUT' - else 'stop', - 'result': res}) + return jsonify({'remote_user': remote_user, 'data': data}) + return jsonify({'anchor': f'f2b-jail/{remote_user}', + 'table': f'f2b-{name}', + 'action': 'start' if request.method == 'PUT' + else 'stop', + 'result': res}) @app.route("/ban", methods=['PUT', 'DELETE']) @@ -117,11 +116,11 @@ def ban(): table=f'f2b-{name}', operation='delete', value=str(ip)) - return json.dumps({'anchor': f'f2b-jail/{remote_user}', - 'table': f'f2b-{name}', - 'operation': 'add' if request.method == 'PUT' - else 'delete', - 'result': res}) + return jsonify({'anchor': f'f2b-jail/{remote_user}', + 'table': f'f2b-{name}', + 'operation': 'add' if request.method == 'PUT' + else 'delete', + 'result': res}) @app.errorhandler(ValueError) @@ -130,10 +129,10 @@ def permission_err(error): Show a json parsable error if the value is illegal ''' logging.fatal(error) - return json.dumps({'error': str(error)}), 500 + return jsonify({'error': str(error)}), 500 @auth.error_handler def auth_error(): logging.error('Access Denied') - return json.dumps({'error': 'Access Denied'}), 401 + return jsonify({'error': 'Access Denied'}), 401