1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-11-26 22:31:16 +00:00
syncthing-android/patches/golang/all/dns.patch

91 lines
2.1 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,16 +9,59 @@
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"
"sync/atomic"
"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, JoinHostPort(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
@@ -42,7 +85,7 @@ func dnsReadConfig(filename string) *dnsConfig {
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
@@ -51,7 +94,7 @@ func dnsReadConfig(filename string) *dnsConfig {
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
@@ -130,7 +173,7 @@ func dnsReadConfig(filename string) *dnsConfig {
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()