1
0
Fork 0
mirror of https://github.com/syncthing/syncthing-android.git synced 2024-12-26 12:51:28 +00:00

Really fix DNS

The previous patch was merged against a different version of Go, plus had bugs.
Now given I am able to build stuff, this seems to do the right job.
This commit is contained in:
AudriusButkevicius 2016-04-01 20:42:50 -04:00
parent 09bba7140a
commit 96afcdf132
2 changed files with 55 additions and 80 deletions

View file

@ -60,14 +60,12 @@ mkdir -p "$GOROOT_FINAL"
pushd ext/golang/go/src
git reset --hard HEAD
# Apply patches to Golang
for PATCH in $MYDIR/patches/golang/all/*.patch; do
if ! patch -R -p1 --dry-run <$PATCH &>/dev/null; then
echo "Applying $PATCH"
patch -p1 <$PATCH
else
echo "Patch $PATCH already applied"
fi
echo "Applying $PATCH"
patch -p1 <$PATCH
done
set +e

View file

@ -1,8 +1,25 @@
diff --git a/net/dnsclient_unix.go b/net/dnsclient_unix.go
index 17188f0..15a1663 100644
--- a/net/dnsclient_unix.go
+++ b/net/dnsclient_unix.go
@@ -279,12 +279,6 @@ func (conf *resolverConfig) tryUpdate(name string) {
return
}
conf.modTime = fi.ModTime()
- } else {
- // If modTime wasn't set prior, assume nothing has changed.
- if conf.modTime.IsZero() {
- return
- }
- conf.modTime = time.Time{}
}
dnsConf := dnsReadConfig(name)
diff --git a/net/dnsconfig_unix.go b/net/dnsconfig_unix.go
index 6073fdb..9df3cec 100644
index 6073fdb..bd6ca41 100644
--- a/net/dnsconfig_unix.go
+++ b/net/dnsconfig_unix.go
@@ -8,18 +8,57 @@
@@ -8,7 +8,53 @@
package net
@ -11,97 +28,62 @@ index 6073fdb..9df3cec 100644
+ "fmt"
+ "os/exec"
+ "strings"
+ "sync"
+ "time"
+)
+
+var (
+ logOnce sync.Once
+)
+var currentNS []string
+
+func getDefaultNS() []string {
+ var servers []string
+ for _, prop := range []string{"net.dns", "net.dns1", "net.dns2", "net.dns3", "net.dns4"} {
+ for _, prop := range []string{"net.dns1", "net.dns2"} {
+ cmd := exec.Command("getprop", prop)
+ if cmd.Run() == nil {
+ outputBytes, err := cmd.Output()
+ if err != nil {
+ continue
+ }
+
+ output := strings.Trim(string(outputBytes), "\n")
+ if ParseIP(output) != nil {
+ servers = append(servers, output)
+ }
+ outputBytes, err := cmd.Output()
+ if err != nil {
+ continue
+ }
+ output := strings.Trim(string(outputBytes), "\n")
+ if ParseIP(output) != nil {
+ servers = append(servers, output)
+ }
+ }
+
+ if servers == nil {
+ if len(servers) == 0 {
+ servers = []string{"8.8.8.8", "8.8.4.4", "4.2.2.1"}
+ }
+
+ logOnce.Do(func() {
+ fmt.Println("Using the following DNS servers", servers)
+ })
+ if !slicesEqual(currentNS, servers) {
+ fmt.Println("Using DNS servers:", servers)
+ currentNS = servers
+ }
+
+ return 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
+ }
+ }
+
+ return true
+}
type dnsConfig struct {
- servers []string // servers to use
- search []string // suffixes to append to local name
- ndots int // number of dots in name to trigger absolute lookup
- timeout int // seconds before giving up on packet
- attempts int // lost packets before giving up on server
- rotate bool // round robin among servers
- unknownOpt bool // anything unknown was encountered
- lookup []string // OpenBSD top-level database "lookup" order
- err error // any error that occurs during open of resolv.conf
+ servers []string // servers to use
+ search []string // suffixes to append to local name
+ ndots int // number of dots in name to trigger absolute lookup
+ timeout int // seconds before giving up on packet
+ attempts int // lost packets before giving up on server
+ rotate bool // round robin among servers
+ unknownOpt bool // anything unknown was encountered
+ lookup []string // OpenBSD top-level database "lookup" order
+ err error // any error that occurs during open of resolv.conf
+ mtime time.Time // time of resolv.conf modification
}
// See resolv.conf(5) on a Linux machine.
@@ -33,11 +72,20 @@ func dnsReadConfig(filename string) *dnsConfig {
servers []string // servers to use
@@ -33,7 +79,7 @@ func dnsReadConfig(filename string) *dnsConfig {
}
file, err := open(filename)
if err != nil {
- conf.servers = defaultNS
+ conf.servers = getDefaultNS()
+ conf.mtime = time.Now()
conf.err = err
return conf
}
defer file.close()
+ if fi, err := file.file.Stat(); err == nil {
+ conf.mtime = fi.ModTime()
+ } else {
+ conf.mtime = time.Now()
+ conf.servers = getDefaultNS()
+ conf.err = err
+ return conf
+ }
for line, ok := file.readLine(); ok; line, ok = file.readLine() {
if len(line) > 0 && (line[0] == ';' || line[0] == '#') {
// comment.
@@ -51,7 +99,7 @@ func dnsReadConfig(filename string) *dnsConfig {
case "nameserver": // add one name server
if len(f) > 1 && len(conf.servers) < 3 { // small, but the standard limit
// One more check: make sure server name is
- // just an IP address. Otherwise we need DNS
+ // just an IP address. Otherwise we need DNS
// to look it up.
if parseIPv4(f[1]) != nil {
conf.servers = append(conf.servers, f[1])
@@ -110,7 +158,7 @@ func dnsReadConfig(filename string) *dnsConfig {
@@ -110,7 +156,7 @@ func dnsReadConfig(filename string) *dnsConfig {
}
}
if len(conf.servers) == 0 {
@ -110,8 +92,3 @@ index 6073fdb..9df3cec 100644
}
return conf
}
@@ -118,3 +166,4 @@ func dnsReadConfig(filename string) *dnsConfig {
func hasPrefix(s, prefix string) bool {
return len(s) >= len(prefix) && s[:len(prefix)] == prefix
}
+