mirror of
https://github.com/jetkvm/kvm.git
synced 2025-09-16 08:38:14 +00:00
Remove LED sync source option and add keypress reporting while still working with devices that haven't been upgraded We return the modifiers as the valid bitmask so that the VirtualKeyboard and InfoBar can represent the correct keys as down. This is important when we have strokes like Left-Control + Right-Control + Keypad-1 (used in switching KVMs and such). Fix handling of modifier keys in client and also removed the extraneous resetKeyboardState. Manage state to eliminate rerenders by judicious use of useMemo. Centralized keyboard layout and localized display maps Move keyboardOptions to useKeyboardLayouts Added translations for display maps. Add documentation on the legacy support. Return the KeysDownState from keyboardReport Clear out the hidErrorRollOver once sent to reset the keyboard to nothing down. Handles the returned KeysDownState from keyboardReport Now passes all logic through handleKeyPress. If we get a state back from a keyboardReport, use it and also enable keypressReport because we now know it's an upgraded device. Added exposition on isoCode management Fix de-DE chars to reflect German E2 keyboard. https://kbdlayout.info/kbdgre2/overview+virtualkeys Ran go modernize Morphs Interface{} to any Ranges over SplitSeq and FieldSeq for iterating splits Used min for end calculation remote_mount.Read Used range 16 in wol.createMagicPacket DID NOT apply the Omitempty cleanup. Strong typed in the typescript realm. Cleanup react state management to enable upgrading Zustand
138 lines
3.0 KiB
Go
138 lines
3.0 KiB
Go
package network
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"sync"
|
|
|
|
"golang.org/x/net/idna"
|
|
)
|
|
|
|
const (
|
|
hostnamePath = "/etc/hostname"
|
|
hostsPath = "/etc/hosts"
|
|
)
|
|
|
|
var (
|
|
hostnameLock sync.Mutex = sync.Mutex{}
|
|
)
|
|
|
|
func updateEtcHosts(hostname string, fqdn string) error {
|
|
// update /etc/hosts
|
|
hostsFile, err := os.OpenFile(hostsPath, os.O_RDWR|os.O_SYNC, os.ModeExclusive)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open %s: %w", hostsPath, err)
|
|
}
|
|
defer hostsFile.Close()
|
|
|
|
// read all lines
|
|
if _, err := hostsFile.Seek(0, io.SeekStart); err != nil {
|
|
return fmt.Errorf("failed to seek %s: %w", hostsPath, err)
|
|
}
|
|
|
|
lines, err := io.ReadAll(hostsFile)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to read %s: %w", hostsPath, err)
|
|
}
|
|
|
|
newLines := []string{}
|
|
hostLine := fmt.Sprintf("127.0.1.1\t%s %s", hostname, fqdn)
|
|
hostLineExists := false
|
|
|
|
for line := range strings.SplitSeq(string(lines), "\n") {
|
|
if strings.HasPrefix(line, "127.0.1.1") {
|
|
hostLineExists = true
|
|
line = hostLine
|
|
}
|
|
newLines = append(newLines, line)
|
|
}
|
|
|
|
if !hostLineExists {
|
|
newLines = append(newLines, hostLine)
|
|
}
|
|
|
|
if err := hostsFile.Truncate(0); err != nil {
|
|
return fmt.Errorf("failed to truncate %s: %w", hostsPath, err)
|
|
}
|
|
|
|
if _, err := hostsFile.Seek(0, io.SeekStart); err != nil {
|
|
return fmt.Errorf("failed to seek %s: %w", hostsPath, err)
|
|
}
|
|
|
|
if _, err := hostsFile.Write([]byte(strings.Join(newLines, "\n"))); err != nil {
|
|
return fmt.Errorf("failed to write %s: %w", hostsPath, err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func ToValidHostname(hostname string) string {
|
|
ascii, err := idna.Lookup.ToASCII(hostname)
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return ascii
|
|
}
|
|
|
|
func SetHostname(hostname string, fqdn string) error {
|
|
hostnameLock.Lock()
|
|
defer hostnameLock.Unlock()
|
|
|
|
hostname = ToValidHostname(strings.TrimSpace(hostname))
|
|
fqdn = ToValidHostname(strings.TrimSpace(fqdn))
|
|
|
|
if hostname == "" {
|
|
return fmt.Errorf("invalid hostname: %s", hostname)
|
|
}
|
|
|
|
if fqdn == "" {
|
|
fqdn = hostname
|
|
}
|
|
|
|
// update /etc/hostname
|
|
if err := os.WriteFile(hostnamePath, []byte(hostname), 0644); err != nil {
|
|
return fmt.Errorf("failed to write %s: %w", hostnamePath, err)
|
|
}
|
|
|
|
// update /etc/hosts
|
|
if err := updateEtcHosts(hostname, fqdn); err != nil {
|
|
return fmt.Errorf("failed to update /etc/hosts: %w", err)
|
|
}
|
|
|
|
// run hostname
|
|
if err := exec.Command("hostname", "-F", hostnamePath).Run(); err != nil {
|
|
return fmt.Errorf("failed to run hostname: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *NetworkInterfaceState) setHostnameIfNotSame() error {
|
|
hostname := s.GetHostname()
|
|
currentHostname, _ := os.Hostname()
|
|
|
|
fqdn := fmt.Sprintf("%s.%s", hostname, s.GetDomain())
|
|
|
|
if currentHostname == hostname && s.currentFqdn == fqdn && s.currentHostname == hostname {
|
|
return nil
|
|
}
|
|
|
|
scopedLogger := s.l.With().Str("hostname", hostname).Str("fqdn", fqdn).Logger()
|
|
|
|
err := SetHostname(hostname, fqdn)
|
|
if err != nil {
|
|
scopedLogger.Error().Err(err).Msg("failed to set hostname")
|
|
return err
|
|
}
|
|
|
|
s.currentHostname = hostname
|
|
s.currentFqdn = fqdn
|
|
|
|
scopedLogger.Info().Msg("hostname set")
|
|
|
|
return nil
|
|
}
|