Use jsonify instead of rolling with json.dumps()
This commit is contained in:
parent
5b14dd078a
commit
851429da46
37
app.py
37
app.py
@ -1,9 +1,8 @@
|
|||||||
from flask import Flask, request
|
from flask import Flask, request, jsonify
|
||||||
from flask_httpauth import HTTPBasicAuth
|
from flask_httpauth import HTTPBasicAuth
|
||||||
from ipaddress import ip_address
|
from ipaddress import ip_address
|
||||||
import logging
|
import logging
|
||||||
import re
|
import re
|
||||||
import json
|
|
||||||
from pfctl import pfctl_table_op, pfctl_cfg_read, pfctl_cfg_write
|
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}',
|
res = pfctl_table_op('f2b-jail/{remote_user}',
|
||||||
table='f2b-{name}',
|
table='f2b-{name}',
|
||||||
operation='flush')
|
operation='flush')
|
||||||
return json.dumps({'anchor': f'f2b-jail/{remote_user}',
|
return jsonify({'anchor': f'f2b-jail/{remote_user}',
|
||||||
'table': f'f2b-{name}',
|
'table': f'f2b-{name}',
|
||||||
'operation': 'flush',
|
'operation': 'flush',
|
||||||
'result': res})
|
'result': res})
|
||||||
|
|
||||||
|
|
||||||
@app.route("/register", methods=['PUT', 'DELETE'])
|
@app.route("/register", methods=['PUT', 'DELETE'])
|
||||||
@ -87,12 +86,12 @@ def register():
|
|||||||
table=f'f2b-{name}',
|
table=f'f2b-{name}',
|
||||||
operation='kill')
|
operation='kill')
|
||||||
logging.info(f'pfctl -a f2b-jail/{remote_user} -f-')
|
logging.info(f'pfctl -a f2b-jail/{remote_user} -f-')
|
||||||
return json.dumps({'remote_user': remote_user, 'data': data})
|
return jsonify({'remote_user': remote_user, 'data': data})
|
||||||
return json.dumps({'anchor': f'f2b-jail/{remote_user}',
|
return jsonify({'anchor': f'f2b-jail/{remote_user}',
|
||||||
'table': f'f2b-{name}',
|
'table': f'f2b-{name}',
|
||||||
'action': 'start' if request.method == 'PUT'
|
'action': 'start' if request.method == 'PUT'
|
||||||
else 'stop',
|
else 'stop',
|
||||||
'result': res})
|
'result': res})
|
||||||
|
|
||||||
|
|
||||||
@app.route("/ban", methods=['PUT', 'DELETE'])
|
@app.route("/ban", methods=['PUT', 'DELETE'])
|
||||||
@ -117,11 +116,11 @@ def ban():
|
|||||||
table=f'f2b-{name}',
|
table=f'f2b-{name}',
|
||||||
operation='delete',
|
operation='delete',
|
||||||
value=str(ip))
|
value=str(ip))
|
||||||
return json.dumps({'anchor': f'f2b-jail/{remote_user}',
|
return jsonify({'anchor': f'f2b-jail/{remote_user}',
|
||||||
'table': f'f2b-{name}',
|
'table': f'f2b-{name}',
|
||||||
'operation': 'add' if request.method == 'PUT'
|
'operation': 'add' if request.method == 'PUT'
|
||||||
else 'delete',
|
else 'delete',
|
||||||
'result': res})
|
'result': res})
|
||||||
|
|
||||||
|
|
||||||
@app.errorhandler(ValueError)
|
@app.errorhandler(ValueError)
|
||||||
@ -130,10 +129,10 @@ def permission_err(error):
|
|||||||
Show a json parsable error if the value is illegal
|
Show a json parsable error if the value is illegal
|
||||||
'''
|
'''
|
||||||
logging.fatal(error)
|
logging.fatal(error)
|
||||||
return json.dumps({'error': str(error)}), 500
|
return jsonify({'error': str(error)}), 500
|
||||||
|
|
||||||
|
|
||||||
@auth.error_handler
|
@auth.error_handler
|
||||||
def auth_error():
|
def auth_error():
|
||||||
logging.error('Access Denied')
|
logging.error('Access Denied')
|
||||||
return json.dumps({'error': 'Access Denied'}), 401
|
return jsonify({'error': 'Access Denied'}), 401
|
||||||
|
Loading…
x
Reference in New Issue
Block a user