mirror of https://github.com/jetkvm/kvm.git
fix: mDNS options
This commit is contained in:
parent
775b0f1049
commit
02382e4632
|
|
@ -146,14 +146,17 @@ func (m *MDNS) start(allowRestart bool) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// Start starts the mDNS server
|
||||
func (m *MDNS) Start() error {
|
||||
return m.start(false)
|
||||
}
|
||||
|
||||
// Restart restarts the mDNS server
|
||||
func (m *MDNS) Restart() error {
|
||||
return m.start(true)
|
||||
}
|
||||
|
||||
// Stop stops the mDNS server
|
||||
func (m *MDNS) Stop() error {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
|
|
@ -165,26 +168,46 @@ func (m *MDNS) Stop() error {
|
|||
return m.conn.Close()
|
||||
}
|
||||
|
||||
func (m *MDNS) SetLocalNames(localNames []string, always bool) error {
|
||||
if reflect.DeepEqual(m.localNames, localNames) && !always {
|
||||
return nil
|
||||
func (m *MDNS) setLocalNames(localNames []string) {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
|
||||
if reflect.DeepEqual(m.localNames, localNames) {
|
||||
return
|
||||
}
|
||||
|
||||
m.localNames = localNames
|
||||
_ = m.Restart()
|
||||
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
func (m *MDNS) SetListenOptions(listenOptions *MDNSListenOptions) error {
|
||||
func (m *MDNS) setListenOptions(listenOptions *MDNSListenOptions) {
|
||||
m.lock.Lock()
|
||||
defer m.lock.Unlock()
|
||||
|
||||
if m.listenOptions != nil &&
|
||||
m.listenOptions.IPv4 == listenOptions.IPv4 &&
|
||||
m.listenOptions.IPv6 == listenOptions.IPv6 {
|
||||
return nil
|
||||
return
|
||||
}
|
||||
|
||||
m.listenOptions = listenOptions
|
||||
_ = m.Restart()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetLocalNames sets the local names and restarts the mDNS server
|
||||
func (m *MDNS) SetLocalNames(localNames []string) error {
|
||||
m.setLocalNames(localNames)
|
||||
return m.Restart()
|
||||
}
|
||||
|
||||
// SetListenOptions sets the listen options and restarts the mDNS server
|
||||
func (m *MDNS) SetListenOptions(listenOptions *MDNSListenOptions) error {
|
||||
m.setListenOptions(listenOptions)
|
||||
return m.Restart()
|
||||
}
|
||||
|
||||
// SetOptions sets the local names and listen options and restarts the mDNS server
|
||||
func (m *MDNS) SetOptions(options *MDNSOptions) error {
|
||||
m.setLocalNames(options.LocalNames)
|
||||
m.setListenOptions(options.ListenOptions)
|
||||
return m.Restart()
|
||||
}
|
||||
|
|
|
|||
18
mdns.go
18
mdns.go
|
|
@ -1,23 +1,23 @@
|
|||
package kvm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/jetkvm/kvm/internal/mdns"
|
||||
)
|
||||
|
||||
var mDNS *mdns.MDNS
|
||||
|
||||
func initMdns() error {
|
||||
options := getMdnsOptions()
|
||||
if options == nil {
|
||||
return fmt.Errorf("failed to get mDNS options")
|
||||
}
|
||||
|
||||
m, err := mdns.NewMDNS(&mdns.MDNSOptions{
|
||||
Logger: logger,
|
||||
LocalNames: []string{
|
||||
"jetkvm", "jetkvm.local",
|
||||
// networkManager.GetHostname(),
|
||||
// networkManager.GetFQDN(),
|
||||
},
|
||||
ListenOptions: &mdns.MDNSListenOptions{
|
||||
IPv4: config.NetworkConfig.MDNSMode.String != "disabled",
|
||||
IPv6: config.NetworkConfig.MDNSMode.String != "disabled",
|
||||
},
|
||||
LocalNames: options.LocalNames,
|
||||
ListenOptions: options.ListenOptions,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
|||
45
network.go
45
network.go
|
|
@ -32,19 +32,47 @@ func toRpcNetworkSettings(config *types.NetworkConfig) *RpcNetworkSettings {
|
|||
}
|
||||
}
|
||||
|
||||
func getMdnsOptions() *mdns.MDNSOptions {
|
||||
if networkManager == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var ipv4, ipv6 bool
|
||||
switch config.NetworkConfig.MDNSMode.String {
|
||||
case "auto":
|
||||
ipv4 = true
|
||||
ipv6 = true
|
||||
case "ipv4_only":
|
||||
ipv4 = true
|
||||
case "ipv6_only":
|
||||
ipv6 = true
|
||||
}
|
||||
|
||||
return &mdns.MDNSOptions{
|
||||
LocalNames: []string{
|
||||
networkManager.Hostname(),
|
||||
networkManager.FQDN(),
|
||||
},
|
||||
ListenOptions: &mdns.MDNSListenOptions{
|
||||
IPv4: ipv4,
|
||||
IPv6: ipv6,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func restartMdns() {
|
||||
if mDNS == nil {
|
||||
return
|
||||
}
|
||||
|
||||
_ = mDNS.SetListenOptions(&mdns.MDNSListenOptions{
|
||||
IPv4: config.NetworkConfig.MDNSMode.String != "disabled",
|
||||
IPv6: config.NetworkConfig.MDNSMode.String != "disabled",
|
||||
})
|
||||
_ = mDNS.SetLocalNames([]string{
|
||||
networkManager.Hostname(),
|
||||
networkManager.FQDN(),
|
||||
}, true)
|
||||
options := getMdnsOptions()
|
||||
if options == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err := mDNS.SetOptions(options); err != nil {
|
||||
networkLogger.Error().Err(err).Msg("failed to restart mDNS")
|
||||
}
|
||||
}
|
||||
|
||||
func triggerTimeSyncOnNetworkStateChange() {
|
||||
|
|
@ -115,6 +143,7 @@ func initNetwork() error {
|
|||
nc := config.NetworkConfig
|
||||
|
||||
nm := nmlite.NewNetworkManager(context.Background(), networkLogger)
|
||||
networkLogger.Info().Interface("networkConfig", nc).Str("hostname", nc.Hostname.String).Str("domain", nc.Domain.String).Msg("initializing network manager")
|
||||
_ = setHostname(nm, nc.Hostname.String, nc.Domain.String)
|
||||
nm.SetOnInterfaceStateChange(networkStateChanged)
|
||||
if err := nm.AddInterface(NetIfName, nc); err != nil {
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ func (hm *ResolvConfManager) getDomain() string {
|
|||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
return "local"
|
||||
}
|
||||
|
||||
func (hm *ResolvConfManager) reconcileHostname() error {
|
||||
|
|
|
|||
|
|
@ -163,7 +163,10 @@ func (nm *NetworkManager) GetInterfaceState(iface string) (*types.InterfaceState
|
|||
return nil, err
|
||||
}
|
||||
|
||||
return im.GetState(), nil
|
||||
state := im.GetState()
|
||||
state.Hostname = nm.Hostname()
|
||||
|
||||
return state, nil
|
||||
}
|
||||
|
||||
// GetInterfaceConfig returns the current configuration of a specific interface
|
||||
|
|
|
|||
Loading…
Reference in New Issue