package dnsimport ()// ClientConfig wraps the contents of the /etc/resolv.conf file.typeClientConfigstruct { Servers []string// servers to use Search []string// suffixes to append to local name Port string// what port to use Ndots int// number of dots in name to trigger absolute lookup Timeout int// seconds before giving up on packet Attempts int// lost packets before giving up on server, not used in the package dns}// ClientConfigFromFile parses a resolv.conf(5) like file and returns// a *ClientConfig.func ( string) (*ClientConfig, error) { , := os.Open()if != nil {returnnil, }defer .Close()returnClientConfigFromReader()}// ClientConfigFromReader works like ClientConfigFromFile but takes an io.Reader as argumentfunc ( io.Reader) (*ClientConfig, error) { := new(ClientConfig) := bufio.NewScanner() .Servers = make([]string, 0) .Search = make([]string, 0) .Port = "53" .Ndots = 1 .Timeout = 5 .Attempts = 2for .Scan() {if := .Err(); != nil {returnnil, } := .Text() := strings.Fields()iflen() < 1 {continue }switch [0] {case"nameserver": // add one name serveriflen() > 1 {// One more check: make sure server name is // just an IP address. Otherwise we need DNS // to look it up. := [1] .Servers = append(.Servers, ) }case"domain": // set search path to just this domainiflen() > 1 { .Search = make([]string, 1) .Search[0] = [1] } else { .Search = make([]string, 0) }case"search": // set search path to given servers .Search = cloneSlice([1:])case"options": // magic optionsfor , := range [1:] {switch {caselen() >= 6 && [:6] == "ndots:": , := strconv.Atoi([6:])if < 0 { = 0 } elseif > 15 { = 15 } .Ndots = caselen() >= 8 && [:8] == "timeout:": , := strconv.Atoi([8:])if < 1 { = 1 } .Timeout = caselen() >= 9 && [:9] == "attempts:": , := strconv.Atoi([9:])if < 1 { = 1 } .Attempts = case == "rotate":/* not imp */ } } } }return , nil}// NameList returns all of the names that should be queried based on the// config. It is based off of go's net/dns name building, but it does not// check the length of the resulting names.func ( *ClientConfig) ( string) []string {// if this domain is already fully qualified, no append needed.ifIsFqdn() {return []string{} }// Check to see if the name has more labels than Ndots. Do this before making // the domain fully qualified. := CountLabel() > .Ndots// Make the domain fully qualified. = Fqdn()// Make a list of names based off search. := []string{}// If name has enough dots, try that first.if { = append(, ) }for , := range .Search { = append(, Fqdn(+)) }// If we didn't have enough dots, try after suffixes.if ! { = append(, ) }return}
The pages are generated with Goldsv0.8.2. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.