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
199 lines
4.1 KiB
Go
199 lines
4.1 KiB
Go
package udhcpc
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"time"
|
|
|
|
"github.com/fsnotify/fsnotify"
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
const (
|
|
DHCPLeaseFile = "/run/udhcpc.%s.info"
|
|
DHCPPidFile = "/run/udhcpc.%s.pid"
|
|
)
|
|
|
|
type DHCPClient struct {
|
|
InterfaceName string
|
|
leaseFile string
|
|
pidFile string
|
|
lease *Lease
|
|
logger *zerolog.Logger
|
|
process *os.Process
|
|
onLeaseChange func(lease *Lease)
|
|
}
|
|
|
|
type DHCPClientOptions struct {
|
|
InterfaceName string
|
|
PidFile string
|
|
Logger *zerolog.Logger
|
|
OnLeaseChange func(lease *Lease)
|
|
}
|
|
|
|
var defaultLogger = zerolog.New(os.Stdout).Level(zerolog.InfoLevel)
|
|
|
|
func NewDHCPClient(options *DHCPClientOptions) *DHCPClient {
|
|
if options.Logger == nil {
|
|
options.Logger = &defaultLogger
|
|
}
|
|
|
|
l := options.Logger.With().Str("interface", options.InterfaceName).Logger()
|
|
return &DHCPClient{
|
|
InterfaceName: options.InterfaceName,
|
|
logger: &l,
|
|
leaseFile: fmt.Sprintf(DHCPLeaseFile, options.InterfaceName),
|
|
pidFile: options.PidFile,
|
|
onLeaseChange: options.OnLeaseChange,
|
|
}
|
|
}
|
|
|
|
func (c *DHCPClient) getWatchPaths() []string {
|
|
watchPaths := make(map[string]any)
|
|
watchPaths[filepath.Dir(c.leaseFile)] = nil
|
|
|
|
if c.pidFile != "" {
|
|
watchPaths[filepath.Dir(c.pidFile)] = nil
|
|
}
|
|
|
|
paths := make([]string, 0)
|
|
for path := range watchPaths {
|
|
paths = append(paths, path)
|
|
}
|
|
return paths
|
|
}
|
|
|
|
// Run starts the DHCP client and watches the lease file for changes.
|
|
// this isn't a blocking call, and the lease file is reloaded when a change is detected.
|
|
func (c *DHCPClient) Run() error {
|
|
err := c.loadLeaseFile()
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
|
return err
|
|
}
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer watcher.Close()
|
|
|
|
go func() {
|
|
for {
|
|
select {
|
|
case event, ok := <-watcher.Events:
|
|
if !ok {
|
|
continue
|
|
}
|
|
if !event.Has(fsnotify.Write) && !event.Has(fsnotify.Create) {
|
|
continue
|
|
}
|
|
|
|
if event.Name == c.leaseFile {
|
|
c.logger.Debug().
|
|
Str("event", event.Op.String()).
|
|
Str("path", event.Name).
|
|
Msg("udhcpc lease file updated, reloading lease")
|
|
_ = c.loadLeaseFile()
|
|
}
|
|
case err, ok := <-watcher.Errors:
|
|
if !ok {
|
|
return
|
|
}
|
|
c.logger.Error().Err(err).Msg("error watching lease file")
|
|
}
|
|
}
|
|
}()
|
|
|
|
for _, path := range c.getWatchPaths() {
|
|
err = watcher.Add(path)
|
|
if err != nil {
|
|
c.logger.Error().
|
|
Err(err).
|
|
Str("path", path).
|
|
Msg("failed to watch directory")
|
|
return err
|
|
}
|
|
}
|
|
|
|
// TODO: update udhcpc pid file
|
|
// we'll comment this out for now because the pid might change
|
|
// process := c.GetProcess()
|
|
// if process == nil {
|
|
// c.logger.Error().Msg("udhcpc process not found")
|
|
// }
|
|
|
|
// block the goroutine until the lease file is updated
|
|
<-make(chan struct{})
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *DHCPClient) loadLeaseFile() error {
|
|
file, err := os.ReadFile(c.leaseFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
data := string(file)
|
|
if data == "" {
|
|
c.logger.Debug().Msg("udhcpc lease file is empty")
|
|
return nil
|
|
}
|
|
|
|
lease := &Lease{}
|
|
err = UnmarshalDHCPCLease(lease, string(file))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
isFirstLoad := c.lease == nil
|
|
|
|
// Skip processing if lease hasn't changed to avoid unnecessary wake-ups.
|
|
if reflect.DeepEqual(c.lease, lease) {
|
|
return nil
|
|
}
|
|
|
|
c.lease = lease
|
|
|
|
if lease.IPAddress == nil {
|
|
c.logger.Info().
|
|
Interface("lease", lease).
|
|
Str("data", string(file)).
|
|
Msg("udhcpc lease cleared")
|
|
return nil
|
|
}
|
|
|
|
msg := "udhcpc lease updated"
|
|
if isFirstLoad {
|
|
msg = "udhcpc lease loaded"
|
|
}
|
|
|
|
leaseExpiry, err := lease.SetLeaseExpiry()
|
|
if err != nil {
|
|
c.logger.Error().Err(err).Msg("failed to get dhcp lease expiry")
|
|
} else {
|
|
expiresIn := time.Until(leaseExpiry)
|
|
c.logger.Info().
|
|
Interface("expiry", leaseExpiry).
|
|
Str("expiresIn", expiresIn.String()).
|
|
Msg("current dhcp lease expiry time calculated")
|
|
}
|
|
|
|
c.onLeaseChange(lease)
|
|
|
|
c.logger.Info().
|
|
Str("ip", lease.IPAddress.String()).
|
|
Str("leaseTime", lease.LeaseTime.String()).
|
|
Interface("data", lease).
|
|
Msg(msg)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (c *DHCPClient) GetLease() *Lease {
|
|
return c.lease
|
|
}
|