SSH tunnel cleanup and parse fixup (#731)

This commit is contained in:
Dan Sosedoff 2024-05-22 20:09:29 -07:00 committed by GitHub
parent 40f582d1ea
commit 63f1150056
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8,6 +8,7 @@ import (
"net" "net"
"net/url" "net/url"
"os" "os"
"path/filepath"
"strings" "strings"
"sync" "sync"
"time" "time"
@ -35,8 +36,8 @@ type Tunnel struct {
Listener *net.TCPListener Listener *net.TCPListener
} }
func privateKeyPath() string { func defaultKeyPath() string {
return os.Getenv("HOME") + "/.ssh/id_rsa" return filepath.Join(os.Getenv("HOME"), ".ssh/id_rsa")
} }
func expandKeyPath(path string) string { func expandKeyPath(path string) string {
@ -61,7 +62,7 @@ func parsePrivateKey(keyPath string, keyPass string) (ssh.Signer, error) {
signer, err := ssh.ParsePrivateKey(buff) signer, err := ssh.ParsePrivateKey(buff)
if _, ok := err.(*ssh.PassphraseMissingError); ok { if _, ok := err.(*ssh.PassphraseMissingError); ok {
if keyPass == "" { if keyPass == "" {
return nil, errors.New("SSH key password is not provided") return nil, errors.New("ssh key password is not provided")
} }
return sshkeys.ParseEncryptedPrivateKey(buff, []byte(keyPass)) return sshkeys.ParseEncryptedPrivateKey(buff, []byte(keyPass))
} }
@ -75,13 +76,13 @@ func makeConfig(info *shared.SSHInfo) (*ssh.ClientConfig, error) {
// Try to use user-provided key, fallback to system default key // Try to use user-provided key, fallback to system default key
keyPath := info.Key keyPath := info.Key
if keyPath == "" { if keyPath == "" {
keyPath = privateKeyPath() keyPath = defaultKeyPath()
} else { } else {
keyPath = expandKeyPath(keyPath) keyPath = expandKeyPath(keyPath)
} }
if !fileExists(keyPath) { if !fileExists(keyPath) {
return nil, errors.New("ssh public key not found at " + keyPath) return nil, fmt.Errorf("ssh public key not found at path %q", keyPath)
} }
// Append public key authentication method // Append public key authentication method
@ -129,11 +130,11 @@ func (tunnel *Tunnel) handleConnection(local net.Conn) {
return return
} }
wg := sync.WaitGroup{} wg := &sync.WaitGroup{}
wg.Add(2) wg.Add(2)
go tunnel.copy(&wg, local, remote) go tunnel.copy(wg, local, remote)
go tunnel.copy(&wg, remote, local) go tunnel.copy(wg, remote, local)
wg.Wait() wg.Wait()
local.Close() local.Close()