diff --git a/.golangci.yml b/.golangci.yml index ceaff8e2b..5acc9cf3f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -7,6 +7,7 @@ linters: - err113 - exhaustive - exhaustruct + - exhaustruct_v5 - forbidigo - funcorder - funlen diff --git a/cmd/syncthing/crash_reporting.go b/cmd/syncthing/crash_reporting.go index 21152dc0a..3b5d6be51 100644 --- a/cmd/syncthing/crash_reporting.go +++ b/cmd/syncthing/crash_reporting.go @@ -21,7 +21,6 @@ import ( "github.com/syncthing/syncthing/internal/slogutil" "github.com/syncthing/syncthing/lib/build" - "github.com/syncthing/syncthing/lib/dialer" "github.com/syncthing/syncthing/lib/tlsutil" ) @@ -30,17 +29,6 @@ const ( putRequestTimeout = time.Minute ) -// The client used for uploading crash reports. Crash reports are rare, so -// the connection is not kept open after an upload. -var crashReportClient = &http.Client{ - Transport: &http.Transport{ - DialContext: dialer.DialContext, - Proxy: http.ProxyFromEnvironment, - DisableKeepAlives: true, - TLSClientConfig: tlsutil.SecureDefaultWithTLS12(), - }, -} - // uploadPanicLogs attempts to upload all the panic logs in the named // directory to the crash reporting server as urlBase. Uploads are attempted // with the newest log first. @@ -102,7 +90,7 @@ func uploadPanicLog(ctx context.Context, urlBase, file string) error { defer headCancel() headReq = headReq.WithContext(headCtx) - resp, err := crashReportClient.Do(headReq) + resp, err := tlsutil.ShortLivedHTTPClient.Do(headReq) if err != nil { return err } @@ -123,7 +111,7 @@ func uploadPanicLog(ctx context.Context, urlBase, file string) error { defer putCancel() putReq = putReq.WithContext(putCtx) - resp, err = crashReportClient.Do(putReq) + resp, err = tlsutil.ShortLivedHTTPClient.Do(putReq) if err != nil { return err } diff --git a/lib/api/testdata/config/config.xml b/lib/api/testdata/config/config.xml index 4aa0f6e39..423c1d3f7 100644 --- a/lib/api/testdata/config/config.xml +++ b/lib/api/testdata/config/config.xml @@ -110,7 +110,6 @@ 2 tmwxxCqi https://data.syncthing.net/newdata - false 1800 true 12 diff --git a/lib/config/config_test.go b/lib/config/config_test.go index 7d4e4024a..d995044b8 100644 --- a/lib/config/config_test.go +++ b/lib/config/config_test.go @@ -77,7 +77,6 @@ func TestDefaultValues(t *testing.T) { MinHomeDiskFree: Size{1, "%"}, URURL: "https://data.syncthing.net/newdata", URInitialDelayS: 1800, - URPostInsecurely: false, ReleasesURL: "https://upgrades.syncthing.net/meta.json", AlwaysLocalNets: []string{}, OverwriteRemoteDevNames: false, @@ -284,7 +283,6 @@ func TestOverriddenValues(t *testing.T) { URAccepted: 4, URURL: "https://localhost/newdata", URInitialDelayS: 800, - URPostInsecurely: true, ReleasesURL: "https://localhost/releases", AlwaysLocalNets: []string{}, OverwriteRemoteDevNames: true, diff --git a/lib/config/optionsconfiguration.go b/lib/config/optionsconfiguration.go index 7d7f306b2..0440d7758 100644 --- a/lib/config/optionsconfiguration.go +++ b/lib/config/optionsconfiguration.go @@ -40,7 +40,6 @@ type OptionsConfiguration struct { URSeen int `json:"urSeen" xml:"urSeen"` URUniqueID string `json:"urUniqueId" xml:"urUniqueID"` URURL string `json:"urURL" xml:"urURL" default:"https://data.syncthing.net/newdata"` - URPostInsecurely bool `json:"urPostInsecurely" xml:"urPostInsecurely" default:"false"` URInitialDelayS int `json:"urInitialDelayS" xml:"urInitialDelayS" default:"1800"` AutoUpgradeIntervalH int `json:"autoUpgradeIntervalH" xml:"autoUpgradeIntervalH" default:"12"` UpgradeToPreReleases bool `json:"upgradeToPreReleases" xml:"upgradeToPreReleases"` diff --git a/lib/config/testdata/overridenvalues.xml b/lib/config/testdata/overridenvalues.xml index ee25aab4e..94c9f686e 100644 --- a/lib/config/testdata/overridenvalues.xml +++ b/lib/config/testdata/overridenvalues.xml @@ -31,7 +31,6 @@ 8 4 800 - true https://localhost/releases true 100 diff --git a/lib/relay/client/dynamic.go b/lib/relay/client/dynamic.go index f1911822a..dff3eeece 100644 --- a/lib/relay/client/dynamic.go +++ b/lib/relay/client/dynamic.go @@ -15,7 +15,6 @@ import ( "time" "github.com/syncthing/syncthing/lib/build" - "github.com/syncthing/syncthing/lib/dialer" "github.com/syncthing/syncthing/lib/osutil" "github.com/syncthing/syncthing/lib/rand" "github.com/syncthing/syncthing/lib/relay/protocol" @@ -33,17 +32,6 @@ type dynamicClient struct { client *staticClient } -// The client used for looking up available relays. Lookups are infrequent, -// so the connection is not kept open afterwards. -var relayPoolClient = &http.Client{ - Transport: &http.Transport{ - DialContext: dialer.DialContext, - Proxy: http.ProxyFromEnvironment, - DisableKeepAlives: true, - TLSClientConfig: tlsutil.SecureDefaultWithTLS12(), - }, -} - func newDynamicClient(uri *url.URL, certs []tls.Certificate, invitations chan protocol.SessionInvitation, timeout time.Duration) *dynamicClient { c := &dynamicClient{ pooladdr: uri, @@ -68,7 +56,7 @@ func (c *dynamicClient) serve(ctx context.Context) error { return err } req.Header.Set("User-Agent", build.UserAgent()) - data, err := relayPoolClient.Do(req) + data, err := tlsutil.ShortLivedHTTPClient.Do(req) if err != nil { l.Debugln(c, "failed to lookup dynamic relays", err) return err diff --git a/lib/tlsutil/tlsutil.go b/lib/tlsutil/tlsutil.go index b758ce1a7..c6fc62128 100644 --- a/lib/tlsutil/tlsutil.go +++ b/lib/tlsutil/tlsutil.go @@ -19,12 +19,26 @@ import ( "fmt" "math/big" "net" + "net/http" "os" "time" + "github.com/syncthing/syncthing/lib/dialer" "github.com/syncthing/syncthing/lib/rand" ) +// ShortLivedHTTPClient is used for infrequent HTTP requests. It supports +// HTTP/2, but closes connections instead of keeping them around while idle. +var ShortLivedHTTPClient = &http.Client{ + Transport: &http.Transport{ + DialContext: dialer.DialContext, + Proxy: http.ProxyFromEnvironment, + DisableKeepAlives: true, + ForceAttemptHTTP2: true, + TLSClientConfig: SecureDefaultWithTLS12(), + }, +} + var ( ErrIdentificationFailed = errors.New("failed to identify socket type") diff --git a/lib/upgrade/upgrade_supported.go b/lib/upgrade/upgrade_supported.go index 97b04ee88..48790be84 100644 --- a/lib/upgrade/upgrade_supported.go +++ b/lib/upgrade/upgrade_supported.go @@ -13,6 +13,7 @@ import ( "archive/zip" "bytes" "compress/gzip" + "context" "encoding/json" "errors" "io" @@ -28,10 +29,8 @@ import ( "github.com/shirou/gopsutil/v4/host" "github.com/syncthing/syncthing/internal/slogutil" "github.com/syncthing/syncthing/lib/build" - "github.com/syncthing/syncthing/lib/dialer" "github.com/syncthing/syncthing/lib/signature" "github.com/syncthing/syncthing/lib/tlsutil" - "golang.org/x/net/http2" ) const DisabledByCompilation = false @@ -61,45 +60,42 @@ const ( maxMetadataSize = 10 << 20 // 10 MiB ) -var upgradeClient = &http.Client{ - Timeout: readTimeout, - Transport: &http.Transport{ - DialContext: dialer.DialContext, - Proxy: http.ProxyFromEnvironment, - DisableKeepAlives: true, // upgrade checks are hours apart, so don't keep the connection open - TLSClientConfig: tlsutil.SecureDefaultWithTLS12(), - }, -} - var osVersion string func init() { - _ = http2.ConfigureTransport(upgradeClient.Transport.(*http.Transport)) osVersion, _ = host.KernelVersion() osVersion = strings.TrimSpace(osVersion) } -func upgradeClientGet(url string) (*http.Response, error) { - req, err := http.NewRequest(http.MethodGet, url, nil) +func upgradeClientGet(url string) (*http.Response, context.CancelFunc, error) { + ctx, cancel := context.WithTimeout(context.Background(), readTimeout) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { - return nil, err + cancel() + return nil, nil, err } req.Header.Set("User-Agent", build.UserAgent()) if osVersion != "" { req.Header.Set("Syncthing-Os-Version", osVersion) } - return upgradeClient.Do(req) + resp, err := tlsutil.ShortLivedHTTPClient.Do(req) + if err != nil { + cancel() + return nil, nil, err + } + return resp, cancel, nil } // FetchLatestReleases returns the latest releases. The "current" parameter // is used for setting the User-Agent only. func FetchLatestReleases(releasesURL, current string) []Release { - resp, err := upgradeClientGet(releasesURL) + resp, cancel, err := upgradeClientGet(releasesURL) if err != nil { slog.Warn("Failed to fetch latest release information", slogutil.Error(err)) return nil } + defer cancel() defer resp.Body.Close() if resp.StatusCode > 299 { slog.Warn("Failed to fetch latest release information", slogutil.Error(resp.Status)) @@ -221,14 +217,16 @@ func upgradeToURL(archiveName, binary string, url string) error { func readRelease(archiveName, dir, url string) (string, error) { l.Debugf("loading %q", url) - req, err := http.NewRequest(http.MethodGet, url, nil) + ctx, cancel := context.WithTimeout(context.Background(), readTimeout) + defer cancel() + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { return "", err } req.Header.Add("Accept", "application/octet-stream") req.Header.Set("User-Agent", build.UserAgent()) - resp, err := upgradeClient.Do(req) + resp, err := tlsutil.ShortLivedHTTPClient.Do(req) if err != nil { return "", err } diff --git a/lib/ur/failurereporting.go b/lib/ur/failurereporting.go index 85a9c9152..1aa8eee4a 100644 --- a/lib/ur/failurereporting.go +++ b/lib/ur/failurereporting.go @@ -19,7 +19,6 @@ import ( "github.com/syncthing/syncthing/internal/slogutil" "github.com/syncthing/syncthing/lib/build" "github.com/syncthing/syncthing/lib/config" - "github.com/syncthing/syncthing/lib/dialer" "github.com/syncthing/syncthing/lib/events" "github.com/syncthing/syncthing/lib/svcutil" "github.com/syncthing/syncthing/lib/tlsutil" @@ -198,15 +197,6 @@ func sendFailureReports(ctx context.Context, reports []contract.FailureReport, u panic(err) } - client := &http.Client{ - Transport: &http.Transport{ - DialContext: dialer.DialContext, - Proxy: http.ProxyFromEnvironment, - DisableKeepAlives: true, // a new client is used for each send, so don't keep the connection open - TLSClientConfig: tlsutil.SecureDefaultWithTLS12(), - }, - } - reqCtx, reqCancel := context.WithTimeout(ctx, sendTimeout) defer reqCancel() req, err := http.NewRequestWithContext(reqCtx, http.MethodPost, url, &b) @@ -217,7 +207,7 @@ func sendFailureReports(ctx context.Context, reports []contract.FailureReport, u req.Header.Set("Content-Type", "application/json") req.Header.Set("User-Agent", build.UserAgent()) - resp, err := client.Do(req) + resp, err := tlsutil.ShortLivedHTTPClient.Do(req) if err != nil { slog.WarnContext(ctx, "Failed to send failure report", slogutil.Error(err)) return diff --git a/lib/ur/usage_report.go b/lib/ur/usage_report.go index 44a4b24c9..ef6abc7f6 100644 --- a/lib/ur/usage_report.go +++ b/lib/ur/usage_report.go @@ -9,7 +9,6 @@ package ur import ( "bytes" "context" - "crypto/tls" "encoding/json" "log/slog" "math/rand" @@ -28,9 +27,9 @@ import ( "github.com/syncthing/syncthing/lib/build" "github.com/syncthing/syncthing/lib/config" "github.com/syncthing/syncthing/lib/connections" - "github.com/syncthing/syncthing/lib/dialer" "github.com/syncthing/syncthing/lib/protocol" "github.com/syncthing/syncthing/lib/scanner" + "github.com/syncthing/syncthing/lib/tlsutil" "github.com/syncthing/syncthing/lib/upgrade" "github.com/syncthing/syncthing/lib/ur/contract" ) @@ -357,25 +356,13 @@ func (s *Service) sendUsageReport(ctx context.Context) error { return err } - client := &http.Client{ - Transport: &http.Transport{ - DialContext: dialer.DialContext, - Proxy: http.ProxyFromEnvironment, - DisableKeepAlives: true, // reports are sent once a day, so don't keep the connection open - TLSClientConfig: &tls.Config{ - InsecureSkipVerify: s.cfg.Options().URPostInsecurely, - MinVersion: tls.VersionTLS12, - ClientSessionCache: tls.NewLRUClientSessionCache(0), - }, - }, - } req, err := http.NewRequestWithContext(ctx, http.MethodPost, s.cfg.Options().URURL, &b) if err != nil { return err } req.Header.Set("Content-Type", "application/json") req.Header.Set("User-Agent", build.UserAgent()) - resp, err := client.Do(req) + resp, err := tlsutil.ShortLivedHTTPClient.Do(req) if err != nil { return err }