-
-
Notifications
You must be signed in to change notification settings - Fork 976
Add Darwin multi-server support for local DNS settings #4837
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -79,10 +79,11 @@ func (s *systemConfigurator) applyDNSConfig(config HostDNSConfig, stateManager * | |||||||||
| searchDomains = append(searchDomains, strings.TrimSuffix(""+dConf.Domain, ".")) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| configServerIPs := []netip.Addr{config.ServerIP} | ||||||||||
| matchKey := getKeyWithInput(netbirdDNSStateKeyFormat, matchSuffix) | ||||||||||
| var err error | ||||||||||
| if len(matchDomains) != 0 { | ||||||||||
| err = s.addMatchDomains(matchKey, strings.Join(matchDomains, " "), config.ServerIP, config.ServerPort) | ||||||||||
| err = s.addMatchDomains(matchKey, strings.Join(matchDomains, " "), configServerIPs, config.ServerPort) | ||||||||||
| } else { | ||||||||||
| log.Infof("removing match domains from the system") | ||||||||||
| err = s.removeKeyFromSystemConfig(matchKey) | ||||||||||
|
|
@@ -94,7 +95,7 @@ func (s *systemConfigurator) applyDNSConfig(config HostDNSConfig, stateManager * | |||||||||
|
|
||||||||||
| searchKey := getKeyWithInput(netbirdDNSStateKeyFormat, searchSuffix) | ||||||||||
| if len(searchDomains) != 0 { | ||||||||||
| err = s.addSearchDomains(searchKey, strings.Join(searchDomains, " "), config.ServerIP, config.ServerPort) | ||||||||||
| err = s.addSearchDomains(searchKey, strings.Join(searchDomains, " "), configServerIPs, config.ServerPort) | ||||||||||
| } else { | ||||||||||
| log.Infof("removing search domains from the system") | ||||||||||
| err = s.removeKeyFromSystemConfig(searchKey) | ||||||||||
|
|
@@ -168,20 +169,20 @@ func (s *systemConfigurator) removeKeyFromSystemConfig(key string) error { | |||||||||
| } | ||||||||||
|
|
||||||||||
| func (s *systemConfigurator) addLocalDNS() error { | ||||||||||
| if !s.systemDNSSettings.ServerIP.IsValid() || len(s.systemDNSSettings.Domains) == 0 { | ||||||||||
| if len(s.systemDNSSettings.ServerIPs) == 0 || len(s.systemDNSSettings.Domains) == 0 { | ||||||||||
| if err := s.recordSystemDNSSettings(true); err != nil { | ||||||||||
| return fmt.Errorf("recordSystemDNSSettings(): %w", err) | ||||||||||
| } | ||||||||||
| } | ||||||||||
| localKey := getKeyWithInput(netbirdDNSStateKeyFormat, localSuffix) | ||||||||||
| if !s.systemDNSSettings.ServerIP.IsValid() || len(s.systemDNSSettings.Domains) == 0 { | ||||||||||
| if len(s.systemDNSSettings.ServerIPs) == 0 || len(s.systemDNSSettings.Domains) == 0 { | ||||||||||
| log.Info("Not enabling local DNS server") | ||||||||||
| return nil | ||||||||||
| } | ||||||||||
|
|
||||||||||
| if err := s.addSearchDomains( | ||||||||||
| localKey, | ||||||||||
| strings.Join(s.systemDNSSettings.Domains, " "), s.systemDNSSettings.ServerIP, s.systemDNSSettings.ServerPort, | ||||||||||
| strings.Join(s.systemDNSSettings.Domains, " "), s.systemDNSSettings.ServerIPs, s.systemDNSSettings.ServerPort, | ||||||||||
| ); err != nil { | ||||||||||
| return fmt.Errorf("add search domains: %w", err) | ||||||||||
| } | ||||||||||
|
|
@@ -190,7 +191,7 @@ func (s *systemConfigurator) addLocalDNS() error { | |||||||||
| } | ||||||||||
|
|
||||||||||
| func (s *systemConfigurator) recordSystemDNSSettings(force bool) error { | ||||||||||
| if s.systemDNSSettings.ServerIP.IsValid() && len(s.systemDNSSettings.Domains) != 0 && !force { | ||||||||||
| if len(s.systemDNSSettings.ServerIPs) != 0 && len(s.systemDNSSettings.Domains) != 0 && !force { | ||||||||||
| return nil | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -203,31 +204,22 @@ func (s *systemConfigurator) recordSystemDNSSettings(force bool) error { | |||||||||
| return nil | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func (s *systemConfigurator) getSystemDNSSettings() (SystemDNSSettings, error) { | ||||||||||
| primaryServiceKey, _, err := s.getPrimaryService() | ||||||||||
| if err != nil || primaryServiceKey == "" { | ||||||||||
| return SystemDNSSettings{}, fmt.Errorf("couldn't find the primary service key: %w", err) | ||||||||||
| } | ||||||||||
| dnsServiceKey := getKeyWithInput(primaryServiceStateKeyFormat, primaryServiceKey) | ||||||||||
| line := buildCommandLine("show", dnsServiceKey, "") | ||||||||||
| stdinCommands := wrapCommand(line) | ||||||||||
|
|
||||||||||
| b, err := runSystemConfigCommand(stdinCommands) | ||||||||||
| if err != nil { | ||||||||||
| return SystemDNSSettings{}, fmt.Errorf("sending the command: %w", err) | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func parseSystemConfigOutput(output []byte) (SystemDNSSettings, error) { | ||||||||||
| var dnsSettings SystemDNSSettings | ||||||||||
| localDomainsMap := make(map[string]struct{}) | ||||||||||
| inSearchDomainsArray := false | ||||||||||
| inServerAddressesArray := false | ||||||||||
|
|
||||||||||
| scanner := bufio.NewScanner(bytes.NewReader(b)) | ||||||||||
| scanner := bufio.NewScanner(bytes.NewReader(output)) | ||||||||||
| for scanner.Scan() { | ||||||||||
| line := strings.TrimSpace(scanner.Text()) | ||||||||||
| switch { | ||||||||||
| case strings.HasPrefix(line, "DomainName :"): | ||||||||||
| domainName := strings.TrimSpace(strings.Split(line, ":")[1]) | ||||||||||
| dnsSettings.Domains = append(dnsSettings.Domains, domainName) | ||||||||||
| if _, exists := localDomainsMap[domainName]; !exists { | ||||||||||
| localDomainsMap[domainName] = struct{}{} | ||||||||||
| dnsSettings.Domains = append(dnsSettings.Domains, domainName) | ||||||||||
| } | ||||||||||
|
Comment on lines
217
to
+222
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Add case-insensitive domain deduplication. The current deduplication logic is case-sensitive, but DNS domain names are case-insensitive. For example, "Example.com" and "example.com" should be treated as the same domain. Apply this diff to normalize domains to lowercase before deduplication: case strings.HasPrefix(line, "DomainName :"):
- domainName := strings.TrimSpace(strings.Split(line, ":")[1])
+ domainName := strings.ToLower(strings.TrimSpace(strings.Split(line, ":")[1]))
if _, exists := localDomainsMap[domainName]; !exists {
localDomainsMap[domainName] = struct{}{}
dnsSettings.Domains = append(dnsSettings.Domains, domainName)
} if inSearchDomainsArray {
- searchDomain := strings.Split(line, " : ")[1]
+ searchDomain := strings.ToLower(strings.Split(line, " : ")[1])
if _, exists := localDomainsMap[searchDomain]; !exists {
localDomainsMap[searchDomain] = struct{}{}
dnsSettings.Domains = append(dnsSettings.Domains, searchDomain)
}Also applies to: 234-239 🤖 Prompt for AI Agents |
||||||||||
| case line == "SearchDomains : <array> {": | ||||||||||
| inSearchDomainsArray = true | ||||||||||
| continue | ||||||||||
|
|
@@ -241,12 +233,16 @@ func (s *systemConfigurator) getSystemDNSSettings() (SystemDNSSettings, error) { | |||||||||
|
|
||||||||||
| if inSearchDomainsArray { | ||||||||||
| searchDomain := strings.Split(line, " : ")[1] | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add bounds checking to prevent potential panics. Lines 235 and 241 use Apply this diff to add defensive bounds checking: if inSearchDomainsArray {
- searchDomain := strings.Split(line, " : ")[1]
+ parts := strings.Split(line, " : ")
+ if len(parts) < 2 {
+ continue
+ }
+ searchDomain := parts[1]
if _, exists := localDomainsMap[searchDomain]; !exists {
localDomainsMap[searchDomain] = struct{}{}
dnsSettings.Domains = append(dnsSettings.Domains, searchDomain)
}
} else if inServerAddressesArray {
- address := strings.Split(line, " : ")[1]
+ parts := strings.Split(line, " : ")
+ if len(parts) < 2 {
+ continue
+ }
+ address := parts[1]
if ip, err := netip.ParseAddr(address); err == nil {
if ip.IsValid() {
dnsSettings.ServerIPs = append(dnsSettings.ServerIPs, ip.Unmap())
}
}Also applies to: 241-241 🤖 Prompt for AI Agents |
||||||||||
| dnsSettings.Domains = append(dnsSettings.Domains, searchDomain) | ||||||||||
| if _, exists := localDomainsMap[searchDomain]; !exists { | ||||||||||
| localDomainsMap[searchDomain] = struct{}{} | ||||||||||
| dnsSettings.Domains = append(dnsSettings.Domains, searchDomain) | ||||||||||
| } | ||||||||||
| } else if inServerAddressesArray { | ||||||||||
| address := strings.Split(line, " : ")[1] | ||||||||||
| if ip, err := netip.ParseAddr(address); err == nil && ip.Is4() { | ||||||||||
| dnsSettings.ServerIP = ip.Unmap() | ||||||||||
| inServerAddressesArray = false // Stop reading after finding the first IPv4 address | ||||||||||
| if ip, err := netip.ParseAddr(address); err == nil { | ||||||||||
| if ip.IsValid() { | ||||||||||
| dnsSettings.ServerIPs = append(dnsSettings.ServerIPs, ip.Unmap()) | ||||||||||
| } | ||||||||||
|
Comment on lines
+243
to
+245
|
||||||||||
| if ip.IsValid() { | |
| dnsSettings.ServerIPs = append(dnsSettings.ServerIPs, ip.Unmap()) | |
| } | |
| dnsSettings.ServerIPs = append(dnsSettings.ServerIPs, ip.Unmap()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
checking twice falls inline with what was previously done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line attempts to access
config.ServerIPwhich no longer exists after the API change. TheHostDNSConfigstruct should have aServerIPorServerIPsfield that matches the new API. Verify thatHostDNSConfighas been updated accordingly, or this should use the correct field name.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is completely wrong on AI's part