2015-09-21 08:43:36 +00:00
|
|
|
// Copyright (C) 2015 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-09-21 08:43:36 +00:00
|
|
|
|
2015-09-20 13:30:25 +00:00
|
|
|
package discover
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-11-21 07:41:15 +00:00
|
|
|
"context"
|
2015-09-20 13:30:25 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2020-08-18 07:26:33 +00:00
|
|
|
"fmt"
|
2015-09-20 13:30:25 +00:00
|
|
|
"io"
|
2022-04-09 14:04:56 +00:00
|
|
|
"net"
|
2015-09-20 13:30:25 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
stdsync "sync"
|
|
|
|
"time"
|
|
|
|
|
2022-04-09 14:04:56 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/connections/registry"
|
2015-10-13 18:52:22 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/dialer"
|
2015-09-20 13:30:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/events"
|
2015-09-22 17:38:46 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2023-08-30 19:58:05 +00:00
|
|
|
"golang.org/x/net/http2"
|
2015-09-20 13:30:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type globalClient struct {
|
|
|
|
server string
|
|
|
|
addrList AddressLister
|
|
|
|
announceClient httpClient
|
|
|
|
queryClient httpClient
|
|
|
|
noAnnounce bool
|
2018-01-05 14:18:32 +00:00
|
|
|
noLookup bool
|
2019-08-15 14:29:37 +00:00
|
|
|
evLogger events.Logger
|
2015-09-20 13:30:25 +00:00
|
|
|
errorHolder
|
|
|
|
}
|
|
|
|
|
|
|
|
type httpClient interface {
|
2020-02-13 13:43:00 +00:00
|
|
|
Get(ctx context.Context, url string) (*http.Response, error)
|
|
|
|
Post(ctx context.Context, url, ctype string, data io.Reader) (*http.Response, error)
|
2015-09-20 13:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2020-05-11 13:02:22 +00:00
|
|
|
defaultReannounceInterval = 30 * time.Minute
|
|
|
|
announceErrorRetryInterval = 5 * time.Minute
|
2022-04-23 14:12:25 +00:00
|
|
|
requestTimeout = 30 * time.Second
|
2020-05-11 13:02:22 +00:00
|
|
|
maxAddressChangesBetweenAnnouncements = 10
|
2015-09-20 13:30:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type announcement struct {
|
2016-05-04 19:38:12 +00:00
|
|
|
Addresses []string `json:"addresses"`
|
2015-09-20 13:30:25 +00:00
|
|
|
}
|
|
|
|
|
2023-03-04 11:16:57 +00:00
|
|
|
func (a announcement) MarshalJSON() ([]byte, error) {
|
|
|
|
type announcementCopy announcement
|
|
|
|
|
|
|
|
a.Addresses = sanitizeRelayAddresses(a.Addresses)
|
|
|
|
|
|
|
|
aCopy := announcementCopy(a)
|
|
|
|
return json.Marshal(aCopy)
|
|
|
|
}
|
|
|
|
|
2015-09-20 13:30:25 +00:00
|
|
|
type serverOptions struct {
|
|
|
|
insecure bool // don't check certificate
|
|
|
|
noAnnounce bool // don't announce
|
2018-01-05 14:18:32 +00:00
|
|
|
noLookup bool // don't use for lookups
|
2015-09-20 13:30:25 +00:00
|
|
|
id string // expected server device ID
|
|
|
|
}
|
|
|
|
|
2015-12-01 08:57:53 +00:00
|
|
|
// A lookupError is any other error but with a cache validity time attached.
|
|
|
|
type lookupError struct {
|
2020-06-16 07:27:34 +00:00
|
|
|
msg string
|
2015-12-01 08:57:53 +00:00
|
|
|
cacheFor time.Duration
|
|
|
|
}
|
|
|
|
|
2020-06-16 07:27:34 +00:00
|
|
|
func (e *lookupError) Error() string { return e.msg }
|
|
|
|
|
|
|
|
func (e *lookupError) CacheFor() time.Duration {
|
2015-12-01 08:57:53 +00:00
|
|
|
return e.cacheFor
|
|
|
|
}
|
|
|
|
|
2022-04-09 14:04:56 +00:00
|
|
|
func NewGlobal(server string, cert tls.Certificate, addrList AddressLister, evLogger events.Logger, registry *registry.Registry) (FinderService, error) {
|
2015-09-20 13:30:25 +00:00
|
|
|
server, opts, err := parseOptions(server)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var devID protocol.DeviceID
|
|
|
|
if opts.id != "" {
|
|
|
|
devID, err = protocol.DeviceIDFromString(opts.id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The http.Client used for announcements. It needs to have our
|
|
|
|
// certificate to prove our identity, and may or may not verify the server
|
|
|
|
// certificate depending on the insecure setting.
|
2022-04-09 14:04:56 +00:00
|
|
|
var dialContext func(ctx context.Context, network, addr string) (net.Conn, error)
|
|
|
|
if registry != nil {
|
|
|
|
dialContext = dialer.DialContextReusePortFunc(registry)
|
|
|
|
} else {
|
|
|
|
dialContext = dialer.DialContext
|
|
|
|
}
|
2020-02-13 13:43:00 +00:00
|
|
|
var announceClient httpClient = &contextClient{&http.Client{
|
2015-10-21 12:24:33 +00:00
|
|
|
Timeout: requestTimeout,
|
2023-08-30 19:58:05 +00:00
|
|
|
Transport: http2EnabledTransport(&http.Transport{
|
|
|
|
DialContext: dialContext,
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
DisableKeepAlives: true, // announcements are few and far between, so don't keep the connection open
|
2015-09-20 13:30:25 +00:00
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: opts.insecure,
|
|
|
|
Certificates: []tls.Certificate{cert},
|
2023-08-30 19:58:05 +00:00
|
|
|
MinVersion: tls.VersionTLS12,
|
2024-09-24 06:55:04 +00:00
|
|
|
ClientSessionCache: tls.NewLRUClientSessionCache(0),
|
2015-09-20 13:30:25 +00:00
|
|
|
},
|
2023-08-30 19:58:05 +00:00
|
|
|
}),
|
2020-02-13 13:43:00 +00:00
|
|
|
}}
|
2015-09-20 13:30:25 +00:00
|
|
|
if opts.id != "" {
|
|
|
|
announceClient = newIDCheckingHTTPClient(announceClient, devID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// The http.Client used for queries. We don't need to present our
|
|
|
|
// certificate here, so lets not include it. May be insecure if requested.
|
2020-02-13 13:43:00 +00:00
|
|
|
var queryClient httpClient = &contextClient{&http.Client{
|
2015-10-21 12:24:33 +00:00
|
|
|
Timeout: requestTimeout,
|
2023-08-30 19:58:05 +00:00
|
|
|
Transport: http2EnabledTransport(&http.Transport{
|
|
|
|
DialContext: dialer.DialContext,
|
|
|
|
Proxy: http.ProxyFromEnvironment,
|
|
|
|
IdleConnTimeout: time.Second,
|
2015-09-20 13:30:25 +00:00
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
InsecureSkipVerify: opts.insecure,
|
2023-08-30 19:58:05 +00:00
|
|
|
MinVersion: tls.VersionTLS12,
|
2024-09-24 06:55:04 +00:00
|
|
|
ClientSessionCache: tls.NewLRUClientSessionCache(0),
|
2015-09-20 13:30:25 +00:00
|
|
|
},
|
2023-08-30 19:58:05 +00:00
|
|
|
}),
|
2020-02-13 13:43:00 +00:00
|
|
|
}}
|
2015-09-20 13:30:25 +00:00
|
|
|
if opts.id != "" {
|
|
|
|
queryClient = newIDCheckingHTTPClient(queryClient, devID)
|
|
|
|
}
|
|
|
|
|
|
|
|
cl := &globalClient{
|
|
|
|
server: server,
|
|
|
|
addrList: addrList,
|
|
|
|
announceClient: announceClient,
|
|
|
|
queryClient: queryClient,
|
|
|
|
noAnnounce: opts.noAnnounce,
|
2018-01-05 14:18:32 +00:00
|
|
|
noLookup: opts.noLookup,
|
2019-08-15 14:29:37 +00:00
|
|
|
evLogger: evLogger,
|
2015-09-20 13:30:25 +00:00
|
|
|
}
|
2018-01-05 14:18:32 +00:00
|
|
|
if !opts.noAnnounce {
|
2022-08-23 13:44:11 +00:00
|
|
|
// If we are supposed to announce, it's an error until we've done so.
|
2018-01-05 14:18:32 +00:00
|
|
|
cl.setError(errors.New("not announced"))
|
|
|
|
}
|
2015-09-20 13:30:25 +00:00
|
|
|
|
|
|
|
return cl, nil
|
|
|
|
}
|
|
|
|
|
2016-05-04 19:38:12 +00:00
|
|
|
// Lookup returns the list of addresses where the given device is available
|
2020-02-13 13:43:00 +00:00
|
|
|
func (c *globalClient) Lookup(ctx context.Context, device protocol.DeviceID) (addresses []string, err error) {
|
2018-01-05 14:18:32 +00:00
|
|
|
if c.noLookup {
|
2020-06-16 07:27:34 +00:00
|
|
|
return nil, &lookupError{
|
|
|
|
msg: "lookups not supported",
|
2018-01-05 14:18:32 +00:00
|
|
|
cacheFor: time.Hour,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-20 13:30:25 +00:00
|
|
|
qURL, err := url.Parse(c.server)
|
|
|
|
if err != nil {
|
2016-05-04 19:38:12 +00:00
|
|
|
return nil, err
|
2015-09-20 13:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
q := qURL.Query()
|
|
|
|
q.Set("device", device.String())
|
|
|
|
qURL.RawQuery = q.Encode()
|
|
|
|
|
2020-02-13 13:43:00 +00:00
|
|
|
resp, err := c.queryClient.Get(ctx, qURL.String())
|
2015-09-20 13:30:25 +00:00
|
|
|
if err != nil {
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("globalClient.Lookup", qURL, err)
|
2016-05-04 19:38:12 +00:00
|
|
|
return nil, err
|
2015-09-20 13:30:25 +00:00
|
|
|
}
|
2023-08-30 19:58:05 +00:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2015-09-20 13:30:25 +00:00
|
|
|
resp.Body.Close()
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("globalClient.Lookup", qURL, resp.Status)
|
2015-12-01 08:57:53 +00:00
|
|
|
err := errors.New(resp.Status)
|
2015-12-01 10:19:39 +00:00
|
|
|
if secs, atoiErr := strconv.Atoi(resp.Header.Get("Retry-After")); atoiErr == nil && secs > 0 {
|
2020-06-16 07:27:34 +00:00
|
|
|
err = &lookupError{
|
|
|
|
msg: resp.Status,
|
2015-12-01 08:57:53 +00:00
|
|
|
cacheFor: time.Duration(secs) * time.Second,
|
|
|
|
}
|
|
|
|
}
|
2016-05-04 19:38:12 +00:00
|
|
|
return nil, err
|
2015-09-20 13:30:25 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 07:59:47 +00:00
|
|
|
bs, err := io.ReadAll(resp.Body)
|
2016-05-06 22:01:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-20 13:30:25 +00:00
|
|
|
resp.Body.Close()
|
2016-05-06 22:01:56 +00:00
|
|
|
|
|
|
|
var ann announcement
|
|
|
|
err = json.Unmarshal(bs, &ann)
|
2016-05-04 19:38:12 +00:00
|
|
|
return ann.Addresses, err
|
2015-09-20 13:30:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *globalClient) String() string {
|
|
|
|
return "global@" + c.server
|
|
|
|
}
|
|
|
|
|
2020-11-17 12:19:04 +00:00
|
|
|
func (c *globalClient) Serve(ctx context.Context) error {
|
2015-09-20 13:30:25 +00:00
|
|
|
if c.noAnnounce {
|
|
|
|
// We're configured to not do announcements, only lookups. To maintain
|
|
|
|
// the same interface, we just pause here if Serve() is run.
|
2019-11-21 07:41:15 +00:00
|
|
|
<-ctx.Done()
|
2020-11-17 12:19:04 +00:00
|
|
|
return ctx.Err()
|
2015-09-20 13:30:25 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 13:02:22 +00:00
|
|
|
timer := time.NewTimer(5 * time.Second)
|
2015-09-20 13:30:25 +00:00
|
|
|
defer timer.Stop()
|
|
|
|
|
2019-08-15 14:29:37 +00:00
|
|
|
eventSub := c.evLogger.Subscribe(events.ListenAddressesChanged)
|
|
|
|
defer eventSub.Unsubscribe()
|
2015-09-20 13:30:25 +00:00
|
|
|
|
2020-05-11 13:02:22 +00:00
|
|
|
timerResetCount := 0
|
|
|
|
|
2015-09-20 13:30:25 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-eventSub.C():
|
2020-05-11 13:02:22 +00:00
|
|
|
if timerResetCount < maxAddressChangesBetweenAnnouncements {
|
|
|
|
// Defer announcement by 2 seconds, essentially debouncing
|
|
|
|
// if we have a stream of events incoming in quick succession.
|
|
|
|
timer.Reset(2 * time.Second)
|
|
|
|
} else if timerResetCount == maxAddressChangesBetweenAnnouncements {
|
|
|
|
// Yet only do it if we haven't had to reset maxAddressChangesBetweenAnnouncements times in a row,
|
|
|
|
// so if something is flip-flopping within 2 seconds, we don't end up in a permanent reset loop.
|
|
|
|
l.Warnf("Detected a flip-flopping listener")
|
|
|
|
c.setError(errors.New("flip flopping listener"))
|
|
|
|
// Incrementing the count above 10 will prevent us from warning or setting the error again
|
|
|
|
// It will also suppress event based resets until we've had a proper round after announceErrorRetryInterval
|
|
|
|
timer.Reset(announceErrorRetryInterval)
|
|
|
|
}
|
|
|
|
timerResetCount++
|
2015-09-20 13:30:25 +00:00
|
|
|
case <-timer.C:
|
2020-05-11 13:02:22 +00:00
|
|
|
timerResetCount = 0
|
2020-02-13 13:43:00 +00:00
|
|
|
c.sendAnnouncement(ctx, timer)
|
2015-09-20 13:30:25 +00:00
|
|
|
|
2019-11-21 07:41:15 +00:00
|
|
|
case <-ctx.Done():
|
2020-11-17 12:19:04 +00:00
|
|
|
return ctx.Err()
|
2015-09-20 13:30:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-13 13:43:00 +00:00
|
|
|
func (c *globalClient) sendAnnouncement(ctx context.Context, timer *time.Timer) {
|
2015-09-20 13:30:25 +00:00
|
|
|
var ann announcement
|
|
|
|
if c.addrList != nil {
|
2016-05-04 19:38:12 +00:00
|
|
|
ann.Addresses = c.addrList.ExternalAddresses()
|
2015-09-20 13:30:25 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 19:38:12 +00:00
|
|
|
if len(ann.Addresses) == 0 {
|
2017-11-17 09:12:35 +00:00
|
|
|
// There are legitimate cases for not having anything to announce,
|
|
|
|
// yet still using global discovery for lookups. Do not error out
|
|
|
|
// here.
|
|
|
|
c.setError(nil)
|
2015-09-20 13:30:25 +00:00
|
|
|
timer.Reset(announceErrorRetryInterval)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// The marshal doesn't fail, I promise.
|
|
|
|
postData, _ := json.Marshal(ann)
|
|
|
|
|
2020-08-25 09:48:14 +00:00
|
|
|
l.Debugf("%s Announcement: %v", c, ann)
|
2015-09-20 13:30:25 +00:00
|
|
|
|
2020-02-13 13:43:00 +00:00
|
|
|
resp, err := c.announceClient.Post(ctx, c.server, "application/json", bytes.NewReader(postData))
|
2015-09-20 13:30:25 +00:00
|
|
|
if err != nil {
|
2020-08-25 09:48:14 +00:00
|
|
|
l.Debugln(c, "announce POST:", err)
|
2015-09-20 13:30:25 +00:00
|
|
|
c.setError(err)
|
|
|
|
timer.Reset(announceErrorRetryInterval)
|
|
|
|
return
|
|
|
|
}
|
2020-08-25 09:48:14 +00:00
|
|
|
l.Debugln(c, "announce POST:", resp.Status)
|
2015-09-20 13:30:25 +00:00
|
|
|
resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
2020-08-25 09:48:14 +00:00
|
|
|
l.Debugln(c, "announce POST:", resp.Status)
|
2015-09-20 13:30:25 +00:00
|
|
|
c.setError(errors.New(resp.Status))
|
|
|
|
|
|
|
|
if h := resp.Header.Get("Retry-After"); h != "" {
|
|
|
|
// The server has a recommendation on when we should
|
|
|
|
// retry. Follow it.
|
|
|
|
if secs, err := strconv.Atoi(h); err == nil && secs > 0 {
|
2020-08-25 09:48:14 +00:00
|
|
|
l.Debugln(c, "announce Retry-After:", secs, err)
|
2015-09-20 13:30:25 +00:00
|
|
|
timer.Reset(time.Duration(secs) * time.Second)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
timer.Reset(announceErrorRetryInterval)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.setError(nil)
|
|
|
|
|
|
|
|
if h := resp.Header.Get("Reannounce-After"); h != "" {
|
|
|
|
// The server has a recommendation on when we should
|
|
|
|
// reannounce. Follow it.
|
|
|
|
if secs, err := strconv.Atoi(h); err == nil && secs > 0 {
|
2020-08-25 09:48:14 +00:00
|
|
|
l.Debugln(c, "announce Reannounce-After:", secs, err)
|
2015-09-20 13:30:25 +00:00
|
|
|
timer.Reset(time.Duration(secs) * time.Second)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
timer.Reset(defaultReannounceInterval)
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:32:45 +00:00
|
|
|
func (*globalClient) Cache() map[protocol.DeviceID]CacheEntry {
|
2015-09-20 13:30:25 +00:00
|
|
|
// The globalClient doesn't do caching
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseOptions parses and strips away any ?query=val options, setting the
|
|
|
|
// corresponding field in the serverOptions struct. Unknown query options are
|
|
|
|
// ignored and removed.
|
|
|
|
func parseOptions(dsn string) (server string, opts serverOptions, err error) {
|
|
|
|
p, err := url.Parse(dsn)
|
|
|
|
if err != nil {
|
|
|
|
return "", serverOptions{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab known options from the query string
|
|
|
|
q := p.Query()
|
|
|
|
opts.id = q.Get("id")
|
|
|
|
opts.insecure = opts.id != "" || queryBool(q, "insecure")
|
|
|
|
opts.noAnnounce = queryBool(q, "noannounce")
|
2018-01-05 14:18:32 +00:00
|
|
|
opts.noLookup = queryBool(q, "nolookup")
|
2015-09-20 13:30:25 +00:00
|
|
|
|
|
|
|
// Check for disallowed combinations
|
|
|
|
if p.Scheme == "http" {
|
|
|
|
if !opts.insecure {
|
|
|
|
return "", serverOptions{}, errors.New("http without insecure not supported")
|
|
|
|
}
|
|
|
|
if !opts.noAnnounce {
|
|
|
|
return "", serverOptions{}, errors.New("http without noannounce not supported")
|
|
|
|
}
|
|
|
|
} else if p.Scheme != "https" {
|
|
|
|
return "", serverOptions{}, errors.New("unsupported scheme " + p.Scheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the query string
|
|
|
|
p.RawQuery = ""
|
|
|
|
server = p.String()
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// queryBool returns the query parameter parsed as a boolean. An empty value
|
|
|
|
// ("?foo") is considered true, as is any value string except false
|
|
|
|
// ("?foo=false").
|
|
|
|
func queryBool(q url.Values, key string) bool {
|
|
|
|
if _, ok := q[key]; !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return q.Get(key) != "false"
|
|
|
|
}
|
|
|
|
|
|
|
|
type idCheckingHTTPClient struct {
|
|
|
|
httpClient
|
|
|
|
id protocol.DeviceID
|
|
|
|
}
|
|
|
|
|
|
|
|
func newIDCheckingHTTPClient(client httpClient, id protocol.DeviceID) *idCheckingHTTPClient {
|
|
|
|
return &idCheckingHTTPClient{
|
|
|
|
httpClient: client,
|
|
|
|
id: id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *idCheckingHTTPClient) check(resp *http.Response) error {
|
|
|
|
if resp.TLS == nil {
|
|
|
|
return errors.New("security: not TLS")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.TLS.PeerCertificates) == 0 {
|
|
|
|
return errors.New("security: no certificates")
|
|
|
|
}
|
|
|
|
|
|
|
|
id := protocol.NewDeviceID(resp.TLS.PeerCertificates[0].Raw)
|
|
|
|
if !id.Equals(c.id) {
|
|
|
|
return errors.New("security: incorrect device id")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-13 13:43:00 +00:00
|
|
|
func (c *idCheckingHTTPClient) Get(ctx context.Context, url string) (*http.Response, error) {
|
|
|
|
resp, err := c.httpClient.Get(ctx, url)
|
2015-09-20 13:30:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := c.check(resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2020-02-13 13:43:00 +00:00
|
|
|
func (c *idCheckingHTTPClient) Post(ctx context.Context, url, ctype string, data io.Reader) (*http.Response, error) {
|
|
|
|
resp, err := c.httpClient.Post(ctx, url, ctype, data)
|
2015-09-20 13:30:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := c.check(resp); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type errorHolder struct {
|
|
|
|
err error
|
|
|
|
mut stdsync.Mutex // uses stdlib sync as I want this to be trivially embeddable, and there is no risk of blocking
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *errorHolder) setError(err error) {
|
|
|
|
e.mut.Lock()
|
|
|
|
e.err = err
|
|
|
|
e.mut.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *errorHolder) Error() error {
|
|
|
|
e.mut.Lock()
|
|
|
|
err := e.err
|
|
|
|
e.mut.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
2020-02-13 13:43:00 +00:00
|
|
|
|
|
|
|
type contextClient struct {
|
|
|
|
*http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *contextClient) Get(ctx context.Context, url string) (*http.Response, error) {
|
2023-08-30 19:58:05 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
2020-02-13 13:43:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return c.Client.Do(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *contextClient) Post(ctx context.Context, url, ctype string, data io.Reader) (*http.Response, error) {
|
2023-08-30 19:58:05 +00:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, data)
|
2020-02-13 13:43:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", ctype)
|
|
|
|
return c.Client.Do(req)
|
|
|
|
}
|
2020-08-18 07:26:33 +00:00
|
|
|
|
|
|
|
func globalDiscoveryIdentity(addr string) string {
|
|
|
|
return "global discovery server " + addr
|
|
|
|
}
|
|
|
|
|
|
|
|
func ipv4Identity(port int) string {
|
|
|
|
return fmt.Sprintf("IPv4 local broadcast discovery on port %d", port)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ipv6Identity(addr string) string {
|
|
|
|
return fmt.Sprintf("IPv6 local multicast discovery on address %s", addr)
|
|
|
|
}
|
2023-08-30 19:58:05 +00:00
|
|
|
|
|
|
|
func http2EnabledTransport(t *http.Transport) *http.Transport {
|
|
|
|
_ = http2.ConfigureTransport(t)
|
|
|
|
return t
|
|
|
|
}
|