feat(config): add links and dns settings

- links: short names mapped to host selectors, validated against chains
  and static host names
- dns.suggest / dns.save_link: ask, always or never
- dns.domains: domain rules per name pattern (mx* -> easysahre.de)
- the wizard edits the dns settings under Resolver & cache and keeps links;
  setup.SaveTo reports to a chosen writer

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Sebastian UnterschützandClaude Opus 5.5 committed 2026-11-18 13:07:08 +01:00
1 parent c408d529a0
commit 5c41a5eb6f
6 files changed
+290 -6

No files matched your search

+13 -2
View File
@@ -26,7 +26,12 @@ type Config struct {
Hosts []StaticHost `mapstructure:"hosts"`
// Groups name lists of hosts and selectors for netssh multi (@name).
Groups map[string][]string `mapstructure:"groups"`
Multi MultiConfig `mapstructure:"multi"`
// Links map a short name to a host selector: netssh NAME connects to it,
// and the shell gets an alias NAME. Created after a DNS suggestion or
// with netssh link add.
Links map[string]string `mapstructure:"links"`
DNS DNSConfig `mapstructure:"dns"`
Multi MultiConfig `mapstructure:"multi"`
}
type ShellConfig struct {
@@ -185,7 +190,7 @@ func Load() (*Config, error) {
v.SetEnvPrefix("NETSSH")
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
v.AutomaticEnv()
for _, key := range []string{"netbox.url", "netbox.token", "netbox.token_version", "resolver.strategies", "resolver.management_subnets", "resolver.interface_name", "resolver.hostname_domain", "cache.ttl", "cache.path", "ssh.default_user", "ssh.default_port", "shortcuts.domains", "shortcuts.strip_hyphens", "shortcuts.show", "shell.alias", "ssh.proxy_jump", "ssh.pre_connect", "ssh.post_disconnect", "tui.history_limit", "activity.enabled", "activity.retention_days", "activity.max_entries", "ssh.host_key_checking", "ssh.target", "ssh.address_family", "completion.max_results", "multi.multiplexer", "multi.panes_per_window", "ssh.warn_status", "tui.columns", "tui.reachability_timeout", "resolver.fallback", "resolver.fallback_timeout"} {
for _, key := range []string{"netbox.url", "netbox.token", "netbox.token_version", "resolver.strategies", "resolver.management_subnets", "resolver.interface_name", "resolver.hostname_domain", "cache.ttl", "cache.path", "ssh.default_user", "ssh.default_port", "shortcuts.domains", "shortcuts.strip_hyphens", "shortcuts.show", "shell.alias", "ssh.proxy_jump", "ssh.pre_connect", "ssh.post_disconnect", "tui.history_limit", "activity.enabled", "activity.retention_days", "activity.max_entries", "ssh.host_key_checking", "ssh.target", "ssh.address_family", "completion.max_results", "multi.multiplexer", "multi.panes_per_window", "ssh.warn_status", "tui.columns", "tui.reachability_timeout", "resolver.fallback", "resolver.fallback_timeout", "dns.suggest", "dns.save_link"} {
if err := v.BindEnv(key); err != nil {
return nil, err
}
@@ -239,6 +244,12 @@ func Load() (*Config, error) {
if err := cfg.ValidateHosts(); err != nil {
return nil, err
}
if err := cfg.ValidateLinks(); err != nil {
return nil, err
}
if err := cfg.ValidateDNS(); err != nil {
return nil, err
}
if err := cfg.ValidateOperations(); err != nil {
return nil, err
}
+96
View File
@@ -0,0 +1,96 @@
package config
import (
"fmt"
"path"
"strings"
)
// DNSConfig controls what netssh does with names that are neither in NetBox
// nor in the cache: resolve them through DNS and suggest the owning host.
type DNSConfig struct {
// Suggest is ask (default), always (take the first suggestion without
// asking, also in scripts) or never (no DNS lookup, plain "not found").
Suggest string `mapstructure:"suggest"`
// SaveLink is ask (default), always (save the shortcut without asking)
// or never, for the link offered after a suggestion.
SaveLink string `mapstructure:"save_link"`
// Domains are appended to names without a dot that match the rule.
Domains []DNSDomain `mapstructure:"domains"`
}
// DNSDomain appends Domain to names matching Match (a shell glob such as
// "mx*"; empty matches every name).
type DNSDomain struct {
Match string `mapstructure:"match"`
Domain string `mapstructure:"domain"`
}
// DNS modes for dns.suggest and dns.save_link.
const (
DNSAsk = "ask"
DNSAlways = "always"
DNSNever = "never"
)
// SuggestMode returns dns.suggest, ask by default.
func (d DNSConfig) SuggestMode() string { return dnsMode(d.Suggest) }
// SaveLinkMode returns dns.save_link, ask by default.
func (d DNSConfig) SaveLinkMode() string { return dnsMode(d.SaveLink) }
func dnsMode(mode string) string {
if mode == "" {
return DNSAsk
}
return strings.ToLower(mode)
}
// Names lists the DNS names to try for what the user typed. A name with a
// dot is used as is. A name without a dot gets the domain of every rule it
// matches, in order; only when no rule matches is the bare name looked up.
func (d DNSConfig) Names(query string) []string {
if strings.Contains(query, ".") {
return []string{query}
}
var names []string
seen := map[string]bool{}
lower := strings.ToLower(query)
for _, rule := range d.Domains {
if rule.Match != "" {
if ok, _ := path.Match(strings.ToLower(rule.Match), lower); !ok {
continue
}
}
name := query + "." + strings.Trim(strings.TrimSpace(rule.Domain), ".")
if !seen[strings.ToLower(name)] {
seen[strings.ToLower(name)] = true
names = append(names, name)
}
}
if len(names) == 0 {
return []string{query}
}
return names
}
// ValidateDNS rejects unknown modes, broken patterns and empty domains.
func (c *Config) ValidateDNS() error {
for key, mode := range map[string]string{"dns.suggest": c.DNS.Suggest, "dns.save_link": c.DNS.SaveLink} {
switch dnsMode(mode) {
case DNSAsk, DNSAlways, DNSNever:
default:
return fmt.Errorf("%s must be ask, always, or never", key)
}
}
for i, rule := range c.DNS.Domains {
if _, err := path.Match(rule.Match, ""); err != nil {
return fmt.Errorf("dns.domains[%d]: invalid match pattern %q", i, rule.Match)
}
domain := strings.Trim(strings.TrimSpace(rule.Domain), ".")
if domain == "" || strings.ContainsAny(domain, " \t/@*?") || strings.HasPrefix(domain, "-") {
return fmt.Errorf("dns.domains[%d]: invalid domain %q", i, rule.Domain)
}
}
return nil
}
+31
View File
@@ -60,3 +60,34 @@ func (c *Config) StaticHost(name string) *StaticHost {
}
return nil
}
// Link returns the target of the link named name (case-insensitive).
func (c *Config) Link(name string) (string, bool) {
for key, target := range c.Links {
if strings.EqualFold(key, name) {
return target, true
}
}
return "", false
}
// ValidateLinks keeps link names usable as selectors and shell aliases and
// rejects chains, which could loop.
func (c *Config) ValidateLinks() error {
for name, target := range c.Links {
if !staticHostName.MatchString(name) || strings.Contains(name, "/") {
return fmt.Errorf("links: name %q must start with a letter or digit and contain only letters, digits, dots, underscores, or hyphens", name)
}
if c.StaticHost(name) != nil {
return fmt.Errorf("links: %q is also a static host", name)
}
target = strings.TrimSpace(target)
if target == "" || strings.HasPrefix(target, "-") || strings.ContainsAny(target, " \t\n\r@") {
return fmt.Errorf("links.%s: invalid target %q", name, target)
}
if _, chained := c.Link(target); chained {
return fmt.Errorf("links.%s: target %q is another link", name, target)
}
}
return nil
}
+12
View File
@@ -106,3 +106,15 @@ func TestTUIColumnsValidation(t *testing.T) {
}
}
}
func TestDNSConfigValidation(t *testing.T) {
cfg := loadFromDir(t, writeConfig(t, "netbox:\n url: https://nb\ndns:\n suggest: Always\n domains:\n - match: \"mx*\"\n domain: easysahre.de\n"))
if cfg.DNS.SuggestMode() != DNSAlways || cfg.DNS.SaveLinkMode() != DNSAsk || len(cfg.DNS.Domains) != 1 {
t.Fatalf("dns: %+v", cfg.DNS)
}
for _, bad := range []DNSConfig{{Suggest: "sometimes"}, {SaveLink: "yes"}, {Domains: []DNSDomain{{Match: "[", Domain: "a.de"}}}, {Domains: []DNSDomain{{Domain: " "}}}, {Domains: []DNSDomain{{Domain: "a b.de"}}}} {
if err := (&Config{DNS: bad}).ValidateDNS(); err == nil {
t.Errorf("%+v accepted", bad)
}
}
}