mirror of
https://github.com/syncthing/syncthing.git
synced 2026-11-20 00:51:22 -08:00
fix: use global short lived HTTP client with proper HTTP/2 support (#10901)
In e7b8efb41 I disabled keepalives in several HTTP clients, which
inadvertently also disabled HTTP/2. However, the TLS setup announced h2
as a supported protocol, which caused confusion. This fixes that, and
makes sure to use the same client in all relevant places.
The old option to do usage reporting posts with InsecureSkipVerify goes
away -- nobody should have ever used it.
---------
Signed-off-by: Jakob Borg <[email protected]>
This commit is contained in:
1 parent
9bb4747df2
commit
05b6704ef5
11 files changed
+39
-78
No files matched your search
@@ -7,6 +7,7 @@ linters:
|
||||
- err113
|
||||
- exhaustive
|
||||
- exhaustruct
|
||||
- exhaustruct_v5
|
||||
- forbidigo
|
||||
- funcorder
|
||||
- funlen
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Vendored
-1
@@ -110,7 +110,6 @@
|
||||
<urSeen>2</urSeen>
|
||||
<urUniqueID>tmwxxCqi</urUniqueID>
|
||||
<urURL>https://data.syncthing.net/newdata</urURL>
|
||||
<urPostInsecurely>false</urPostInsecurely>
|
||||
<urInitialDelayS>1800</urInitialDelayS>
|
||||
<restartOnWakeup>true</restartOnWakeup>
|
||||
<autoUpgradeIntervalH>12</autoUpgradeIntervalH>
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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"`
|
||||
|
||||
-1
@@ -31,7 +31,6 @@
|
||||
<urSeen>8</urSeen>
|
||||
<urAccepted>4</urAccepted>
|
||||
<urInitialDelayS>800</urInitialDelayS>
|
||||
<urPostInsecurely>true</urPostInsecurely>
|
||||
<releasesURL>https://localhost/releases</releasesURL>
|
||||
<overwriteRemoteDeviceNamesOnConnect>true</overwriteRemoteDeviceNamesOnConnect>
|
||||
<tempIndexMinBlocks>100</tempIndexMinBlocks>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
+2
-15
@@ -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
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user