Skip to content

ssh_tunnel_manager.gui.main_window

main_window

SSH Tunnel Manager - Simplified Main Window

Classes

SSHTunnelManager

Bases: QMainWindow

Simplified SSH Tunnel Manager with clean component-based architecture.

Source code in src/ssh_tunnel_manager/gui/main_window.py
 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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
class SSHTunnelManager(QMainWindow):
    """Simplified SSH Tunnel Manager with clean component-based architecture."""

    def __init__(self):
        super().__init__()
        self.setWindowTitle(f"{APP_NAME} - Professional")
        self.setGeometry(100, 100, 1000, 700)

        # Core managers
        self.config_manager = ConfigurationManager()
        self.active_tunnels: Dict[str, TunnelProcess] = {}

        # UI Components
        self.toolbar_manager = ToolbarManager(self)
        self.table_widget = TunnelTableWidget(self)
        self.log_widget = LogWidget(self)
        self.file_ops_manager = FileOperationsManager(self)
        self.rtsp_handler = RTSPHandler(self)
        self.rdp_handler = RDPHandler(self)
        self.network_scanner = NetworkScannerManager(self)
        self.powershell_generator = PowerShellGeneratorManager(self)
        self.ssh_key_manager = SSHKeyManager(self)
        self.ssh_key_deployment = SSHKeyDeploymentManager(self)

        # Setup
        self._setup_ui()
        self._setup_connections()
        self._setup_menu()
        self._setup_system_tray()

        # Initialize data
        self._load_configurations()
        self._start_monitoring()

    def _setup_ui(self):
        """Setup the main UI."""
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        main_layout = QVBoxLayout(central_widget)

        # Toolbar
        toolbar_layout = self.toolbar_manager.create_toolbar()
        main_layout.addLayout(toolbar_layout)

        # Main content splitter
        content_splitter = QSplitter(Qt.Vertical)

        # Table
        table = self.table_widget.create_table()
        content_splitter.addWidget(table)

        # Log
        log_group = self.log_widget.create_log_widget()
        content_splitter.addWidget(log_group)

        content_splitter.setStretchFactor(0, 3)
        content_splitter.setStretchFactor(1, 1)

        main_layout.addWidget(content_splitter)

        # Status bar
        self.statusBar().showMessage("Ready")

        # Initial log
        self.log("SSH Tunnel Manager started")

    def _setup_connections(self):
        """Connect component signals."""
        # Toolbar signals
        self.toolbar_manager.add_tunnel.connect(self._add_tunnel)
        self.toolbar_manager.edit_tunnel.connect(self._edit_tunnel)
        self.toolbar_manager.delete_tunnel.connect(self._delete_tunnel)
        self.toolbar_manager.start_tunnel.connect(self._start_tunnel)
        self.toolbar_manager.stop_tunnel.connect(self._stop_tunnel)
        self.toolbar_manager.test_tunnel.connect(self._test_tunnel)
        self.toolbar_manager.browse_files.connect(self._browse_files)
        self.toolbar_manager.browse_remote_files.connect(self._browse_remote_files)
        self.toolbar_manager.open_web_browser.connect(self._open_web_browser)
        self.toolbar_manager.launch_rtsp.connect(self.rtsp_handler.launch_rtsp)
        self.toolbar_manager.launch_rdp.connect(self.rdp_handler.launch_rdp)
        self.toolbar_manager.show_network_scanner.connect(self.network_scanner.show_scanner)
        self.toolbar_manager.show_powershell_generator.connect(self.powershell_generator.show_generator)

        # Table selection
        self.table_widget.selection_changed.connect(self._on_selection_changed)

        # Table context menu
        self.table_widget.context_menu_start.connect(self._start_tunnel_by_name)
        self.table_widget.context_menu_stop.connect(self._stop_tunnel_by_name)
        self.table_widget.context_menu_browse_files.connect(self._browse_files_by_name)
        self.table_widget.context_menu_browse_remote_files.connect(self._browse_remote_files_by_name)
        self.table_widget.context_menu_launch_rtsp.connect(self.rtsp_handler.launch_rtsp_by_name)
        self.table_widget.context_menu_launch_rdp.connect(self.rdp_handler.launch_rdp_by_name)
        self.table_widget.context_menu_deploy_ssh_key.connect(self._deploy_ssh_key_by_name)

        # File operations
        self.file_ops_manager.log_message.connect(self.log)
        self.file_ops_manager.tunnel_needed.connect(self._start_tunnel_by_name)
        self.file_ops_manager.refresh_table.connect(self._refresh_table)
        self.file_ops_manager.set_managers(self.config_manager, self.active_tunnels)

        # Setup RTSP handler
        self.rtsp_handler.set_managers(self.config_manager, self.active_tunnels, self.log)

        # Setup RDP handler
        self.rdp_handler.set_managers(self.config_manager, self.active_tunnels, self.log)

    def _setup_menu(self):
        """Setup minimal menu."""
        menubar = self.menuBar()

        # File menu
        file_menu = menubar.addMenu("File")
        exit_action = QAction("Exit", self)
        exit_action.triggered.connect(self.close)
        file_menu.addAction(exit_action)

        # Tools menu
        tools_menu = menubar.addMenu("Tools")
        ssh_key_action = QAction("๐Ÿ”‘ Generate SSH Key...", self)
        ssh_key_action.triggered.connect(self._setup_ssh_key)
        tools_menu.addAction(ssh_key_action)

        deploy_key_action = QAction("๐Ÿ“ค Deploy SSH Key...", self)
        deploy_key_action.triggered.connect(self._deploy_ssh_key)
        tools_menu.addAction(deploy_key_action)

        powershell_action = QAction("๐Ÿ“œ PowerShell SSH Setup...", self)
        powershell_action.triggered.connect(self.powershell_generator.show_generator)
        tools_menu.addAction(powershell_action)

        tools_menu.addSeparator()
        network_scanner_action = QAction("๐Ÿ” Network Scanner", self)
        network_scanner_action.triggered.connect(self.network_scanner.show_scanner)
        tools_menu.addAction(network_scanner_action)

        # Help menu
        help_menu = menubar.addMenu("Help")
        about_action = QAction("About", self)
        about_action.triggered.connect(self._show_about)
        help_menu.addAction(about_action)

    def _setup_system_tray(self):
        """Setup system tray."""
        if not QSystemTrayIcon.isSystemTrayAvailable():
            return

        self.tray_icon = QSystemTrayIcon(self)

        # Create icon
        try:
            icon = self.style().standardIcon(self.style().StandardPixmap.SP_ComputerIcon)
            if icon.isNull():
                pixmap = QPixmap(16, 16)
                pixmap.fill(Qt.blue)
                icon = QIcon(pixmap)
        except:
            icon = QIcon()

        self.tray_icon.setIcon(icon)
        self.tray_icon.setToolTip("SSH Tunnel Manager")

        # Tray menu
        tray_menu = QMenu()
        show_action = QAction("Show", self)
        show_action.triggered.connect(self.show)
        tray_menu.addAction(show_action)

        quit_action = QAction("Quit", self)
        quit_action.triggered.connect(self._quit_application)
        tray_menu.addAction(quit_action)

        self.tray_icon.setContextMenu(tray_menu)
        self.tray_icon.show()

    def _load_configurations(self):
        """Load and display configurations."""
        self.config_manager.load_configurations()
        self._refresh_table()

    def _start_monitoring(self):
        """Start tunnel monitoring."""
        self.monitor_thread = TunnelMonitorThread(self.active_tunnels)
        self.monitor_thread.status_update.connect(self._update_tunnel_status)
        self.monitor_thread.connection_lost.connect(self._handle_connection_lost)
        self.monitor_thread.start()

    def _refresh_table(self):
        """Refresh the tunnel table."""
        configs = self.config_manager.get_all_configurations()
        self.table_widget.refresh_table(configs, self.active_tunnels)

    def _on_selection_changed(self, selected: bool, config_name: str):
        """Handle table selection changes."""
        if selected and config_name:
            config = self.config_manager.get_configuration(config_name)
            is_running = config_name in self.active_tunnels and self.active_tunnels[config_name].is_running

            # Determine service capabilities
            web_enabled = (config and config.tunnel_type == 'local' and 
                         config.remote_port in [80, 443, 8080, 8443, 8000, 3000, 5000, 9000])
            rtsp_enabled = (config and config.tunnel_type == 'local' and 
                          config.remote_port in [554, 8554])
            rdp_enabled = (config and config.tunnel_type == 'local' and 
                         config.remote_port == 3389)  # Standard RDP port

            self.toolbar_manager.update_button_states(True, is_running, web_enabled, rtsp_enabled, rdp_enabled)
        else:
            self.toolbar_manager.update_button_states(False)

    def _update_tunnel_status(self, name: str, is_running: bool):
        """Update tunnel status from monitor."""
        if name in self.active_tunnels:
            self.active_tunnels[name].is_running = is_running
        self._refresh_table()

    def _handle_connection_lost(self, name: str):
        """Handle connection lost event (limited to 10 messages)."""
        self.log(f"โš ๏ธ Tunnel {name} disconnected unexpectedly")

    # Action handlers (simplified - delegate to main_window_actions.py)
    def _add_tunnel(self):
        """Add tunnel - delegate to actions."""
        from .main_window_actions import MainWindowActions
        actions = MainWindowActions()
        actions.config_manager = self.config_manager
        actions.log = self.log
        actions.refresh_table = self._refresh_table
        actions.parent_widget = self  # Set the parent widget
        actions.add_tunnel(parent=self)  # Pass self as parent

    def _edit_tunnel(self):
        """Edit tunnel - delegate to actions."""
        from PySide6.QtWidgets import QMessageBox, QDialog

        config_name = self.table_widget.get_selected_config_name()
        if not config_name:
            QMessageBox.information(self, "Information", "Please select a tunnel to edit.")
            return

        config = self.config_manager.get_configuration(config_name)
        if not config:
            QMessageBox.warning(self, "Error", f"Configuration '{config_name}' not found.")
            return

        from ssh_tunnel_manager.gui.dialogs.tunnel_config import TunnelConfigDialog
        dialog = TunnelConfigDialog(config=config, parent=self)
        if dialog.exec() == QDialog.Accepted:
            updated_config = dialog.get_config()

            success, error_msg = self.config_manager.update_configuration(config_name, updated_config)
            if success:
                self._refresh_table()
                self.log(f"Updated tunnel configuration: {updated_config.name}")
            else:
                QMessageBox.warning(self, "Error", error_msg)

    def _delete_tunnel(self):
        """Delete tunnel."""
        from PySide6.QtWidgets import QMessageBox

        config_name = self.table_widget.get_selected_config_name()
        if not config_name:
            QMessageBox.information(self, "Information", "Please select a tunnel to delete.")
            return

        # Ask for confirmation
        reply = QMessageBox.question(
            self, "Confirm Delete",
            f"Are you sure you want to delete tunnel '{config_name}'?",
            QMessageBox.Yes | QMessageBox.No
        )

        if reply == QMessageBox.Yes:
            # Stop tunnel if running
            if config_name in self.active_tunnels:
                self.active_tunnels[config_name].stop()
                del self.active_tunnels[config_name]

            # Remove from configurations
            success = self.config_manager.delete_configuration(config_name)
            if success:
                self._refresh_table()
                self.log(f"Deleted tunnel configuration: {config_name}")
            else:
                QMessageBox.warning(self, "Error", f"Failed to delete configuration '{config_name}'")

    def _start_tunnel(self):
        """Start selected tunnel."""
        config_name = self.table_widget.get_selected_config_name()
        if config_name:
            self._start_tunnel_by_name(config_name)

    def _start_tunnel_by_name(self, config_name: str):
        """Start tunnel by name."""
        config = self.config_manager.get_configuration(config_name)
        if not config:
            return

        self.log(f"๐Ÿš€ Starting tunnel: {config_name}")

        if config_name not in self.active_tunnels:
            self.active_tunnels[config_name] = TunnelProcess(config, None)

        tunnel = self.active_tunnels[config_name]

        # Set to starting status and refresh table to show yellow indicator
        tunnel.status = tunnel.STATUS_STARTING
        self._refresh_table()

        if tunnel.start():
            self.log(f"โœ… Tunnel started: {config_name}")
            self._refresh_table()
        else:
            self.log(f"โŒ Failed to start tunnel: {config_name}")
            self._refresh_table()

    def _stop_tunnel(self):
        """Stop selected tunnel."""
        config_name = self.table_widget.get_selected_config_name()
        if not config_name or config_name not in self.active_tunnels:
            return

        self.log(f"๐Ÿ›‘ Stopping tunnel: {config_name}")
        tunnel = self.active_tunnels[config_name]
        tunnel.stop()
        self._refresh_table()

    def _test_tunnel(self):
        """Test tunnel connection."""
        from PySide6.QtWidgets import QMessageBox
        from ..utils.connection_tester import ConnectionTester

        config_name = self.table_widget.get_selected_config_name()
        if not config_name:
            QMessageBox.information(self, "Information", "Please select a tunnel to test.")
            return

        config = self.config_manager.get_configuration(config_name)
        if not config:
            QMessageBox.warning(self, "Error", f"Configuration '{config_name}' not found.")
            return

        # Check if tunnel is running
        is_running = config_name in self.active_tunnels and self.active_tunnels[config_name].is_running
        if not is_running:
            QMessageBox.warning(self, "Test Error", f"Tunnel '{config_name}' is not running. Please start the tunnel first.")
            return

        self.log(f"๐Ÿงช Testing tunnel: {config_name}")

        try:
            if config.tunnel_type == 'local':
                # Test local port connectivity
                if ConnectionTester.test_local_port(config.local_port):
                    self.log(f"โœ… Local port {config.local_port} is accessible")

                    # Test tunnel connection
                    success, message = ConnectionTester.test_tunnel_connection(config)
                    if success:
                        self.log(f"โœ… {message}")
                        QMessageBox.information(self, "Test Result", 
                                              f"โœ… Tunnel '{config_name}' is working correctly!\n\n{message}")
                    else:
                        self.log(f"โš ๏ธ {message}")
                        QMessageBox.warning(self, "Test Result",
                                          f"โš ๏ธ Tunnel '{config_name}' has issues:\n\n{message}")
                else:
                    self.log(f"โŒ Local port {config.local_port} is not accessible")
                    QMessageBox.critical(self, "Test Result",
                                       f"โŒ Tunnel '{config_name}' test failed!\n\n"
                                       f"Local port {config.local_port} is not accessible")

            elif config.tunnel_type == 'dynamic':
                # Test SOCKS proxy
                if ConnectionTester.test_local_port(config.local_port):
                    self.log(f"โœ… SOCKS proxy on port {config.local_port} is accessible")
                    QMessageBox.information(self, "Test Result",
                                          f"โœ… SOCKS proxy '{config_name}' is working!\n\n"
                                          f"Proxy available on localhost:{config.local_port}")
                else:
                    self.log(f"โŒ SOCKS proxy port {config.local_port} is not accessible")
                    QMessageBox.critical(self, "Test Result",
                                       f"โŒ SOCKS proxy '{config_name}' test failed!\n\n"
                                       f"Port {config.local_port} is not accessible")
            else:
                # Remote tunnel
                self.log(f"โ„น๏ธ Remote tunnel testing limited - check remote server")
                QMessageBox.information(self, "Test Result",
                                     f"โ„น๏ธ Remote tunnel '{config_name}' status:\n\n"
                                     f"The tunnel appears to be running, but remote tunnels "
                                     f"can only be tested from the remote server side.")
        except Exception as e:
            self.log(f"โŒ Error testing tunnel: {str(e)}")
            QMessageBox.critical(self, "Test Error", f"Test failed: {str(e)}")

    def _browse_files(self):
        """Browse files via SFTP."""
        config_name = self.table_widget.get_selected_config_name()
        if not config_name:
            return

        config = self.config_manager.get_configuration(config_name)
        if config:
            self.file_ops_manager.browse_files(config_name, config)

    def _browse_remote_files(self):
        """Browse files on the remote host (destination of the tunnel) via SFTP."""
        config_name = self.table_widget.get_selected_config_name()
        if not config_name:
            from PySide6.QtWidgets import QMessageBox
            QMessageBox.information(self, "Information", "Please select a tunnel to browse remote files.")
            return

        config = self.config_manager.get_configuration(config_name)
        if config:
            self.file_ops_manager.browse_remote_files(config_name, config)

    def _open_web_browser(self):
        """Open web browser for tunnel."""
        config_name = self.table_widget.get_selected_config_name()
        config = self.config_manager.get_configuration(config_name)
        if config and config.tunnel_type == 'local':
            url = f"http://localhost:{config.local_port}"
            webbrowser.open(url)
            self.log(f"๐ŸŒ Opened web browser: {url}")

    def _setup_ssh_key(self):
        """Setup SSH key."""
        # Get currently selected tunnel config if any
        config_name = self.table_widget.get_selected_config_name()
        tunnel_config = None
        if config_name:
            tunnel_config = self.config_manager.get_configuration(config_name)
            if tunnel_config:
                tunnel_config = {
                    'name': config_name,
                    'host': tunnel_config.ssh_host,
                    'port': tunnel_config.ssh_port,
                    'username': tunnel_config.ssh_user
                }

        self.ssh_key_manager.show_key_generator(tunnel_config)

    def _deploy_ssh_key(self):
        """Deploy SSH key to server."""
        # Get currently selected tunnel config if any
        config_name = self.table_widget.get_selected_config_name()
        tunnel_config = None
        if config_name:
            tunnel_config = self.config_manager.get_configuration(config_name)
            if tunnel_config:
                tunnel_config = {
                    'name': config_name,
                    'host': tunnel_config.ssh_host,
                    'port': tunnel_config.ssh_port,
                    'username': tunnel_config.ssh_user
                }

        self.ssh_key_deployment.deploy_key_for_tunnel(tunnel_config)

    def _deploy_ssh_key_by_name(self, config_name: str):
        """Deploy SSH key for a specific tunnel by name."""
        tunnel_config = self.config_manager.get_configuration(config_name)
        if tunnel_config:
            tunnel_config_dict = {
                'name': config_name,
                'host': tunnel_config.ssh_host,
                'port': tunnel_config.ssh_port,
                'username': tunnel_config.ssh_user
            }
            self.ssh_key_deployment.deploy_key_for_tunnel(tunnel_config_dict)

    def _show_about(self):
        """Show about dialog."""
        QMessageBox.about(self, "About", f"{APP_NAME}\nProfessional SSH Tunnel Manager")

    def log(self, message: str):
        """Log a message."""
        self.log_widget.log(message)

    def closeEvent(self, event):
        """Handle close event."""
        if QSystemTrayIcon.isSystemTrayAvailable() and hasattr(self, 'tray_icon'):
            self.hide()
            event.ignore()
        else:
            self._quit_application()

    def _quit_application(self):
        """Quit application."""
        # Stop all tunnels
        for tunnel in self.active_tunnels.values():
            tunnel.stop()

        # Stop monitoring
        if hasattr(self, 'monitor_thread'):
            self.monitor_thread.stop()
            self.monitor_thread.wait()

        QApplication.quit()

    def _stop_tunnel_by_name(self, config_name: str):
        """Stop tunnel by name."""
        if config_name in self.active_tunnels:
            self.log(f"๐Ÿ›‘ Stopping tunnel: {config_name}")
            tunnel = self.active_tunnels[config_name]
            tunnel.stop()
            self._refresh_table()

    def _browse_files_by_name(self, config_name: str):
        """Browse files for a specific tunnel by name."""
        config = self.config_manager.get_configuration(config_name)
        if config:
            self.file_ops_manager.browse_files(config_name, config)

    def _browse_remote_files_by_name(self, config_name: str):
        """Browse remote files for a specific tunnel by name."""
        config = self.config_manager.get_configuration(config_name)
        if config:
            self.file_ops_manager.browse_remote_files(config_name, config)
Functions
closeEvent(event)

Handle close event.

Source code in src/ssh_tunnel_manager/gui/main_window.py
def closeEvent(self, event):
    """Handle close event."""
    if QSystemTrayIcon.isSystemTrayAvailable() and hasattr(self, 'tray_icon'):
        self.hide()
        event.ignore()
    else:
        self._quit_application()
log(message)

Log a message.

Source code in src/ssh_tunnel_manager/gui/main_window.py
def log(self, message: str):
    """Log a message."""
    self.log_widget.log(message)