Update ssh_tunneler.py
Browse files- ssh_tunneler.py +65 -15
ssh_tunneler.py
CHANGED
|
@@ -58,6 +58,8 @@ class SSHTunnel:
|
|
| 58 |
self.connection_status = "disconnected"
|
| 59 |
self.connection_error = None
|
| 60 |
self.threads = []
|
|
|
|
|
|
|
| 61 |
|
| 62 |
# Set up logging
|
| 63 |
self.logger = logging.getLogger('ssh_tunnel')
|
|
@@ -168,25 +170,62 @@ class SSHTunnel:
|
|
| 168 |
Monitor the connection and reconnect if necessary.
|
| 169 |
"""
|
| 170 |
while self.should_reconnect:
|
| 171 |
-
|
| 172 |
-
self.logger.info("Connection is down, attempting to reconnect...")
|
| 173 |
-
successful = self.connect()
|
| 174 |
-
if not successful:
|
| 175 |
-
self.logger.warning(f"Reconnection failed, waiting {self.reconnect_interval} seconds...")
|
| 176 |
-
time.sleep(self.reconnect_interval)
|
| 177 |
|
| 178 |
-
#
|
| 179 |
-
|
| 180 |
-
self.
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
time.sleep(5) # Check connection status every 5 seconds
|
| 189 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
def _send_keepalive(self):
|
| 191 |
"""
|
| 192 |
Send keepalive packets to maintain the SSH connection.
|
|
@@ -210,6 +249,17 @@ class SSHTunnel:
|
|
| 210 |
dict: Status information including whether the tunnel is running,
|
| 211 |
connection status, and any error messages.
|
| 212 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 213 |
return {
|
| 214 |
"is_running": self.is_running,
|
| 215 |
"status": self.connection_status,
|
|
|
|
| 58 |
self.connection_status = "disconnected"
|
| 59 |
self.connection_error = None
|
| 60 |
self.threads = []
|
| 61 |
+
self.last_check_time = 0
|
| 62 |
+
self.check_interval = 10 # Check tunnel every 10 seconds
|
| 63 |
|
| 64 |
# Set up logging
|
| 65 |
self.logger = logging.getLogger('ssh_tunnel')
|
|
|
|
| 170 |
Monitor the connection and reconnect if necessary.
|
| 171 |
"""
|
| 172 |
while self.should_reconnect:
|
| 173 |
+
current_time = time.time()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
+
# Only check tunnel every check_interval seconds
|
| 176 |
+
if current_time - self.last_check_time >= self.check_interval:
|
| 177 |
+
self.last_check_time = current_time
|
| 178 |
+
|
| 179 |
+
# Perform active check of tunnel status
|
| 180 |
+
is_active = self._check_tunnel_active()
|
| 181 |
+
|
| 182 |
+
if not is_active and self.is_running:
|
| 183 |
+
self.logger.warning("Active check detected tunnel is down despite is_running=True")
|
| 184 |
+
self.stop(reconnect=True)
|
| 185 |
+
continue
|
| 186 |
+
|
| 187 |
+
if not self.is_running:
|
| 188 |
+
self.logger.info("Connection is down, attempting to reconnect...")
|
| 189 |
+
successful = self.connect()
|
| 190 |
+
if not successful:
|
| 191 |
+
self.logger.warning(f"Reconnection failed, waiting {self.reconnect_interval} seconds...")
|
| 192 |
+
time.sleep(self.reconnect_interval)
|
| 193 |
+
|
| 194 |
+
# Check if transport is still active
|
| 195 |
+
elif self.transport and not self.transport.is_active():
|
| 196 |
+
self.logger.warning("Transport is no longer active")
|
| 197 |
+
self.stop(reconnect=True)
|
| 198 |
+
|
| 199 |
+
# Check for activity timeout (in case keepalive fails)
|
| 200 |
+
elif time.time() - self.last_activity_time > self.reconnect_interval * 2:
|
| 201 |
+
self.logger.warning("No activity detected, reconnecting...")
|
| 202 |
+
self.stop(reconnect=True)
|
| 203 |
|
| 204 |
time.sleep(5) # Check connection status every 5 seconds
|
| 205 |
|
| 206 |
+
def _check_tunnel_active(self):
|
| 207 |
+
"""
|
| 208 |
+
Actively check if the tunnel is working by attempting a simple operation.
|
| 209 |
+
|
| 210 |
+
Returns:
|
| 211 |
+
bool: True if the tunnel is active, False otherwise
|
| 212 |
+
"""
|
| 213 |
+
if not self.transport or not self.client:
|
| 214 |
+
return False
|
| 215 |
+
|
| 216 |
+
try:
|
| 217 |
+
# Try to execute a simple command to test connection
|
| 218 |
+
if self.transport.is_active():
|
| 219 |
+
# Try to open a test channel
|
| 220 |
+
test_channel = self.transport.open_channel("session")
|
| 221 |
+
if test_channel:
|
| 222 |
+
test_channel.close()
|
| 223 |
+
return True
|
| 224 |
+
return False
|
| 225 |
+
except Exception as e:
|
| 226 |
+
self.logger.warning(f"Active tunnel check failed: {str(e)}")
|
| 227 |
+
return False
|
| 228 |
+
|
| 229 |
def _send_keepalive(self):
|
| 230 |
"""
|
| 231 |
Send keepalive packets to maintain the SSH connection.
|
|
|
|
| 249 |
dict: Status information including whether the tunnel is running,
|
| 250 |
connection status, and any error messages.
|
| 251 |
"""
|
| 252 |
+
# Do an active check if it's been more than check_interval since last check
|
| 253 |
+
current_time = time.time()
|
| 254 |
+
if current_time - self.last_check_time >= self.check_interval:
|
| 255 |
+
self.last_check_time = current_time
|
| 256 |
+
is_active = self._check_tunnel_active()
|
| 257 |
+
if not is_active and self.is_running:
|
| 258 |
+
self.logger.warning("Status check: tunnel appears down but marked as running")
|
| 259 |
+
self.is_running = False
|
| 260 |
+
self.connection_status = "disconnected"
|
| 261 |
+
self.connection_error = "Tunnel appears to be down"
|
| 262 |
+
|
| 263 |
return {
|
| 264 |
"is_running": self.is_running,
|
| 265 |
"status": self.connection_status,
|