|
45 | 45 | import uuid |
46 | 46 | import warnings |
47 | 47 | import re |
| 48 | +import time |
| 49 | +import signal |
| 50 | +import select |
| 51 | +import sys |
48 | 52 | from collections import deque |
49 | 53 | from struct import unpack |
50 | 54 |
|
@@ -990,25 +994,34 @@ def send_startup(totp_value=None): |
990 | 994 | # ✅ If TOTP not provided initially, allow 3 retry attempts |
991 | 995 | if not totp: |
992 | 996 | max_attempts = 3 |
| 997 | + timeout_seconds = 300 # 5 minutes timeout |
993 | 998 | for attempt in range(max_attempts): |
994 | 999 | try: |
995 | | - totp_input = input(f"🔐 Enter TOTP (attempt {attempt+1}/{max_attempts}): ").strip() |
996 | | - if not totp_input: |
997 | | - print("⚠️ TOTP cannot be empty.") |
998 | | - continue |
999 | | - totp = totp_input |
1000 | | - self.close_socket() |
1001 | | - self.socket = self.establish_socket_connection(self.address_list) |
1002 | | - self._logger.info(f"🚀 Retrying with TOTP: '{totp}'") |
1003 | | - # ✅ Re-init required attributes |
1004 | | - self.backend_pid = 0 |
1005 | | - self.backend_key = 0 |
1006 | | - self.transaction_status = None |
1007 | | - self.session_id = None |
1008 | | - self._logger.debug("✅ Startup message sent with TOTP.") |
1009 | | - # Send new startup message with updated TOTP |
1010 | | - send_startup(totp_value=totp) |
1011 | | - break |
| 1000 | + print(f"🔐 Enter TOTP (attempt {attempt+1}/{max_attempts}): ", end="", flush=True) |
| 1001 | + ready, _, _ = select.select([sys.stdin], [], [], timeout_seconds) |
| 1002 | + if ready: |
| 1003 | + totp_input = sys.stdin.readline().strip() |
| 1004 | + if not totp_input: |
| 1005 | + print("⚠️ TOTP cannot be empty.") |
| 1006 | + continue |
| 1007 | + totp = totp_input |
| 1008 | + self.close_socket() |
| 1009 | + self.socket = self.establish_socket_connection(self.address_list) |
| 1010 | + self._logger.info(f"🚀 Retrying with TOTP: '{totp}'") |
| 1011 | + # ✅ Re-init required attributes |
| 1012 | + self.backend_pid = 0 |
| 1013 | + self.backend_key = 0 |
| 1014 | + self.transaction_status = None |
| 1015 | + self.session_id = None |
| 1016 | + self._logger.debug("✅ Startup message sent with TOTP.") |
| 1017 | + # Send new startup message with updated TOTP |
| 1018 | + send_startup(totp_value=totp) |
| 1019 | + break |
| 1020 | + else: |
| 1021 | + print("⏰ Session timed out. No TOTP entered within time limit.") |
| 1022 | + self._logger.error("Session timeout: No TOTP entered within time limit.") |
| 1023 | + self.close_socket() |
| 1024 | + raise errors.ConnectionError("Session timeout: No TOTP entered within time limit.") |
1012 | 1025 | except (KeyboardInterrupt, EOFError): |
1013 | 1026 | raise errors.ConnectionError("TOTP input cancelled.") |
1014 | 1027 | else: |
|
0 commit comments