Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
539 lines (445 loc) · 21 KB
/
Copy pathmain.py
File metadata and controls
539 lines (445 loc) · 21 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
#!/usr/bin/env python3
"""TeleGuard - Professional Telegram Account Manager
A secure, professional-grade Telegram bot for managing multiple user accounts
with advanced OTP destroyer protection against unauthorized access attempts.
Developed by:
- @Meher_Mankar (https://t.me/Meher_Mankar)
- @Gutkesh (https://t.me/Gutkesh)
Repository: https://github.com/MeherMankar/TeleGuard
Support: https://t.me/ContactXYZrobot
Documentation: https://github.com/MeherMankar/TeleGuard/wiki
License: MIT
Version: 2.0.0
"""
# Set UTF-8 encoding for Windows compatibility
import os
os.environ['PYTHONIOENCODING'] = 'utf-8'
import asyncio
import logging
import warnings
import signal
# Suppress experimental async session warning from Telethon
warnings.filterwarnings("ignore", category=UserWarning, module="telethon")
import sys
import time
import traceback
from logging.handlers import RotatingFileHandler
from pathlib import Path
from typing import NoReturn
from aiohttp import web
# Ensure stdout/stderr use UTF-8 on Windows so logging can emit emojis
try:
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8")
if hasattr(sys.stderr, "reconfigure"):
sys.stderr.reconfigure(encoding="utf-8")
except Exception:
# Fall back silently if reconfigure not available or fails
pass
# Monkey-patch StreamHandler.emit to guard against encoding errors (Windows consoles)
_orig_streamhandler_emit = logging.StreamHandler.emit
def _safe_streamhandler_emit(self, record):
try:
_orig_streamhandler_emit(self, record)
except UnicodeEncodeError:
try:
msg = self.format(record)
# Replace any non-encodable characters so write doesn't fail
safe_msg = msg.encode(getattr(self.stream, 'encoding', 'utf-8'), errors='replace').decode(getattr(self.stream, 'encoding', 'utf-8'), errors='replace')
stream = self.stream
stream.write(safe_msg + self.terminator)
self.flush()
except Exception:
try:
self.handleError(record)
except Exception:
pass
logging.StreamHandler.emit = _safe_streamhandler_emit
try:
from teleguard import AccountManager, __version__
from teleguard.core.client_manager import get_client_manager
from teleguard.core.task_queue import task_queue
from teleguard.core.database_manager import init_database_manager, db_manager
from teleguard.utils.health_server import health_checker
from teleguard.utils.logger import get_logger, BotLogger
except ImportError as e:
print(f"Failed to import TeleGuard modules: {e}")
print(
"💡 Please ensure all dependencies are installed: pip install -r requirements.txt"
)
sys.exit(1)
# Configure professional logging
log_dir = Path("logs")
log_dir.mkdir(exist_ok=True)
# Enhanced logging setup with rotation and structured format
# Create formatters
detailed_formatter = logging.Formatter(
'%(asctime)s | %(name)-20s | %(levelname)-8s | %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
simple_formatter = logging.Formatter(
'%(levelname)s: %(message)s'
)
# Configure root logger
root_logger = logging.getLogger()
root_logger.setLevel(logging.INFO)
# File handler with rotation (10MB max, keep 5 files)
file_handler = RotatingFileHandler(
log_dir / "teleguard.log",
maxBytes=10*1024*1024, # 10MB
backupCount=5,
encoding="utf-8"
)
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(detailed_formatter)
# Safe console handler that handles Unicode on Windows cp1252 consoles
class SafeConsoleHandler(logging.StreamHandler):
"""Console handler that safely handles Unicode characters on Windows"""
def emit(self, record):
try:
msg = self.format(record)
stream = self.stream
encoding = getattr(stream, 'encoding', 'utf-8') or 'utf-8'
# Encode with 'replace' so unmappable chars become '?' instead of crashing
safe_msg = msg.encode(encoding, errors='replace').decode(encoding, errors='replace')
stream.write(safe_msg + self.terminator)
self.flush()
except Exception:
self.handleError(record)
console_handler = SafeConsoleHandler()
# Console shows only WARNING+ to keep output clean.
# All INFO detail goes to the rotating log file.
console_handler.setLevel(logging.WARNING)
console_handler.setFormatter(simple_formatter)
# Remove all existing handlers from root logger
root_logger.handlers.clear()
# Add handlers to root logger
root_logger.addHandler(file_handler)
root_logger.addHandler(console_handler)
# Configure teleguard loggers - remove their handlers and let them propagate to root
for logger_name in ["teleguard", "teleguard.core", "teleguard.handlers", "teleguard.utils"]:
mod_logger = logging.getLogger(logger_name)
mod_logger.handlers.clear() # Remove any existing handlers
mod_logger.setLevel(logging.INFO)
mod_logger.propagate = True # Use root logger's handlers
# Silence noisy external modules
for mod in ["telethon", "aiosqlite", "pymongo", "redis", "asyncio", "motor", "urllib3", "aiohttp"]:
mod_logger = logging.getLogger(mod)
mod_logger.handlers.clear() # Remove any existing handlers
mod_logger.setLevel(logging.ERROR)
mod_logger.propagate = True # Use root logger's handlers
# Get logger after configuration
logger = get_logger(__name__)
logger.debug("Logging system configured with UTF-8 support")
async def perform_startup_checks() -> bool:
"""Perform comprehensive startup health checks.
Returns:
bool: True if all checks pass, False otherwise
"""
logger.info("🔍 Performing startup health checks...")
try:
health_status = await health_checker.get_health_status()
if health_status.get("status") == "healthy":
logger.info("✅ All health checks passed")
return True
else:
logger.error("❌ Health checks failed: %s", health_status.get("issues", []))
return False
except Exception as e:
logger.error("💥 Health check error: %s", str(e))
return False
async def graceful_shutdown() -> None:
"""Perform graceful shutdown of all services."""
logger.info("🔄 Initiating graceful shutdown...")
shutdown_tasks = []
start_time = time.time()
try:
# Cancel all pending tasks
logger.info("🛑 Cancelling pending tasks...")
tasks = [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]
for task in tasks:
task.cancel()
# Wait for tasks to cancel
await asyncio.gather(*tasks, return_exceptions=True)
# Stop task queue
logger.info("⏹️ Stopping task queue...")
shutdown_tasks.append(task_queue.stop())
# Shutdown client manager
logger.info("🔌 Shutting down client manager...")
client_manager = get_client_manager()
if client_manager:
shutdown_tasks.append(client_manager.shutdown_all_managed_clients())
else:
logger.debug("No client manager to shutdown")
# Wait for all shutdown tasks with timeout
logger.info("⏳ Waiting for shutdown tasks to complete (10s timeout)...")
await asyncio.wait_for(
asyncio.gather(*shutdown_tasks, return_exceptions=True), timeout=10.0
)
elapsed = time.time() - start_time
logger.info("✅ Graceful shutdown completed in %.2f seconds", elapsed)
except asyncio.TimeoutError:
elapsed = time.time() - start_time
logger.warning("⚠️ Shutdown timeout reached after %.2f seconds, forcing exit", elapsed)
except Exception as e:
logger.error("💥 Error during shutdown: %s", str(e))
logger.debug("Shutdown error details:", exc_info=True)
finally:
# Ensure logging is flushed
logger.debug("💾 Flushing log handlers...")
for handler in logging.getLogger().handlers:
handler.flush()
def setup_signal_handlers() -> None:
"""Setup signal handlers for graceful shutdown."""
def signal_handler(signum: int, _) -> NoReturn:
signal_name = signal.Signals(signum).name
logger.info("📶 Received %s signal, initiating shutdown", signal_name)
print(f"\n📶 Received {signal_name} signal, shutting down gracefully...")
# Create shutdown task
try:
loop = asyncio.get_running_loop()
except RuntimeError:
loop = None
if loop and loop.is_running():
loop.create_task(graceful_shutdown())
sys.exit(0)
# Register signal handlers
signal.signal(signal.SIGINT, signal_handler) # Ctrl+C
signal.signal(signal.SIGTERM, signal_handler) # Termination signal
if hasattr(signal, "SIGHUP"): # Unix only
signal.signal(signal.SIGHUP, signal_handler)
async def health_check(request):
"""Health check endpoint for cloud platforms"""
client_ip = request.remote
user_agent = request.headers.get('User-Agent', 'Unknown')
logger.debug("🌡️ Health check from %s (UA: %s)", client_ip, user_agent)
response_data = {
"status": "healthy",
"service": "teleguard",
"timestamp": time.time()
}
return web.json_response(response_data)
async def ip_status(request):
"""IP status endpoint for monitoring"""
from teleguard.core.session_guardian import get_guardian
guardian = get_guardian()
if guardian:
return web.json_response({
"current_ip": guardian.current_ip,
"ip_changes": guardian.ip_change_count,
"stability": "high" if guardian.ip_change_count < 3 else "medium" if guardian.ip_change_count < 10 else "low",
"last_check": guardian.last_ip_check
})
return web.json_response({"status": "guardian_not_initialized"})
async def start_web_server():
"""Start enhanced web server with IP monitoring"""
logger.info("🌐 Setting up enhanced health check server...")
app = web.Application()
app.router.add_get("/health", health_check)
app.router.add_get("/ip", ip_status)
app.router.add_get("/", health_check)
logger.debug("🔗 Enhanced routes configured")
port = int(os.getenv("PORT", 8000))
logger.info("🔌 Attempting to start web server on port %d", port)
runner = web.AppRunner(app)
await runner.setup()
# Try multiple ports if the default is in use
ports_tried = []
for attempt_port in [port, port + 1, port + 2, 8001, 8080, 3000]:
try:
site = web.TCPSite(runner, "0.0.0.0", attempt_port)
await site.start()
print(f"Health server running on port {attempt_port}")
logger.info("✅ Health check server started successfully on port %d", attempt_port)
return runner
except OSError as e:
ports_tried.append(attempt_port)
if "10048" in str(e) or "Address already in use" in str(e):
logger.debug("🚫 Port %d in use, trying next...", attempt_port)
continue
logger.error("💥 Unexpected error on port %d: %s", attempt_port, str(e))
raise
logger.warning("⚠️ Could not start health server - all ports in use: %s", ports_tried)
return runner
def print_startup_banner() -> None:
"""Print professional startup banner."""
try:
# Dynamically center version in the banner slot (12 characters wide)
banner = f"""
╔══════════════════════════════════════════════════════════════╗
║ TeleGuard v{__version__:<12} ║
║ Professional Telegram Account Manager ║
╠══════════════════════════════════════════════════════════════╣
║ OTP Destroyer Protection Multi-Account Support ║
║ Military-Grade Encryption Advanced Automation ║
║ Health Monitoring Activity Simulation ║
╠══════════════════════════════════════════════════════════════╣
║ Developers: @Meher_Mankar & @Gutkesh ║
║ GitHub: github.com/MeherMankar/TeleGuard ║
║ Support: t.me/ContactXYZrobot ║
║ Docs: github.com/MeherMankar/TeleGuard/wiki ║
╚══════════════════════════════════════════════════════════════╝
"""
print(banner)
except UnicodeEncodeError:
# Fallback for systems with encoding issues
print("=" * 60)
print(f" TeleGuard v{__version__}")
print(" Professional Telegram Account Manager")
print("=" * 60)
print("Features:")
print("- OTP Destroyer Protection")
print("- Multi-Account Support")
print("- Military-Grade Encryption")
print("- Advanced Automation")
print("=" * 60)
print("Developers: @Meher_Mankar & @Gutkesh")
print("GitHub: github.com/MeherMankar/TeleGuard")
print("Support: t.me/ContactXYZrobot")
print("=" * 60)
async def main() -> None:
"""Main application entry point with comprehensive error handling."""
# Force rebuild - fixed indentation issue
startup_time = time.time()
logger.debug("TeleGuard application starting up")
try:
# Print startup banner
print_startup_banner()
logger.debug("Startup banner displayed")
# Setup signal handlers
setup_signal_handlers()
logger.debug("Signal handlers configured")
# Setup global error handler
BotLogger.setup_global_error_handler()
logger.debug("Global error handler configured")
# Database initialization
logger.debug("Initializing database connections")
print("Connecting to database...")
await init_database_manager()
health = await db_manager.health_check()
if health.get('mongodb') and health.get('redis'):
print("Database connected successfully")
logger.info("✅ Database health check passed - MongoDB: %s, Redis: %s",
health.get('mongodb'), health.get('redis'))
else:
logger.warning("⚠️ Database health check issues: %s", health)
# ── Start FastAPI dashboard server (replaces aiohttp health server) ──
# FastAPI handles /health, /ip, AND all /api/* routes on the single PORT
try:
from backend.main import start_api_server
api_task = asyncio.create_task(start_api_server())
logger.info("🌐 FastAPI dashboard server starting on port %s", os.getenv("PORT", "8080"))
# Give it a moment to bind before bot starts
await asyncio.sleep(1)
except Exception as api_err:
logger.error(f"💥 Dashboard API server failed to start: {api_err}")
raise RuntimeError(f"Cannot start API server: {api_err}")
# ─────────────────────────────────────────────────────────────────────
# Initialize Koyeb optimization
if os.getenv('KOYEB_OPTIMIZATION_ENABLED', 'true').lower() == 'true':
logger.debug("Starting Koyeb optimization")
from teleguard.utils.koyeb_optimizer import koyeb_optimizer
await koyeb_optimizer.start_optimization()
# Initialize session guardian with IP monitoring
logger.debug("Initializing session guardian")
from teleguard.core.session_guardian import init_guardian
from teleguard.utils.guardian_config import load_guardian_config
guardian_config = load_guardian_config()
g = init_guardian(guardian_config)
# Start IP monitoring background task (requires running event loop)
await g.start_monitoring()
logger.debug("Session guardian active")
print("\n" + "="*50)
logger.debug("Initializing TeleGuard bot")
# Start bot without timeout on cloud platforms
try:
async with AccountManager() as bot:
startup_elapsed = time.time() - startup_time
koyeb_status = " + Koyeb optimized" if os.getenv('KOYEB_OPTIMIZATION_ENABLED', 'true').lower() == 'true' else ""
print(f"\nTeleGuard is ready! 🌐 Smart IP monitoring active{koyeb_status}")
logger.info("✨ TeleGuard bot ready! Startup completed in %.2f seconds", startup_elapsed)
logger.debug("Starting bot main loop")
await bot.run()
except Exception as e:
# Handle rate limits and other startup errors
logger.error("🚨 Bot startup error: %s", str(e))
if "FloodWaitError" in str(e) or "wait of" in str(e):
import re
wait_match = re.search(r'wait of (\d+) seconds', str(e))
if wait_match:
wait_time = int(wait_match.group(1))
logger.info("⏳ Waiting %d seconds due to rate limit...", wait_time)
print(f"\nTelegram rate limit - waiting {wait_time} seconds...")
print("📝 This is normal, the bot will start automatically")
# Keep health server running during wait
await asyncio.sleep(min(wait_time, 300)) # Cap at 5 minutes
logger.info("✅ Rate limit wait completed, retrying...")
print("Retrying startup...")
try:
async with AccountManager() as bot:
logger.info("✅ Bot started successfully after rate limit")
print("Bot started successfully after rate limit")
await bot.run()
except Exception as retry_error:
logger.error("💥 Retry failed: %s", str(retry_error))
print(f"Retry failed: {retry_error}")
# Keep health server running
while True:
await asyncio.sleep(60)
else:
logger.error("🚨 Could not parse wait time from rate limit error")
raise e
else:
logger.error("💥 Unhandled startup error: %s", str(e))
logger.debug("Startup error details:", exc_info=True)
raise e
except KeyboardInterrupt:
logger.debug("Keyboard interrupt received")
print("\nShutting down TeleGuard...")
await graceful_shutdown()
sys.exit(0)
except ImportError as e:
logger.error("📦 Missing dependencies: %s", str(e))
print(f"\nMissing dependencies: {e}")
print("Run: pip install -r requirements.txt")
sys.exit(1)
except FileNotFoundError as e:
logger.error("📁 Configuration file not found: %s", str(e))
print(f"\nConfiguration error: {e}")
print("Ensure .env file exists with required variables")
sys.exit(1)
except Exception as e:
total_runtime = time.time() - startup_time
logger.error("💥 Fatal application error after %.2f seconds: %s", total_runtime, str(e))
logger.error("📋 Full traceback: %s", traceback.format_exc())
print(f"\n🚨 Fatal error occurred: {e}")
print("📝 Check logs/teleguard.log for detailed error information")
print("🆘 Need help? Contact: https://t.me/ContactXYZrobot")
try:
await BotLogger.log_error("Fatal Error", str(e), context=f"main.py after {total_runtime:.2f}s")
except Exception:
pass
try:
await graceful_shutdown()
except Exception as shutdown_error:
logger.error("💥 Shutdown error: %s", str(shutdown_error))
# Exit immediately on fatal startup errors
print("\n⚠️ Bot process will exit due to fatal error")
sys.exit(1)
if __name__ == "__main__":
# Use uvloop on Linux/macOS for a significant asyncio speed boost.
# uvloop 0.21+ supports Python 3.13. Falls back to the default loop on Windows.
try:
import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
logger.debug("uvloop event loop policy active")
except ImportError:
pass # Windows or uvloop not installed — use default asyncio loop
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\nGoodbye!")
sys.exit(0)
except Exception as e:
print(f"\nCritical error: {e}")
sys.exit(1)