2014-05-05 18:51:32 -04:00
|
|
|
package resolvconf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io/ioutil"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-09-15 23:30:10 -04:00
|
|
|
var (
|
|
|
|
nsRegexp = regexp.MustCompile(`^\s*nameserver\s*(([0-9]+\.){3}([0-9]+))\s*$`)
|
|
|
|
searchRegexp = regexp.MustCompile(`^\s*search\s*(([^\s]+\s*)*)$`)
|
|
|
|
)
|
|
|
|
|
2014-05-05 18:51:32 -04:00
|
|
|
func Get() ([]byte, error) {
|
|
|
|
resolv, err := ioutil.ReadFile("/etc/resolv.conf")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resolv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getLines parses input into lines and strips away comments.
|
|
|
|
func getLines(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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNameservers returns nameservers (if any) listed in /etc/resolv.conf
|
|
|
|
func GetNameservers(resolvConf []byte) []string {
|
|
|
|
nameservers := []string{}
|
|
|
|
for _, line := range getLines(resolvConf, []byte("#")) {
|
2014-09-15 23:30:10 -04:00
|
|
|
var ns = nsRegexp.FindSubmatch(line)
|
2014-05-05 18:51:32 -04:00
|
|
|
if len(ns) > 0 {
|
|
|
|
nameservers = append(nameservers, string(ns[1]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nameservers
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNameserversAsCIDR returns nameservers (if any) listed in
|
|
|
|
// /etc/resolv.conf as CIDR blocks (e.g., "1.2.3.4/32")
|
|
|
|
// This function's output is intended for net.ParseCIDR
|
|
|
|
func GetNameserversAsCIDR(resolvConf []byte) []string {
|
|
|
|
nameservers := []string{}
|
|
|
|
for _, nameserver := range GetNameservers(resolvConf) {
|
|
|
|
nameservers = append(nameservers, nameserver+"/32")
|
|
|
|
}
|
|
|
|
return nameservers
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSearchDomains returns search domains (if any) listed in /etc/resolv.conf
|
|
|
|
// If more than one search line is encountered, only the contents of the last
|
|
|
|
// one is returned.
|
|
|
|
func GetSearchDomains(resolvConf []byte) []string {
|
|
|
|
domains := []string{}
|
|
|
|
for _, line := range getLines(resolvConf, []byte("#")) {
|
2014-09-15 23:30:10 -04:00
|
|
|
match := searchRegexp.FindSubmatch(line)
|
2014-05-05 18:51:32 -04:00
|
|
|
if match == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
domains = strings.Fields(string(match[1]))
|
|
|
|
}
|
|
|
|
return domains
|
|
|
|
}
|
|
|
|
|
|
|
|
func Build(path string, dns, dnsSearch []string) error {
|
|
|
|
content := bytes.NewBuffer(nil)
|
|
|
|
for _, dns := range dns {
|
|
|
|
if _, err := content.WriteString("nameserver " + dns + "\n"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(dnsSearch) > 0 {
|
2014-06-26 07:03:23 -04:00
|
|
|
if searchString := strings.Join(dnsSearch, " "); strings.Trim(searchString, " ") != "." {
|
|
|
|
if _, err := content.WriteString("search " + searchString + "\n"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-05-05 18:51:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ioutil.WriteFile(path, content.Bytes(), 0644)
|
|
|
|
}
|