feat: hostname resolver strategy and static hosts
Add the hostname resolver strategy. It connects by the NetBox name instead of an IP, with resolver.hostname_domain appended to names without a dot. Its position in the chain decides whether it is always used or only as a fallback (e.g. [primary_ip, hostname]); names that are not valid DNS hostnames fall through to the next strategy. Add static hosts under hosts: for machines that are not in NetBox, with address, user, port, tags, and their own SSH options. They are checked before NetBox (by name or shortcut), never expire, survive cache refresh and clear, and work without any NetBox configured. The cache overlays them after every load and stores only their last use, so they appear in completion, aliases, the TUI and its recent list. SSH options merge global, static host, then rules; rules can match kind: static. Manage them with netssh hosts list|add|remove and in netssh configure, which can now also continue without NetBox. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5821dfbebd
commit
a950a82552
22 files changed
+1024
-49
No files matched your search
@@ -0,0 +1,62 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// StaticKind is the kind of hosts defined under hosts: instead of NetBox.
|
||||
const StaticKind = "static"
|
||||
|
||||
// StaticHost is a host that is not in NetBox, defined in the config.
|
||||
type StaticHost struct {
|
||||
Name string `mapstructure:"name"`
|
||||
Address string `mapstructure:"address"` // IP address or DNS name
|
||||
User string `mapstructure:"user"`
|
||||
Port int `mapstructure:"port"`
|
||||
Tags []string `mapstructure:"tags"`
|
||||
SSH SSHOptions `mapstructure:"ssh"`
|
||||
}
|
||||
|
||||
var staticHostName = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]*$`)
|
||||
|
||||
// ValidateHosts keeps static host names usable as selectors and addresses
|
||||
// safe to pass to ssh.
|
||||
func (c *Config) ValidateHosts() error {
|
||||
seen := make(map[string]bool)
|
||||
for i, host := range c.Hosts {
|
||||
scope := fmt.Sprintf("hosts[%d]", i)
|
||||
if !staticHostName.MatchString(host.Name) {
|
||||
return fmt.Errorf("%s: name %q must start with a letter or digit and contain only letters, digits, dots, underscores, or hyphens", scope, host.Name)
|
||||
}
|
||||
name := strings.ToLower(host.Name)
|
||||
if seen[name] {
|
||||
return fmt.Errorf("%s: duplicate host name %q", scope, host.Name)
|
||||
}
|
||||
seen[name] = true
|
||||
if host.Address == "" || strings.HasPrefix(host.Address, "-") || strings.ContainsAny(host.Address, " \t\n\r/@") {
|
||||
return fmt.Errorf("%s: address %q must be an IP address or hostname", scope, host.Address)
|
||||
}
|
||||
if host.Port < 0 || host.Port > 65535 {
|
||||
return fmt.Errorf("%s: port must be between 1 and 65535", scope)
|
||||
}
|
||||
if strings.HasPrefix(host.User, "-") || strings.ContainsAny(host.User, " \t\n\r@") {
|
||||
return fmt.Errorf("%s: invalid user %q", scope, host.User)
|
||||
}
|
||||
if err := host.SSH.Validate(scope + ".ssh"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// StaticHost returns the static host with the given name, or nil.
|
||||
func (c *Config) StaticHost(name string) *StaticHost {
|
||||
for i := range c.Hosts {
|
||||
if strings.EqualFold(c.Hosts[i].Name, name) {
|
||||
return &c.Hosts[i]
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user