Browse Source

Work around broken DNS on Android for usage reporting

Jakob Borg 11 years ago
parent
commit
18e5cb6793
3 changed files with 23 additions and 5 deletions
  1. 4 1
      cmd/syncthing/gui.go
  2. 4 1
      cmd/syncthing/main.go
  3. 15 3
      cmd/syncthing/usage_report.go

+ 4 - 1
cmd/syncthing/gui.go

@@ -251,7 +251,10 @@ func restPostConfig(req *http.Request, m *model.Model) {
 			// Set the corresponding options in newCfg so we don't trigger the restart check if this was the only option change
 			newCfg.Options.URDeclined = false
 			newCfg.Options.URAccepted = usageReportVersion
-			sendUsageRport(m)
+			err := sendUsageReport(m)
+			if err != nil {
+				l.Infoln("Usage report:", err)
+			}
 			go usageReportingLoop(m)
 		} else if !newCfg.Options.UREnabled && cfg.Options.UREnabled {
 			// UR was disabled

+ 4 - 1
cmd/syncthing/main.go

@@ -401,7 +401,10 @@ func main() {
 		go usageReportingLoop(m)
 		go func() {
 			time.Sleep(10 * time.Minute)
-			sendUsageRport(m)
+			err := sendUsageReport(m)
+			if err != nil {
+				l.Infoln("Usage report:", err)
+			}
 		}()
 	}
 

+ 15 - 3
cmd/syncthing/usage_report.go

@@ -5,6 +5,7 @@ import (
 	"crypto/rand"
 	"crypto/sha256"
 	"encoding/json"
+	"net"
 	"net/http"
 	"runtime"
 	"strings"
@@ -63,11 +64,19 @@ func reportData(m *model.Model) map[string]interface{} {
 	return res
 }
 
-func sendUsageRport(m *model.Model) error {
+func sendUsageReport(m *model.Model) error {
 	d := reportData(m)
 	var b bytes.Buffer
 	json.NewEncoder(&b).Encode(d)
-	_, err := http.Post("https://data.syncthing.net/newdata", "application/json", &b)
+
+	// This works around the lack of DNS resolution on Android... :()
+	tr := &http.Transport{
+		Dial: func(network, addr string) (net.Conn, error) {
+			return net.Dial(network, "194.126.249.13:443")
+		},
+	}
+	client := &http.Client{Transport: tr}
+	_, err := client.Post("https://data.syncthing.net/newdata", "application/json", &b)
 	return err
 }
 
@@ -80,7 +89,10 @@ loop:
 		case <-stopUsageReportingCh:
 			break loop
 		case <-t.C:
-			sendUsageRport(m)
+			err := sendUsageReport(m)
+			if err != nil {
+				l.Infoln("Usage report:", err)
+			}
 		}
 	}
 	l.Infoln("Stopping usage reporting")