1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-27 06:41:15 +00:00
syncthing-android/patches/golang/all/dns.patch

90 lines
1.9 KiB
Diff
Raw Normal View History

--- /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
import (
2016-03-28 14:57:42 +00:00
+ "fmt"
"os"
2016-03-28 14:57:42 +00:00
+ "os/exec"
+ "strings"
"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
+ for _, prop := range []string{"net.dns1", "net.dns2"} {
2016-03-28 14:57:42 +00:00
+ cmd := exec.Command("getprop", prop)
+ outputBytes, err := cmd.Output()
+ if err != nil {
+ continue
+ }
+ output := strings.Trim(string(outputBytes), "\n")
+ if ParseIP(output) != nil {
+ servers = append(servers, output + ":53")
2016-03-28 14:57:42 +00:00
+ }
+ }
+
+ if len(servers) == 0 {
+ 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
+ }
+
+ 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
+
+ return true
2016-03-28 14:57:42 +00:00
+}
+
2016-03-28 14:57:42 +00:00
type dnsConfig struct {
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()
conf.search = dnsDefaultSearch()
2016-03-28 14:57:42 +00:00
conf.err = err
return conf
@@ -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()
}
if len(conf.search) == 0 {
conf.search = dnsDefaultSearch()