Make USB network settings configurable

This commit is contained in:
Daniel Lorch 2025-05-15 01:08:00 +02:00
parent b512e4d817
commit 0317cccd7b
5 changed files with 64 additions and 38 deletions

View File

@ -75,28 +75,29 @@ func (m *KeyboardMacro) Validate() error {
} }
type Config struct { type Config struct {
CloudURL string `json:"cloud_url"` CloudURL string `json:"cloud_url"`
CloudAppURL string `json:"cloud_app_url"` CloudAppURL string `json:"cloud_app_url"`
CloudToken string `json:"cloud_token"` CloudToken string `json:"cloud_token"`
GoogleIdentity string `json:"google_identity"` GoogleIdentity string `json:"google_identity"`
JigglerEnabled bool `json:"jiggler_enabled"` JigglerEnabled bool `json:"jiggler_enabled"`
AutoUpdateEnabled bool `json:"auto_update_enabled"` AutoUpdateEnabled bool `json:"auto_update_enabled"`
IncludePreRelease bool `json:"include_pre_release"` IncludePreRelease bool `json:"include_pre_release"`
HashedPassword string `json:"hashed_password"` HashedPassword string `json:"hashed_password"`
LocalAuthToken string `json:"local_auth_token"` LocalAuthToken string `json:"local_auth_token"`
LocalAuthMode string `json:"localAuthMode"` //TODO: fix it with migration LocalAuthMode string `json:"localAuthMode"` //TODO: fix it with migration
WakeOnLanDevices []WakeOnLanDevice `json:"wake_on_lan_devices"` WakeOnLanDevices []WakeOnLanDevice `json:"wake_on_lan_devices"`
KeyboardMacros []KeyboardMacro `json:"keyboard_macros"` KeyboardMacros []KeyboardMacro `json:"keyboard_macros"`
EdidString string `json:"hdmi_edid_string"` EdidString string `json:"hdmi_edid_string"`
ActiveExtension string `json:"active_extension"` ActiveExtension string `json:"active_extension"`
DisplayMaxBrightness int `json:"display_max_brightness"` DisplayMaxBrightness int `json:"display_max_brightness"`
DisplayDimAfterSec int `json:"display_dim_after_sec"` DisplayDimAfterSec int `json:"display_dim_after_sec"`
DisplayOffAfterSec int `json:"display_off_after_sec"` DisplayOffAfterSec int `json:"display_off_after_sec"`
TLSMode string `json:"tls_mode"` // options: "self-signed", "user-defined", "" TLSMode string `json:"tls_mode"` // options: "self-signed", "user-defined", ""
UsbConfig *usbgadget.Config `json:"usb_config"` UsbConfig *usbgadget.Config `json:"usb_config"`
UsbDevices *usbgadget.Devices `json:"usb_devices"` UsbDevices *usbgadget.Devices `json:"usb_devices"`
NetworkConfig *network.NetworkConfig `json:"network_config"` NetworkConfig *network.NetworkConfig `json:"network_config"`
DefaultLogLevel string `json:"default_log_level"` UsbNetworkConfig *network.UsbNetworkConfig `json:"usb_network_config"`
DefaultLogLevel string `json:"default_log_level"`
} }
const configPath = "/userdata/kvm_config.json" const configPath = "/userdata/kvm_config.json"
@ -128,7 +129,13 @@ var defaultConfig = &Config{
EthernetNcm: false, EthernetNcm: false,
EthernetRndis: false, EthernetRndis: false,
}, },
NetworkConfig: &network.NetworkConfig{}, NetworkConfig: &network.NetworkConfig{
NatEnable: false,
},
UsbNetworkConfig: &network.UsbNetworkConfig{
IPv4Addr: "172.16.55.1",
IPv4Network: "172.16.55.0/24",
},
DefaultLogLevel: "INFO", DefaultLogLevel: "INFO",
} }

View File

@ -48,6 +48,13 @@ type NetworkConfig struct {
TimeSyncOrdering []string `json:"time_sync_ordering,omitempty" one_of:"http,ntp,ntp_dhcp,ntp_user_provided,ntp_fallback" default:"ntp,http"` TimeSyncOrdering []string `json:"time_sync_ordering,omitempty" one_of:"http,ntp,ntp_dhcp,ntp_user_provided,ntp_fallback" default:"ntp,http"`
TimeSyncDisableFallback null.Bool `json:"time_sync_disable_fallback,omitempty" default:"false"` TimeSyncDisableFallback null.Bool `json:"time_sync_disable_fallback,omitempty" default:"false"`
TimeSyncParallel null.Int `json:"time_sync_parallel,omitempty" default:"4"` TimeSyncParallel null.Int `json:"time_sync_parallel,omitempty" default:"4"`
NatEnable bool `json:"nat_enable,omitempty" default:"false" required:"true"`
}
type UsbNetworkConfig struct {
IPv4Addr string `json:"ipv4_addr,omitempty" validate_type:"ipv4" default:"172.16.55.1" required:"true"`
IPv4Network string `json:"ipv4_network,omitempty" validate_type:"ipv4" default:"172.16.55.0/24" required:"true"`
} }
func (c *NetworkConfig) GetMDNSMode() *mdns.MDNSListenOptions { func (c *NetworkConfig) GetMDNSMode() *mdns.MDNSListenOptions {

View File

@ -13,19 +13,27 @@ const (
func (s *NetworkInterfaceState) reconfigureNat(wantNat bool, sourceAddr string) error { func (s *NetworkInterfaceState) reconfigureNat(wantNat bool, sourceAddr string) error {
scopedLogger := s.l.With().Str("iface", s.interfaceName).Logger() scopedLogger := s.l.With().Str("iface", s.interfaceName).Logger()
if !wantNat {
if s.natEnabled {
scopedLogger.Info().Msg("disabling NAT")
err := disableNat()
if err != nil {
s.l.Error().Err(err).Msg("failed to disable NAT")
}
}
return nil
}
if wantNat && s.IsOnline() { if wantNat && s.IsOnline() {
scopedLogger.Info().Msg("enabling NAT") scopedLogger.Info().Msg("enabling NAT")
err := enableNat(sourceAddr, s.interfaceName, s.IPv4String()) err := enableNat(sourceAddr, s.interfaceName, s.IPv4String())
if err != nil { if err != nil {
s.l.Error().Err(err).Msg("failed to enable NAT") s.l.Error().Err(err).Msg("failed to enable NAT")
} }
} else { s.natEnabled = true
scopedLogger.Info().Msg("disabling NAT") return nil
err := disableNat()
if err != nil {
s.l.Error().Err(err).Msg("failed to disable NAT")
}
} }
return nil return nil
} }
@ -63,4 +71,4 @@ func disableNat() error {
} }
return nil return nil
} }

View File

@ -37,6 +37,8 @@ type NetworkInterfaceState struct {
onInitialCheck func(state *NetworkInterfaceState) onInitialCheck func(state *NetworkInterfaceState)
cbConfigChange func(config *NetworkConfig) cbConfigChange func(config *NetworkConfig)
natEnabled bool
checked bool checked bool
} }
@ -50,6 +52,7 @@ type NetworkInterfaceOptions struct {
OnDhcpLeaseChange func(lease *udhcpc.Lease) OnDhcpLeaseChange func(lease *udhcpc.Lease)
OnConfigChange func(config *NetworkConfig) OnConfigChange func(config *NetworkConfig)
NetworkConfig *NetworkConfig NetworkConfig *NetworkConfig
UsbNetworkConfig *UsbNetworkConfig
} }
func NewNetworkInterfaceState(opts *NetworkInterfaceOptions) (*NetworkInterfaceState, error) { func NewNetworkInterfaceState(opts *NetworkInterfaceOptions) (*NetworkInterfaceState, error) {
@ -73,15 +76,15 @@ func NewNetworkInterfaceState(opts *NetworkInterfaceOptions) (*NetworkInterfaceS
stateLock: sync.Mutex{}, stateLock: sync.Mutex{},
l: l, l: l,
onStateChange: func(s *NetworkInterfaceState) { onStateChange: func(s *NetworkInterfaceState) {
s.reconfigureNat(true, "172.16.55.0/24") s.reconfigureNat(opts.NetworkConfig.NatEnable, opts.UsbNetworkConfig.IPv4Network)
opts.OnStateChange(s) opts.OnStateChange(s)
}, },
onInitialCheck: func(s *NetworkInterfaceState) { onInitialCheck: func(s *NetworkInterfaceState) {
s.reconfigureNat(true, "172.16.55.0/24") s.reconfigureNat(opts.NetworkConfig.NatEnable, opts.UsbNetworkConfig.IPv4Network)
opts.OnInitialCheck(s) opts.OnInitialCheck(s)
}, },
cbConfigChange: opts.OnConfigChange, cbConfigChange: opts.OnConfigChange,
config: opts.NetworkConfig, config: opts.NetworkConfig,
} }
// create the dhcp client // create the dhcp client

View File

@ -32,10 +32,11 @@ func initNetwork() error {
ensureConfigLoaded() ensureConfigLoaded()
state, err := network.NewNetworkInterfaceState(&network.NetworkInterfaceOptions{ state, err := network.NewNetworkInterfaceState(&network.NetworkInterfaceOptions{
DefaultHostname: GetDefaultHostname(), DefaultHostname: GetDefaultHostname(),
InterfaceName: NetIfName, InterfaceName: NetIfName,
NetworkConfig: config.NetworkConfig, NetworkConfig: config.NetworkConfig,
Logger: networkLogger, UsbNetworkConfig: config.UsbNetworkConfig,
Logger: networkLogger,
OnStateChange: func(state *network.NetworkInterfaceState) { OnStateChange: func(state *network.NetworkInterfaceState) {
networkStateChanged() networkStateChanged()
}, },