-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add dns os cache clearing to avoid cached high ttl negative responses
- Loading branch information
1 parent
50a2048
commit fb70471
Showing
7 changed files
with
135 additions
and
37 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,57 @@ | ||
package net | ||
|
||
import ( | ||
"os/exec" | ||
"runtime" | ||
) | ||
|
||
// DNSOSCache represents DNS caching system that Operating System configures. | ||
type DNSOSCache struct { | ||
logger Logger | ||
logTag string | ||
} | ||
|
||
func NewDNSOSCache(logger Logger) DNSOSCache { | ||
return DNSOSCache{logger, "dns.DNSOSCache"} | ||
} | ||
|
||
func (c DNSOSCache) Flush() { | ||
switch runtime.GOOS { | ||
case "darwin": | ||
// Most OS Xs have mDNSResponder which caches entries going thru native DNS resolution. | ||
// If cache isnt cleared before our own DNS resolution takes over, following case may | ||
// happen (inability to resolve addresses that were "negatively" cached): | ||
// - before starting kwt, resolve 'foo.test' | ||
// - mDNSResponder will cache negative result with a very high TTL | ||
// because foo.test isnt typically resolvable | ||
// - start kwt net start --dns-map test=127.0.0.1 | ||
// - resolve 'foo.test' again, expecting 127.0.0.1 | ||
// - via dig it works because it bypasses OS X resolution | ||
// - via curl it does not work since negative result is still cached by OS X | ||
// See mDNSResponder's internal cache via: | ||
// $ log stream --predicate 'process == "mDNSResponder"' --info | ||
// $ sudo killall -INFO mDNSResponder | ||
c.flushOSX() | ||
|
||
default: | ||
c.logger.Debug(c.logTag, "Skipping clearing of OS DNS cache") | ||
} | ||
} | ||
|
||
func (c DNSOSCache) flushOSX() { | ||
out, err := exec.Command("killall", "-HUP", "mDNSResponder").CombinedOutput() | ||
if err != nil { | ||
c.logger.Debug(c.logTag, "Failed clearing mDNSResponder cache: %s (output: %s)", err, out) | ||
} else { | ||
c.logger.Debug(c.logTag, "Successfully cleared via mDNSResponder") | ||
return | ||
} | ||
|
||
// Try flushing Directory Service cache which may on some versions of OS X do the trick | ||
out, err = exec.Command("discoveryutil", "udnsflushcaches").CombinedOutput() | ||
if err != nil { | ||
c.logger.Debug(c.logTag, "Failed clearing via discoveryutil: %s (output: %s)", err, out) | ||
} else { | ||
c.logger.Debug(c.logTag, "Successfully cleared via discoveryutil") | ||
} | ||
} |
This file contains 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 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