mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #1613 from thijsterlouw/proper_resolvconf_parsing
Proper resolv.conf parsing
This commit is contained in:
commit
84431ec03c
2 changed files with 29 additions and 3 deletions
|
@ -781,21 +781,37 @@ func GetResolvConf() ([]byte, error) {
|
|||
// CheckLocalDns looks into the /etc/resolv.conf,
|
||||
// it returns true if there is a local nameserver or if there is no nameserver.
|
||||
func CheckLocalDns(resolvConf []byte) bool {
|
||||
if !bytes.Contains(resolvConf, []byte("nameserver")) {
|
||||
var parsedResolvConf = StripComments(resolvConf, []byte("#"))
|
||||
if !bytes.Contains(parsedResolvConf, []byte("nameserver")) {
|
||||
return true
|
||||
}
|
||||
|
||||
for _, ip := range [][]byte{
|
||||
[]byte("127.0.0.1"),
|
||||
[]byte("127.0.1.1"),
|
||||
} {
|
||||
if bytes.Contains(resolvConf, ip) {
|
||||
if bytes.Contains(parsedResolvConf, ip) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// StripComments parses input into lines and strips away comments.
|
||||
func StripComments(input []byte, commentMarker []byte) []byte {
|
||||
lines := bytes.Split(input, []byte("\n"))
|
||||
var output []byte
|
||||
for _, currentLine := range lines {
|
||||
var commentIndex = bytes.Index(currentLine, commentMarker)
|
||||
if ( commentIndex == -1 ) {
|
||||
output = append(output, currentLine...)
|
||||
} else {
|
||||
output = append(output, currentLine[:commentIndex]...)
|
||||
}
|
||||
output = append(output, []byte("\n")...)
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
func ParseHost(host string, port int, addr string) string {
|
||||
if strings.HasPrefix(addr, "unix://") {
|
||||
return addr
|
||||
|
|
|
@ -323,6 +323,16 @@ func TestCheckLocalDns(t *testing.T) {
|
|||
nameserver 10.0.2.3
|
||||
search dotcloud.net`: false,
|
||||
`# Dynamic
|
||||
#nameserver 127.0.0.1
|
||||
nameserver 10.0.2.3
|
||||
search dotcloud.net`: false,
|
||||
`# Dynamic
|
||||
nameserver 10.0.2.3 #not used 127.0.1.1
|
||||
search dotcloud.net`: false,
|
||||
`# Dynamic
|
||||
#nameserver 10.0.2.3
|
||||
#search dotcloud.net`: true,
|
||||
`# Dynamic
|
||||
nameserver 127.0.0.1
|
||||
search dotcloud.net`: true,
|
||||
`# Dynamic
|
||||
|
|
Loading…
Reference in a new issue