-
-
Notifications
You must be signed in to change notification settings - Fork 133
feat: add macOS SystemConfiguration DNS support and internal strategy #227
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,222 @@ | ||
| // +build darwin | ||
|
|
||
| package config | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "net" | ||
| "os/exec" | ||
| "regexp" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/miekg/dns" | ||
| ) | ||
|
|
||
| // scutilResolver represents a parsed resolver from scutil --dns output | ||
| type scutilResolver struct { | ||
| number int | ||
| nameservers []string | ||
| domain string | ||
| searchDomains []string | ||
| options []string | ||
| ifIndex string | ||
| flags string | ||
phrawzty marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // GetDefaultServers retrieves DNS configuration from macOS SystemConfiguration | ||
| // by parsing the output of 'scutil --dns'. Falls back to /etc/resolv.conf on failure. | ||
| func GetDefaultServers() ([]string, int, []string, error) { | ||
| // Try scutil first | ||
| resolvers, ndots, search, err := getResolversFromScutil() | ||
| if err != nil { | ||
| // Fallback to /etc/resolv.conf | ||
| return fallbackToResolvConf() | ||
| } | ||
|
|
||
| return resolvers, ndots, search, nil | ||
| } | ||
|
|
||
| // getResolversFromScutil executes scutil --dns and parses the output | ||
| func getResolversFromScutil() ([]string, int, []string, error) { | ||
| // Execute scutil --dns | ||
| cmd := exec.Command("scutil", "--dns") | ||
| var stdout, stderr bytes.Buffer | ||
| cmd.Stdout = &stdout | ||
| cmd.Stderr = &stderr | ||
phrawzty marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if err := cmd.Run(); err != nil { | ||
| return nil, 0, nil, fmt.Errorf("scutil execution failed: %w", err) | ||
| } | ||
|
|
||
| output := stdout.String() | ||
| if len(strings.TrimSpace(output)) == 0 { | ||
| return nil, 0, nil, fmt.Errorf("scutil returned empty output") | ||
| } | ||
|
|
||
| // Parse the output | ||
| resolvers, err := parseScutilOutput(output) | ||
| if err != nil { | ||
| return nil, 0, nil, fmt.Errorf("failed to parse scutil output: %w", err) | ||
| } | ||
|
|
||
| // Filter out mDNS resolvers | ||
| validResolvers := make([]scutilResolver, 0) | ||
| for _, r := range resolvers { | ||
| if !isMDNS(r) && len(r.nameservers) > 0 { | ||
| validResolvers = append(validResolvers, r) | ||
| } | ||
| } | ||
|
|
||
| if len(validResolvers) == 0 { | ||
| return nil, 0, nil, fmt.Errorf("no valid resolvers found") | ||
| } | ||
|
|
||
| // Aggregate nameservers from all valid resolvers | ||
| // This allows the "internal" strategy to find domain-specific corporate DNS servers | ||
| nameservers := make([]string, 0) | ||
| seen := make(map[string]bool) | ||
|
|
||
| for _, resolver := range validResolvers { | ||
| for _, ns := range resolver.nameservers { | ||
| ip := net.ParseIP(ns) | ||
| // Skip link-local and duplicates | ||
| if isUnicastLinkLocal(ip) || seen[ns] { | ||
| continue | ||
| } | ||
| nameservers = append(nameservers, ns) | ||
| seen[ns] = true | ||
| } | ||
| } | ||
|
|
||
| // Aggregate search domains from all valid resolvers | ||
| searchDomains := aggregateSearchDomains(validResolvers) | ||
|
|
||
| // ndots: try to read from /etc/resolv.conf, default to 1 | ||
| ndots := 1 | ||
| if cfg, err := dns.ClientConfigFromFile("/etc/resolv.conf"); err == nil { | ||
| ndots = cfg.Ndots | ||
| } | ||
|
|
||
| return nameservers, ndots, searchDomains, nil | ||
| } | ||
|
|
||
| // parseScutilOutput parses the output of scutil --dns | ||
| func parseScutilOutput(output string) ([]scutilResolver, error) { | ||
| lines := strings.Split(output, "\n") | ||
| resolvers := make([]scutilResolver, 0) | ||
|
|
||
| var current *scutilResolver | ||
| resolverRe := regexp.MustCompile(`^resolver #(\d+)`) | ||
| nameserverRe := regexp.MustCompile(`^\s+nameserver\[\d+\]\s*:\s*(.+)`) | ||
| domainRe := regexp.MustCompile(`^\s+domain\s*:\s*(.+)`) | ||
| searchDomainRe := regexp.MustCompile(`^\s+search domain\[\d+\]\s*:\s*(.+)`) | ||
| optionsRe := regexp.MustCompile(`^\s+options\s*:\s*(.+)`) | ||
|
|
||
| for _, line := range lines { | ||
| // Check for resolver start | ||
| if matches := resolverRe.FindStringSubmatch(line); matches != nil { | ||
| if current != nil { | ||
| resolvers = append(resolvers, *current) | ||
| } | ||
| num, _ := strconv.Atoi(matches[1]) | ||
| current = &scutilResolver{ | ||
| number: num, | ||
| nameservers: make([]string, 0), | ||
| searchDomains: make([]string, 0), | ||
| options: make([]string, 0), | ||
| } | ||
| continue | ||
| } | ||
|
|
||
| if current == nil { | ||
| continue | ||
| } | ||
|
|
||
| // Parse nameserver | ||
| if matches := nameserverRe.FindStringSubmatch(line); matches != nil { | ||
| current.nameservers = append(current.nameservers, strings.TrimSpace(matches[1])) | ||
| continue | ||
| } | ||
|
|
||
| // Parse domain | ||
| if matches := domainRe.FindStringSubmatch(line); matches != nil { | ||
| current.domain = strings.TrimSpace(matches[1]) | ||
| continue | ||
| } | ||
|
|
||
| // Parse search domain | ||
| if matches := searchDomainRe.FindStringSubmatch(line); matches != nil { | ||
| current.searchDomains = append(current.searchDomains, strings.TrimSpace(matches[1])) | ||
| continue | ||
| } | ||
|
|
||
| // Parse options | ||
| if matches := optionsRe.FindStringSubmatch(line); matches != nil { | ||
| opts := strings.Fields(strings.TrimSpace(matches[1])) | ||
| current.options = append(current.options, opts...) | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| // Don't forget the last resolver | ||
| if current != nil { | ||
| resolvers = append(resolvers, *current) | ||
| } | ||
|
|
||
| return resolvers, nil | ||
| } | ||
|
|
||
| // isMDNS checks if a resolver is for mDNS (.local) | ||
| func isMDNS(r scutilResolver) bool { | ||
| for _, opt := range r.options { | ||
| if opt == "mdns" { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // aggregateSearchDomains collects search domains from all resolvers | ||
| func aggregateSearchDomains(resolvers []scutilResolver) []string { | ||
| seen := make(map[string]bool) | ||
| result := make([]string, 0) | ||
|
|
||
| for _, r := range resolvers { | ||
| // Add domain if present | ||
| if r.domain != "" && !seen[r.domain] { | ||
| result = append(result, r.domain) | ||
| seen[r.domain] = true | ||
| } | ||
|
|
||
| // Add search domains | ||
| for _, sd := range r.searchDomains { | ||
| if !seen[sd] { | ||
| result = append(result, sd) | ||
| seen[sd] = true | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return result | ||
| } | ||
|
|
||
| // fallbackToResolvConf falls back to the traditional /etc/resolv.conf | ||
| func fallbackToResolvConf() ([]string, int, []string, error) { | ||
| cfg, err := dns.ClientConfigFromFile("/etc/resolv.conf") | ||
| if err != nil { | ||
| return nil, 0, nil, err | ||
| } | ||
|
|
||
| servers := make([]string, 0) | ||
| for _, server := range cfg.Servers { | ||
| ip := net.ParseIP(server) | ||
| if isUnicastLinkLocal(ip) { | ||
| continue | ||
| } | ||
| servers = append(servers, server) | ||
| } | ||
|
|
||
| return servers, cfg.Ndots, cfg.Search, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| // +build !windows | ||
| // +build !windows,!darwin | ||
|
|
||
| package config | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.