2016-11-30 21:42:58 +00:00
|
|
|
--- /net/dnsconfig_unix.go 2016-08-20 08:14:05.763235602 -0400
|
|
|
|
+++ /net/dnsconfig_unix.go 2016-08-20 08:14:05.763235602 -0400
|
|
|
|
@@ -9,15 +9,58 @@
|
2016-03-28 14:57:42 +00:00
|
|
|
package net
|
|
|
|
|
2016-11-30 21:42:58 +00:00
|
|
|
import (
|
2016-03-28 14:57:42 +00:00
|
|
|
+ "fmt"
|
2016-11-30 21:42:58 +00:00
|
|
|
"os"
|
2016-03-28 14:57:42 +00:00
|
|
|
+ "os/exec"
|
|
|
|
+ "strings"
|
2016-11-30 21:42:58 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
- defaultNS = []string{"127.0.0.1:53", "[::1]:53"}
|
|
|
|
+ currentNS []string
|
|
|
|
getHostname = os.Hostname // variable for testing
|
|
|
|
)
|
|
|
|
|
2016-03-28 14:57:42 +00:00
|
|
|
+func getDefaultNS() []string {
|
|
|
|
+ var servers []string
|
2016-04-02 00:42:50 +00:00
|
|
|
+ for _, prop := range []string{"net.dns1", "net.dns2"} {
|
2016-03-28 14:57:42 +00:00
|
|
|
+ cmd := exec.Command("getprop", prop)
|
2016-04-02 00:42:50 +00:00
|
|
|
+ outputBytes, err := cmd.Output()
|
|
|
|
+ if err != nil {
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ output := strings.Trim(string(outputBytes), "\n")
|
|
|
|
+ if ParseIP(output) != nil {
|
2016-11-30 21:42:58 +00:00
|
|
|
+ servers = append(servers, output + ":53")
|
2016-03-28 14:57:42 +00:00
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
2016-04-02 00:42:50 +00:00
|
|
|
+ if len(servers) == 0 {
|
2016-11-30 21:42:58 +00:00
|
|
|
+ servers = []string{"8.8.8.8:53", "8.8.4.4:53", "4.2.2.1:53"}
|
2016-03-28 14:57:42 +00:00
|
|
|
+ }
|
|
|
|
+
|
2016-04-02 00:42:50 +00:00
|
|
|
+ if !slicesEqual(currentNS, servers) {
|
|
|
|
+ fmt.Println("Using DNS servers:", servers)
|
|
|
|
+ currentNS = servers
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return currentNS
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func slicesEqual(a, b []string) bool {
|
|
|
|
+ if len(a) != len(b) {
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for i := range a {
|
|
|
|
+ if a[i] != b[i] {
|
|
|
|
+ return false
|
|
|
|
+ }
|
|
|
|
+ }
|
2016-03-28 14:57:42 +00:00
|
|
|
+
|
2016-04-02 00:42:50 +00:00
|
|
|
+ return true
|
2016-03-28 14:57:42 +00:00
|
|
|
+}
|
2016-11-30 21:42:58 +00:00
|
|
|
+
|
2016-03-28 14:57:42 +00:00
|
|
|
type dnsConfig struct {
|
2016-11-30 21:42:58 +00:00
|
|
|
servers []string // server addresses (in host:port form) to use
|
|
|
|
search []string // rooted suffixes to append to local name
|
|
|
|
@@ -40,7 +83,7 @@
|
2016-03-28 14:57:42 +00:00
|
|
|
}
|
|
|
|
file, err := open(filename)
|
|
|
|
if err != nil {
|
|
|
|
- conf.servers = defaultNS
|
|
|
|
+ conf.servers = getDefaultNS()
|
2016-11-30 21:42:58 +00:00
|
|
|
conf.search = dnsDefaultSearch()
|
2016-03-28 14:57:42 +00:00
|
|
|
conf.err = err
|
|
|
|
return conf
|
2016-11-30 21:42:58 +00:00
|
|
|
@@ -49,7 +92,7 @@
|
|
|
|
if fi, err := file.file.Stat(); err == nil {
|
|
|
|
conf.mtime = fi.ModTime()
|
|
|
|
} else {
|
|
|
|
- conf.servers = defaultNS
|
|
|
|
+ conf.servers = getDefaultNS()
|
|
|
|
conf.search = dnsDefaultSearch()
|
|
|
|
conf.err = err
|
|
|
|
return conf
|
|
|
|
@@ -126,7 +169,7 @@
|
2016-03-28 14:57:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(conf.servers) == 0 {
|
|
|
|
- conf.servers = defaultNS
|
|
|
|
+ conf.servers = getDefaultNS()
|
|
|
|
}
|
2016-11-30 21:42:58 +00:00
|
|
|
if len(conf.search) == 0 {
|
|
|
|
conf.search = dnsDefaultSearch()
|