Skip to content

Commit 5c430f7

Browse files
committed
Enhance documentation across the codebase
1 parent eddbb3c commit 5c430f7

12 files changed

Lines changed: 908 additions & 83 deletions

File tree

cmd/config.go

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,74 @@ import (
1010
"github.com/spf13/cobra"
1111
)
1212

13-
// configCmd represents the config command
13+
// configCmd represents the config command and its subcommands.
14+
// It provides functionality for configuration file management including
15+
// generation and validation operations.
1416
var configCmd = &cobra.Command{
1517
Use: "config",
1618
Short: "Configuration management commands",
19+
Long: `Configuration management commands for Tunn tunneling tool.
20+
21+
This command provides subcommands for:
22+
• Generating sample configuration files
23+
• Validating existing configuration files
24+
25+
Use the subcommands to manage your Tunn configuration files effectively.`,
1726
}
1827

28+
// generateCmd represents the config generate command.
29+
// It creates sample configuration files for different tunnel modes.
1930
var generateCmd = &cobra.Command{
2031
Use: "generate",
2132
Short: "Generate a sample configuration file",
22-
Long: `Generate a sample configuration file with examples for all supported modes.`,
23-
Run: generateConfig,
33+
Long: `Generate a sample configuration file with examples for all supported modes.
34+
35+
The generated configuration file includes all available options with example values,
36+
making it easy to customize for your specific tunneling requirements.
37+
38+
Supported modes:
39+
• direct: Direct connection with optional WebSocket upgrade
40+
• proxy: Connection through HTTP proxy with WebSocket upgrade
41+
42+
Examples:
43+
tunn config generate --mode direct --output config.json
44+
tunn config generate --mode proxy --output proxy-config.json`,
45+
Run: generateConfig,
2446
}
2547

48+
// validateCmd represents the config validate command.
49+
// It validates the syntax and content of configuration files.
2650
var validateCmd = &cobra.Command{
2751
Use: "validate",
2852
Short: "Validate configuration file",
29-
Long: `Validate the syntax and content of a configuration file.`,
30-
Run: validateConfig,
53+
Long: `Validate the syntax and content of a configuration file.
54+
55+
This command checks:
56+
• JSON syntax correctness
57+
• Required fields presence
58+
• Field value validity
59+
• Mode-specific configuration requirements
60+
61+
The validation ensures your configuration file is ready for use with Tunn.
62+
63+
Examples:
64+
tunn config validate --config config.json
65+
tunn config validate -c /path/to/config.json`,
66+
Run: validateConfig,
3167
}
3268

69+
// validateFlags holds the command-line flags for the validate subcommand.
3370
var validateFlags struct {
3471
configPath string
3572
}
3673

74+
// generateFlags holds the command-line flags for the generate subcommand.
3775
var generateFlags struct {
3876
output string
3977
mode string
4078
}
4179

80+
// init initializes the config command and its subcommands with their respective flags.
4281
func init() {
4382
rootCmd.AddCommand(configCmd)
4483
configCmd.AddCommand(generateCmd)
@@ -51,6 +90,9 @@ func init() {
5190
validateCmd.MarkFlagRequired("config")
5291
}
5392

93+
// generateConfig generates a sample configuration file based on the specified mode.
94+
// It creates a configuration template with example values that users can customize
95+
// for their specific tunneling needs.
5496
func generateConfig(cmd *cobra.Command, args []string) {
5597
var sampleConfig *config.Config
5698

@@ -108,6 +150,9 @@ func generateConfig(cmd *cobra.Command, args []string) {
108150
fmt.Printf("Success: Sample %s mode configuration generated: %s\n", generateFlags.mode, generateFlags.output)
109151
}
110152

153+
// validateConfig validates an existing configuration file for syntax and content correctness.
154+
// It loads the configuration file and performs comprehensive validation checks to ensure
155+
// all required fields are present and valid for the specified tunnel mode.
111156
func validateConfig(cmd *cobra.Command, args []string) {
112157
configPath := validateFlags.configPath
113158
if configPath == "" {

cmd/root.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// Package cmd provides the command-line interface for the Tunn SSH tunneling tool.
2+
//
3+
// This package implements the CLI commands using the Cobra library, handling
4+
// configuration loading, command parsing, and tunnel management initialization.
5+
// It supports multiple subcommands for configuration management and tunnel operations.
16
package cmd
27

38
import (
@@ -12,14 +17,32 @@ import (
1217
)
1318

1419
var (
15-
configFile string
20+
// configFile holds the path to the configuration file specified by the user.
21+
configFile string
22+
23+
// tunnelConfig contains the loaded and validated tunnel configuration.
1624
tunnelConfig *config.Config
1725
)
1826

19-
// rootCmd represents the base command when called without any subcommands
27+
// rootCmd represents the base command when called without any subcommands.
28+
// It loads the configuration file and starts the tunnel manager.
2029
var rootCmd = &cobra.Command{
21-
Use: "tunn",
22-
Short: "A powerful tunnel tool for secure connections",
30+
Use: "tunn",
31+
Short: "A powerful tunnel tool for secure connections",
32+
Long: `Tunn is a cross-platform SSH tunneling tool that creates secure connections
33+
through direct connections over WebSocket and HTTP proxies.
34+
35+
Features:
36+
• Multiple tunnel modes: Direct connection and Proxy
37+
• WebSocket-based SSH tunnels for better bypass capabilities
38+
• SOCKS5 and HTTP proxy support
39+
• Domain spoofing capabilities
40+
• Cross-platform support (Windows, Linux, macOS)
41+
42+
Examples:
43+
tunn --config config.json
44+
tunn config generate --mode direct --output myconfig.json
45+
tunn config validate --config myconfig.json`,
2346
Version: "v0.1.2",
2447
PreRun: func(cmd *cobra.Command, args []string) {
2548
var err error
@@ -32,19 +55,23 @@ var rootCmd = &cobra.Command{
3255
}
3356

3457
// Execute adds all child commands to the root command and sets flags appropriately.
58+
// This is called by main.main(). It only needs to happen once to the rootCmd.
3559
func Execute() {
3660
if err := rootCmd.Execute(); err != nil {
3761
fmt.Println(err)
3862
os.Exit(1)
3963
}
4064
}
4165

66+
// init initializes the root command with persistent flags and configuration.
4267
func init() {
4368
rootCmd.PersistentFlags().StringVarP(&configFile, "config", "c", "config.json", "config file path")
4469
rootCmd.CompletionOptions.DisableDefaultCmd = true
4570
rootCmd.SetHelpCommand(&cobra.Command{Use: "no-help", Hidden: true})
4671
}
4772

73+
// runTunnel is the main execution function that starts the tunnel manager
74+
// with the loaded configuration.
4875
func runTunnel(cmd *cobra.Command, args []string) {
4976
fmt.Printf("Mode: %s\n", tunnelConfig.Mode)
5077
fmt.Printf("\n")

internal/tunnel/manager.go

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
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.
110
package tunnel
211

312
import (
@@ -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.
1629
type 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
2345
func 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
3065
func (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
67113
func (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.
83136
func (m *Manager) waitForShutdown() {
84137
sigChan := make(chan os.Signal, 1)
85138
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)

main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,23 @@
1+
// Package main provides the entry point for the Tunn SSH tunneling tool.
2+
//
3+
// Tunn is a cross-platform SSH tunneling application that creates secure
4+
// connections through direct connections over WebSocket and HTTP proxies.
5+
// It supports both SOCKS5 and HTTP proxy modes for flexible tunneling solutions.
6+
//
7+
// Usage:
8+
//
9+
// tunn --config config.json
10+
// tunn config generate --mode direct
11+
// tunn config validate --config myconfig.json
12+
//
13+
// For more information, see the README.md file or run:
14+
//
15+
// tunn --help
116
package main
217

318
import "tunn/cmd"
419

20+
// main is the application entry point that delegates execution to the CLI command handler.
521
func main() {
622
cmd.Execute()
723
}

0 commit comments

Comments
 (0)