Files
ssh-access-tui/internal/keymgr/clientconfig.go
T
2026-07-22 15:14:44 +03:00

40 lines
1.6 KiB
Go

package keymgr
import (
"fmt"
"strings"
)
// RenderClientConfig produces an OpenSSH client config (works identically on
// Windows' built-in OpenSSH client, macOS, and Linux). It defines exactly
// one connection, "jump", carrying one LocalForward per allowed service —
// the user opens it once (see INSTRUCTIONS.txt) and every service becomes
// reachable at once on the listed local ports, for as long as that one
// connection stays open.
//
// This file is convenience only, not a security boundary: nothing stops a
// user from editing it or invoking ssh by hand. The actual restriction to
// these destinations is enforced server-side via authorized_keys
// permitopen= (see internal/sysadm/authorizedkeys.go) independent of what
// this file says.
func RenderClientConfig(jumpHost string, jumpPort int, username, keyFileName string, forwards []AllocatedForward) string {
var b strings.Builder
fmt.Fprintf(&b, "# ssh-access-tui client config for %s\n", username)
fmt.Fprintf(&b, "# Generated %s. Regenerated whenever your allowed services change.\n\n", genTimestamp())
fmt.Fprintf(&b, "Host jump\n")
fmt.Fprintf(&b, " HostName %s\n", jumpHost)
fmt.Fprintf(&b, " Port %d\n", jumpPort)
fmt.Fprintf(&b, " User %s\n", username)
fmt.Fprintf(&b, " IdentityFile ~/.ssh/%s\n", keyFileName)
fmt.Fprintf(&b, " IdentitiesOnly yes\n")
for _, f := range forwards {
comment := f.HostName + "/" + f.ServiceName
if f.Description != "" {
comment += ": " + f.Description
}
fmt.Fprintf(&b, " LocalForward %d %s:%d # %s\n", f.LocalPort, f.Address, f.Port, comment)
}
return b.String()
}