flask-Humanify 0.1.2__py3-none-any.whl → 0.1.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- flask_humanify/__init__.py +1 -1
- flask_humanify/features/rate_limiter.py +1 -1
- flask_humanify/humanify.py +19 -2
- {flask_humanify-0.1.2.dist-info → flask_humanify-0.1.3.dist-info}/METADATA +16 -3
- {flask_humanify-0.1.2.dist-info → flask_humanify-0.1.3.dist-info}/RECORD +8 -8
- {flask_humanify-0.1.2.dist-info → flask_humanify-0.1.3.dist-info}/WHEEL +0 -0
- {flask_humanify-0.1.2.dist-info → flask_humanify-0.1.3.dist-info}/licenses/LICENSE +0 -0
- {flask_humanify-0.1.2.dist-info → flask_humanify-0.1.3.dist-info}/top_level.txt +0 -0
flask_humanify/__init__.py
CHANGED
@@ -51,7 +51,7 @@ class RateLimiter:
|
|
51
51
|
Before request hook.
|
52
52
|
"""
|
53
53
|
ip = get_client_ip(request)
|
54
|
-
if request.endpoint
|
54
|
+
if request.endpoint in ["humanify.rate_limited", "humanify.access_denied"]:
|
55
55
|
return
|
56
56
|
if self.is_rate_limited(ip or "127.0.0.1"):
|
57
57
|
return redirect(
|
flask_humanify/humanify.py
CHANGED
@@ -2,7 +2,8 @@ from dataclasses import dataclass
|
|
2
2
|
import logging
|
3
3
|
from typing import List, Optional
|
4
4
|
|
5
|
-
from
|
5
|
+
from werkzeug.wrappers import Response
|
6
|
+
from flask import Blueprint, request, render_template, redirect, url_for, current_app
|
6
7
|
from .ipset import IPSetClient, ensure_server_running
|
7
8
|
from .utils import get_client_ip, get_return_url
|
8
9
|
|
@@ -120,6 +121,22 @@ class Humanify:
|
|
120
121
|
{"Cache-Control": "public, max-age=15552000"},
|
121
122
|
)
|
122
123
|
|
124
|
+
def register_middleware(self, action: str = "deny_access"):
|
125
|
+
"""
|
126
|
+
Register the middleware.
|
127
|
+
"""
|
128
|
+
|
129
|
+
self.app = self.app or current_app
|
130
|
+
|
131
|
+
@self.app.before_request
|
132
|
+
def before_request():
|
133
|
+
"""
|
134
|
+
Before request hook.
|
135
|
+
"""
|
136
|
+
if self.is_bot:
|
137
|
+
if action == "deny_access":
|
138
|
+
return self.deny_access()
|
139
|
+
|
123
140
|
@property
|
124
141
|
def is_bot(self) -> HumanifyResult:
|
125
142
|
"""
|
@@ -131,7 +148,7 @@ class Humanify:
|
|
131
148
|
ip_groups = self.ipset_client.lookup_ip(ip)
|
132
149
|
return HumanifyResult.from_ip_groups(ip, ip_groups)
|
133
150
|
|
134
|
-
def
|
151
|
+
def deny_access(self) -> Response:
|
135
152
|
"""
|
136
153
|
Redirect to the access denied page.
|
137
154
|
"""
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: flask-Humanify
|
3
|
-
Version: 0.1.
|
3
|
+
Version: 0.1.3
|
4
4
|
Summary: Protect against bots and DDoS attacks
|
5
5
|
Author-email: TN3W <tn3w@protonmail.com>
|
6
6
|
License-Expression: Apache-2.0
|
@@ -42,19 +42,32 @@ from flask_Humanify import Humanify
|
|
42
42
|
app = Flask(__name__)
|
43
43
|
humanify = Humanify(app)
|
44
44
|
|
45
|
+
# Register the middleware to deny access to bots
|
46
|
+
humanify.register_middleware(action="deny_access")
|
47
|
+
|
45
48
|
@app.route("/")
|
46
49
|
def index():
|
47
50
|
"""
|
48
51
|
A route that is protected against bots and DDoS attacks.
|
49
52
|
"""
|
50
|
-
if humanify.is_bot:
|
51
|
-
return humanify.redirect_to_access_denied()
|
52
53
|
return "Hello, Human!"
|
53
54
|
|
54
55
|
if __name__ == "__main__":
|
55
56
|
app.run()
|
56
57
|
```
|
57
58
|
|
59
|
+
Not using the middleware:
|
60
|
+
```python
|
61
|
+
@app.route("/")
|
62
|
+
def index():
|
63
|
+
"""
|
64
|
+
A route that is protected against bots and DDoS attacks.
|
65
|
+
"""
|
66
|
+
if humanify.is_bot:
|
67
|
+
return humanify.deny_access()
|
68
|
+
return "Hello, Human!"
|
69
|
+
```
|
70
|
+
|
58
71
|
## Usage
|
59
72
|
|
60
73
|
### Installation
|
@@ -1,14 +1,14 @@
|
|
1
|
-
flask_humanify/__init__.py,sha256=
|
2
|
-
flask_humanify/humanify.py,sha256=
|
1
|
+
flask_humanify/__init__.py,sha256=LxR6tCZClN04nVGXLyET5HGZ7mv0vOQrROj8Dvwe-M0,269
|
2
|
+
flask_humanify/humanify.py,sha256=3tZx3RunXLVKGYJ6LukhsTuc-LFurMVf8pAQpMf0kUI,4213
|
3
3
|
flask_humanify/ipset.py,sha256=ZZZRtgtddkZr2q1zuI6803hJQ8OodVHNdyZvZGqpmMI,10866
|
4
4
|
flask_humanify/utils.py,sha256=CJ4FPhNJ75FuWcAn_ZuZkqRa9HR3jVvYOu9NZaN4L1o,2461
|
5
5
|
flask_humanify/datasets/ipset.json,sha256=YNPqwI109lYkfvZeOPsoDH_dKJxOCs0G2nvx_s2mvqU,30601191
|
6
|
-
flask_humanify/features/rate_limiter.py,sha256=
|
6
|
+
flask_humanify/features/rate_limiter.py,sha256=YhAh4xAZTiqdMbS028jA_v6FeZEB1v6JtbOVubjGZao,2203
|
7
7
|
flask_humanify/templates/access_denied.html,sha256=Y7EzM53LRBMJBniczTWYvinHuPPFQVKrqvCmKaqLHak,3165
|
8
8
|
flask_humanify/templates/oneclick_captcha.html,sha256=CnK4qTOdjIrlnFB8lytuK8pEcpj1dL5uYzj0ZNBkx-E,6241
|
9
9
|
flask_humanify/templates/rate_limited.html,sha256=LSS1-c9F3YLxXF-jiyyNN6vIMoIAdmd6ObO1PDcjIxI,3110
|
10
|
-
flask_humanify-0.1.
|
11
|
-
flask_humanify-0.1.
|
12
|
-
flask_humanify-0.1.
|
13
|
-
flask_humanify-0.1.
|
14
|
-
flask_humanify-0.1.
|
10
|
+
flask_humanify-0.1.3.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
|
11
|
+
flask_humanify-0.1.3.dist-info/METADATA,sha256=fFcFNB3xEAp7HtSQjFvW9kH_bQpo1GGZUPhZ1NJLFoo,3048
|
12
|
+
flask_humanify-0.1.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
13
|
+
flask_humanify-0.1.3.dist-info/top_level.txt,sha256=9-c6uhxwCpPE3BJYge1Y9Z_bYmWitI0fY5RgqMiFWr0,15
|
14
|
+
flask_humanify-0.1.3.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|