|
@@ -148,7 +148,7 @@ func getLines(input []byte, commentMarker []byte) [][]byte {
|
|
|
|
|
|
// GetNameservers returns nameservers (if any) listed in /etc/resolv.conf
|
|
// GetNameservers returns nameservers (if any) listed in /etc/resolv.conf
|
|
func GetNameservers(resolvConf []byte, kind int) []string {
|
|
func GetNameservers(resolvConf []byte, kind int) []string {
|
|
- nameservers := []string{}
|
|
|
|
|
|
+ var nameservers []string
|
|
for _, line := range getLines(resolvConf, []byte("#")) {
|
|
for _, line := range getLines(resolvConf, []byte("#")) {
|
|
var ns [][]byte
|
|
var ns [][]byte
|
|
if kind == IP {
|
|
if kind == IP {
|
|
@@ -169,7 +169,7 @@ func GetNameservers(resolvConf []byte, kind int) []string {
|
|
// /etc/resolv.conf as CIDR blocks (e.g., "1.2.3.4/32")
|
|
// /etc/resolv.conf as CIDR blocks (e.g., "1.2.3.4/32")
|
|
// This function's output is intended for net.ParseCIDR
|
|
// This function's output is intended for net.ParseCIDR
|
|
func GetNameserversAsCIDR(resolvConf []byte) []string {
|
|
func GetNameserversAsCIDR(resolvConf []byte) []string {
|
|
- nameservers := []string{}
|
|
|
|
|
|
+ var nameservers []string
|
|
for _, nameserver := range GetNameservers(resolvConf, IP) {
|
|
for _, nameserver := range GetNameservers(resolvConf, IP) {
|
|
var address string
|
|
var address string
|
|
// If IPv6, strip zone if present
|
|
// If IPv6, strip zone if present
|
|
@@ -187,7 +187,7 @@ func GetNameserversAsCIDR(resolvConf []byte) []string {
|
|
// If more than one search line is encountered, only the contents of the last
|
|
// If more than one search line is encountered, only the contents of the last
|
|
// one is returned.
|
|
// one is returned.
|
|
func GetSearchDomains(resolvConf []byte) []string {
|
|
func GetSearchDomains(resolvConf []byte) []string {
|
|
- domains := []string{}
|
|
|
|
|
|
+ var domains []string
|
|
for _, line := range getLines(resolvConf, []byte("#")) {
|
|
for _, line := range getLines(resolvConf, []byte("#")) {
|
|
match := searchRegexp.FindSubmatch(line)
|
|
match := searchRegexp.FindSubmatch(line)
|
|
if match == nil {
|
|
if match == nil {
|
|
@@ -202,7 +202,7 @@ func GetSearchDomains(resolvConf []byte) []string {
|
|
// If more than one options line is encountered, only the contents of the last
|
|
// If more than one options line is encountered, only the contents of the last
|
|
// one is returned.
|
|
// one is returned.
|
|
func GetOptions(resolvConf []byte) []string {
|
|
func GetOptions(resolvConf []byte) []string {
|
|
- options := []string{}
|
|
|
|
|
|
+ var options []string
|
|
for _, line := range getLines(resolvConf, []byte("#")) {
|
|
for _, line := range getLines(resolvConf, []byte("#")) {
|
|
match := optionsRegexp.FindSubmatch(line)
|
|
match := optionsRegexp.FindSubmatch(line)
|
|
if match == nil {
|
|
if match == nil {
|
|
@@ -213,10 +213,11 @@ func GetOptions(resolvConf []byte) []string {
|
|
return options
|
|
return options
|
|
}
|
|
}
|
|
|
|
|
|
-// Build writes a configuration file to path containing a "nameserver" entry
|
|
|
|
-// for every element in dns, a "search" entry for every element in
|
|
|
|
-// dnsSearch, and an "options" entry for every element in dnsOptions.
|
|
|
|
-func Build(path string, dns, dnsSearch, dnsOptions []string) (*File, error) {
|
|
|
|
|
|
+// Build generates and writes a configuration file to path containing a nameserver
|
|
|
|
+// entry for every element in nameservers, a "search" entry for every element in
|
|
|
|
+// dnsSearch, and an "options" entry for every element in dnsOptions. It returns
|
|
|
|
+// a File containing the generated content and its (sha256) hash.
|
|
|
|
+func Build(path string, nameservers, dnsSearch, dnsOptions []string) (*File, error) {
|
|
content := bytes.NewBuffer(nil)
|
|
content := bytes.NewBuffer(nil)
|
|
if len(dnsSearch) > 0 {
|
|
if len(dnsSearch) > 0 {
|
|
if searchString := strings.Join(dnsSearch, " "); strings.Trim(searchString, " ") != "." {
|
|
if searchString := strings.Join(dnsSearch, " "); strings.Trim(searchString, " ") != "." {
|
|
@@ -225,7 +226,7 @@ func Build(path string, dns, dnsSearch, dnsOptions []string) (*File, error) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- for _, dns := range dns {
|
|
|
|
|
|
+ for _, dns := range nameservers {
|
|
if _, err := content.WriteString("nameserver " + dns + "\n"); err != nil {
|
|
if _, err := content.WriteString("nameserver " + dns + "\n"); err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|