refactor: refactor project structure to use golang best practices
This commit is contained in:
parent
5b16cef525
commit
4ed6a61b1d
10 changed files with 617 additions and 702 deletions
55
internal/ssh/client.go
Normal file
55
internal/ssh/client.go
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
package ssh
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/ssh"
|
||||
"golang.org/x/crypto/ssh/knownhosts"
|
||||
)
|
||||
|
||||
type Connection struct {
|
||||
Client *ssh.Client
|
||||
}
|
||||
|
||||
func NewConnection(user, host string, port int, auth ssh.AuthMethod) (*Connection, error) {
|
||||
home, err := os.UserHomeDir()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get home dir: %w", err)
|
||||
}
|
||||
knownHostsPath := filepath.Join(home, ".ssh", "known_hosts")
|
||||
|
||||
hkCallback, err := knownhosts.New(knownHostsPath)
|
||||
if err != nil {
|
||||
slog.Warn("Could not load known_hosts, ensure you connected manually once.", "path", knownHostsPath)
|
||||
return nil, fmt.Errorf("known_hosts error: %w", err)
|
||||
}
|
||||
|
||||
cfg := &ssh.ClientConfig{
|
||||
User: user,
|
||||
Auth: []ssh.AuthMethod{auth},
|
||||
HostKeyCallback: hkCallback,
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
addr := fmt.Sprintf("%s:%d", host, port)
|
||||
slog.Debug("Dialing SSH", "target", addr)
|
||||
|
||||
client, err := ssh.Dial("tcp", addr, cfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ssh dial failed: %w", err)
|
||||
}
|
||||
slog.Debug("SSH connection established", "target", addr)
|
||||
|
||||
return &Connection{Client: client}, nil
|
||||
}
|
||||
|
||||
func (c *Connection) Close() error {
|
||||
if c.Client != nil {
|
||||
return c.Client.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue