2022-11-23 13:39:42 +01:00
|
|
|
package dns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2023-02-13 15:25:11 +01:00
|
|
|
|
|
|
|
nbdns "github.com/netbirdio/netbird/dns"
|
2022-11-23 13:39:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type hostManager interface {
|
|
|
|
applyDNSConfig(config hostDNSConfig) error
|
|
|
|
restoreHostDNS() error
|
2023-05-17 00:03:26 +02:00
|
|
|
supportCustomPort() bool
|
2022-11-23 13:39:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type hostDNSConfig struct {
|
|
|
|
domains []domainConfig
|
|
|
|
routeAll bool
|
|
|
|
serverIP string
|
|
|
|
serverPort int
|
|
|
|
}
|
|
|
|
|
|
|
|
type domainConfig struct {
|
2023-02-13 15:25:11 +01:00
|
|
|
disabled bool
|
2022-11-23 13:39:42 +01:00
|
|
|
domain string
|
|
|
|
matchOnly bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockHostConfigurator struct {
|
2023-05-17 00:03:26 +02:00
|
|
|
applyDNSConfigFunc func(config hostDNSConfig) error
|
|
|
|
restoreHostDNSFunc func() error
|
|
|
|
supportCustomPortFunc func() bool
|
2022-11-23 13:39:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockHostConfigurator) applyDNSConfig(config hostDNSConfig) error {
|
|
|
|
if m.applyDNSConfigFunc != nil {
|
|
|
|
return m.applyDNSConfigFunc(config)
|
|
|
|
}
|
|
|
|
return fmt.Errorf("method applyDNSSettings is not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mockHostConfigurator) restoreHostDNS() error {
|
|
|
|
if m.restoreHostDNSFunc != nil {
|
|
|
|
return m.restoreHostDNSFunc()
|
|
|
|
}
|
|
|
|
return fmt.Errorf("method restoreHostDNS is not implemented")
|
|
|
|
}
|
|
|
|
|
2023-05-17 00:03:26 +02:00
|
|
|
func (m *mockHostConfigurator) supportCustomPort() bool {
|
|
|
|
if m.supportCustomPortFunc != nil {
|
|
|
|
return m.supportCustomPortFunc()
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-11-23 13:39:42 +01:00
|
|
|
func newNoopHostMocker() hostManager {
|
|
|
|
return &mockHostConfigurator{
|
2023-05-17 00:03:26 +02:00
|
|
|
applyDNSConfigFunc: func(config hostDNSConfig) error { return nil },
|
|
|
|
restoreHostDNSFunc: func() error { return nil },
|
|
|
|
supportCustomPortFunc: func() bool { return true },
|
2022-11-23 13:39:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func dnsConfigToHostDNSConfig(dnsConfig nbdns.Config, ip string, port int) hostDNSConfig {
|
|
|
|
config := hostDNSConfig{
|
|
|
|
routeAll: false,
|
|
|
|
serverIP: ip,
|
|
|
|
serverPort: port,
|
|
|
|
}
|
|
|
|
for _, nsConfig := range dnsConfig.NameServerGroups {
|
2023-02-13 15:25:11 +01:00
|
|
|
if len(nsConfig.NameServers) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2022-11-23 13:39:42 +01:00
|
|
|
if nsConfig.Primary {
|
|
|
|
config.routeAll = true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, domain := range nsConfig.Domains {
|
|
|
|
config.domains = append(config.domains, domainConfig{
|
|
|
|
domain: strings.TrimSuffix(domain, "."),
|
2023-10-19 19:32:42 +02:00
|
|
|
matchOnly: !nsConfig.SearchDomainsEnabled,
|
2022-11-23 13:39:42 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, customZone := range dnsConfig.CustomZones {
|
|
|
|
config.domains = append(config.domains, domainConfig{
|
|
|
|
domain: strings.TrimSuffix(customZone.Domain, "."),
|
|
|
|
matchOnly: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|