DEL MAX_FILE_PER_IP rate limit as it does not get flushed and the other rate limit is more than enough
This commit is contained in:
parent
5b631a20b1
commit
1bcdc4cc1c
1 changed files with 1 additions and 19 deletions
18
main.py
18
main.py
|
@ -10,8 +10,6 @@ import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import hashlib
|
import hashlib
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
from collections import defaultdict
|
|
||||||
import threading
|
|
||||||
|
|
||||||
|
|
||||||
DOMAIN = os.getenv('DOMAIN', 'linedump.com')
|
DOMAIN = os.getenv('DOMAIN', 'linedump.com')
|
||||||
|
@ -30,10 +28,6 @@ UPLOAD_DIR = Path("uploads")
|
||||||
UPLOAD_DIR.mkdir(exist_ok=True)
|
UPLOAD_DIR.mkdir(exist_ok=True)
|
||||||
|
|
||||||
MAX_FILE_SIZE = MAX_FILE_SIZE_MB * 1024 * 1024 # Convert MB to bytes
|
MAX_FILE_SIZE = MAX_FILE_SIZE_MB * 1024 * 1024 # Convert MB to bytes
|
||||||
MAX_FILES_PER_IP = 100
|
|
||||||
|
|
||||||
file_counter = defaultdict(int)
|
|
||||||
lock = threading.Lock()
|
|
||||||
|
|
||||||
def generate_random_path(length: int = None) -> str:
|
def generate_random_path(length: int = None) -> str:
|
||||||
if length is None:
|
if length is None:
|
||||||
|
@ -48,11 +42,6 @@ def get_client_ip(request: Request) -> str:
|
||||||
return request.client.host
|
return request.client.host
|
||||||
|
|
||||||
|
|
||||||
def check_file_limit(request: Request) -> bool:
|
|
||||||
client_ip = get_client_ip(request)
|
|
||||||
with lock:
|
|
||||||
return file_counter[client_ip] < MAX_FILES_PER_IP
|
|
||||||
|
|
||||||
def validate_content(content: str) -> bool:
|
def validate_content(content: str) -> bool:
|
||||||
"""Basic validation for content size and encoding"""
|
"""Basic validation for content size and encoding"""
|
||||||
if len(content) > MAX_FILE_SIZE:
|
if len(content) > MAX_FILE_SIZE:
|
||||||
|
@ -73,9 +62,6 @@ def validate_content(content: str) -> bool:
|
||||||
@limiter.limit(RATE_LIMIT)
|
@limiter.limit(RATE_LIMIT)
|
||||||
async def upload_text(request: Request):
|
async def upload_text(request: Request):
|
||||||
|
|
||||||
if not check_file_limit(request):
|
|
||||||
raise HTTPException(status_code=429, detail="File limit exceeded")
|
|
||||||
|
|
||||||
body = await request.body()
|
body = await request.body()
|
||||||
content = body.decode('utf-8', errors='ignore')
|
content = body.decode('utf-8', errors='ignore')
|
||||||
|
|
||||||
|
@ -95,10 +81,6 @@ async def upload_text(request: Request):
|
||||||
with open(file_path, 'w', encoding='utf-8') as f:
|
with open(file_path, 'w', encoding='utf-8') as f:
|
||||||
f.write(content)
|
f.write(content)
|
||||||
|
|
||||||
client_ip = get_client_ip(request)
|
|
||||||
with lock:
|
|
||||||
file_counter[client_ip] += 1
|
|
||||||
|
|
||||||
base_url = f"https://{request.headers.get('host', request.url.netloc)}"
|
base_url = f"https://{request.headers.get('host', request.url.netloc)}"
|
||||||
return f"{base_url}/{random_path}\n"
|
return f"{base_url}/{random_path}\n"
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue