refactor: move V1 code into v1/ subdirectory
- git mv cmd/ internal/ pkg/ test/ go.mod go.sum Makefile build.sh docs/ v1/ - Create v1/README.md with V1 documentation - Update root README for V1 + V2 structure - V1 still builds (cd v1 && go build ./cmd/hostkeeper) and 105 tests pass - Root is now clean for V2 development
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
package ssh
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
cryptossh "golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
// getAuthMethods returns SSH authentication methods based on host config
|
||||
func (c *Client) getAuthMethods() ([]cryptossh.AuthMethod, error) {
|
||||
var authMethods []cryptossh.AuthMethod
|
||||
|
||||
switch c.host.Auth.Type {
|
||||
case "password":
|
||||
if c.host.Auth.Password == "" {
|
||||
return nil, fmt.Errorf("password auth requires password")
|
||||
}
|
||||
authMethods = append(authMethods, cryptossh.Password(c.host.Auth.Password))
|
||||
|
||||
case "key":
|
||||
signer, err := c.getKeySigner()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to setup key authentication: %w", err)
|
||||
}
|
||||
authMethods = append(authMethods, cryptossh.PublicKeys(signer))
|
||||
|
||||
case "both":
|
||||
// Try password first
|
||||
if c.host.Auth.Password != "" {
|
||||
authMethods = append(authMethods, cryptossh.Password(c.host.Auth.Password))
|
||||
}
|
||||
// Then try key
|
||||
signer, err := c.getKeySigner()
|
||||
if err == nil {
|
||||
authMethods = append(authMethods, cryptossh.PublicKeys(signer))
|
||||
}
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported authentication type: %s", c.host.Auth.Type)
|
||||
}
|
||||
|
||||
if len(authMethods) == 0 {
|
||||
return nil, fmt.Errorf("no authentication methods configured")
|
||||
}
|
||||
|
||||
return authMethods, nil
|
||||
}
|
||||
|
||||
// getKeySigner returns an SSH signer for key-based authentication
|
||||
//
|
||||
// Phase 1: Loads key from KeyID as a file path, or from common SSH key locations
|
||||
// Phase 2: Will integrate with the storage layer for encrypted key storage
|
||||
func (c *Client) getKeySigner() (cryptossh.Signer, error) {
|
||||
var keyPath string
|
||||
|
||||
if c.host.Auth.KeyID != "" {
|
||||
// If KeyID looks like a path, use it directly
|
||||
if strings.HasPrefix(c.host.Auth.KeyID, "/") || strings.HasPrefix(c.host.Auth.KeyID, "~") {
|
||||
keyPath = expandPath(c.host.Auth.KeyID)
|
||||
} else {
|
||||
// Try ~/.ssh/<keyID>
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get home directory: %w", err)
|
||||
}
|
||||
keyPath = filepath.Join(home, ".ssh", c.host.Auth.KeyID)
|
||||
}
|
||||
} else {
|
||||
// Fall back to default key locations
|
||||
keyPath = getDefaultKeyPath()
|
||||
}
|
||||
|
||||
// Read the key file
|
||||
keyData, err := os.ReadFile(keyPath) //nolint:gosec
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read key file %s: %w", keyPath, err)
|
||||
}
|
||||
|
||||
// Parse the key (support passphrase-protected keys)
|
||||
var signer cryptossh.Signer
|
||||
if c.host.Auth.Password != "" {
|
||||
signer, err = cryptossh.ParsePrivateKeyWithPassphrase(keyData, []byte(c.host.Auth.Password))
|
||||
} else if c.passphraseCallback != nil {
|
||||
// Try without passphrase first
|
||||
signer, err = cryptossh.ParsePrivateKey(keyData)
|
||||
if err != nil && strings.Contains(err.Error(), "encrypted") {
|
||||
// Key is encrypted, prompt for passphrase
|
||||
passphrase := c.passphraseCallback()
|
||||
if passphrase != "" {
|
||||
signer, err = cryptossh.ParsePrivateKeyWithPassphrase(keyData, []byte(passphrase))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
signer, err = cryptossh.ParsePrivateKey(keyData)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse private key: %w", err)
|
||||
}
|
||||
|
||||
return signer, nil
|
||||
}
|
||||
|
||||
// getDefaultKeyPath returns the first existing default SSH key path
|
||||
func getDefaultKeyPath() string {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
candidates := []string{
|
||||
"id_ed25519",
|
||||
"id_rsa",
|
||||
"id_ecdsa",
|
||||
"id_dsa",
|
||||
}
|
||||
|
||||
for _, name := range candidates {
|
||||
path := filepath.Join(home, ".ssh", name)
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return path
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
// expandPath expands ~ to the home directory
|
||||
func expandPath(path string) string {
|
||||
if strings.HasPrefix(path, "~/") {
|
||||
home, err := os.UserHomeDir()
|
||||
if err == nil {
|
||||
return filepath.Join(home, path[2:])
|
||||
}
|
||||
}
|
||||
return path
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
package ssh
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"golang.org/x/term"
|
||||
|
||||
"git.tukangketik.id/swanadiva/hostkeeper/internal/errors"
|
||||
"git.tukangketik.id/swanadiva/hostkeeper/internal/models"
|
||||
cryptossh "golang.org/x/crypto/ssh"
|
||||
)
|
||||
|
||||
// Client represents an SSH client
|
||||
type Client struct {
|
||||
host *models.Host
|
||||
timeout time.Duration
|
||||
client *cryptossh.Client
|
||||
config *cryptossh.ClientConfig
|
||||
hostKeyCallback cryptossh.HostKeyCallback
|
||||
passphraseCallback func() string // called to get passphrase for encrypted keys
|
||||
}
|
||||
|
||||
// NewClient creates a new SSH client
|
||||
func NewClient(host *models.Host, timeout time.Duration) *Client {
|
||||
return &Client{
|
||||
host: host,
|
||||
timeout: timeout,
|
||||
}
|
||||
}
|
||||
|
||||
// SetHostKeyCallback sets the host key verification callback
|
||||
func (c *Client) SetHostKeyCallback(cb cryptossh.HostKeyCallback) {
|
||||
c.hostKeyCallback = cb
|
||||
}
|
||||
|
||||
// SetPassphraseCallback sets the callback for getting key passphrases
|
||||
func (c *Client) SetPassphraseCallback(cb func() string) {
|
||||
c.passphraseCallback = cb
|
||||
}
|
||||
|
||||
// Connect establishes an SSH connection
|
||||
func (c *Client) Connect(ctx context.Context) error {
|
||||
// Create SSH configuration
|
||||
if err := c.setupConfig(); err != nil {
|
||||
return fmt.Errorf("failed to setup SSH config: %w", err)
|
||||
}
|
||||
|
||||
// Create connection context with timeout
|
||||
connCtx, cancel := context.WithTimeout(ctx, c.timeout)
|
||||
defer cancel()
|
||||
|
||||
// Establish TCP connection
|
||||
address := fmt.Sprintf("%s:%d", c.host.Hostname, c.host.Port)
|
||||
conn, err := c.dialTCP(connCtx, address)
|
||||
if err != nil {
|
||||
return errors.HandleSSHError(err)
|
||||
}
|
||||
|
||||
// Establish SSH connection over TCP
|
||||
sshConn, chans, reqs, err := cryptossh.NewClientConn(conn, address, c.config)
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
return errors.HandleSSHError(err)
|
||||
}
|
||||
|
||||
c.client = cryptossh.NewClient(sshConn, chans, reqs)
|
||||
return nil
|
||||
}
|
||||
|
||||
// dialTCP establishes a TCP connection
|
||||
func (c *Client) dialTCP(ctx context.Context, address string) (net.Conn, error) {
|
||||
d := net.Dialer{}
|
||||
return d.DialContext(ctx, "tcp", address)
|
||||
}
|
||||
|
||||
// setupConfig creates SSH client configuration
|
||||
func (c *Client) setupConfig() error {
|
||||
hostKeyCallback := cryptossh.InsecureIgnoreHostKey()
|
||||
if c.hostKeyCallback != nil {
|
||||
hostKeyCallback = c.hostKeyCallback
|
||||
}
|
||||
|
||||
config := &cryptossh.ClientConfig{
|
||||
User: c.host.Username,
|
||||
HostKeyCallback: hostKeyCallback,
|
||||
Timeout: c.timeout,
|
||||
}
|
||||
|
||||
// Configure authentication methods
|
||||
authMethods, err := c.getAuthMethods()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to setup authentication: %w", err)
|
||||
}
|
||||
|
||||
config.Auth = authMethods
|
||||
c.config = config
|
||||
return nil
|
||||
}
|
||||
|
||||
// Execute runs a command on the remote server
|
||||
func (c *Client) Execute(_ context.Context, cmd string) (string, error) {
|
||||
if c.client == nil {
|
||||
return "", fmt.Errorf("not connected to server")
|
||||
}
|
||||
|
||||
session, err := c.client.NewSession()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to create session: %w", err)
|
||||
}
|
||||
defer session.Close()
|
||||
|
||||
output, err := session.CombinedOutput(cmd)
|
||||
if err != nil {
|
||||
return string(output), fmt.Errorf("command execution failed: %w", err)
|
||||
}
|
||||
|
||||
return string(output), nil
|
||||
}
|
||||
|
||||
// Shell opens an interactive shell session
|
||||
func (c *Client) Shell() error {
|
||||
if c.client == nil {
|
||||
return fmt.Errorf("not connected to server")
|
||||
}
|
||||
|
||||
session, err := c.client.NewSession()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create session: %w", err)
|
||||
}
|
||||
defer session.Close()
|
||||
|
||||
// Get current terminal state
|
||||
fd := int(os.Stdin.Fd())
|
||||
oldState, err := term.MakeRaw(fd)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to set raw terminal: %w", err)
|
||||
}
|
||||
defer term.Restore(fd, oldState)
|
||||
|
||||
// Set up terminal modes
|
||||
modes := cryptossh.TerminalModes{
|
||||
cryptossh.ECHO: 1,
|
||||
cryptossh.TTY_OP_ISPEED: 14400,
|
||||
cryptossh.TTY_OP_OSPEED: 14400,
|
||||
}
|
||||
|
||||
// Get terminal size
|
||||
width, height, err := term.GetSize(fd)
|
||||
if err != nil {
|
||||
width = 80
|
||||
height = 24
|
||||
}
|
||||
|
||||
// Request PTY
|
||||
if err := session.RequestPty("xterm-256color", height, width, modes); err != nil {
|
||||
return fmt.Errorf("failed to request PTY: %w", err)
|
||||
}
|
||||
|
||||
// Handle window changes
|
||||
sigwinch := make(chan os.Signal, 1)
|
||||
signal.Notify(sigwinch, os.Signal(syscall.SIGWINCH))
|
||||
go func() {
|
||||
for range sigwinch {
|
||||
w, h, err := term.GetSize(fd)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
session.WindowChange(h, w)
|
||||
}
|
||||
}()
|
||||
defer signal.Stop(sigwinch)
|
||||
|
||||
// Link I/O
|
||||
session.Stdin = os.Stdin
|
||||
session.Stdout = os.Stdout
|
||||
session.Stderr = os.Stderr
|
||||
|
||||
// Start shell
|
||||
if err := session.Shell(); err != nil {
|
||||
return fmt.Errorf("failed to start shell: %w", err)
|
||||
}
|
||||
|
||||
// Wait for shell to exit
|
||||
if err := session.Wait(); err != nil {
|
||||
if exitErr, ok := err.(*cryptossh.ExitError); ok {
|
||||
if exitErr.ExitStatus() != 0 {
|
||||
return fmt.Errorf("shell exited with status %d", exitErr.ExitStatus())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("shell session error: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close closes the SSH connection
|
||||
func (c *Client) Close() error {
|
||||
if c.client != nil {
|
||||
return c.client.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetClient returns the underlying SSH client
|
||||
func (c *Client) GetClient() *cryptossh.Client {
|
||||
return c.client
|
||||
}
|
||||
|
||||
// IsConnected returns true if the client has an active connection
|
||||
func (c *Client) IsConnected() bool {
|
||||
return c.client != nil
|
||||
}
|
||||
Reference in New Issue
Block a user