2023-11-02 19:04:33 +01:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/client/internal/listener"
|
|
|
|
)
|
|
|
|
|
|
|
|
type notifier struct {
|
|
|
|
listener listener.NetworkChangeListener
|
|
|
|
listenerMux sync.Mutex
|
|
|
|
searchDomains []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newNotifier(initialSearchDomains []string) *notifier {
|
|
|
|
sort.Strings(initialSearchDomains)
|
|
|
|
return ¬ifier{
|
|
|
|
searchDomains: initialSearchDomains,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *notifier) setListener(listener listener.NetworkChangeListener) {
|
|
|
|
n.listenerMux.Lock()
|
|
|
|
defer n.listenerMux.Unlock()
|
|
|
|
n.listener = listener
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *notifier) onNewSearchDomains(searchDomains []string) {
|
|
|
|
sort.Strings(searchDomains)
|
|
|
|
|
|
|
|
if len(n.searchDomains) != len(searchDomains) {
|
|
|
|
n.searchDomains = searchDomains
|
|
|
|
n.notify()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if reflect.DeepEqual(n.searchDomains, searchDomains) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
n.searchDomains = searchDomains
|
|
|
|
n.notify()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *notifier) notify() {
|
|
|
|
n.listenerMux.Lock()
|
|
|
|
defer n.listenerMux.Unlock()
|
|
|
|
if n.listener == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func(l listener.NetworkChangeListener) {
|
2023-12-18 11:46:58 +01:00
|
|
|
l.OnNetworkChanged("")
|
2023-11-02 19:04:33 +01:00
|
|
|
}(n.listener)
|
|
|
|
}
|