Fix module import path and installation instructions
This commit is contained in:
parent
ee9c0edf8a
commit
e136c30266
@ -10,7 +10,7 @@ A Go-based command-line tool to manage SSH tunnels. This tool allows you to:
|
|||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```
|
```
|
||||||
go install git.mvl.sh/vleeuwenmenno/sshtunnel/cmd@latest
|
go install git.mvl.sh/vleeuwenmenno/sshtunnel@latest
|
||||||
```
|
```
|
||||||
|
|
||||||
Or clone this repository and build it yourself:
|
Or clone this repository and build it yourself:
|
||||||
|
184
cmd/start.go
184
cmd/start.go
@ -8,8 +8,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"git.mvl.sh/vleeuwenmenno/sshtunnel/pkg/stats"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"sshtunnel/pkg/stats"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -28,106 +28,106 @@ var startCmd = &cobra.Command{
|
|||||||
The tunnel will run in the background and can be managed using the list and stop commands.`,
|
The tunnel will run in the background and can be managed using the list and stop commands.`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
// Check required flags
|
// Check required flags
|
||||||
// Generate the SSH command with appropriate flags for reliable background operation
|
// Generate the SSH command with appropriate flags for reliable background operation
|
||||||
sshArgs := []string{
|
sshArgs := []string{
|
||||||
"-N", // Don't execute remote command
|
"-N", // Don't execute remote command
|
||||||
"-f", // Run in background
|
"-f", // Run in background
|
||||||
"-L", fmt.Sprintf("%d:%s:%d", localPort, remoteHost, remotePort),
|
"-L", fmt.Sprintf("%d:%s:%d", localPort, remoteHost, remotePort),
|
||||||
|
}
|
||||||
|
|
||||||
|
if identity != "" {
|
||||||
|
sshArgs = append(sshArgs, "-i", identity)
|
||||||
|
}
|
||||||
|
|
||||||
|
sshArgs = append(sshArgs, sshServer)
|
||||||
|
sshCmd := exec.Command("ssh", sshArgs...)
|
||||||
|
|
||||||
|
// Capture output for debugging
|
||||||
|
var outputBuffer strings.Builder
|
||||||
|
sshCmd.Stdout = &outputBuffer
|
||||||
|
sshCmd.Stderr = &outputBuffer
|
||||||
|
|
||||||
|
// Run the command (not just Start) - the -f flag means it will return immediately
|
||||||
|
// after going to the background
|
||||||
|
if err := sshCmd.Run(); err != nil {
|
||||||
|
fmt.Printf("Error starting SSH tunnel: %v\n", err)
|
||||||
|
fmt.Printf("SSH output: %s\n", outputBuffer.String())
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The PID from cmd.Process is no longer valid since ssh -f forks
|
||||||
|
// We need to find the actual SSH process PID
|
||||||
|
actualPID, err := findSSHTunnelPID(localPort)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Warning: Could not determine tunnel PID: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store tunnel information
|
||||||
|
id := generateTunnelID()
|
||||||
|
pid := 0
|
||||||
|
|
||||||
|
if actualPID > 0 {
|
||||||
|
pid = actualPID
|
||||||
|
}
|
||||||
|
|
||||||
|
tunnel := Tunnel{
|
||||||
|
ID: id,
|
||||||
|
LocalPort: localPort,
|
||||||
|
RemotePort: remotePort,
|
||||||
|
RemoteHost: remoteHost,
|
||||||
|
SSHServer: sshServer,
|
||||||
|
PID: pid,
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := saveTunnel(tunnel); err != nil {
|
||||||
|
fmt.Printf("Error saving tunnel information: %v\n", err)
|
||||||
|
if pid > 0 {
|
||||||
|
// Try to kill the process
|
||||||
|
process, _ := os.FindProcess(pid)
|
||||||
|
if process != nil {
|
||||||
|
process.Kill()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
if identity != "" {
|
// Initialize statistics for this tunnel
|
||||||
sshArgs = append(sshArgs, "-i", identity)
|
statsManager, err := stats.NewStatsManager()
|
||||||
}
|
if err == nil {
|
||||||
|
err = statsManager.InitStats(id, localPort)
|
||||||
sshArgs = append(sshArgs, sshServer)
|
|
||||||
sshCmd := exec.Command("ssh", sshArgs...)
|
|
||||||
|
|
||||||
// Capture output for debugging
|
|
||||||
var outputBuffer strings.Builder
|
|
||||||
sshCmd.Stdout = &outputBuffer
|
|
||||||
sshCmd.Stderr = &outputBuffer
|
|
||||||
|
|
||||||
// Run the command (not just Start) - the -f flag means it will return immediately
|
|
||||||
// after going to the background
|
|
||||||
if err := sshCmd.Run(); err != nil {
|
|
||||||
fmt.Printf("Error starting SSH tunnel: %v\n", err)
|
|
||||||
fmt.Printf("SSH output: %s\n", outputBuffer.String())
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// The PID from cmd.Process is no longer valid since ssh -f forks
|
|
||||||
// We need to find the actual SSH process PID
|
|
||||||
actualPID, err := findSSHTunnelPID(localPort)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Warning: Could not determine tunnel PID: %v\n", err)
|
fmt.Printf("Warning: Failed to initialize statistics: %v\n", err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Store tunnel information
|
// Verify tunnel is actually working
|
||||||
id := generateTunnelID()
|
time.Sleep(500 * time.Millisecond)
|
||||||
pid := 0
|
active := verifyTunnelActive(localPort)
|
||||||
|
status := "ACTIVE"
|
||||||
|
if !active {
|
||||||
|
status = "UNKNOWN"
|
||||||
|
}
|
||||||
|
|
||||||
if actualPID > 0 {
|
fmt.Printf("Started SSH tunnel (ID: %d): localhost:%d -> %s:%d (%s) [PID: %d] [Status: %s]\n",
|
||||||
pid = actualPID
|
id, localPort, remoteHost, remotePort, sshServer, pid, status)
|
||||||
}
|
},
|
||||||
|
}
|
||||||
|
|
||||||
tunnel := Tunnel{
|
func init() {
|
||||||
ID: id,
|
rootCmd.AddCommand(startCmd)
|
||||||
LocalPort: localPort,
|
|
||||||
RemotePort: remotePort,
|
|
||||||
RemoteHost: remoteHost,
|
|
||||||
SSHServer: sshServer,
|
|
||||||
PID: pid,
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := saveTunnel(tunnel); err != nil {
|
// Add flags for the start command
|
||||||
fmt.Printf("Error saving tunnel information: %v\n", err)
|
startCmd.Flags().IntVarP(&localPort, "local", "l", 0, "Local port to forward")
|
||||||
if pid > 0 {
|
startCmd.Flags().IntVarP(&remotePort, "remote", "r", 0, "Remote port to forward to")
|
||||||
// Try to kill the process
|
startCmd.Flags().StringVarP(&remoteHost, "host", "H", "localhost", "Remote host to forward to")
|
||||||
process, _ := os.FindProcess(pid)
|
startCmd.Flags().StringVarP(&sshServer, "server", "s", "", "SSH server address (user@host)")
|
||||||
if process != nil {
|
startCmd.Flags().StringVarP(&identity, "identity", "i", "", "Path to SSH identity file")
|
||||||
process.Kill()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize statistics for this tunnel
|
// Mark required flags
|
||||||
statsManager, err := stats.NewStatsManager()
|
startCmd.MarkFlagRequired("local")
|
||||||
if err == nil {
|
startCmd.MarkFlagRequired("remote")
|
||||||
err = statsManager.InitStats(id, localPort)
|
startCmd.MarkFlagRequired("server")
|
||||||
if err != nil {
|
}
|
||||||
fmt.Printf("Warning: Failed to initialize statistics: %v\n", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Verify tunnel is actually working
|
|
||||||
time.Sleep(500 * time.Millisecond)
|
|
||||||
active := verifyTunnelActive(localPort)
|
|
||||||
status := "ACTIVE"
|
|
||||||
if !active {
|
|
||||||
status = "UNKNOWN"
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("Started SSH tunnel (ID: %d): localhost:%d -> %s:%d (%s) [PID: %d] [Status: %s]\n",
|
|
||||||
id, localPort, remoteHost, remotePort, sshServer, pid, status)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
rootCmd.AddCommand(startCmd)
|
|
||||||
|
|
||||||
// Add flags for the start command
|
|
||||||
startCmd.Flags().IntVarP(&localPort, "local", "l", 0, "Local port to forward")
|
|
||||||
startCmd.Flags().IntVarP(&remotePort, "remote", "r", 0, "Remote port to forward to")
|
|
||||||
startCmd.Flags().StringVarP(&remoteHost, "host", "H", "localhost", "Remote host to forward to")
|
|
||||||
startCmd.Flags().StringVarP(&sshServer, "server", "s", "", "SSH server address (user@host)")
|
|
||||||
startCmd.Flags().StringVarP(&identity, "identity", "i", "", "Path to SSH identity file")
|
|
||||||
|
|
||||||
// Mark required flags
|
|
||||||
startCmd.MarkFlagRequired("local")
|
|
||||||
startCmd.MarkFlagRequired("remote")
|
|
||||||
startCmd.MarkFlagRequired("server")
|
|
||||||
}
|
|
||||||
|
|
||||||
// verifyTunnelActive checks if the tunnel is actually working
|
// verifyTunnelActive checks if the tunnel is actually working
|
||||||
func verifyTunnelActive(port int) bool {
|
func verifyTunnelActive(port int) bool {
|
||||||
|
@ -5,8 +5,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"sshtunnel/pkg/monitor"
|
"git.mvl.sh/vleeuwenmenno/sshtunnel/pkg/monitor"
|
||||||
"sshtunnel/pkg/stats"
|
"git.mvl.sh/vleeuwenmenno/sshtunnel/pkg/stats"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
@ -5,8 +5,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
|
"git.mvl.sh/vleeuwenmenno/sshtunnel/pkg/stats"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"sshtunnel/pkg/stats"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
2
go.mod
2
go.mod
@ -1,4 +1,4 @@
|
|||||||
module sshtunnel
|
module git.mvl.sh/vleeuwenmenno/sshtunnel
|
||||||
|
|
||||||
go 1.22.2
|
go 1.22.2
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user