django-smart-layer 0.1.0__tar.gz
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.
- django_smart_layer-0.1.0/PKG-INFO +9 -0
- django_smart_layer-0.1.0/README.md +395 -0
- django_smart_layer-0.1.0/django_smart_layer.egg-info/PKG-INFO +9 -0
- django_smart_layer-0.1.0/django_smart_layer.egg-info/SOURCES.txt +23 -0
- django_smart_layer-0.1.0/django_smart_layer.egg-info/dependency_links.txt +1 -0
- django_smart_layer-0.1.0/django_smart_layer.egg-info/requires.txt +5 -0
- django_smart_layer-0.1.0/django_smart_layer.egg-info/top_level.txt +1 -0
- django_smart_layer-0.1.0/pyproject.toml +22 -0
- django_smart_layer-0.1.0/setup.cfg +4 -0
- django_smart_layer-0.1.0/smartlayer/__init__.py +1 -0
- django_smart_layer-0.1.0/smartlayer/admin.py +20 -0
- django_smart_layer-0.1.0/smartlayer/apps.py +39 -0
- django_smart_layer-0.1.0/smartlayer/management/__init__.py +0 -0
- django_smart_layer-0.1.0/smartlayer/management/commands/AILogAnalyser.py +184 -0
- django_smart_layer-0.1.0/smartlayer/management/commands/__init__.py +0 -0
- django_smart_layer-0.1.0/smartlayer/middleware/AIAnomalyDetector.py +337 -0
- django_smart_layer-0.1.0/smartlayer/middleware/AIRequestValidator.py +133 -0
- django_smart_layer-0.1.0/smartlayer/middleware/WatchLog.py +60 -0
- django_smart_layer-0.1.0/smartlayer/middleware/__init__.py +11 -0
- django_smart_layer-0.1.0/smartlayer/middleware/rate_Limiter.py +110 -0
- django_smart_layer-0.1.0/smartlayer/migrations/0001_initial.py +73 -0
- django_smart_layer-0.1.0/smartlayer/migrations/__init__.py +0 -0
- django_smart_layer-0.1.0/smartlayer/models.py +120 -0
- django_smart_layer-0.1.0/smartlayer/utils.py +60 -0
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: django-smart-layer
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: AI-powered Django middleware for security, monitoring and rate limiting
|
|
5
|
+
Requires-Python: >=3.10
|
|
6
|
+
Requires-Dist: django>=4.2
|
|
7
|
+
Requires-Dist: httpx>=0.27
|
|
8
|
+
Provides-Extra: scheduler
|
|
9
|
+
Requires-Dist: apscheduler>=3.10; extra == "scheduler"
|
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
# 🛡️ django-smart-layer
|
|
2
|
+
|
|
3
|
+
> **AI-powered middleware for Django** — security, rate limiting, anomaly detection, and log analysis.
|
|
4
|
+
> Drop it in. Configure once. Forget about it.
|
|
5
|
+
|
|
6
|
+
---
|
|
7
|
+
|
|
8
|
+
## Why django-smart-layer?
|
|
9
|
+
|
|
10
|
+
Every Django app eventually needs the same things:
|
|
11
|
+
|
|
12
|
+
- 🔒 Block malicious requests before they touch your views
|
|
13
|
+
- 🤖 Detect bots and scrapers automatically
|
|
14
|
+
- 💳 Enforce subscription plan limits without writing boilerplate
|
|
15
|
+
- 📋 Understand what happened in your app — in plain English
|
|
16
|
+
|
|
17
|
+
**Smart Layer gives you all of this in one pip install.**
|
|
18
|
+
|
|
19
|
+
No external services. No accounts. No infrastructure.
|
|
20
|
+
Just add it to `MIDDLEWARE` and you're protected.
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
## What's Inside
|
|
25
|
+
|
|
26
|
+
| Middleware | Job | AI? |
|
|
27
|
+
|---|---|---|
|
|
28
|
+
| `AIAnomalyDetector` | Detects bots and attack patterns | ✅ |
|
|
29
|
+
| `AIRequestValidator` | Blocks SQL injection, XSS, prompt injection | ✅ |
|
|
30
|
+
| `RateLimiter` | Enforces per-plan, per-path request limits | ❌ |
|
|
31
|
+
| `WatchLog` | Logs every request to your database | ❌ |
|
|
32
|
+
| `analyse_logs` | Morning report — plain English summary | ✅ |
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## How It All Fits Together
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
Incoming Request
|
|
40
|
+
│
|
|
41
|
+
▼
|
|
42
|
+
┌───────────────────────┐
|
|
43
|
+
│ AIAnomalyDetector │ Is this user a bot? Suspicious pattern?
|
|
44
|
+
└───────────┬───────────┘ Blocked → 403
|
|
45
|
+
│
|
|
46
|
+
▼
|
|
47
|
+
┌───────────────────────┐
|
|
48
|
+
│ AIRequestValidator │ Is this payload malicious?
|
|
49
|
+
└───────────┬───────────┘ Blocked → 403
|
|
50
|
+
│
|
|
51
|
+
▼
|
|
52
|
+
┌───────────────────────┐
|
|
53
|
+
│ RateLimiter │ Is this user over their plan limit?
|
|
54
|
+
└───────────┬───────────┘ Blocked → 429
|
|
55
|
+
│
|
|
56
|
+
▼
|
|
57
|
+
┌───────────────────────┐
|
|
58
|
+
│ WatchLog │ Log everything — always runs
|
|
59
|
+
└───────────┬───────────┘
|
|
60
|
+
│
|
|
61
|
+
▼
|
|
62
|
+
Your Django View ✅
|
|
63
|
+
Only clean requests reach here.
|
|
64
|
+
|
|
65
|
+
Every morning → python manage.py analyse_logs
|
|
66
|
+
Plain English report saved to Django admin
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
## Quick Start
|
|
72
|
+
|
|
73
|
+
### 1. Install
|
|
74
|
+
|
|
75
|
+
```bash
|
|
76
|
+
pip install django-smart-layer
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
With auto-scheduling support:
|
|
80
|
+
```bash
|
|
81
|
+
pip install django-smart-layer[scheduler]
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### 2. Add to settings
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
INSTALLED_APPS = [
|
|
88
|
+
...
|
|
89
|
+
'smartlayer',
|
|
90
|
+
]
|
|
91
|
+
|
|
92
|
+
MIDDLEWARE = [
|
|
93
|
+
'smartlayer.middleware.AIAnomalyDetector', # 1st — bot detection
|
|
94
|
+
'smartlayer.middleware.AIRequestValidator', # 2nd — payload validation
|
|
95
|
+
'smartlayer.middleware.RateLimiter', # 3rd — rate limiting
|
|
96
|
+
'smartlayer.middleware.WatchLog', # 4th — logging (always last)
|
|
97
|
+
...
|
|
98
|
+
]
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 3. Run migrations
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
python manage.py migrate
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### 4. Configure
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
SMART_MIDDLEWARE = {
|
|
111
|
+
|
|
112
|
+
# ── AI Backend ──────────────────────────────────────────────────────
|
|
113
|
+
'AI_API_KEY': 'your-api-key',
|
|
114
|
+
'AI_BASE_URL': 'https://api.groq.com/openai/v1',
|
|
115
|
+
'AI_MODEL': 'llama3-8b-8192',
|
|
116
|
+
|
|
117
|
+
# ── Rate Limiter ─────────────────────────────────────────────────────
|
|
118
|
+
'PLAN_FIELD': 'plan', # field name on your User model — e.g. user.plan
|
|
119
|
+
|
|
120
|
+
'RATE_LIMIT_PLANS': {
|
|
121
|
+
'free': {
|
|
122
|
+
'/api/generate/': {'per_minute': 2, 'per_day': 50},
|
|
123
|
+
},
|
|
124
|
+
'basic': {
|
|
125
|
+
'/api/generate/': {'per_minute': 10, 'per_day': 500},
|
|
126
|
+
'/api/export/': {'per_minute': 5, 'per_day': 100},
|
|
127
|
+
},
|
|
128
|
+
'premium': {
|
|
129
|
+
'/api/generate/': {'per_minute': 50, 'per_day': 5000},
|
|
130
|
+
'/api/export/': {'per_minute': 20, 'per_day': 1000},
|
|
131
|
+
'/api/analytics/':{'per_minute': 100, 'per_day': 10000},
|
|
132
|
+
},
|
|
133
|
+
},
|
|
134
|
+
|
|
135
|
+
# ── Log Analysis ─────────────────────────────────────────────────────
|
|
136
|
+
'LOG_RETENTION_DAYS': 30, # auto delete logs older than 30 days
|
|
137
|
+
'ANALYSE_LOGS_AT': '06:00', # auto run report daily at 6am (needs apscheduler)
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
That's it. Your app is protected. ✅
|
|
142
|
+
|
|
143
|
+
---
|
|
144
|
+
|
|
145
|
+
## Middleware — In Detail
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
### 🤖 AIAnomalyDetector
|
|
150
|
+
|
|
151
|
+
Watches request patterns and blocks bots before they can do damage.
|
|
152
|
+
|
|
153
|
+
**Three instant block rules:**
|
|
154
|
+
|
|
155
|
+
```
|
|
156
|
+
1. Empty user agent → block immediately
|
|
157
|
+
2. 50+ requests in 10 seconds → block immediately
|
|
158
|
+
3. 75%+ errors in last 2 minutes → block immediately
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
**Suspicion scoring for subtle attacks:**
|
|
162
|
+
|
|
163
|
+
| Signal | Score |
|
|
164
|
+
|---|---|
|
|
165
|
+
| Suspicious user agent (curl, scrapy, wget...) | +2 |
|
|
166
|
+
| Elevated request rate (20–49 in 10s) | +3 |
|
|
167
|
+
| Moderate error rate (40–74%) | +2 |
|
|
168
|
+
| Hitting sensitive paths (/admin, /.env) | +4 |
|
|
169
|
+
| Scanning 15+ distinct endpoints per minute | +2 |
|
|
170
|
+
| Sequential ID probing (/users/1, /users/2...) | +5 |
|
|
171
|
+
| Burst after long idle on same endpoint | +2 |
|
|
172
|
+
|
|
173
|
+
Score ≥ 8 → blocked immediately.
|
|
174
|
+
Score 4–7 → AI asked in background. Banned on next request if AI says BLOCK.
|
|
175
|
+
|
|
176
|
+
> ⚡ New users get a **grace period** — first 20 requests are never scored.
|
|
177
|
+
> Legitimate users exploring your app are never penalised.
|
|
178
|
+
|
|
179
|
+
**Returns:** `403 Forbidden`
|
|
180
|
+
|
|
181
|
+
---
|
|
182
|
+
|
|
183
|
+
### 🛡️ AIRequestValidator
|
|
184
|
+
|
|
185
|
+
Scans every request body for attacks before they reach your views.
|
|
186
|
+
|
|
187
|
+
**Stage 1 — Pattern matching (instant, free)**
|
|
188
|
+
|
|
189
|
+
Detects SQL injection, XSS, path traversal, shell injection,
|
|
190
|
+
prompt injection, null bytes, and encoding tricks.
|
|
191
|
+
|
|
192
|
+
```
|
|
193
|
+
Score 0 → safe, no AI call needed
|
|
194
|
+
Score 1–2 → borderline, sent to AI
|
|
195
|
+
Score 3+ → obviously malicious, blocked immediately
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
**Stage 2 — AI analysis (only for borderline requests)**
|
|
199
|
+
|
|
200
|
+
Catches clever attacks that bypass regex:
|
|
201
|
+
encoded attacks, split-field attacks, business logic abuse,
|
|
202
|
+
social engineering, and obfuscated payloads.
|
|
203
|
+
|
|
204
|
+
Confidence > 85% → blocked.
|
|
205
|
+
|
|
206
|
+
> 💡 File uploads (multipart) are skipped automatically.
|
|
207
|
+
|
|
208
|
+
**Returns:** `403 Forbidden`
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
### ⏱️ RateLimiter
|
|
213
|
+
|
|
214
|
+
Enforces per-user, per-plan, per-path limits. Built for SaaS.
|
|
215
|
+
|
|
216
|
+
**Supports four limit types — use any combination:**
|
|
217
|
+
|
|
218
|
+
```python
|
|
219
|
+
'RATE_LIMIT_PLANS': {
|
|
220
|
+
'free': {
|
|
221
|
+
'/api/generate/': {
|
|
222
|
+
'per_minute': 2,
|
|
223
|
+
'per_hour': 20,
|
|
224
|
+
'per_day': 100,
|
|
225
|
+
'lifetime': 1000, # never resets
|
|
226
|
+
},
|
|
227
|
+
},
|
|
228
|
+
}
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
**Key behaviours:**
|
|
232
|
+
- Routes only in `premium` automatically return `403` for lower plan users
|
|
233
|
+
- Each plan gets **independent counters** — upgrading starts fresh
|
|
234
|
+
- Cache-based counting — zero extra DB load for time-based limits
|
|
235
|
+
- Lifetime limits use atomic DB increments — race condition safe
|
|
236
|
+
|
|
237
|
+
**Returns:** `429 Too Many Requests`
|
|
238
|
+
|
|
239
|
+
---
|
|
240
|
+
|
|
241
|
+
### 📝 WatchLog
|
|
242
|
+
|
|
243
|
+
Silently records every request to the database. Zero configuration needed.
|
|
244
|
+
|
|
245
|
+
Writes happen in a **background thread** — response returns instantly,
|
|
246
|
+
database write happens after. Zero performance impact.
|
|
247
|
+
|
|
248
|
+
**What gets saved:**
|
|
249
|
+
|
|
250
|
+
| Field | Example |
|
|
251
|
+
|---|---|
|
|
252
|
+
| `method` | `GET` |
|
|
253
|
+
| `path` | `/api/generate/` |
|
|
254
|
+
| `status_code` | `200` |
|
|
255
|
+
| `response_time_ms` | `143.2` |
|
|
256
|
+
| `timestamp` | `2024-01-15 14:32:01` |
|
|
257
|
+
| `user_id` | `42` (authenticated users) |
|
|
258
|
+
| `ip_address` | `192.168.1.1` (anonymous only) |
|
|
259
|
+
| `was_blocked` | `True / False` |
|
|
260
|
+
|
|
261
|
+
---
|
|
262
|
+
|
|
263
|
+
### 📊 analyse_logs
|
|
264
|
+
|
|
265
|
+
Reads yesterday's logs and writes a plain English report using AI.
|
|
266
|
+
|
|
267
|
+
```bash
|
|
268
|
+
python manage.py analyse_logs
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
**What it covers:**
|
|
272
|
+
- Overall API health assessment
|
|
273
|
+
- Error rate and what it means
|
|
274
|
+
- Slowest endpoints and likely causes
|
|
275
|
+
- Suspicious activity worth investigating
|
|
276
|
+
- 2–3 clear actionable recommendations
|
|
277
|
+
|
|
278
|
+
**Report saved to Django admin → Daily Reports. Always accessible.**
|
|
279
|
+
|
|
280
|
+
**Auto cleanup:** Logs older than `LOG_RETENTION_DAYS` deleted automatically.
|
|
281
|
+
Your database never grows out of control.
|
|
282
|
+
|
|
283
|
+
**Auto schedule (requires apscheduler):**
|
|
284
|
+
|
|
285
|
+
```python
|
|
286
|
+
SMART_MIDDLEWARE = {
|
|
287
|
+
...
|
|
288
|
+
'ANALYSE_LOGS_AT': '06:00', # runs every day at 6am automatically
|
|
289
|
+
}
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
**Or use cron:**
|
|
293
|
+
|
|
294
|
+
```bash
|
|
295
|
+
0 6 * * * /path/to/venv/bin/python /path/to/manage.py analyse_logs
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
---
|
|
299
|
+
|
|
300
|
+
## AI Providers
|
|
301
|
+
|
|
302
|
+
Works with any OpenAI-compatible provider:
|
|
303
|
+
|
|
304
|
+
| Provider | `AI_BASE_URL` | Notes |
|
|
305
|
+
|---|---|---|
|
|
306
|
+
| **Groq** | `https://api.groq.com/openai/v1` | Fast, generous free tier — recommended |
|
|
307
|
+
| **OpenAI** | `https://api.openai.com/v1` | Most capable |
|
|
308
|
+
| **Gemini** | `https://generativelanguage.googleapis.com/v1beta/openai` | Google free tier |
|
|
309
|
+
| **Ollama** | `http://localhost:11434/v1` | Fully local, completely free |
|
|
310
|
+
|
|
311
|
+
> 💡 `RateLimiter` and `WatchLog` need zero AI configuration.
|
|
312
|
+
> Only `AIAnomalyDetector`, `AIRequestValidator`, and `analyse_logs` need a key.
|
|
313
|
+
|
|
314
|
+
---
|
|
315
|
+
|
|
316
|
+
## Complete Settings Reference
|
|
317
|
+
|
|
318
|
+
```python
|
|
319
|
+
SMART_MIDDLEWARE = {
|
|
320
|
+
|
|
321
|
+
# AI — required for AI middlewares and analyse_logs
|
|
322
|
+
'AI_API_KEY': 'your-key',
|
|
323
|
+
'AI_BASE_URL': 'https://api.groq.com/openai/v1',
|
|
324
|
+
'AI_MODEL': 'llama3-8b-8192',
|
|
325
|
+
|
|
326
|
+
# RateLimiter
|
|
327
|
+
'PLAN_FIELD': 'plan', # field name on User model
|
|
328
|
+
'RATE_LIMIT_PLANS': {
|
|
329
|
+
'free': {
|
|
330
|
+
'/api/generate/': {
|
|
331
|
+
'per_minute': 2,
|
|
332
|
+
'per_hour': 20,
|
|
333
|
+
'per_day': 100,
|
|
334
|
+
'lifetime': 1000,
|
|
335
|
+
},
|
|
336
|
+
},
|
|
337
|
+
'premium': {
|
|
338
|
+
'/api/generate/': {
|
|
339
|
+
'per_minute': 50,
|
|
340
|
+
'per_day': 5000,
|
|
341
|
+
},
|
|
342
|
+
},
|
|
343
|
+
},
|
|
344
|
+
|
|
345
|
+
# analyse_logs
|
|
346
|
+
'LOG_RETENTION_DAYS': 30, # default: 30
|
|
347
|
+
'ANALYSE_LOGS_AT': '06:00', # remove to use cron instead
|
|
348
|
+
|
|
349
|
+
# AIAnomalyDetector — optional tuning
|
|
350
|
+
'grey_suspicion_threshold': 4,
|
|
351
|
+
'grey_hard_block_score': 8,
|
|
352
|
+
'grey_sensitive_paths': [
|
|
353
|
+
'/admin', '/.env', '/api/token',
|
|
354
|
+
],
|
|
355
|
+
}
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
---
|
|
359
|
+
|
|
360
|
+
## Requirements
|
|
361
|
+
|
|
362
|
+
- Python 3.10+
|
|
363
|
+
- Django 4.2+
|
|
364
|
+
- `httpx` — installed automatically
|
|
365
|
+
- `apscheduler` — optional, only for `ANALYSE_LOGS_AT`
|
|
366
|
+
|
|
367
|
+
---
|
|
368
|
+
|
|
369
|
+
## Known Limitations
|
|
370
|
+
|
|
371
|
+
| Limitation | Workaround |
|
|
372
|
+
|---|---|
|
|
373
|
+
| Coordinated attacks from many IPs | Use Cloudflare or AWS WAF in front |
|
|
374
|
+
| Slow drip attacks (1 req/hour over days) | Will appear in `analyse_logs` report |
|
|
375
|
+
| AI backend unreachable | All middleware fails open — app never breaks |
|
|
376
|
+
| Cache resets on server restart | Use Redis cache for persistent rate limiting |
|
|
377
|
+
|
|
378
|
+
---
|
|
379
|
+
|
|
380
|
+
## Roadmap
|
|
381
|
+
|
|
382
|
+
- [ ] Usage dashboard at `/smart-layer/usage/`
|
|
383
|
+
- [ ] Grey-zone AI analysis in `AIAnomalyDetector`
|
|
384
|
+
- [ ] Email delivery for daily reports
|
|
385
|
+
- [ ] Test suite
|
|
386
|
+
|
|
387
|
+
---
|
|
388
|
+
|
|
389
|
+
## License
|
|
390
|
+
|
|
391
|
+
MIT — free to use, modify, and distribute.
|
|
392
|
+
|
|
393
|
+
---
|
|
394
|
+
|
|
395
|
+
*Built for Django developers who want real protection without the complexity.*
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: django-smart-layer
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: AI-powered Django middleware for security, monitoring and rate limiting
|
|
5
|
+
Requires-Python: >=3.10
|
|
6
|
+
Requires-Dist: django>=4.2
|
|
7
|
+
Requires-Dist: httpx>=0.27
|
|
8
|
+
Provides-Extra: scheduler
|
|
9
|
+
Requires-Dist: apscheduler>=3.10; extra == "scheduler"
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
django_smart_layer.egg-info/PKG-INFO
|
|
4
|
+
django_smart_layer.egg-info/SOURCES.txt
|
|
5
|
+
django_smart_layer.egg-info/dependency_links.txt
|
|
6
|
+
django_smart_layer.egg-info/requires.txt
|
|
7
|
+
django_smart_layer.egg-info/top_level.txt
|
|
8
|
+
smartlayer/__init__.py
|
|
9
|
+
smartlayer/admin.py
|
|
10
|
+
smartlayer/apps.py
|
|
11
|
+
smartlayer/models.py
|
|
12
|
+
smartlayer/utils.py
|
|
13
|
+
smartlayer/management/__init__.py
|
|
14
|
+
smartlayer/management/commands/AILogAnalyser.py
|
|
15
|
+
smartlayer/management/commands/__init__.py
|
|
16
|
+
smartlayer/middleware/AIAnomalyDetector.py
|
|
17
|
+
smartlayer/middleware/AIRequestValidator.py
|
|
18
|
+
smartlayer/middleware/Rate_Limiter.py
|
|
19
|
+
smartlayer/middleware/WatchLog.py
|
|
20
|
+
smartlayer/middleware/__init__.py
|
|
21
|
+
smartlayer/middleware/rate_Limiter.py
|
|
22
|
+
smartlayer/migrations/0001_initial.py
|
|
23
|
+
smartlayer/migrations/__init__.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
smartlayer
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "django-smart-layer"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "AI-powered Django middleware for security, monitoring and rate limiting"
|
|
5
|
+
requires-python = ">=3.10"
|
|
6
|
+
dependencies = [
|
|
7
|
+
"django>=4.2",
|
|
8
|
+
"httpx>=0.27",
|
|
9
|
+
]
|
|
10
|
+
|
|
11
|
+
[project.optional-dependencies]
|
|
12
|
+
scheduler = [
|
|
13
|
+
"apscheduler>=3.10",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
[build-system]
|
|
17
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
18
|
+
build-backend = "setuptools.build_meta"
|
|
19
|
+
|
|
20
|
+
[tool.setuptools.packages.find]
|
|
21
|
+
where = ["."]
|
|
22
|
+
include = ["smartlayer*"]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
default_app_config = 'smartlayer.apps.SmartLayerConfig'
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# smart_layer/admin.py
|
|
2
|
+
from django.contrib import admin
|
|
3
|
+
from smartlayer.models import DailyReport, RequestLog, BannedUser
|
|
4
|
+
|
|
5
|
+
@admin.register(DailyReport)
|
|
6
|
+
class DailyReportAdmin(admin.ModelAdmin):
|
|
7
|
+
list_display = ['date', 'created_at']
|
|
8
|
+
readonly_fields= ['date', 'report', 'created_at']
|
|
9
|
+
ordering = ['-date']
|
|
10
|
+
|
|
11
|
+
@admin.register(RequestLog)
|
|
12
|
+
class RequestLogAdmin(admin.ModelAdmin):
|
|
13
|
+
list_display = ['method', 'path', 'status_code', 'response_time_ms', 'user_id', 'was_blocked', 'timestamp']
|
|
14
|
+
readonly_fields= ['method', 'path', 'status_code', 'response_time_ms', 'user_id', 'ip_address', 'was_blocked', 'timestamp']
|
|
15
|
+
ordering = ['-timestamp']
|
|
16
|
+
|
|
17
|
+
@admin.register(BannedUser)
|
|
18
|
+
class BannedUserAdmin(admin.ModelAdmin):
|
|
19
|
+
list_display = ['ip_address', 'reason', 'banned_at', 'expires_at']
|
|
20
|
+
ordering = ['-banned_at']
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from django.apps import AppConfig
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class SmartLayerConfig(AppConfig):
|
|
5
|
+
name = 'smartlayer' # ← fixed
|
|
6
|
+
|
|
7
|
+
def ready(self):
|
|
8
|
+
from django.conf import settings
|
|
9
|
+
config = getattr(settings, 'SMART_MIDDLEWARE', {})
|
|
10
|
+
|
|
11
|
+
schedule_time = config.get('ANALYSE_LOGS_AT')
|
|
12
|
+
if not schedule_time:
|
|
13
|
+
return
|
|
14
|
+
|
|
15
|
+
try:
|
|
16
|
+
from apscheduler.schedulers.background import BackgroundScheduler
|
|
17
|
+
from django.core.management import call_command
|
|
18
|
+
|
|
19
|
+
hour, minute = schedule_time.split(':')
|
|
20
|
+
|
|
21
|
+
scheduler = BackgroundScheduler()
|
|
22
|
+
scheduler.add_job(
|
|
23
|
+
lambda: call_command('analyse_logs'),
|
|
24
|
+
'cron',
|
|
25
|
+
hour=int(hour),
|
|
26
|
+
minute=int(minute),
|
|
27
|
+
id='smartlayer_analyse_logs',
|
|
28
|
+
replace_existing=True
|
|
29
|
+
)
|
|
30
|
+
scheduler.start()
|
|
31
|
+
|
|
32
|
+
except ImportError:
|
|
33
|
+
import warnings
|
|
34
|
+
warnings.warn( # ← added warning
|
|
35
|
+
"[Smart Layer] ANALYSE_LOGS_AT is set but apscheduler is not installed. "
|
|
36
|
+
"Run: pip install apscheduler "
|
|
37
|
+
"Or remove ANALYSE_LOGS_AT and use cron instead.",
|
|
38
|
+
RuntimeWarning
|
|
39
|
+
)
|
|
File without changes
|