Przeglądaj źródła

Allow configuration of usage reporting URL

Jakob Borg 10 lat temu
rodzic
commit
46364a38c6

+ 13 - 8
cmd/syncthing/usage_report.go

@@ -10,6 +10,7 @@ import (
 	"bytes"
 	"crypto/rand"
 	"crypto/sha256"
+	"crypto/tls"
 	"encoding/json"
 	"fmt"
 	"net"
@@ -238,17 +239,21 @@ func (s *usageReportingService) sendUsageReport() error {
 	var b bytes.Buffer
 	json.NewEncoder(&b).Encode(d)
 
-	var client = http.DefaultClient
+	transp := &http.Transport{}
+	client := &http.Client{Transport: transp}
 	if BuildEnv == "android" {
 		// 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")
-			},
+		transp.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)
+
+	if cfg.Options().URPostInsecurely {
+		transp.TLSClientConfig = &tls.Config{
+			InsecureSkipVerify: true,
+		}
+	}
+	_, err := client.Post(cfg.Options().URURL, "application/json", &b)
 	return err
 }
 
@@ -258,7 +263,7 @@ func (s *usageReportingService) Serve() {
 	l.Infoln("Starting usage reporting")
 	defer l.Infoln("Stopping usage reporting")
 
-	t := time.NewTimer(10 * time.Minute) // time to initial report at start
+	t := time.NewTimer(time.Duration(cfg.Options().URInitialDelayS) * time.Second) // time to initial report at start
 	for {
 		select {
 		case <-s.stop:

+ 3 - 0
lib/config/config.go

@@ -234,6 +234,9 @@ type OptionsConfiguration struct {
 	UPnPTimeoutS            int      `xml:"upnpTimeoutSeconds" json:"upnpTimeoutSeconds" default:"10"`
 	URAccepted              int      `xml:"urAccepted" json:"urAccepted"` // Accepted usage reporting version; 0 for off (undecided), -1 for off (permanently)
 	URUniqueID              string   `xml:"urUniqueID" json:"urUniqueId"` // Unique ID for reporting purposes, regenerated when UR is turned on.
+	URURL                   string   `xml:"urURL" json:"urURL" default:"https://data.syncthing.net/newdata"`
+	URPostInsecurely        bool     `xml:"urPostInsecurely" json:"urPostInsecurely" default:"false"` // For testing
+	URInitialDelayS         int      `xml:"urInitialDelayS" json:"urInitialDelayS" default:"1800"`
 	RestartOnWakeup         bool     `xml:"restartOnWakeup" json:"restartOnWakeup" default:"true"`
 	AutoUpgradeIntervalH    int      `xml:"autoUpgradeIntervalH" json:"autoUpgradeIntervalH" default:"12"` // 0 for off
 	KeepTemporariesH        int      `xml:"keepTemporariesH" json:"keepTemporariesH" default:"24"`         // 0 for off

+ 6 - 0
lib/config/config_test.go

@@ -60,6 +60,9 @@ func TestDefaultValues(t *testing.T) {
 		PingTimeoutS:            30,
 		PingIdleTimeS:           60,
 		MinHomeDiskFreePct:      1,
+		URURL:                   "https://data.syncthing.net/newdata",
+		URInitialDelayS:         1800,
+		URPostInsecurely:        false,
 	}
 
 	cfg := New(device1)
@@ -175,6 +178,9 @@ func TestOverriddenValues(t *testing.T) {
 		PingTimeoutS:            60,
 		PingIdleTimeS:           120,
 		MinHomeDiskFreePct:      5.2,
+		URURL:                   "https://localhost/newdata",
+		URInitialDelayS:         800,
+		URPostInsecurely:        true,
 	}
 
 	cfg, err := Load("testdata/overridenvalues.xml", device1)

+ 3 - 0
lib/config/testdata/overridenvalues.xml

@@ -32,5 +32,8 @@
         <pingTimeoutS>60</pingTimeoutS>
         <pingIdleTimeS>120</pingIdleTimeS>
         <minHomeDiskFreePct>5.2</minHomeDiskFreePct>
+        <urURL>https://localhost/newdata</urURL>
+        <urInitialDelayS>800</urInitialDelayS>
+        <urPostInsecurely>true</urPostInsecurely>
     </options>
 </configuration>

+ 8 - 2
test/h2/config.xml

@@ -12,6 +12,7 @@
         <hashers>0</hashers>
         <order>random</order>
         <ignoreDelete>false</ignoreDelete>
+        <scanProgressInterval>0</scanProgressInterval>
     </folder>
     <folder id="s12" path="s12-2" ro="false" rescanIntervalS="15" ignorePerms="false" autoNormalize="true">
         <device id="I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU"></device>
@@ -23,6 +24,7 @@
         <hashers>0</hashers>
         <order>random</order>
         <ignoreDelete>false</ignoreDelete>
+        <scanProgressInterval>0</scanProgressInterval>
     </folder>
     <folder id="s23" path="s23-2" ro="false" rescanIntervalS="15" ignorePerms="false" autoNormalize="true">
         <device id="JMFJCXB-GZDE4BN-OCJE3VF-65GYZNU-AIVJRET-3J6HMRQ-AUQIGJO-FKNHMQU"></device>
@@ -34,6 +36,7 @@
         <hashers>0</hashers>
         <order>random</order>
         <ignoreDelete>false</ignoreDelete>
+        <scanProgressInterval>0</scanProgressInterval>
     </folder>
     <device id="I6KAH76-66SLLLB-5PFXSOA-UFJCDZC-YAOMLEK-CP2GB32-BV5RQST-3PSROAU" name="s1" compression="metadata" introducer="false">
         <address>tcp://127.0.0.1:22001</address>
@@ -68,8 +71,11 @@
         <upnpLeaseMinutes>0</upnpLeaseMinutes>
         <upnpRenewalMinutes>1</upnpRenewalMinutes>
         <upnpTimeoutSeconds>10</upnpTimeoutSeconds>
-        <urAccepted>-1</urAccepted>
-        <urUniqueID></urUniqueID>
+        <urAccepted>2</urAccepted>
+        <urUniqueID>621mlbJP</urUniqueID>
+        <urURL>https://localhost:8443/newdata</urURL>
+        <urPostInsecurely>true</urPostInsecurely>
+        <urInitialDelayS>10</urInitialDelayS>
         <restartOnWakeup>true</restartOnWakeup>
         <autoUpgradeIntervalH>12</autoUpgradeIntervalH>
         <keepTemporariesH>24</keepTemporariesH>