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 {
CloudURL string `json:"cloud_url"`
CloudAppURL string `json:"cloud_app_url"`
CloudToken string `json:"cloud_token"`
GoogleIdentity string `json:"google_identity"`
JigglerEnabled bool `json:"jiggler_enabled"`
AutoUpdateEnabled bool `json:"auto_update_enabled"`
IncludePreRelease bool `json:"include_pre_release"`
HashedPassword string `json:"hashed_password"`
LocalAuthToken string `json:"local_auth_token"`
LocalAuthMode string `json:"localAuthMode"` //TODO: fix it with migration
WakeOnLanDevices []WakeOnLanDevice `json:"wake_on_lan_devices"`
KeyboardMacros []KeyboardMacro `json:"keyboard_macros"`
EdidString string `json:"hdmi_edid_string"`
ActiveExtension string `json:"active_extension"`
DisplayMaxBrightness int `json:"display_max_brightness"`
DisplayDimAfterSec int `json:"display_dim_after_sec"`
DisplayOffAfterSec int `json:"display_off_after_sec"`
TLSMode string `json:"tls_mode"` // options: "self-signed", "user-defined", ""
UsbConfig *usbgadget.Config `json:"usb_config"`
UsbDevices *usbgadget.Devices `json:"usb_devices"`
NetworkConfig *network.NetworkConfig `json:"network_config"`
DefaultLogLevel string `json:"default_log_level"`
CloudURL string `json:"cloud_url"`
CloudAppURL string `json:"cloud_app_url"`
CloudToken string `json:"cloud_token"`
GoogleIdentity string `json:"google_identity"`
JigglerEnabled bool `json:"jiggler_enabled"`
AutoUpdateEnabled bool `json:"auto_update_enabled"`
IncludePreRelease bool `json:"include_pre_release"`
HashedPassword string `json:"hashed_password"`
LocalAuthToken string `json:"local_auth_token"`
LocalAuthMode string `json:"localAuthMode"` //TODO: fix it with migration
WakeOnLanDevices []WakeOnLanDevice `json:"wake_on_lan_devices"`
KeyboardMacros []KeyboardMacro `json:"keyboard_macros"`
EdidString string `json:"hdmi_edid_string"`
ActiveExtension string `json:"active_extension"`
DisplayMaxBrightness int `json:"display_max_brightness"`
DisplayDimAfterSec int `json:"display_dim_after_sec"`
DisplayOffAfterSec int `json:"display_off_after_sec"`
TLSMode string `json:"tls_mode"` // options: "self-signed", "user-defined", ""
UsbConfig *usbgadget.Config `json:"usb_config"`
UsbDevices *usbgadget.Devices `json:"usb_devices"`
NetworkConfig *network.NetworkConfig `json:"network_config"`
UsbNetworkConfig *network.UsbNetworkConfig `json:"usb_network_config"`
DefaultLogLevel string `json:"default_log_level"`
}
const configPath = "/userdata/kvm_config.json"
@ -128,7 +129,13 @@ var defaultConfig = &Config{
EthernetNcm: 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",
}

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"`
TimeSyncDisableFallback null.Bool `json:"time_sync_disable_fallback,omitempty" default:"false"`
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 {

View File

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

View File

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

View File

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