| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- // sshpool.go — Pool de conexiones SSH persistentes (equivalente Go del
- // ControlMaster/ControlPersist del JS, SPEC §7.2): UNA conexión por router,
- // reutilizada entre ticks del poller, con reconexión automática y backoff
- // exponencial (30 s base, máx 5 min) para no martillear un router caído.
- //
- // known_hosts con semántica accept-new junto a la clave (como el JS):
- // host desconocido → se acepta y se anota; host conocido con clave distinta
- // → se rechaza (posible MITM).
- package adapters
- import (
- "context"
- "errors"
- "fmt"
- "net"
- "os"
- "path/filepath"
- "sync"
- "time"
- "github.com/gnacho/netpulse/server-go/internal/sshkey"
- "golang.org/x/crypto/ssh"
- "golang.org/x/crypto/ssh/knownhosts"
- )
- const (
- sshDialTimeout = 4 * time.Second // ConnectTimeout=4 del JS
- sshDefaultTimeout = 5 * time.Second // SSH_TIMEOUT_MS del JS
- sshBackoffBase = 30 * time.Second
- sshBackoffMax = 5 * time.Minute
- )
- // SSHPool gestiona conexiones persistentes a los routers.
- type SSHPool struct {
- keyPath string
- signer ssh.Signer
- khPath string
- mu sync.Mutex
- conns map[string]*sshConn
- closed bool
- }
- type sshConn struct {
- client *ssh.Client
- failures int
- notUntil time.Time
- }
- // NewSSHPool crea el pool cargando la clave privada (ed25519 sin passphrase).
- func NewSSHPool(keyPath string) (*SSHPool, error) {
- pem, err := os.ReadFile(keyPath)
- if err != nil {
- return nil, fmt.Errorf("leer clave SSH: %w", err)
- }
- signer, err := ssh.ParsePrivateKey(pem)
- if err != nil {
- return nil, fmt.Errorf("parsear clave SSH: %w", err)
- }
- return &SSHPool{
- keyPath: keyPath,
- signer: signer,
- khPath: sshkey.KnownHostsPath(keyPath),
- conns: map[string]*sshConn{},
- }, nil
- }
- // hostKeyCallback implementa accept-new sobre el known_hosts propio.
- func (p *SSHPool) hostKeyCallback() (ssh.HostKeyCallback, error) {
- if _, err := os.Stat(p.khPath); os.IsNotExist(err) {
- if err := os.MkdirAll(filepath.Dir(p.khPath), 0o700); err == nil {
- _ = os.WriteFile(p.khPath, nil, 0o600)
- }
- }
- checker, err := knownhosts.New(p.khPath)
- if err != nil {
- return nil, err
- }
- return func(hostname string, remote net.Addr, key ssh.PublicKey) error {
- err := checker(hostname, remote, key)
- if err == nil {
- return nil
- }
- var keyErr *knownhosts.KeyError
- if errors.As(err, &keyErr) && len(keyErr.Want) > 0 {
- return err // clave CAMBIADA → rechazar
- }
- // Host desconocido → accept-new
- line := knownhosts.Line([]string{knownhosts.Normalize(hostname)}, key)
- f, ferr := os.OpenFile(p.khPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600)
- if ferr != nil {
- return ferr
- }
- defer f.Close()
- _, ferr = f.WriteString(line + "\n")
- return ferr
- }, nil
- }
- // dial abre (o reabre) la conexión a un host respetando el backoff.
- func (p *SSHPool) dial(host string) (*ssh.Client, error) {
- p.mu.Lock()
- entry := p.conns[host]
- if entry == nil {
- entry = &sshConn{}
- p.conns[host] = entry
- }
- if entry.client != nil {
- c := entry.client
- p.mu.Unlock()
- return c, nil
- }
- if time.Now().Before(entry.notUntil) {
- p.mu.Unlock()
- return nil, fmt.Errorf("ssh %s: en backoff tras %d fallos", host, entry.failures)
- }
- p.mu.Unlock()
- cb, err := p.hostKeyCallback()
- if err != nil {
- return nil, err
- }
- cfg := &ssh.ClientConfig{
- User: "root",
- Auth: []ssh.AuthMethod{ssh.PublicKeys(p.signer)},
- HostKeyCallback: cb,
- Timeout: sshDialTimeout,
- }
- client, err := ssh.Dial("tcp", net.JoinHostPort(host, "22"), cfg)
- p.mu.Lock()
- defer p.mu.Unlock()
- if err != nil {
- entry.failures++
- backoff := sshBackoffBase << min(entry.failures-1, 4)
- if backoff > sshBackoffMax {
- backoff = sshBackoffMax
- }
- entry.notUntil = time.Now().Add(backoff)
- return nil, fmt.Errorf("ssh %s: %w", host, err)
- }
- entry.client = client
- entry.failures = 0
- entry.notUntil = time.Time{}
- return client, nil
- }
- // drop invalida la conexión de un host (la próxima llamada rediala).
- func (p *SSHPool) drop(host string) {
- p.mu.Lock()
- defer p.mu.Unlock()
- if e := p.conns[host]; e != nil && e.client != nil {
- _ = e.client.Close()
- e.client = nil
- }
- }
- // Run ejecuta cmd en host con timeout (por defecto 5 s, como el JS).
- // Una sesión fallida por canal roto invalida la conexión (reconexión).
- func (p *SSHPool) Run(host, cmd string, timeout time.Duration) (string, error) {
- if timeout <= 0 {
- timeout = sshDefaultTimeout
- }
- client, err := p.dial(host)
- if err != nil {
- return "", err
- }
- session, err := client.NewSession()
- if err != nil {
- p.drop(host)
- return "", fmt.Errorf("ssh %s: session: %w", host, err)
- }
- defer session.Close()
- type result struct {
- out []byte
- err error
- }
- ch := make(chan result, 1)
- go func() {
- out, err := session.Output(cmd)
- ch <- result{out, err}
- }()
- select {
- case res := <-ch:
- if res.err != nil {
- var ee *ssh.ExitError
- if !errors.As(res.err, &ee) {
- p.drop(host) // canal roto, no exit status
- }
- return "", fmt.Errorf("ssh %s: %w", host, res.err)
- }
- return string(res.out), nil
- case <-time.After(timeout):
- _ = session.Close()
- p.drop(host)
- return "", fmt.Errorf("ssh %s: timeout (%s)", host, timeout)
- }
- }
- // RunCtx es Run con cancelación por contexto.
- func (p *SSHPool) RunCtx(ctx context.Context, host, cmd string, timeout time.Duration) (string, error) {
- type res struct {
- out string
- err error
- }
- ch := make(chan res, 1)
- go func() {
- out, err := p.Run(host, cmd, timeout)
- ch <- res{out, err}
- }()
- select {
- case r := <-ch:
- return r.out, r.err
- case <-ctx.Done():
- return "", ctx.Err()
- }
- }
- // Close cierra todas las conexiones del pool.
- func (p *SSHPool) Close() {
- p.mu.Lock()
- defer p.mu.Unlock()
- if p.closed {
- return
- }
- p.closed = true
- for _, e := range p.conns {
- if e.client != nil {
- _ = e.client.Close()
- e.client = nil
- }
- }
- }
- func min(a, b int) int {
- if a < b {
- return a
- }
- return b
- }
|