Files
Reims-Go/internal/driver/endpoints.go
T

161 lines
4.6 KiB
Go

package driver
import (
"fmt"
"net/netip"
"regexp"
"strings"
"unicode/utf8"
)
// Endpoint kinds are a closed list. The host runs one fixed command template
// per kind and never a command line supplied by a driver.
const (
EndpointSSH = "ssh"
EndpointWSL = "wsl"
EndpointRDP = "rdp"
EndpointVMConnect = "vmconnect"
EndpointADB = "adb"
)
// DefaultRDPPort is used when an rdp endpoint omits its port.
const DefaultRDPPort = 3389
// VMEndpoint is one way to reach a running guest.
type VMEndpoint struct {
Kind string `json:"kind"`
Host string `json:"host,omitempty"`
Port int `json:"port,omitempty"`
User string `json:"user,omitempty"`
Distro string `json:"distro,omitempty"`
VM string `json:"vm,omitempty"`
Serial string `json:"serial,omitempty"`
Ready bool `json:"ready"`
}
var (
reEndpointHost = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9.-]{0,252}$`)
reEndpointUser = regexp.MustCompile(`^[A-Za-z0-9._-]+$`)
reEndpointDistro = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$`)
reEndpointSerial = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$`)
)
const vmconnectForbidden = "\"'`&|<>^%$;\\"
func validEndpointHost(host string) bool {
if host == "" {
return false
}
if _, err := netip.ParseAddr(host); err == nil {
return true
}
return reEndpointHost.MatchString(host)
}
// ValidateEndpoint checks one endpoint against the rules of its kind.
func ValidateEndpoint(ep VMEndpoint) error {
switch ep.Kind {
case EndpointSSH:
if ep.Host != "" && !validEndpointHost(ep.Host) {
return fmt.Errorf("ssh endpoint has invalid host %q", ep.Host)
}
if ep.Port < 1 || ep.Port > 65535 {
return fmt.Errorf("ssh endpoint has invalid port %d", ep.Port)
}
if ep.User != "" && !reEndpointUser.MatchString(ep.User) {
return fmt.Errorf("ssh endpoint has invalid user %q", ep.User)
}
case EndpointWSL:
if !reEndpointDistro.MatchString(ep.Distro) {
return fmt.Errorf("wsl endpoint has invalid distro %q", ep.Distro)
}
case EndpointRDP:
if !validEndpointHost(ep.Host) {
return fmt.Errorf("rdp endpoint has invalid host %q", ep.Host)
}
if ep.Port != 0 && (ep.Port < 1 || ep.Port > 65535) {
return fmt.Errorf("rdp endpoint has invalid port %d", ep.Port)
}
case EndpointVMConnect:
vm := strings.TrimSpace(ep.VM)
if vm == "" || utf8.RuneCountInString(vm) > 128 || vm != ep.VM {
return fmt.Errorf("vmconnect endpoint has invalid VM name %q", ep.VM)
}
if vm[0] == '-' || vm[0] == '/' {
return fmt.Errorf("vmconnect endpoint VM name %q may not start with %q", ep.VM, vm[0])
}
for _, r := range vm {
if r < 0x20 || r == 0x7f || strings.ContainsRune(vmconnectForbidden, r) {
return fmt.Errorf("vmconnect endpoint VM name %q contains an invalid character", ep.VM)
}
}
case EndpointADB:
if !reEndpointSerial.MatchString(ep.Serial) {
return fmt.Errorf("adb endpoint has invalid serial %q", ep.Serial)
}
default:
return fmt.Errorf("unknown endpoint kind %q", ep.Kind)
}
return nil
}
// ValidateEndpoints checks every endpoint and fails on the first bad one.
func ValidateEndpoints(eps []VMEndpoint) error {
for _, ep := range eps {
if err := ValidateEndpoint(ep); err != nil {
return err
}
}
return nil
}
// SyncSSHEndpoint keeps VMInfo.SSHPort and the ssh endpoint consistent, so
// existing SSH shortcuts keep reading SSHPort.
func SyncSSHEndpoint(vm *VMInfo) {
if vm == nil {
return
}
for _, ep := range vm.Endpoints {
if ep.Kind == EndpointSSH {
if vm.SSHPort == 0 && ep.Port > 0 {
vm.SSHPort = ep.Port
}
return
}
}
if vm.SSHPort > 0 {
vm.Endpoints = append(append([]VMEndpoint(nil), vm.Endpoints...), VMEndpoint{
Kind: EndpointSSH, Host: "127.0.0.1", Port: vm.SSHPort, Ready: vm.State == "running",
})
}
}
// endpointPriority lists kinds from the one a graphical user wants first.
var endpointPriority = []string{EndpointVMConnect, EndpointRDP, EndpointWSL, EndpointSSH, EndpointADB}
// EndpointPriorityIndex returns the rank of kind (lower is preferred); unknown
// kinds sort last.
func EndpointPriorityIndex(kind string) int {
for i, k := range endpointPriority {
if k == kind {
return i
}
}
return len(endpointPriority)
}
// PrimaryHostedEndpoint picks the ready endpoint a hosted VM shows as its
// primary action: vmconnect, then rdp, wsl, ssh and adb.
func PrimaryHostedEndpoint(eps []VMEndpoint) (VMEndpoint, bool) {
best, found := VMEndpoint{}, false
for _, ep := range eps {
if !ep.Ready {
continue
}
if !found || EndpointPriorityIndex(ep.Kind) < EndpointPriorityIndex(best.Kind) {
best, found = ep, true
}
}
return best, found
}