|
19 | 19 | from chatbot import chatbot |
20 | 20 |
|
21 | 21 | import utils |
| 22 | +from flask_ipban import IpBan |
22 | 23 |
|
23 | 24 | logging.config.fileConfig(os.getenv('LOGGING_FILE_CONFIG', './logging.conf')) |
24 | 25 | logger = logging.getLogger('nfdi_search_engine') |
25 | 26 | app = Flask(__name__) |
| 27 | +ip_ban = IpBan() |
| 28 | +ip_ban.init_app(app) |
26 | 29 | app.config.from_object(Config) |
27 | 30 | Session(app) |
28 | 31 |
|
@@ -1051,6 +1054,64 @@ def get_preference_index(obj, field_name): |
1051 | 1054 |
|
1052 | 1055 | #endregion |
1053 | 1056 |
|
| 1057 | + |
| 1058 | +#region IP BAN |
| 1059 | + |
| 1060 | +@app.route('/get-block-list', methods=['GET']) |
| 1061 | +@utils.timeit |
| 1062 | +def get_block_list(): |
| 1063 | + utils.log_activity("get_block_list") |
| 1064 | + s = '<html><body>' |
| 1065 | + s += '<table class="table" style="width: 100%"><thead>\n' |
| 1066 | + s += '<tr><th>ip</th><th>count</th><th>permanent</th><th>url</th><th>timestamp</th></tr>\n' |
| 1067 | + s += '</thead><tbody>\n' |
| 1068 | + for k, r in ip_ban.get_block_list().items(): |
| 1069 | + s += '<tr><td>{}</td><td>{}</td><td>{}</td><td>{}</td><td>{}</td></tr>\n'.format(k, r['count'], |
| 1070 | + r.get('permanent', ''), |
| 1071 | + r.get('url', ''), |
| 1072 | + r['timestamp']) |
| 1073 | + |
| 1074 | + s += '</body></html>' |
| 1075 | + return s |
| 1076 | + |
| 1077 | +@app.route('/ip-ban-block/<string:ip_address>', methods=['GET']) |
| 1078 | +@utils.timeit |
| 1079 | +def ip_ban_block(ip_address): |
| 1080 | + utils.log_activity(f"ip_ban_block: {ip_address}") |
| 1081 | + ip_ban.block(ip=ip_address) |
| 1082 | + return f"IP address {ip_address} has been blocked." |
| 1083 | + |
| 1084 | +@app.route('/ip-ban-add/<string:ip_address>', methods=['GET']) |
| 1085 | +@utils.timeit |
| 1086 | +def ip_ban_add(ip_address): |
| 1087 | + utils.log_activity(f"ip_ban_add: {ip_address}") |
| 1088 | + ip_ban.add(ip=ip_address) |
| 1089 | + return f"IP address {ip_address} has been added to the list." |
| 1090 | + |
| 1091 | +@app.route('/ip-ban-remove/<string:ip_address>', methods=['GET']) |
| 1092 | +@utils.timeit |
| 1093 | +def ip_ban_remove(ip_address): |
| 1094 | + utils.log_activity(f"ip_ban_remove: {ip_address}") |
| 1095 | + ip_ban.remove(ip=ip_address) |
| 1096 | + return f"IP address {ip_address} has been removed from the list." |
| 1097 | + |
| 1098 | +@app.route('/ip-whitelist-add/<string:ip_address>', methods=['GET']) |
| 1099 | +@utils.timeit |
| 1100 | +def ip_whitelist_add(ip_address): |
| 1101 | + utils.log_activity(f"ip_whitelist_add: {ip_address}") |
| 1102 | + ip_ban.ip_whitelist_add(ip=ip_address) |
| 1103 | + return f"IP address {ip_address} has been added to the whitelist." |
| 1104 | + |
| 1105 | +@app.route('/ip-whitelist-remove/<string:ip_address>', methods=['GET']) |
| 1106 | +@utils.timeit |
| 1107 | +def ip_whitelist_remove(ip_address): |
| 1108 | + utils.log_activity(f"ip_whitelist_remove: {ip_address}") |
| 1109 | + ip_ban.ip_whitelist_remove(ip=ip_address) |
| 1110 | + return f"IP address {ip_address} has been removed from the whitelist." |
| 1111 | + |
| 1112 | +#endregion |
| 1113 | + |
| 1114 | + |
1054 | 1115 | #region Control Panel |
1055 | 1116 |
|
1056 | 1117 | @app.route('/control-panel/dashboard') |
|
0 commit comments