Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
90 lines (78 loc) · 2.87 KB
/
Copy pathutils.py
File metadata and controls
90 lines (78 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import re
import os
import hashlib
import asyncio
from pathlib import Path
from typing import Optional, List, Dict, Any
from contextlib import asynccontextmanager
from telethon import TelegramClient
from config import config
def sanitize_log_input(text: str) -> str:
"""Sanitize input for logging to prevent log injection"""
if not text:
return ""
return re.sub(r'[\r\n\t]', ' ', str(text)[:200])
def validate_path(path: str, base_dir: str) -> bool:
"""Validate file path to prevent directory traversal"""
try:
resolved_path = Path(path).resolve()
base_path = Path(base_dir).resolve()
return str(resolved_path).startswith(str(base_path))
except:
return False
def secure_filename(filename: str) -> str:
"""Generate secure filename"""
filename = re.sub(r'[^\w\-_\.]', '', filename)
return filename[:100] if filename else "file"
def hash_session_id(user_id: int) -> str:
"""Generate secure session identifier"""
return hashlib.sha256(f"{user_id}_{config.get('encryption_key', 'default')}".encode()).hexdigest()[:16]
@asynccontextmanager
async def get_telegram_client(session_path: str):
"""Context manager for Telegram client with proper resource cleanup"""
client = None
try:
client = TelegramClient(
session_path,
config.get('api_id'),
config.get('api_hash'),
device_model="Samsung SM-G973F",
system_version="Android 10",
app_version="8.4.1",
lang_code="en",
system_lang_code="en",
proxy=config.get('proxy_url')
)
await client.connect()
yield client
finally:
if client:
await client.disconnect()
class RateLimiter:
def __init__(self, max_calls: int = 30, time_window: int = 60):
self.max_calls = max_calls
self.time_window = time_window
self.calls = {}
def is_allowed(self, user_id: int) -> bool:
import time
now = time.time()
if user_id not in self.calls:
self.calls[user_id] = []
self.calls[user_id] = [call for call in self.calls[user_id] if now - call < self.time_window]
if len(self.calls[user_id]) >= self.max_calls:
return False
self.calls[user_id].append(now)
return True
rate_limiter = RateLimiter()
def format_session_info(info: Dict[str, Any]) -> str:
"""Format session information for display"""
return f"""🆔 {info.get('name', 'Unknown')}
👤 Premium: {'Yes' if info.get('premium') else 'No'}
🔑 ID: {info.get('id', 'N/A')}
📛 Username: @{info.get('username', 'None')}
📞 Phone: {info.get('phone', 'Hidden')}
📬 Dialogs: {info.get('dialogs', 0)}
📇 Contacts: {info.get('contacts', 0)}
📢 Channels: {info.get('channels', 0)}
🤖 Bots: {info.get('bots', 0)}
💬 Groups: {info.get('groups', 0)}"""