Skip to content

Commit 3633c11

Browse files
Merge pull request #297 from semantic-systems/develop
Restricting traffic from bots
2 parents 7c29710 + c4d5f21 commit 3633c11

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

main.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
from chatbot import chatbot
2020

2121
import utils
22+
from flask_ipban import IpBan
2223

2324
logging.config.fileConfig(os.getenv('LOGGING_FILE_CONFIG', './logging.conf'))
2425
logger = logging.getLogger('nfdi_search_engine')
2526
app = Flask(__name__)
27+
ip_ban = IpBan()
28+
ip_ban.init_app(app)
2629
app.config.from_object(Config)
2730
Session(app)
2831

@@ -1051,6 +1054,64 @@ def get_preference_index(obj, field_name):
10511054

10521055
#endregion
10531056

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+
10541115
#region Control Panel
10551116

10561117
@app.route('/control-panel/dashboard')

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ python-dotenv==1.1.0
1414
openai==1.75.0
1515
elasticsearch==8.14.0
1616
ua_parser==1.0.1
17-
email-validator==2.2.0
17+
email-validator==2.2.0
18+
flask-ipban==1.1.5

0 commit comments

Comments
 (0)