1+ // Package tunnel provides the core tunnel management functionality for Tunn.
2+ //
3+ // This package implements the tunnel lifecycle management, including connection
4+ // establishment, SSH client setup, and proxy server initialization. It coordinates
5+ // between the configuration, connection, SSH, and proxy packages to provide
6+ // a complete tunneling solution.
7+ //
8+ // The Manager type handles the entire tunnel lifecycle from initialization
9+ // through graceful shutdown, managing both SSH connections and local proxy servers.
110package tunnel
211
312import (
@@ -12,21 +21,47 @@ import (
1221 "tunn/pkg/ssh"
1322)
1423
15- // Manager manages the tunnel lifecycle
24+ // Manager manages the complete tunnel lifecycle including connection establishment,
25+ // SSH client setup, proxy server initialization, and graceful shutdown.
26+ //
27+ // The Manager coordinates between different components to provide a seamless
28+ // tunneling experience, handling both direct and proxy-based connection modes.
1629type Manager struct {
17- config * config.Config
18- sshClient ssh.Client
19- proxyServer interface {} // Can be either SOCKS5 or HTTP proxy
30+ config * config.Config // The tunnel configuration
31+ sshClient ssh.Client // SSH client for tunneling
32+ proxyServer interface {} // Local proxy server ( SOCKS5 or HTTP)
2033}
2134
22- // NewManager creates a new tunnel manager
35+ // NewManager creates a new tunnel manager with the provided configuration.
36+ //
37+ // The manager will use the configuration to determine the appropriate connection
38+ // method, SSH settings, and proxy type to establish.
39+ //
40+ // Parameters:
41+ // - cfg: The tunnel configuration containing all necessary settings
42+ //
43+ // Returns:
44+ // - *Manager: A new tunnel manager instance ready for startup
2345func NewManager (cfg * config.Config ) * Manager {
2446 return & Manager {
2547 config : cfg ,
2648 }
2749}
2850
29- // Start starts the tunnel and proxy server
51+ // Start establishes the complete tunnel setup and starts all necessary services.
52+ //
53+ // This method performs the following operations in sequence:
54+ // 1. Establishes the base connection (direct or through proxy)
55+ // 2. Creates and initializes the SSH client over the connection
56+ // 3. Starts the SSH transport layer
57+ // 4. Launches the appropriate local proxy server (SOCKS5 or HTTP)
58+ // 5. Waits for shutdown signals to gracefully terminate
59+ //
60+ // The method blocks until a shutdown signal is received, making it suitable
61+ // for use in the main application loop.
62+ //
63+ // Returns:
64+ // - error: An error if any step of the setup process fails
3065func (m * Manager ) Start () error {
3166 // Establish connection
3267 establisher , err := connection .GetEstablisher (m .config .Mode )
@@ -63,7 +98,18 @@ func (m *Manager) Start() error {
6398 return nil
6499}
65100
66- // startProxy starts the appropriate proxy server
101+ // startProxy initializes and starts the appropriate local proxy server based on configuration.
102+ //
103+ // This method creates either a SOCKS5 or HTTP proxy server according to the ProxyType
104+ // setting in the configuration. The proxy server will listen on the configured port
105+ // and forward connections through the established SSH tunnel.
106+ //
107+ // Supported proxy types:
108+ // - "socks5" or "socks": Creates a SOCKS5 proxy server
109+ // - "http": Creates an HTTP proxy server
110+ //
111+ // Returns:
112+ // - error: An error if the proxy type is unsupported or proxy startup fails
67113func (m * Manager ) startProxy () error {
68114 switch m .config .ProxyType {
69115 case "socks5" , "socks" :
@@ -79,7 +125,14 @@ func (m *Manager) startProxy() error {
79125 }
80126}
81127
82- // waitForShutdown waits for shutdown signals
128+ // waitForShutdown blocks and waits for system shutdown signals to gracefully terminate the tunnel.
129+ //
130+ // This method listens for SIGINT (Ctrl+C) and SIGTERM signals, providing a clean
131+ // shutdown mechanism. When a signal is received, it closes the SSH client connection
132+ // and performs cleanup operations.
133+ //
134+ // The method blocks the calling goroutine until a shutdown signal is received,
135+ // making it suitable for use in the main application flow.
83136func (m * Manager ) waitForShutdown () {
84137 sigChan := make (chan os.Signal , 1 )
85138 signal .Notify (sigChan , syscall .SIGINT , syscall .SIGTERM )
0 commit comments